Skip to content

Commit 7405b04

Browse files
committed
feat(helm): honour bindAddress=0 as a metrics disable signal
The `controller.metrics.bindAddress` value mirrors the controller's `--metrics-bind-address` flag, and that flag's documented contract treats `0` as "disable the metrics server" (see go/core/pkg/app/app.go:145-146). Letting the chart silently render an invalid `containerPort: 0` when the operator follows that contract is surprising; treat the binary's disable sentinel as another way of saying `controller.metrics.enabled=false`. Wrap the existing `kagent.controller.metricsPort` helper with a new `kagent.controller.metricsEnabled` helper that returns truthy only when `enabled` is true *and* the resolved port is a real, non-zero value. Switch the Service, both ClusterRoles, the ClusterRoleBinding, and the Deployment env/port wiring over to the new helper so the two disable signals are equivalent in their effect on rendered manifests. The escape-hatch behaviour is unchanged: setting `METRICS_BIND_ADDRESS` via `controller.env` still shifts only the runtime listener and cannot move the chart-rendered Service. Addresses review feedback on #1803. Signed-off-by: Daniel Orbach <ddorbach@gmail.com>
1 parent 77e9a92 commit 7405b04

10 files changed

Lines changed: 77 additions & 9 deletions

helm/kagent/templates/_helpers.tpl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,27 @@ Extract the TCP port from controller.metrics.bindAddress.
138138
Anchors the digit run to the end of the string so every Go-style
139139
address form the controller binary accepts is handled correctly: bare
140140
":port", host-qualified "host:port", and bracketed IPv6 "[::1]:port"
141-
all yield the trailing port. Disabling metrics is exclusively the job
142-
of controller.metrics.enabled=false; bindAddress is expected to carry
143-
a real, non-zero port whenever the metrics resources render.
141+
all yield the trailing port. Returns "0" or "" when the binary's
142+
disable sentinel is in use; callers must consult
143+
`kagent.controller.metricsEnabled` before rendering manifests.
144144
*/}}
145145
{{- define "kagent.controller.metricsPort" -}}
146146
{{- regexFind "[0-9]+$" (.Values.controller.metrics.bindAddress | toString) -}}
147147
{{- end -}}
148148

149+
{{/*
150+
Returns "1" when the controller metrics resources (Service, RBAC,
151+
container port, env vars) should render, empty otherwise. Honours both
152+
disable signals: `controller.metrics.enabled=false` and the binary's
153+
own `--metrics-bind-address=0` sentinel reached through `bindAddress`.
154+
The two are equivalent so the field name keeps faith with the binary's
155+
documented contract (see go/core/pkg/app/app.go).
156+
*/}}
157+
{{- define "kagent.controller.metricsEnabled" -}}
158+
{{- $port := include "kagent.controller.metricsPort" . -}}
159+
{{- if and .Values.controller.metrics.enabled $port (ne $port "0") -}}1{{- end -}}
160+
{{- end -}}
161+
149162
{{/*
150163
PostgreSQL service name for the bundled postgres instance
151164
*/}}

helm/kagent/templates/controller-deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ spec:
8484
{{- else }}
8585
{{ fail "No database connection configured. Set database.postgres.url, database.postgres.urlFile, or enable database.postgres.bundled." }}
8686
{{- end }}
87-
{{- if .Values.controller.metrics.enabled }}
87+
{{- if include "kagent.controller.metricsEnabled" . }}
8888
- name: METRICS_BIND_ADDRESS
8989
value: {{ .Values.controller.metrics.bindAddress | quote }}
9090
- name: METRICS_SECURE

helm/kagent/templates/controller-metrics-service.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if .Values.controller.metrics.enabled }}
1+
{{- if include "kagent.controller.metricsEnabled" . }}
22
apiVersion: v1
33
kind: Service
44
metadata:

