Skip to content

Commit f56e786

Browse files
committed
make metricsServerOptions configurable and add documentation
1 parent 9a639d3 commit f56e786

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ Users can just run `kubectl apply -f <URL for YAML BUNDLE>` to install the proje
8787
kubectl apply -f https://raw.githubusercontent.com/<org>/netbox-operator/<tag or branch>/dist/install.yaml
8888
```
8989

90+
# Monitoring
91+
92+
When the operator is deployed with the default kustomization (located at config/default/) the metrics endpoint is already exposed and provides the [default kubebuilder metrics].
93+
94+
[default kubebuilder metrics]: https://book.kubebuilder.io/reference/metrics-reference.
95+
96+
For the monitoring of the state of the CRs reconciled by the operator [kube state metrics] can be used, check the kube-state-metrics documentation for instructions on configuring it to collect metrics from custom resources.
97+
98+
[kube state metrics]: https://github.com/kubernetes/kube-state-metrics
99+
90100
# Contributing
91101

92102
We cordially invite collaboration from the community to enhance the quality and functionality of this project. Whether you are addressing bugs, introducing new features, refining documentation, or assisting with items on our to-do list, your contributions are highly valued and greatly appreciated. Please take a look at [Contribution guide] for more details.

cmd/main.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ func main() {
6464
var probeAddr string
6565
var secureMetrics bool
6666
var enableHTTP2 bool
67-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
67+
var tlsOpts []func(*tls.Config)
68+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
69+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
6870
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6971
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
7072
"Enable leader election for controller manager. "+
7173
"Enabling this will ensure there is only one active controller manager.")
72-
flag.BoolVar(&secureMetrics, "metrics-secure", false,
74+
flag.BoolVar(&secureMetrics, "metrics-secure", true,
7375
"If set the metrics endpoint is served securely")
7476
flag.BoolVar(&enableHTTP2, "enable-http2", false,
7577
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
@@ -92,23 +94,40 @@ func main() {
9294
c.NextProtos = []string{"http/1.1"}
9395
}
9496

95-
tlsOpts := make([]func(*tls.Config), 0, 1)
9697
if !enableHTTP2 {
9798
tlsOpts = append(tlsOpts, disableHTTP2)
9899
}
99100

100101
webhookServer := webhook.NewServer(webhook.Options{
101102
TLSOpts: tlsOpts,
102103
})
104+
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
105+
// More info:
106+
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/server
107+
// - https://book.kubebuilder.io/reference/metrics.html
108+
metricsServerOptions := metricsserver.Options{
109+
BindAddress: metricsAddr,
110+
SecureServing: secureMetrics,
111+
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
112+
// not provided, self-signed certificates will be generated by default. This option is not recommended for
113+
// production environments as self-signed certificates do not offer the same level of trust and security
114+
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
115+
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
116+
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
117+
TLSOpts: tlsOpts,
118+
}
119+
120+
if secureMetrics {
121+
// FilterProvider is used to protect the metrics endpoint with authn/authz.
122+
// These configurations ensure that only authorized users and service accounts
123+
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
124+
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/filters#WithAuthenticationAndAuthorization
125+
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
126+
}
103127

104128
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
105-
Scheme: scheme,
106-
Metrics: metricsserver.Options{
107-
BindAddress: metricsAddr,
108-
SecureServing: secureMetrics,
109-
TLSOpts: tlsOpts,
110-
FilterProvider: filters.WithAuthenticationAndAuthorization,
111-
},
129+
Scheme: scheme,
130+
Metrics: metricsServerOptions,
112131
WebhookServer: webhookServer,
113132
HealthProbeBindAddress: probeAddr,
114133
LeaderElection: enableLeaderElection,

0 commit comments

Comments
 (0)