@@ -55,7 +55,118 @@ Metrics: metricsserver.Options{
55
55
},
56
56
` ` `
57
57
58
- # # Metrics Protection
58
+ # # Consuming Controller Metrics in Kubebuilder
59
+
60
+ You can consume the metrics exposed by the controller using the `curl`
61
+ command or any other HTTP client such as Prometheus.
62
+
63
+ However, before doing so, ensure that your client has the
64
+ **required RBAC permissions** to access the `/metrics` endpoint.
65
+
66
+ # ## Granting Permissions to Access Metrics
67
+
68
+ Kubebuilder scaffolds a `ClusterRole` with the necessary read permissions under :
69
+
70
+ ` ` `
71
+ config/rbac/metrics_reader_role.yaml
72
+ ` ` `
73
+
74
+ This file contains the required RBAC rules to allow access to the metrics endpoint.
75
+
76
+ <aside class="note">
77
+ <H1>This ClusterRole is only a helper </H1>
78
+
79
+ Kubebuilder **does not scaffold a RoleBinding or ClusterRoleBinding by default.**
80
+ This is an intentional design choice to avoid :
81
+
82
+ - Accidentally binding to the wrong service account,
83
+ - Granting access in restricted environments,
84
+ - Creating conflicts in multi-team or multi-tenant clusters.
85
+
86
+ </aside>
87
+
88
+ # ### Create a ClusterRoleBinding
89
+
90
+ You can create the binding via `kubectl` :
91
+
92
+ ` ` ` bash
93
+ kubectl create clusterrolebinding metrics \
94
+ --clusterrole=<project-prefix>-metrics-reader \
95
+ --serviceaccount=<namespace>:<service-account-name>
96
+ ` ` `
97
+
98
+ Or with a manifest :
99
+
100
+ ` ` ` yaml
101
+ apiVersion: rbac.authorization.k8s.io/v1
102
+ kind: ClusterRoleBinding
103
+ metadata:
104
+ name: allow-metrics-access
105
+ roleRef:
106
+ apiGroup: rbac.authorization.k8s.io
107
+ kind: ClusterRole
108
+ name: metrics-reader
109
+ subjects:
110
+ - kind: ServiceAccount
111
+ name: controller-manager
112
+ namespace: system # Replace 'system' with your controller-manager's namespace
113
+ ` ` `
114
+
115
+ <aside class="note">
116
+ <H1>Why this is manual:</H1>
117
+
118
+ Kubebuilder avoids scaffolding RoleBindings by default because it might :
119
+ - Bind to the wrong ServiceAccount
120
+ - Grant access unnecessarily
121
+ - Cause problems in restricted or multi-tenant clusters
122
+ - This design provides safety and flexibility, but requires manual binding.
123
+ </aside>
124
+
125
+ # ## Testing the Metrics Endpoint (via Curl Pod)
126
+
127
+ If you'd like to manually test access to the metrics endpoint, follow these steps :
128
+
129
+ - Create Role Binding
130
+
131
+ ` ` ` bash
132
+ kubectl create clusterrolebinding <project-name>-metrics-binding \
133
+ --clusterrole=<project-name>-metrics-reader \
134
+ --serviceaccount=<project-name>-system:<project-name>-controller-manager
135
+ ` ` `
136
+
137
+ - Generate a Token
138
+
139
+ ` ` ` bash
140
+ export TOKEN=$(kubectl create token <project-name>-controller-manager -n <project-name>-system)
141
+ echo $TOKEN
142
+ ` ` `
143
+
144
+ - Launch Curl Pod
145
+
146
+ ` ` ` bash
147
+ kubectl run curl-metrics --rm -it --restart=Never \
148
+ --image=curlimages/curl:7.87.0 -n <project-name>-system -- /bin/sh
149
+ ` ` `
150
+
151
+ - Call Metrics Endpoint
152
+
153
+ Inside the pod, use :
154
+
155
+ ` ` ` bash
156
+ curl -v -k -H "Authorization: Bearer $TOKEN" \
157
+ https://<project-name>-controller-manager-metrics-service.<project-name>-system.svc.cluster.local:8443/metrics
158
+ ` ` `
159
+
160
+ <aside class="note">
161
+ <H1>Notes</H1>
162
+
163
+ - Replace `<project-name>`, `<namespace>`, and `<service-account-name>` accordingly.
164
+ - Ensure TLS is enabled and certificates are valid if not skipping verification (`-k`).
165
+
166
+ Check the options to protect your metrics endpoint in the next sections.
167
+ </aside>
168
+
169
+ # # Metrics Protection and available options
59
170
60
171
Unprotected metrics endpoints can expose valuable data to unauthorized users,
61
172
such as system performance, application behavior, and potentially confidential
0 commit comments