forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/fix: enhance cert-manager integration for metrics endpoints (fol…
…low-up to PR kubernetes-sigs#4243) feat/fix: enhance cert-manager integration for metrics endpoints This commit is a follow-up to PR kubernetes-sigs#4243, which introduced support for using cert-manager certificates for securing the metrics endpoint and ServiceMonitor. Related to kubernetes-sigs#3871 and kubernetes-sigs#4003 Key enhancements: - Added support for configuring certificate integration via a Kustomize patch. - Introduced configurable flags for greater flexibility in customization. - Use Certwatcher to allow certificate rotation This configuration provides an option for users to be production-ready. These improvements enhance usability and adaptability while maintaining compatibility with the initial implementation. As the feature has not yet been released, this update ensures a polished and user-friendly integration for upcoming releases.
- Loading branch information
1 parent
e0b86dc
commit 442dea3
Showing
73 changed files
with
1,587 additions
and
746 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
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 |
---|---|---|
|
@@ -76,6 +76,7 @@ func main() { | |
/* | ||
*/ | ||
var metricsAddr string | ||
var metricsCertPath, metricsCertName, metricsCertKey string | ||
var webhookCertPath, webhookCertName, webhookCertKey string | ||
var enableLeaderElection bool | ||
var probeAddr string | ||
|
@@ -93,6 +94,9 @@ func main() { | |
flag.StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.") | ||
flag.StringVar(&webhookCertName, "webhook-cert-name", "tls.crt", "The name of the webhook certificate file.") | ||
flag.StringVar(&webhookCertKey, "webhook-cert-key", "tls.key", "The name of the webhook key file.") | ||
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") | ||
opts := zap.Options{ | ||
|
@@ -118,8 +122,8 @@ func main() { | |
tlsOpts = append(tlsOpts, disableHTTP2) | ||
} | ||
|
||
// Create watchers for metrics certificates | ||
var webhookCertWatcher *certwatcher.CertWatcher | ||
// Create watchers for metrics and webhooks certificates | ||
var metricsCertWatcher, webhookCertWatcher *certwatcher.CertWatcher | ||
|
||
// Initial webhook TLS options | ||
webhookTLSOpts := append([]func(*tls.Config){}, tlsOpts...) | ||
|
@@ -163,17 +167,33 @@ func main() { | |
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info: | ||
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization | ||
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization | ||
} | ||
|
||
// 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. | ||
// 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/default/kustomization.yaml to generate and use certificates | ||
// managed by cert-manager for the metrics server. | ||
// - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification. | ||
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) | ||
|
||
// TODO(user): If cert-manager is enabled in config/default/kustomization.yaml, | ||
// you can uncomment the following lines to use the certificate managed by cert-manager. | ||
// metricsServerOptions.CertDir = "/tmp/k8s-metrics-server/metrics-certs" | ||
// metricsServerOptions.CertName = "tls.crt" | ||
// metricsServerOptions.KeyName = "tls.key" | ||
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) | ||
} | ||
|
||
metricsServerOptions.TLSOpts = append(metricsServerOptions.TLSOpts, func(config *tls.Config) { | ||
config.GetCertificate = metricsCertWatcher.GetCertificate | ||
}) | ||
} | ||
|
||
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ | ||
|
@@ -227,6 +247,14 @@ func main() { | |
} | ||
// +kubebuilder:scaffold:builder | ||
|
||
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) | ||
} | ||
} | ||
|
||
if webhookCertWatcher != nil { | ||
setupLog.Info("Adding webhook certificate watcher to manager") | ||
if err := mgr.Add(webhookCertWatcher); err != nil { | ||
|
20 changes: 20 additions & 0 deletions
20
docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/certificate-metrics.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,20 @@ | ||
# The following manifests contain a self-signed issuer CR and a metrics certificate CR. | ||
# More document can be found at https://docs.cert-manager.io | ||
apiVersion: cert-manager.io/v1 | ||
kind: Certificate | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: metrics-certs # this name should match the one appeared in kustomizeconfig.yaml | ||
namespace: system | ||
spec: | ||
dnsNames: | ||
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize | ||
# replacements in the config/default/kustomization.yaml file. | ||
- SERVICE_NAME.SERVICE_NAMESPACE.svc | ||
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local | ||
issuerRef: | ||
kind: Issuer | ||
name: selfsigned-issuer | ||
secretName: metrics-server-cert |
20 changes: 20 additions & 0 deletions
20
docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/certificate-webhook.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,20 @@ | ||
# The following manifests contain a self-signed issuer CR and a certificate CR. | ||
# More document can be found at https://docs.cert-manager.io | ||
apiVersion: cert-manager.io/v1 | ||
kind: Certificate | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml | ||
namespace: system | ||
spec: | ||
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize | ||
# replacements in the config/default/kustomization.yaml file. | ||
dnsNames: | ||
- SERVICE_NAME.SERVICE_NAMESPACE.svc | ||
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local | ||
issuerRef: | ||
kind: Issuer | ||
name: selfsigned-issuer | ||
secretName: webhook-server-cert |
57 changes: 0 additions & 57 deletions
57
docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/certificate.yaml
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/issuer.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,13 @@ | ||
# The following manifest contains a self-signed issuer CR. | ||
# More information can be found at https://docs.cert-manager.io | ||
# WARNING: Targets CertManager v1.0. Check https://cert-manager.io/docs/installation/upgrading/ for breaking changes. | ||
apiVersion: cert-manager.io/v1 | ||
kind: Issuer | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: selfsigned-issuer | ||
namespace: system | ||
spec: | ||
selfSigned: {} |
4 changes: 3 additions & 1 deletion
4
docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/kustomization.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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
resources: | ||
- certificate.yaml | ||
- issuer.yaml | ||
- certificate-webhook.yaml | ||
- certificate-metrics.yaml | ||
|
||
configurations: | ||
- kustomizeconfig.yaml |
31 changes: 31 additions & 0 deletions
31
...book/src/cronjob-tutorial/testdata/project/config/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,31 @@ | ||
# This patch adds the args and volumes to allow the manager to use the metrics-server certs | ||
# Ensure the volumeMounts field exists by creating it if missing | ||
- op: add | ||
path: /spec/template/spec/containers/0/volumeMounts | ||
value: [] | ||
- op: add | ||
path: /spec/template/spec/containers/0/volumeMounts/- | ||
value: | ||
mountPath: /tmp/k8s-metrics-server/metrics-certs | ||
name: metrics-certs | ||
readOnly: true | ||
- op: add | ||
path: /spec/template/spec/containers/0/args/- | ||
value: --metrics-cert-path=/tmp/k8s-metrics-server/metrics-certs | ||
- op: add | ||
path: /spec/template/spec/volumes | ||
value: [] | ||
- 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 |
21 changes: 0 additions & 21 deletions
21
...c/cronjob-tutorial/testdata/project/config/default/certmanager_metrics_manager_patch.yaml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.