@@ -33,6 +33,7 @@ import (
3333 ctrl "sigs.k8s.io/controller-runtime"
3434 "sigs.k8s.io/controller-runtime/pkg/healthz"
3535 "sigs.k8s.io/controller-runtime/pkg/log/zap"
36+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3637 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3738 "sigs.k8s.io/controller-runtime/pkg/webhook"
3839
@@ -65,14 +66,15 @@ func main() {
6566 var probeAddr string
6667 var secureMetrics bool
6768 var enableHTTP2 bool
68- flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metric endpoint binds to. " +
69- "Use the port :8080. If not set, it will be 0 in order to disable the metrics server" )
69+ var tlsOpts []func (* tls.Config )
70+ flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
71+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
7072 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
7173 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
7274 "Enable leader election for controller manager. " +
7375 "Enabling this will ensure there is only one active controller manager." )
74- flag .BoolVar (& secureMetrics , "metrics-secure" , false ,
75- "If set the metrics endpoint is served securely" )
76+ flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
77+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead. " )
7678 flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
7779 "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
7880 opts := zap.Options {
@@ -95,7 +97,6 @@ func main() {
9597 c .NextProtos = []string {"http/1.1" }
9698 }
9799
98- tlsOpts := []func (* tls.Config ){}
99100 if ! enableHTTP2 {
100101 tlsOpts = append (tlsOpts , disableHTTP2 )
101102 }
@@ -104,13 +105,33 @@ func main() {
104105 TLSOpts : tlsOpts ,
105106 })
106107
108+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
109+ // More info:
110+ // - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/server 111+ // - https://book.kubebuilder.io/reference/metrics.html
112+ metricsServerOptions := metricsserver.Options {
113+ BindAddress : metricsAddr ,
114+ SecureServing : secureMetrics ,
115+ // TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
116+ // not provided, self-signed certificates will be generated by default. This option is not recommended for
117+ // production environments as self-signed certificates do not offer the same level of trust and security
118+ // as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
119+ // unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
120+ // to provide certificates, ensuring the server communicates using trusted and secure certificates.
121+ TLSOpts : tlsOpts ,
122+ }
123+
124+ if secureMetrics {
125+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
126+ // These configurations ensure that only authorized users and service accounts
127+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
128+ // https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/filters#WithAuthenticationAndAuthorization 129+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
130+ }
131+
107132 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
108- Scheme : scheme ,
109- Metrics : metricsserver.Options {
110- BindAddress : metricsAddr ,
111- SecureServing : secureMetrics ,
112- TLSOpts : tlsOpts ,
113- },
133+ Scheme : scheme ,
134+ Metrics : metricsServerOptions ,
114135 WebhookServer : webhookServer ,
115136 HealthProbeBindAddress : probeAddr ,
116137 LeaderElection : enableLeaderElection ,
0 commit comments