@@ -31,6 +31,7 @@ import (
3131 ctrl "sigs.k8s.io/controller-runtime"
3232 "sigs.k8s.io/controller-runtime/pkg/healthz"
3333 "sigs.k8s.io/controller-runtime/pkg/log/zap"
34+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3435 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3536
3637 metalk8sscalitycomv1alpha1 "github.com/scality/metalk8s/operator/api/v1alpha1"
@@ -56,13 +57,15 @@ func main() {
5657 var probeAddr string
5758 var secureMetrics bool
5859 var enableHTTP2 bool
59- flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
60+ var tlsOpts []func (* tls.Config )
61+ flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metric endpoint binds to. " +
62+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
6063 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
6164 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
6265 "Enable leader election for controller manager. " +
6366 "Enabling this will ensure there is only one active controller manager." )
64- flag .BoolVar (& secureMetrics , "metrics-secure" , false ,
65- "If set the metrics endpoint is served securely" )
67+ flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
68+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead. " )
6669 flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
6770 "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
6871 opts := zap.Options {
@@ -84,18 +87,37 @@ func main() {
8487 c .NextProtos = []string {"http/1.1" }
8588 }
8689
87- tlsOpts := []func (* tls.Config ){}
8890 if ! enableHTTP2 {
8991 tlsOpts = append (tlsOpts , disableHTTP2 )
9092 }
9193
94+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
95+ // More info:
96+ // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/server
97+ // - https://book.kubebuilder.io/reference/metrics.html
98+ metricsServerOptions := metricsserver.Options {
99+ BindAddress : metricsAddr ,
100+ SecureServing : secureMetrics ,
101+ // TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
102+ // not provided, self-signed certificates will be generated by default. This option is not recommended for
103+ // production environments as self-signed certificates do not offer the same level of trust and security
104+ // as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
105+ // unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
106+ // to provide certificates, ensuring the server communicates using trusted and secure certificates.
107+ TLSOpts : tlsOpts ,
108+ }
109+
110+ if secureMetrics {
111+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
112+ // These configurations ensure that only authorized users and service accounts
113+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
114+ // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/filters#WithAuthenticationAndAuthorization
115+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
116+ }
117+
92118 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
93- Scheme : scheme ,
94- Metrics : metricsserver.Options {
95- BindAddress : metricsAddr ,
96- SecureServing : secureMetrics ,
97- TLSOpts : tlsOpts ,
98- },
119+ Scheme : scheme ,
120+ Metrics : metricsServerOptions ,
99121 HealthProbeBindAddress : probeAddr ,
100122 LeaderElection : enableLeaderElection ,
101123 LeaderElectionID : "02367e3e.metalk8s.scality.com" ,
0 commit comments