88 "flag"
99 "fmt"
1010 "os"
11+ "path/filepath"
1112 "time"
1213
1314 corev1alpha1 "github.com/ironcore-dev/ironcore/api/core/v1alpha1"
@@ -29,6 +30,7 @@ import (
2930 quotaevaluatorironcore "github.com/ironcore-dev/ironcore/internal/quota/evaluator/ironcore"
3031 "github.com/ironcore-dev/ironcore/utils/quota"
3132 "k8s.io/utils/lru"
33+ "sigs.k8s.io/controller-runtime/pkg/certwatcher"
3234 "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3335 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3436
@@ -103,6 +105,7 @@ func init() {
103105func main () {
104106 var metricsAddr string
105107 var secureMetrics bool
108+ var metricsCertPath , metricsCertName , metricsCertKey string
106109 var enableHTTP2 bool
107110 var enableLeaderElection bool
108111 var probeAddr string
@@ -115,8 +118,12 @@ func main() {
115118 "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
116119 flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
117120 "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
121+ flag .StringVar (& metricsCertPath , "metrics-cert-path" , "" ,
122+ "The directory that contains the metrics server certificate." )
123+ flag .StringVar (& metricsCertName , "metrics-cert-name" , "tls.crt" , "The name of the metrics server certificate file." )
124+ flag .StringVar (& metricsCertKey , "metrics-cert-key" , "tls.key" , "The name of the metrics server key file." )
118125 flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
119- "If set, HTTP/2 will be enabled for the metrics and webhook servers " )
126+ "If set, HTTP/2 will be enabled for the metrics server " )
120127 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
121128 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
122129 "Enable leader election for controller manager. " +
@@ -191,7 +198,7 @@ func main() {
191198 tlsOpts = append (tlsOpts , disableHTTP2 )
192199 }
193200
194- // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
201+ // Metrics endpoint is enabled in 'config/controller/ default/kustomization.yaml'. The Metrics options configure the server.
195202 // More info:
196203 // - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/server 197204 // - https://book.kubebuilder.io/reference/metrics.html
@@ -207,10 +214,37 @@ func main() {
207214 // can access the metrics endpoint. The RBAC are configured in 'config/controller/rbac/kustomization.yaml'. More info:
208215 // https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/filters#WithAuthenticationAndAuthorization 209216 metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
217+ }
218+
219+ // If the certificate is not specified, controller-runtime will automatically
220+ // generate self-signed certificates for the metrics server. While convenient for development and testing,
221+ // this setup is not recommended for production.
222+ //
223+ // TODO(user): If you enable certManager, uncomment the following lines:
224+ // - [METRICS-WITH-CERTS] at config/controller/default/kustomization.yaml to generate and use certificates
225+ // managed by cert-manager for the metrics server.
226+ // - [PROMETHEUS-WITH-CERTS] at config/controller/prometheus/kustomization.yaml for TLS certification.
227+
228+ // Create watchers for metrics certificates
229+ var metricsCertWatcher * certwatcher.CertWatcher
230+
231+ if len (metricsCertPath ) > 0 {
232+ setupLog .Info ("Initializing metrics certificate watcher using provided certificates" ,
233+ "metrics-cert-path" , metricsCertPath , "metrics-cert-name" , metricsCertName , "metrics-cert-key" , metricsCertKey )
234+
235+ var err error
236+ metricsCertWatcher , err = certwatcher .New (
237+ filepath .Join (metricsCertPath , metricsCertName ),
238+ filepath .Join (metricsCertPath , metricsCertKey ),
239+ )
240+ if err != nil {
241+ setupLog .Error (err , "to initialize metrics certificate watcher" , "error" , err )
242+ os .Exit (1 )
243+ }
210244
211- // TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
212- // generate self-signed certificates for the metrics server. While convenient for development and testing,
213- // this setup is not recommended for production.
245+ metricsServerOptions . TLSOpts = append ( metricsServerOptions . TLSOpts , func ( config * tls. Config ) {
246+ config . GetCertificate = metricsCertWatcher . GetCertificate
247+ })
214248 }
215249
216250 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
@@ -226,6 +260,14 @@ func main() {
226260 os .Exit (1 )
227261 }
228262
263+ if metricsCertWatcher != nil {
264+ setupLog .Info ("Adding metrics certificate watcher to manager" )
265+ if err := mgr .Add (metricsCertWatcher ); err != nil {
266+ setupLog .Error (err , "unable to add metrics certificate watcher to manager" )
267+ os .Exit (1 )
268+ }
269+ }
270+
229271 // Register controllers
230272
231273 // compute controllers
0 commit comments