Skip to content

Commit 732a5ee

Browse files
feat/fix: enhance cert-manager integration for metrics endpoints (follow-up to PR kubernetes-sigs#4243)
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
1 parent d240946 commit 732a5ee

File tree

73 files changed

+1646
-768
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

+1646
-768
lines changed

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

Lines changed: 11 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 '55,168s/^#//' $KUSTOMIZATION_FILE_PATH
46-
sed -i '170,185s/^#//' $KUSTOMIZATION_FILE_PATH
46+
sed -i '57,210s/^#//' $KUSTOMIZATION_FILE_PATH
47+
sed -i '212,227s/^#//' $KUSTOMIZATION_FILE_PATH
4748
cd testdata/project-v4/
4849
go mod tidy
4950
@@ -85,10 +86,11 @@ 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 '55,121s/^#//' $KUSTOMIZATION_FILE_PATH
89+
sed -i '57,57s/^#//' $KUSTOMIZATION_FILE_PATH
90+
sed -i '133,162s/^#//' $KUSTOMIZATION_FILE_PATH
8991
# Uncomment only --conversion webhooks CA injection
90-
sed -i '153,168s/^#//' $KUSTOMIZATION_FILE_PATH
91-
sed -i '170,185s/^#//' $KUSTOMIZATION_FILE_PATH
92+
sed -i '195,210s/^#//' $KUSTOMIZATION_FILE_PATH
93+
sed -i '212,227s/^#//' $KUSTOMIZATION_FILE_PATH
9294
cd testdata/project-v4-with-plugins/
9395
go mod tidy
9496
@@ -127,9 +129,10 @@ jobs:
127129
run: |
128130
KUSTOMIZATION_FILE_PATH="testdata/project-v4-multigroup/config/default/kustomization.yaml"
129131
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
130-
# Uncomment all cert-manager injections
131-
sed -i '55,168s/^#//' $KUSTOMIZATION_FILE_PATH
132-
sed -i '170,185s/^#//' $KUSTOMIZATION_FILE_PATH
132+
# Uncomment all cert-manager injections for webhooks only
133+
sed -i '57,57s/^#//' $KUSTOMIZATION_FILE_PATH
134+
sed -i '96,210s/^#//' $KUSTOMIZATION_FILE_PATH
135+
sed -i '212,227s/^#//' $KUSTOMIZATION_FILE_PATH
133136
cd testdata/project-v4-multigroup
134137
go mod tidy
135138

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

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/tls"
2222
"flag"
2323
"os"
24+
"path/filepath"
2425

2526
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2627
// to ensure that exec-entrypoint and run can make use of them.
@@ -30,6 +31,7 @@ import (
3031
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3132
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3233
ctrl "sigs.k8s.io/controller-runtime"
34+
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
3335
"sigs.k8s.io/controller-runtime/pkg/healthz"
3436
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3537
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -74,6 +76,7 @@ func main() {
7476
/*
7577
*/
7678
var metricsAddr string
79+
var metricsCertPath, metricsCertName, metricsCertKey string
7780
var enableLeaderElection bool
7881
var probeAddr string
7982
var secureMetrics bool
@@ -87,6 +90,9 @@ func main() {
8790
"Enabling this will ensure there is only one active controller manager.")
8891
flag.BoolVar(&secureMetrics, "metrics-secure", true,
8992
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
93+
flag.StringVar(&metricsCertPath, "metrics-cert-path", "", "The directory that contains the metrics server certificate.")
94+
flag.StringVar(&metricsCertName, "metrics-cert-name", "tls.crt", "The name of the metrics server certificate file.")
95+
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
9096
flag.BoolVar(&enableHTTP2, "enable-http2", false,
9197
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
9298
opts := zap.Options{
@@ -112,6 +118,9 @@ func main() {
112118
tlsOpts = append(tlsOpts, disableHTTP2)
113119
}
114120

121+
// Create watchers for metrics certificates
122+
var metricsCertWatcher *certwatcher.CertWatcher
123+
115124
webhookServer := webhook.NewServer(webhook.Options{
116125
TLSOpts: tlsOpts,
117126
})
@@ -124,25 +133,38 @@ func main() {
124133
BindAddress: metricsAddr,
125134
SecureServing: secureMetrics,
126135
TLSOpts: tlsOpts,
127-
}
128-
129-
if secureMetrics {
130136
// FilterProvider is used to protect the metrics endpoint with authn/authz.
131137
// These configurations ensure that only authorized users and service accounts
132138
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
133139
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization
134-
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
135-
136-
// TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
137-
// generate self-signed certificates for the metrics server. While convenient for development and testing,
138-
// this setup is not recommended for production.
140+
FilterProvider: filters.WithAuthenticationAndAuthorization,
141+
}
139142

140-
// TODO(user): If cert-manager is enabled in config/default/kustomization.yaml,
141-
// you can uncomment the following lines to use the certificate managed by cert-manager.
142-
// metricsServerOptions.CertDir = "/tmp/k8s-metrics-server/metrics-certs"
143-
// metricsServerOptions.CertName = "tls.crt"
144-
// metricsServerOptions.KeyName = "tls.key"
143+
// If the certificate is not specified, controller-runtime will automatically
144+
// generate self-signed certificates for the metrics server. While convenient for development and testing,
145+
// this setup is not recommended for production.
146+
//
147+
// TODO(user): If you enable certManager, uncomment the following lines:
148+
// - [METRICS-WITH-CERTS] at config/default/kustomization.yaml to generate and use certificates
149+
// managed by cert-manager for the metrics server.
150+
// - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification.
151+
if len(metricsCertPath) > 0 {
152+
setupLog.Info("Initializing metrics certificate watcher using provided certificates",
153+
"metrics-cert-path", metricsCertPath, "metrics-cert-name", metricsCertName, "metrics-cert-key", metricsCertKey)
154+
155+
var err error
156+
metricsCertWatcher, err = certwatcher.New(
157+
filepath.Join(metricsCertPath, metricsCertName),
158+
filepath.Join(metricsCertPath, metricsCertKey),
159+
)
160+
if err != nil {
161+
setupLog.Error(err, "to initialize metrics certificate watcher", "error", err)
162+
os.Exit(1)
163+
}
145164

165+
metricsServerOptions.TLSOpts = append(metricsServerOptions.TLSOpts, func(config *tls.Config) {
166+
config.GetCertificate = metricsCertWatcher.GetCertificate
167+
})
146168
}
147169

148170
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
@@ -196,6 +218,14 @@ func main() {
196218
}
197219
// +kubebuilder:scaffold:builder
198220

221+
if metricsCertWatcher != nil {
222+
setupLog.Info("Adding metrics certificate watcher to manager")
223+
if err := mgr.Add(metricsCertWatcher); err != nil {
224+
setupLog.Error(err, "unable to add metrics certificate watcher to manager")
225+
os.Exit(1)
226+
}
227+
}
228+
199229
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
200230
setupLog.Error(err, "unable to set up health check")
201231
os.Exit(1)
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 # this secret will not be prefixed, since it's not managed by kustomize
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 # this secret will not be prefixed, since it's not managed by kustomize

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)