Skip to content

Commit 57892ba

Browse files
authored
Merge pull request #4243 from camilamacedo86/patch-cert-mamager-service-monitor
✨ Adds a patch to configure ServiceMonitor with to ensure TLS verification using cert-manager certificates
2 parents a9ee390 + 57fdc48 commit 57892ba

File tree

54 files changed

+859
-84
lines changed

Some content is hidden

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

54 files changed

+859
-84
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
run: |
4242
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
4343
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
44-
sed -i '50,177s/^#//' $KUSTOMIZATION_FILE_PATH
44+
sed -i '55,182s/^#//' $KUSTOMIZATION_FILE_PATH
4545
cd testdata/project-v4/
4646
go mod tidy
4747
@@ -82,8 +82,8 @@ jobs:
8282
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
8383
# Uncomment only ValidatingWebhookConfiguration
8484
# from cert-manager replaces
85-
sed -i '50,116s/^#//' $KUSTOMIZATION_FILE_PATH
86-
sed -i '148,177s/^#//' $KUSTOMIZATION_FILE_PATH
85+
sed -i '55,121s/^#//' $KUSTOMIZATION_FILE_PATH
86+
sed -i '153,182s/^#//' $KUSTOMIZATION_FILE_PATH
8787
cd testdata/project-v4-with-plugins/
8888
go mod tidy
8989
@@ -122,7 +122,7 @@ jobs:
122122
run: |
123123
KUSTOMIZATION_FILE_PATH="testdata/project-v4-multigroup/config/default/kustomization.yaml"
124124
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
125-
sed -i '50,177s/^#//' $KUSTOMIZATION_FILE_PATH
125+
sed -i '55,182s/^#//' $KUSTOMIZATION_FILE_PATH
126126
cd testdata/project-v4-multigroup
127127
go mod tidy
128128

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ func main() {
136136
// TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
137137
// generate self-signed certificates for the metrics server. While convenient for development and testing,
138138
// this setup is not recommended for production.
139+
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"
145+
139146
}
140147

141148
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,25 @@ spec:
3333
kind: Issuer
3434
name: selfsigned-issuer
3535
secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize
36+
---
37+
apiVersion: cert-manager.io/v1
38+
kind: Certificate
39+
metadata:
40+
labels:
41+
app.kubernetes.io/name: certificate
42+
app.kubernetes.io/instance: metrics-certs
43+
app.kubernetes.io/component: certificate
44+
app.kubernetes.io/created-by: project
45+
app.kubernetes.io/part-of: project
46+
app.kubernetes.io/managed-by: kustomize
47+
name: metrics-certs # this name should match the one appeared in kustomizeconfig.yaml
48+
namespace: system
49+
spec:
50+
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
51+
dnsNames:
52+
- controller-manager-metrics-service.system.svc
53+
- controller-manager-metrics-service.system.svc.cluster.local
54+
issuerRef:
55+
kind: Issuer
56+
name: selfsigned-issuer
57+
secretName: metrics-server-cert # this secret will not be prefixed, since it's not managed by kustomize
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller-manager
5+
namespace: system
6+
labels:
7+
app.kubernetes.io/name: project
8+
app.kubernetes.io/managed-by: kustomize
9+
spec:
10+
template:
11+
spec:
12+
containers:
13+
- name: manager
14+
volumeMounts:
15+
- mountPath: /tmp/k8s-metrics-server/metrics-certs
16+
name: metrics-certs
17+
readOnly: true
18+
volumes:
19+
- name: metrics-certs
20+
secret:
21+
defaultMode: 420
22+
secretName: metrics-server-cert

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ resources:
3333
# be able to communicate with the Webhook Server.
3434
#- ../network-policy
3535

36-
# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager
36+
# Uncomment the patches line if you enable Metrics
3737
patches:
3838
# [METRICS] The following patch will enable the metrics endpoint using HTTPS and the port :8443.
3939
# More info: https://book.kubebuilder.io/reference/metrics
4040
- path: manager_metrics_patch.yaml
4141
target:
4242
kind: Deployment
4343

