@@ -17,6 +17,7 @@ limitations under the License.
1717package main
1818
1919import (
20+ "crypto/tls"
2021 "flag"
2122 "fmt"
2223 "os"
@@ -65,6 +66,7 @@ func init() {
6566
6667func main () {
6768 var metricsAddr string
69+ var secureMetrics bool
6870 var enableLeaderElection bool
6971 var probeAddr string
7072 var pprofActive bool
@@ -76,8 +78,9 @@ func main() {
7678 var leaseDurationSecond int
7779 var renewDeadlineSecond int
7880
81+ var tlsOpts []func (* tls.Config )
82+
7983 flag .StringVar (& logEncoder , "log-encoder" , "json" , "log encoding ('json' or 'console')" )
80- flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
8184 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
8285 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
8386 "Enable leader election for controller manager. " +
@@ -86,6 +89,36 @@ func main() {
8689 flag .IntVar (& logLevel , "log-level" , int (zapcore .InfoLevel ), "set log level" )
8790 flag .IntVar (& leaseDurationSecond , "lease-duration" , int (leaseDurationSecond ), "manager lease duration in seconds" )
8891 flag .IntVar (& renewDeadlineSecond , "renew-duration" , int (renewDeadlineSecond ), "manager renew duration in seconds" )
92+ flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metrics endpoint binds to. " +
93+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
94+ flag .BoolVar (& secureMetrics , "metrics-secure" , false ,
95+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
96+
97+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
98+ // More info:
99+ // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/server
100+ // - https://book.kubebuilder.io/reference/metrics.html
101+ metricsServerOptions := metricsserver.Options {
102+ BindAddress : metricsAddr ,
103+ SecureServing : secureMetrics ,
104+ // TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
105+ // not provided, self-signed certificates will be generated by default. This option is not recommended for
106+ // production environments as self-signed certificates do not offer the same level of trust and security
107+ // as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
108+ // unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
109+ // to provide certificates, ensuring the server communicates using trusted and secure certificates.
110+ TLSOpts : tlsOpts ,
111+ FilterProvider : filters .WithAuthenticationAndAuthorization ,
112+ }
113+
114+ // TODO: enable https for /metrics endpoint by default
115+ // if secureMetrics {
116+ // // FilterProvider is used to protect the metrics endpoint with authn/authz.
117+ // // These configurations ensure that only authorized users and service accounts
118+ // // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
119+ // // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/filters#WithAuthenticationAndAuthorization
120+ // metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
121+ // }
89122
90123 // see https://github.com/operator-framework/operator-sdk/issues/1813
91124 if leaseDurationSecond < 30 {
@@ -111,10 +144,7 @@ func main() {
111144 ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
112145
113146 baseOptions := ctrl.Options {
114- Metrics : metricsserver.Options {
115- BindAddress : metricsAddr ,
116- FilterProvider : filters .WithAuthenticationAndAuthorization ,
117- },
147+ Metrics : metricsServerOptions ,
118148 Scheme : scheme ,
119149 HealthProbeBindAddress : probeAddr ,
120150 LeaderElection : enableLeaderElection ,
0 commit comments