helm/kagent/templates/rbac/metrics-auth-clusterrole.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if and .Values.controller.metrics.enabled .Values.controller.metrics.secureServing }}
1+
{{- if and (include "kagent.controller.metricsEnabled" .) .Values.controller.metrics.secureServing }}
22
apiVersion: rbac.authorization.k8s.io/v1
33
kind: ClusterRole
44
metadata:

helm/kagent/templates/rbac/metrics-auth-clusterrolebinding.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if and .Values.controller.metrics.enabled .Values.controller.metrics.secureServing }}
1+
{{- if and (include "kagent.controller.metricsEnabled" .) .Values.controller.metrics.secureServing }}
22
apiVersion: rbac.authorization.k8s.io/v1
33
kind: ClusterRoleBinding
44
metadata:

helm/kagent/templates/rbac/metrics-reader-clusterrole.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{- if .Values.controller.metrics.enabled }}
1+
{{- if include "kagent.controller.metricsEnabled" . }}
22
apiVersion: rbac.authorization.k8s.io/v1
33
kind: ClusterRole
44
metadata:

helm/kagent/tests/controller-deployment_test.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,27 @@ tests:
606606
containerPort: 9443
607607
protocol: TCP
608608

609+
- it: should not gain metrics wiring when bindAddress disables metrics
610+
template: controller-deployment.yaml
611+
set:
612+
controller.metrics.enabled: true
613+
controller.metrics.bindAddress: "0"
614+
asserts:
615+
- notContains:
616+
path: spec.template.spec.containers[0].env
617+
content:
618+
name: METRICS_BIND_ADDRESS
619+
any: true
620+
- notContains:
621+
path: spec.template.spec.containers[0].env
622+
content:
623+
name: METRICS_SECURE
624+
any: true
625+
- notContains:
626+
path: spec.template.spec.containers[0].ports
627+
content:
628+
name: metrics
629+
609630
- it: should let controller.env override the chart-supplied metrics env
610631
template: controller-deployment.yaml
611632
set:

helm/kagent/tests/controller-metrics-rbac_test.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,18 @@ tests:
111111
- hasDocuments:
112112
count: 0
113113
template: rbac/metrics-auth-clusterrolebinding.yaml
114+
115+
- it: should skip all metrics rbac when bindAddress disables metrics
116+
set:
117+
controller.metrics.enabled: true
118+
controller.metrics.bindAddress: "0"
119+
asserts:
120+
- hasDocuments:
121+
count: 0
122+
template: rbac/metrics-reader-clusterrole.yaml
123+
- hasDocuments:
124+
count: 0
125+
template: rbac/metrics-auth-clusterrole.yaml
126+
- hasDocuments:
127+
count: 0
128+
template: rbac/metrics-auth-clusterrolebinding.yaml

helm/kagent/tests/controller-metrics-service_test.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ tests:
7272
path: spec.ports[0].targetPort
7373
value: 9443
7474

75+
- it: should not render when bindAddress disables metrics
76+
set:
77+
controller.metrics.enabled: true
78+
controller.metrics.bindAddress: "0"
79+
asserts:
80+
- hasDocuments:
81+
count: 0
82+
83+
- it: should not render when bindAddress is empty
84+
set:
85+
controller.metrics.enabled: true
86+
controller.metrics.bindAddress: ""
87+
asserts:
88+
- hasDocuments:
89+
count: 0
90+
7591
- it: should rename port when secure serving disabled
7692
set:
7793
controller.metrics.enabled: true

helm/kagent/values.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ controller:
230230
# `targetPort` and the pod `containerPort` are derived from it at
231231
# template time, so overriding `METRICS_BIND_ADDRESS` via
232232
# `controller.env` shifts only the runtime listener and leaves the
233-
# rendered Service pointing at the chart-time port.
233+
# rendered Service pointing at the chart-time port. Setting
234+
# `bindAddress: "0"` (or empty) is treated as a disable signal —
235+
# equivalent to `enabled: false` — to keep faith with the controller
236+
# binary's documented contract for `--metrics-bind-address`.
234237
# @default -- disabled
235238
metrics:
236239
enabled: false

0 commit comments

Comments
 (0)