-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance
cert-manager
integration for metrics endpoints
- Loading branch information
Showing
43 changed files
with
1,027 additions
and
410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
corev1alpha1 "github.com/ironcore-dev/ironcore/api/core/v1alpha1" | ||
|
@@ -29,6 +30,7 @@ import ( | |
quotaevaluatorironcore "github.com/ironcore-dev/ironcore/internal/quota/evaluator/ironcore" | ||
"github.com/ironcore-dev/ironcore/utils/quota" | ||
"k8s.io/utils/lru" | ||
"sigs.k8s.io/controller-runtime/pkg/certwatcher" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics/filters" | ||
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
|
||
|
@@ -103,6 +105,7 @@ func init() { | |
func main() { | ||
var metricsAddr string | ||
var secureMetrics bool | ||
var metricsCertPath, metricsCertName, metricsCertKey string | ||
var enableHTTP2 bool | ||
var enableLeaderElection bool | ||
var probeAddr string | ||
|
@@ -115,8 +118,12 @@ func main() { | |
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.") | ||
flag.BoolVar(&secureMetrics, "metrics-secure", true, | ||
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.") | ||
flag.StringVar(&metricsCertPath, "metrics-cert-path", "", | ||
"The directory that contains the metrics server certificate.") | ||
flag.StringVar(&metricsCertName, "metrics-cert-name", "tls.crt", "The name of the metrics server certificate file.") | ||
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.") | ||
flag.BoolVar(&enableHTTP2, "enable-http2", false, | ||
"If set, HTTP/2 will be enabled for the metrics and webhook servers") | ||
"If set, HTTP/2 will be enabled for the metrics server") | ||
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") | ||
flag.BoolVar(&enableLeaderElection, "leader-elect", false, | ||
"Enable leader election for controller manager. "+ | ||
|
@@ -191,7 +198,7 @@ func main() { | |
tlsOpts = append(tlsOpts, disableHTTP2) | ||
} | ||
|
||
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server. | ||
// Metrics endpoint is enabled in 'config/controller/default/kustomization.yaml'. The Metrics options configure the server. | ||
// More info: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/server | ||
// - https://book.kubebuilder.io/reference/metrics.html | ||
|
@@ -207,10 +214,37 @@ func main() { | |
// can access the metrics endpoint. The RBAC are configured in 'config/controller/rbac/kustomization.yaml'. More info: | ||
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization | ||
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization | ||
} | ||
|
||
// If the certificate is not specified, controller-runtime will automatically | ||
// generate self-signed certificates for the metrics server. While convenient for development and testing, | ||
// this setup is not recommended for production. | ||
// | ||
// TODO(user): If you enable certManager, uncomment the following lines: | ||
// - [METRICS-WITH-CERTS] at config/controller/default/kustomization.yaml to generate and use certificates | ||
// managed by cert-manager for the metrics server. | ||
// - [PROMETHEUS-WITH-CERTS] at config/controller/prometheus/kustomization.yaml for TLS certification. | ||
|
||
// Create watchers for metrics certificates | ||
var metricsCertWatcher *certwatcher.CertWatcher | ||
|
||
if len(metricsCertPath) > 0 { | ||
setupLog.Info("Initializing metrics certificate watcher using provided certificates", | ||
"metrics-cert-path", metricsCertPath, "metrics-cert-name", metricsCertName, "metrics-cert-key", metricsCertKey) | ||
|
||
var err error | ||
metricsCertWatcher, err = certwatcher.New( | ||
filepath.Join(metricsCertPath, metricsCertName), | ||
filepath.Join(metricsCertPath, metricsCertKey), | ||
) | ||
if err != nil { | ||
setupLog.Error(err, "to initialize metrics certificate watcher", "error", err) | ||
os.Exit(1) | ||
} | ||
|
||
// TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically | ||
// generate self-signed certificates for the metrics server. While convenient for development and testing, | ||
// this setup is not recommended for production. | ||
metricsServerOptions.TLSOpts = append(metricsServerOptions.TLSOpts, func(config *tls.Config) { | ||
config.GetCertificate = metricsCertWatcher.GetCertificate | ||
}) | ||
} | ||
|
||
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ | ||
|
@@ -226,6 +260,14 @@ func main() { | |
os.Exit(1) | ||
} | ||
|
||
if metricsCertWatcher != nil { | ||
setupLog.Info("Adding metrics certificate watcher to manager") | ||
if err := mgr.Add(metricsCertWatcher); err != nil { | ||
setupLog.Error(err, "unable to add metrics certificate watcher to manager") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// Register controllers | ||
|
||
// compute controllers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
resources: | ||
- certificate.yaml | ||
- certificate-metrics.yaml | ||
|
||
configurations: | ||
- kustomizeconfig.yaml |
30 changes: 30 additions & 0 deletions
30
config/bucketpoollet-broker/default/cert_metrics_manager_patch.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This patch adds the args, volumes, and ports to allow the manager to use the metrics-server certs. | ||
|
||
# Add the volumeMount for the metrics-server certs | ||
- op: add | ||
path: /spec/template/spec/containers/0/volumeMounts/- | ||
value: | ||
mountPath: /tmp/k8s-metrics-server/metrics-certs | ||
name: metrics-certs | ||
readOnly: true | ||
|
||
# Add the --metrics-cert-path argument for the metrics server | ||
- op: add | ||
path: /spec/template/spec/containers/0/args/- | ||
value: --metrics-cert-path=/tmp/k8s-metrics-server/metrics-certs | ||
|
||
# Add the metrics-server certs volume configuration | ||
- op: add | ||
path: /spec/template/spec/volumes/- | ||
value: | ||
name: metrics-certs | ||
secret: | ||
secretName: metrics-server-cert | ||
optional: false | ||
items: | ||
- key: ca.crt | ||
path: ca.crt | ||
- key: tls.crt | ||
path: tls.crt | ||
- key: tls.key | ||
path: tls.key |
Oops, something went wrong.