Skip to content

Commit c6d5050

Browse files
Add an option to enable Prometheus with real certificates
While the install scripts do not enable Prometheus integration by default, solutions running upstream may want to use and enable it with Prometheus. This addition offers a way for upstream users to understand how to properly configure Prometheus using real certificates. At the very least, it serves as documentation and provides an option for those installing from source who want to implement secure Prometheus integration.
1 parent 659787f commit c6d5050

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

cmd/manager/main.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/tls"
2222
"flag"
2323
"fmt"
24+
"log"
2425
"net/http"
2526
"os"
2627
"path/filepath"
@@ -42,6 +43,7 @@ import (
4243
"k8s.io/klog/v2/textlogger"
4344
ctrl "sigs.k8s.io/controller-runtime"
4445
crcache "sigs.k8s.io/controller-runtime/pkg/cache"
46+
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
4547
"sigs.k8s.io/controller-runtime/pkg/client"
4648
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
4749
"sigs.k8s.io/controller-runtime/pkg/healthz"
@@ -98,11 +100,15 @@ func main() {
98100
operatorControllerVersion bool
99101
systemNamespace string
100102
caCertDir string
103+
certFile string
104+
keyFile string
101105
globalPullSecret string
102106
)
103107
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8443", "The address the metric endpoint binds to.")
104108
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
105109
flag.StringVar(&caCertDir, "ca-certs-dir", "", "The directory of TLS certificate to use for verifying HTTPS connections to the Catalogd and docker-registry web servers.")
110+
flag.StringVar(&certFile, "tls-cert", "", "The certificate file used for serving catalog contents over HTTPS. Requires tls-key.")
111+
flag.StringVar(&keyFile, "tls-key", "", "The key file used for serving catalog contents over HTTPS. Requires tls-cert.")
106112
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
107113
"Enable leader election for controller manager. "+
108114
"Enabling this will ensure there is only one active controller manager.")
@@ -122,6 +128,11 @@ func main() {
122128
os.Exit(0)
123129
}
124130

131+
if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") {
132+
setupLog.Error(nil, "unable to configure TLS certificates: tls-cert and tls-key flags must be used together")
133+
os.Exit(1)
134+
}
135+
125136
ctrl.SetLogger(textlogger.NewLogger(textlogger.NewConfig()))
126137

127138
setupLog.Info("starting up the controller", "version info", version.String())
@@ -177,6 +188,21 @@ func main() {
177188

178189
tlsOpts = append(tlsOpts, disableHTTP2)
179190

191+
// Create a new certificate watcher to watch for changes in the certificate files.
192+
// If the certificate files change, the certificate watcher will reload the certificate
193+
// and key files and update the TLS configuration.
194+
cw, err := certwatcher.New(certFile, keyFile)
195+
if err != nil {
196+
log.Fatalf("Failed to initialize certificate watcher: %v", err)
197+
}
198+
199+
// Ensure that metrics is protected with certs managed by cert-manager
200+
// If not informed, the metrics service provided by controller-runtime will generate
201+
// and use its own self-assigned certs which is not recommended for production envs.
202+
tlsOpts = append(tlsOpts, func(cfg *tls.Config) {
203+
cfg.GetCertificate = cw.GetCertificate
204+
})
205+
180206
metricsServerOptions := server.Options{
181207
BindAddress: metricsAddr,
182208
SecureServing: true,
@@ -186,13 +212,6 @@ func main() {
186212
// These configurations ensure that only authorized users and service accounts
187213
// can access the metrics endpoint.
188214
FilterProvider: filters.WithAuthenticationAndAuthorization,
189-
190-
// Ensure that metrics is protected with certs managed by cert-manager
191-
// If not informed, the metrics service provided by controller-runtime will generate
192-
// and use its own self-assigned certs which is not recommended for production envs.
193-
CertDir: "/var/certs/",
194-
CertName: "olm-ca.crt",
195-
KeyName: "ca.crt",
196215
}
197216

198217
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
# Prometheus Monitor Service (Metrics)
1+
# Patch for Prometheus ServiceMonitor to enable secure TLS configuration
2+
# using certificates managed by cert-manager
23
apiVersion: monitoring.coreos.com/v1
34
kind: ServiceMonitor
45
metadata:
5-
labels:
6-
control-plane: operator-controller-controller-manager
76
name: controller-manager-metrics-monitor
87
namespace: system
98
spec:
109
endpoints:
11-
- path: /metrics
12-
port: https
13-
scheme: https
14-
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
15-
tlsConfig:
16-
insecureSkipVerify: true
17-
selector:
18-
matchLabels:
19-
control-plane: operator-controller-controller-manager
10+
- tlsConfig:
11+
insecureSkipVerify: false
12+
ca:
13+
secret:
14+
name: olmv1-ca
15+
key: ca.crt
16+
cert:
17+
secret:
18+
name: olmv1-ca
19+
key: olm-ca.crt
20+
keySecret:
21+
name: olmv1-ca
22+
key: ca.crt

config/components/tls/patches/manager_deployment_cert.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@
77
- op: add
88
path: /spec/template/spec/containers/0/args/-
99
value: "--ca-certs-dir=/var/certs"
10+
- op: add
11+
path: /spec/template/spec/containers/0/args/-
12+
value: "--tls-cert=olm-ca.crt"
13+
- op: add
14+
path: /spec/template/spec/containers/0/args/-
15+
value: "--tls-key=ca.crt"

0 commit comments

Comments
 (0)