Skip to content

Commit f1bcf0e

Browse files
authored
Make metricsServerOptions configurable and add documentation (#226)
* make metricsServerOptions configurable and add documentation * align monitoring roles with new controller-gen version
1 parent d874e49 commit f1bcf0e

10 files changed

+86
-98
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/[email protected]/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/[email protected]/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,

config/rbac/auth_proxy_client_clusterrole.yaml

Lines changed: 0 additions & 16 deletions
This file was deleted.

config/rbac/auth_proxy_role.yaml

Lines changed: 0 additions & 24 deletions
This file was deleted.

config/rbac/auth_proxy_role_binding.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.

config/rbac/auth_proxy_service.yaml

Lines changed: 0 additions & 22 deletions
This file was deleted.

config/rbac/kustomization.yaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ resources:
99
- role_binding.yaml
1010
- leader_election_role.yaml
1111
- leader_election_role_binding.yaml
12-
# Comment the following 4 lines if you want to disable
13-
# the auth proxy (https://github.com/brancz/kube-rbac-proxy)
14-
# which protects your /metrics endpoint.
15-
# - auth_proxy_service.yaml
16-
# - auth_proxy_role.yaml
17-
# - auth_proxy_role_binding.yaml
18-
# - auth_proxy_client_clusterrole.yaml
12+
# The following RBAC configurations are used to protect
13+
# the metrics endpoint with authn/authz. These configurations
14+
# ensure that only authorized users and service accounts
15+
# can access the metrics endpoint. Comment the following
16+
# permissions if you want to disable this protection.
17+
# More info: https://book.kubebuilder.io/reference/metrics.html
18+
- metrics_auth_role.yaml
19+
- metrics_auth_role_binding.yaml
20+
- metrics_reader_role.yaml
1921
# For each CRD, "Editor" and "Viewer" roles are scaffolded by
2022
# default, aiding admins in cluster management. Those roles are
2123
# not used by the Project itself. You can comment the following lines

config/rbac/metrics_auth_role.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRole
3+
metadata:
4+
name: metrics-auth-role
5+
rules:
6+
- apiGroups:
7+
- authentication.k8s.io
8+
resources:
9+
- tokenreviews
10+
verbs:
11+
- create
12+
- apiGroups:
13+
- authorization.k8s.io
14+
resources:
15+
- subjectaccessreviews
16+
verbs:
17+
- create
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
name: metrics-auth-rolebinding
5+
roleRef:
6+
apiGroup: rbac.authorization.k8s.io
7+
kind: ClusterRole
8+
name: metrics-auth-role
9+
subjects:
10+
- kind: ServiceAccount
11+
name: controller-manager
12+
namespace: system

config/rbac/metrics_reader_role.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRole
3+
metadata:
4+
name: metrics-reader
5+
rules:
6+
- nonResourceURLs:
7+
- "/metrics"
8+
verbs:
9+
- get

0 commit comments

Comments
 (0)