44+
# Uncomment the patches line if you enable Metrics and CertManager
45+
# [METRICS WITH CERTMANGER] To enable metrics protected with certmanager, uncomment the following line.
46+
# This patch will protect the metrics with certmanager self-signed certs.
47+
- path: certmanager_metrics_manager_patch.yaml
48+
4449
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
4550
# crd/kustomization.yaml
4651
- path: manager_webhook_patch.yaml
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
resources:
22
- monitor.yaml
3+
4+
# [PROMETHEUS WITH CERTMANAGER] The following patch configures the ServiceMonitor in ../prometheus
5+
# to securely reference certificates created and managed by cert-manager.
6+
# Additionally, ensure that you uncomment the [METRICS WITH CERTMANAGER] patch under config/default/kustomization.yaml
7+
# to mount the "metrics-server-cert" secret in the Manager Deployment.
8+
patches:
9+
- path: monitor_tls_patch.yaml
10+
target:
11+
kind: ServiceMonitor

docs/book/src/cronjob-tutorial/testdata/project/config/prometheus/monitor.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@ spec:
1616
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
1717
tlsConfig:
1818
# TODO(user): The option insecureSkipVerify: true is not recommended for production since it disables
19-
# certificate verification. This poses a significant security risk by making the system vulnerable to
20-
# man-in-the-middle attacks, where an attacker could intercept and manipulate the communication between
21-
# Prometheus and the monitored services. This could lead to unauthorized access to sensitive metrics data,
22-
# compromising the integrity and confidentiality of the information.
23-
# Please use the following options for secure configurations:
24-
# caFile: /etc/metrics-certs/ca.crt
25-
# certFile: /etc/metrics-certs/tls.crt
26-
# keyFile: /etc/metrics-certs/tls.key
19+
# certificate verification, exposing the system to potential man-in-the-middle attacks.
20+
# For production environments, it is recommended to use cert-manager for automatic TLS certificate management.
21+
# To apply this configuration, enable cert-manager and use the patch located at config/prometheus/servicemonitor_tls_patch.yaml,
22+
# which securely references the certificate from the 'metrics-server-cert' secret.
2723
insecureSkipVerify: true
2824
selector:
2925
matchLabels:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Patch for Prometheus ServiceMonitor to enable secure TLS configuration
2+
# using certificates managed by cert-manager
3+
apiVersion: monitoring.coreos.com/v1
4+
kind: ServiceMonitor
5+
metadata:
6+
name: controller-manager-metrics-monitor
7+
namespace: system
8+
spec:
9+
endpoints:
10+
- tlsConfig:
11+
insecureSkipVerify: false
12+
ca:
13+
secret:
14+
name: metrics-server-cert
15+
key: ca.crt
16+
cert:
17+
secret:
18+
name: metrics-server-cert
19+
key: tls.crt
20+
keySecret:
21+
name: metrics-server-cert
22+
key: tls.key

docs/book/src/getting-started/testdata/project/cmd/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ func main() {
116116
// TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
117117
// generate self-signed certificates for the metrics server. While convenient for development and testing,
118118
// this setup is not recommended for production.
119+
120+
// TODO(user): If cert-manager is enabled in config/default/kustomization.yaml,
121+
// you can uncomment the following lines to use the certificate managed by cert-manager.
122+
// metricsServerOptions.CertDir = "/tmp/k8s-metrics-server/metrics-certs"
123+
// metricsServerOptions.CertName = "tls.crt"
124+
// metricsServerOptions.KeyName = "tls.key"
125+
119126
}
120127

121128
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller-manager
5+
namespace: system
6+
labels:
7+
app.kubernetes.io/name: project
8+
app.kubernetes.io/managed-by: kustomize
9+
spec:
10+
template:
11+
spec:
12+
containers:
13+
- name: manager
14+
volumeMounts:
15+
- mountPath: /tmp/k8s-metrics-server/metrics-certs
16+
name: metrics-certs
17+
readOnly: true
18+
volumes:
19+
- name: metrics-certs
20+
secret:
21+
defaultMode: 420
22+
secretName: metrics-server-cert

0 commit comments

Comments
 (0)