Skip to content

Commit 442dea3

Browse files
feat/fix: enhance cert-manager integration for metrics endpoints (follow-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.
1 parent e0b86dc commit 442dea3

File tree

73 files changed

+1587
-746
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1587
-746
lines changed

.github/workflows/test-e2e-samples.yml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ jobs:
4141
run: |
4242
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
4343
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
44+
sed -i '47,49s/^#//' $KUSTOMIZATION_FILE_PATH
4445
# Uncomment all cert-manager injections
45-
sed -i '57,170s/^#//' $KUSTOMIZATION_FILE_PATH
46-
sed -i '172,187s/^#//' $KUSTOMIZATION_FILE_PATH
46+
sed -i '59,212s/^#//' $KUSTOMIZATION_FILE_PATH
47+
sed -i '214,229s/^#//' $KUSTOMIZATION_FILE_PATH
4748
cd testdata/project-v4/
4849
go mod tidy
4950
@@ -85,10 +86,10 @@ jobs:
8586
# Uncomment only ValidatingWebhookConfiguration
8687
# from cert-manager replaces; we are leaving defaulting uncommented
8788
# since this sample has no defaulting webhooks
88-
sed -i '57,123s/^#//' $KUSTOMIZATION_FILE_PATH
89+
sed -i '59,164s/^#//' $KUSTOMIZATION_FILE_PATH
8990
# Uncomment only --conversion webhooks CA injection
90-
sed -i '155,170s/^#//' $KUSTOMIZATION_FILE_PATH
91-
sed -i '172,187s/^#//' $KUSTOMIZATION_FILE_PATH
91+
sed -i '197,212s/^#//' $KUSTOMIZATION_FILE_PATH
92+
sed -i '214,229s/^#//' $KUSTOMIZATION_FILE_PATH
9293
cd testdata/project-v4-with-plugins/
9394
go mod tidy
9495
@@ -127,9 +128,10 @@ jobs:
127128
run: |
128129
KUSTOMIZATION_FILE_PATH="testdata/project-v4-multigroup/config/default/kustomization.yaml"
129130
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
130-
# Uncomment all cert-manager injections
131-
sed -i '57,170s/^#//' $KUSTOMIZATION_FILE_PATH
132-
sed -i '172,187s/^#//' $KUSTOMIZATION_FILE_PATH
131+
# Uncomment all cert-manager injections for webhooks only
132+
sed -i '59,59s/^#//' $KUSTOMIZATION_FILE_PATH
133+
sed -i '98,212s/^#//' $KUSTOMIZATION_FILE_PATH
134+
sed -i '214,229s/^#//' $KUSTOMIZATION_FILE_PATH
133135
cd testdata/project-v4-multigroup
134136
go mod tidy
135137

docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func main() {
7676
/*
7777
*/
7878
var metricsAddr string
79+
var metricsCertPath, metricsCertName, metricsCertKey string
7980
var webhookCertPath, webhookCertName, webhookCertKey string
8081
var enableLeaderElection bool
8182
var probeAddr string
@@ -93,6 +94,9 @@ func main() {
9394
flag.StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.")
9495
flag.StringVar(&webhookCertName, "webhook-cert-name", "tls.crt", "The name of the webhook certificate file.")
9596
flag.StringVar(&webhookCertKey, "webhook-cert-key", "tls.key", "The name of the webhook key file.")
97+
flag.StringVar(&metricsCertPath, "metrics-cert-path", "", "The directory that contains the metrics server certificate.")
98+
flag.StringVar(&metricsCertName, "metrics-cert-name", "tls.crt", "The name of the metrics server certificate file.")
99+
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
96100
flag.BoolVar(&enableHTTP2, "enable-http2", false,
97101
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
98102
opts := zap.Options{
@@ -118,8 +122,8 @@ func main() {
118122
tlsOpts = append(tlsOpts, disableHTTP2)
119123
}
120124

121-
// Create watchers for metrics certificates
122-
var webhookCertWatcher *certwatcher.CertWatcher
125+
// Create watchers for metrics and webhooks certificates
126+
var metricsCertWatcher, webhookCertWatcher *certwatcher.CertWatcher
123127

124128
// Initial webhook TLS options
125129
webhookTLSOpts := append([]func(*tls.Config){}, tlsOpts...)
@@ -163,17 +167,33 @@ func main() {
163167
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
164168
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization
165169
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
170+
}
166171

167-
// TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
168-
// generate self-signed certificates for the metrics server. While convenient for development and testing,
169-
// this setup is not recommended for production.
172+
// If the certificate is not specified, controller-runtime will automatically
173+
// generate self-signed certificates for the metrics server. While convenient for development and testing,
174+
// this setup is not recommended for production.
175+
//
176+
// TODO(user): If you enable certManager, uncomment the following lines:
177+
// - [METRICS-WITH-CERTS] at config/default/kustomization.yaml to generate and use certificates
178+
// managed by cert-manager for the metrics server.
179+
// - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification.
180+
if len(metricsCertPath) > 0 {
181+
setupLog.Info("Initializing metrics certificate watcher using provided certificates",
182+
"metrics-cert-path", metricsCertPath, "metrics-cert-name", metricsCertName, "metrics-cert-key", metricsCertKey)
170183

171-
// TODO(user): If cert-manager is enabled in config/default/kustomization.yaml,
172-
// you can uncomment the following lines to use the certificate managed by cert-manager.
173-
// metricsServerOptions.CertDir = "/tmp/k8s-metrics-server/metrics-certs"
174-
// metricsServerOptions.CertName = "tls.crt"
175-
// metricsServerOptions.KeyName = "tls.key"
184+
var err error
185+
metricsCertWatcher, err = certwatcher.New(
186+
filepath.Join(metricsCertPath, metricsCertName),
187+
filepath.Join(metricsCertPath, metricsCertKey),
188+
)
189+
if err != nil {
190+
setupLog.Error(err, "to initialize metrics certificate watcher", "error", err)
191+
os.Exit(1)
192+
}
176193

194+
metricsServerOptions.TLSOpts = append(metricsServerOptions.TLSOpts, func(config *tls.Config) {
195+
config.GetCertificate = metricsCertWatcher.GetCertificate
196+
})
177197
}
178198

179199
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
@@ -227,6 +247,14 @@ func main() {
227247
}
228248
// +kubebuilder:scaffold:builder
229249

250+
if metricsCertWatcher != nil {
251+
setupLog.Info("Adding metrics certificate watcher to manager")
252+
if err := mgr.Add(metricsCertWatcher); err != nil {
253+
setupLog.Error(err, "unable to add metrics certificate watcher to manager")
254+
os.Exit(1)
255+
}
256+
}
257+
230258
if webhookCertWatcher != nil {
231259
setupLog.Info("Adding webhook certificate watcher to manager")
232260
if err := mgr.Add(webhookCertWatcher); err != nil {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The following manifests contain a self-signed issuer CR and a metrics certificate CR.
2+
# More document can be found at https://docs.cert-manager.io
3+
apiVersion: cert-manager.io/v1
4+
kind: Certificate
5+
metadata:
6+
labels:
7+
app.kubernetes.io/name: project
8+
app.kubernetes.io/managed-by: kustomize
9+
name: metrics-certs # this name should match the one appeared in kustomizeconfig.yaml
10+
namespace: system
11+
spec:
12+
dnsNames:
13+
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
14+
# replacements in the config/default/kustomization.yaml file.
15+
- SERVICE_NAME.SERVICE_NAMESPACE.svc
16+
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
17+
issuerRef:
18+
kind: Issuer
19+
name: selfsigned-issuer
20+
secretName: metrics-server-cert
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The following manifests contain a self-signed issuer CR and a certificate CR.
2+
# More document can be found at https://docs.cert-manager.io
3+
apiVersion: cert-manager.io/v1
4+
kind: Certificate
5+
metadata:
6+
labels:
7+
app.kubernetes.io/name: project
8+
app.kubernetes.io/managed-by: kustomize
9+
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
10+
namespace: system
11+
spec:
12+
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
13+
# replacements in the config/default/kustomization.yaml file.
14+
dnsNames:
15+
- SERVICE_NAME.SERVICE_NAMESPACE.svc
16+
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
17+
issuerRef:
18+
kind: Issuer
19+
name: selfsigned-issuer
20+
secretName: webhook-server-cert

docs/book/src/cronjob-tutorial/testdata/project/config/certmanager/certificate.yaml

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# The following manifest contains a self-signed issuer CR.
2+
# More information can be found at https://docs.cert-manager.io
3+
# WARNING: Targets CertManager v1.0. Check https://cert-manager.io/docs/installation/upgrading/ for breaking changes.
4+
apiVersion: cert-manager.io/v1
5+
kind: Issuer
6+
metadata:
7+
labels:
8+
app.kubernetes.io/name: project
9+
app.kubernetes.io/managed-by: kustomize
10+
name: selfsigned-issuer
11+
namespace: system
12+
spec:
13+
selfSigned: {}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
resources:
2-
- certificate.yaml
2+
- issuer.yaml
3+
- certificate-webhook.yaml
4+
- certificate-metrics.yaml
35

46
configurations:
57
- kustomizeconfig.yaml
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This patch adds the args and volumes to allow the manager to use the metrics-server certs
2+
# Ensure the volumeMounts field exists by creating it if missing
3+
- op: add
4+
path: /spec/template/spec/containers/0/volumeMounts
5+
value: []
6+
- op: add
7+
path: /spec/template/spec/containers/0/volumeMounts/-
8+
value:
9+
mountPath: /tmp/k8s-metrics-server/metrics-certs
10+
name: metrics-certs
11+
readOnly: true
12+
- op: add
13+
path: /spec/template/spec/containers/0/args/-
14+
value: --metrics-cert-path=/tmp/k8s-metrics-server/metrics-certs
15+
- op: add
16+
path: /spec/template/spec/volumes
17+
value: []
18+
- op: add
19+
path: /spec/template/spec/volumes/-
20+
value:
21+
name: metrics-certs
22+
secret:
23+
secretName: metrics-server-cert
24+
optional: false
25+
items:
26+
- key: ca.crt
27+
path: ca.crt
28+
- key: tls.crt
29+
path: tls.crt
30+
- key: tls.key
31+
path: tls.key

docs/book/src/cronjob-tutorial/testdata/project/config/default/certmanager_metrics_manager_patch.yaml

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)