@@ -17,6 +17,7 @@ limitations under the License.
1717package main
1818
1919import (
20+ "crypto/tls"
2021 "errors"
2122 "flag"
2223 "os"
@@ -32,14 +33,15 @@ import (
3233 ctrl "sigs.k8s.io/controller-runtime"
3334 "sigs.k8s.io/controller-runtime/pkg/healthz"
3435 "sigs.k8s.io/controller-runtime/pkg/log/zap"
36+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3537 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3638
3739 "github.com/checkly/checkly-go-sdk"
3840
3941 checklyv1alpha1 "github.com/checkly/checkly-operator/api/checkly/v1alpha1"
4042 checklycontrollers "github.com/checkly/checkly-operator/internal/controller/checkly"
4143 networkingcontrollers "github.com/checkly/checkly-operator/internal/controller/networking"
42- //+ kubebuilder:scaffold:imports
44+ //kubebuilder:scaffold:imports
4345)
4446
4547var (
@@ -51,35 +53,63 @@ func init() {
5153 utilruntime .Must (clientgoscheme .AddToScheme (scheme ))
5254
5355 utilruntime .Must (checklyv1alpha1 .AddToScheme (scheme ))
54- //+ kubebuilder:scaffold:scheme
56+ //kubebuilder:scaffold:scheme
5557}
5658
5759func main () {
5860 var metricsAddr string
5961 var enableLeaderElection bool
6062 var probeAddr string
63+ var secureMetrics bool
6164 var controllerDomain string
62- flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
65+ var tlsOpts []func (* tls.Config )
66+ flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
67+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
6368 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
6469 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
6570 "Enable leader election for controller manager. " +
6671 "Enabling this will ensure there is only one active controller manager." )
72+ flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
73+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
6774 flag .StringVar (& controllerDomain , "controller-domain" , "k8s.checklyhq.com" , "Domain to use for annotations and finalizers." )
6875 opts := zap.Options {
6976 // Development: true,
7077 }
78+
7179 opts .BindFlags (flag .CommandLine )
7280 flag .Parse ()
7381
82+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
83+ // More info:
84+ // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/server
85+ // - https://book.kubebuilder.io/reference/metrics.html
86+ metricsServerOptions := metricsserver.Options {
87+ BindAddress : metricsAddr ,
88+ SecureServing : secureMetrics ,
89+ // TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
90+ // not provided, self-signed certificates will be generated by default. This option is not recommended for
91+ // production environments as self-signed certificates do not offer the same level of trust and security
92+ // as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
93+ // unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
94+ // to provide certificates, ensuring the server communicates using trusted and secure certificates.
95+ TLSOpts : tlsOpts ,
96+ }
97+
98+ if secureMetrics {
99+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
100+ // These configurations ensure that only authorized users and service accounts
101+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
102+ // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/filters#WithAuthenticationAndAuthorization
103+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
104+ }
105+
74106 ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
75107
76108 setupLog .Info ("Controller domain setup" , "value" , controllerDomain )
77109
78110 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
79- Scheme : scheme ,
80- Metrics : metricsserver.Options {
81- BindAddress : metricsAddr ,
82- },
111+ Scheme : scheme ,
112+ Metrics : metricsServerOptions ,
83113 HealthProbeBindAddress : probeAddr ,
84114 LeaderElection : enableLeaderElection ,
85115 LeaderElectionID : "4e7eab13.checklyhq.com" ,
@@ -146,7 +176,7 @@ func main() {
146176 setupLog .Error (err , "unable to create controller" , "controller" , "AlertChannel" )
147177 os .Exit (1 )
148178 }
149- //+ kubebuilder:scaffold:builder
179+ //kubebuilder:scaffold:builder
150180
151181 setupLog .V (1 ).Info ("starting health endpoint" )
152182 if err := mgr .AddHealthzCheck ("healthz" , healthz .Ping ); err != nil {
0 commit comments