Skip to content

Commit 752cb0a

Browse files
committed
Enhance cert-manager integration for metrics endpoints
1 parent 2e4d763 commit 752cb0a

Some content is hidden

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

43 files changed

+1027
-410
lines changed

cmd/ironcore-controller-manager/main.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
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() {
103105
func 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

config/apiserver/certmanager/certificate.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ kind: Certificate
1414
metadata:
1515
name: apiserver-cert # this name should match the one appeared in kustomizeconfig.yaml
1616
namespace: system
17-
spec: # $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize
17+
spec: # SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
1818
dnsNames:
19-
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
20-
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
19+
- SERVICE_NAME.SERVICE_NAMESPACE.svc
20+
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
2121
issuerRef:
2222
kind: Issuer
2323
name: apiserver-selfsigned-issuer

config/apiserver/default/kustomization.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ namespace: ironcore-system
99
namePrefix: ironcore-
1010

1111
# Labels to add to all resources and selectors.
12-
#commonLabels:
13-
# someName: someValue
12+
#labels:
13+
#- includeSelectors: true
14+
# pairs:
15+
# someName: someValue
1416

1517
resources:
1618
- ../rbac

config/apiserver/etcdless/kustomization.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ namespace: ironcore-system
99
namePrefix: ironcore-
1010

1111
# Labels to add to all resources and selectors.
12-
#commonLabels:
13-
# someName: someValue
12+
#labels:
13+
#- includeSelectors: true
14+
# pairs:
15+
# someName: someValue
1416

1517
resources:
1618
- ../rbac

config/bucketpoollet-broker/certmanager/certificate.yaml renamed to config/bucketpoollet-broker/certmanager/certificate-metrics.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ spec:
1212
apiVersion: cert-manager.io/v1
1313
kind: Certificate
1414
metadata:
15-
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
15+
name: metrics-certs # this name should match the one appeared in kustomizeconfig.yaml
1616
namespace: system
17-
spec: # $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize
17+
spec: # SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
1818
dnsNames:
19-
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
20-
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
19+
- SERVICE_NAME.SERVICE_NAMESPACE.svc
20+
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
2121
issuerRef:
2222
kind: Issuer
2323
name: selfsigned-issuer
24-
secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize
24+
secretName: metrics-server-cert # this secret will not be prefixed, since it's not managed by kustomize
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resources:
2-
- certificate.yaml
2+
- certificate-metrics.yaml
33

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

0 commit comments

Comments
 (0)