Skip to content

Commit c55bf50

Browse files
committed
fix(serving): route alerts to the leader and correct proxy wiring
Publish and maintain the elected operator’s alert route with bounded retries, preserve compatibility with older images, and align metrics, proxy, and webhook readiness wiring. Test rendered chart settings and leader handoff.
1 parent 50bb6a0 commit c55bf50

17 files changed

Lines changed: 1920 additions & 13 deletions

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
helm lint charts/kafka-operator --strict --kube-version "$version"
5151
helm template kafka-operator charts/kafka-operator --kube-version "$version" > /dev/null
5252
done
53+
scripts/test-chart-rendering.sh
5354
IFS=. read -r major minor _ <<< "$KUBERNETES_MIN_VERSION"
5455
unsupported="${major}.$((minor - 1)).99"
5556
if output=$(helm template kafka-operator charts/kafka-operator --kube-version "$unsupported" 2>&1); then

‎charts/kafka-operator/README.md‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,93 @@ The [Koperator](https://github.com/adobe/koperator) is a Kubernetes operator to
99

1010
CI covers Kubernetes 1.35.0 and 1.37.0. The chart's `kubeVersion` constraint also accepts distribution-specific version suffixes.
1111

12+
## Alert routing, readiness, and election constraints
13+
14+
The chart enables leader election. Only its elected listener publishes
15+
`kafka.banzaicloud.io/alert-leader: "true"`, which the alert Service requires.
16+
Never set this label on Pod templates. Webhook-serving standbys remain ready;
17+
elected replicas require usable alert serving/routing, and enabled webhooks
18+
require local serving readiness. Liveness is independent of Kafka and election.
19+
Upgrades from label-unaware versions have an alert endpoint gap until a new
20+
replica becomes leader; plan alert retries during that handoff.
21+
22+
Leader routing needs an operator image that publishes the label. With the
23+
default `alertManager.leaderRouting: auto`, the chart enables it only for
24+
operator image tags after 0.32.0, keeps the legacy selector and listener for
25+
older tags, and rejects unversioned tags. Set `true` for an unversioned image
26+
built after 0.32.0, or `false` to keep the legacy selector.
27+
28+
The chart always supplies the operator Pod's `POD_NAMESPACE` through the
29+
downward API, and supplies `POD_NAME` and `POD_UID` only when
30+
`alertManager.enable` is true. Routing uses uncached API reads and UID-checked
31+
updates within that namespace and the exact installation selector:
32+
`control-plane=controller-manager`, `controller-tools.k8s.io=1.0`,
33+
`app.kubernetes.io/component=operator`, and matching
34+
`app.kubernetes.io/name`/`app.kubernetes.io/instance`. Existing chart RBAC grants
35+
Pod get/list/update; custom RBAC must include the operator namespace even when
36+
Kafka is watched elsewhere. Identity/read/write failures fail startup closed;
37+
transient API errors are retried with backoff within a 30-second setup bound.
38+
While serving, the leader checks its label every ten seconds, restores it if
39+
removed, and reports unready while it cannot.
40+
With leader routing, `POD_NAME` and `POD_UID` are reserved: an `additionalEnv`
41+
entry with the same downward API field is rendered once, and any other
42+
`additionalEnv` value for either name fails the render. Remove such overrides
43+
before upgrading.
44+
45+
For local/non-Helm use, absence of **both** `POD_NAME` and `POD_UID` preserves the
46+
legacy listener, including with `POD_NAMESPACE` alone. Partial identity is
47+
rejected. Opting in requires all three inputs, installation labels and matching
48+
Service selector above. With `alertManager.enable: false` the chart omits the
49+
alert Service, `POD_NAME` and `POD_UID`, so the operator keeps the legacy
50+
listener and needs no Pod access for routing.
51+
On graceful shutdown, cancellation closes admission, drains HTTP requests for
52+
up to five seconds, then removes the Pod's own label: its API calls are bounded
53+
to two seconds, plus under 1.3 seconds of conflict backoff. Cleanup failure
54+
cannot keep the listener open. Involuntary Lease loss skips graceful shutdown,
55+
so the process can exit before requests drain or the label is removed; the
56+
label stays on the Pod until the next leader clears stale labels before
57+
publishing its own. Endpoint updates are asynchronous; stale requests may fail
58+
or receive HTTP 503.
59+
60+
Election identity is unchanged. Replicas must enable election, use byte-identical
61+
raw namespace input and share the election namespace. Nonempty `--namespaces`
62+
wins over `WATCH_NAMESPACE`; order, whitespace and duplicates affect the lock
63+
ID even for equivalent watch sets. Do not normalize inputs during rolling
64+
upgrades. Election namespace defaults to the in-cluster service-account
65+
namespace, not watched namespaces or `POD_NAMESPACE`.
66+
Overlapping watch sets (including all-namespaces overlap), differently spelled
67+
equivalent sets and independent election namespaces are unsupported concurrency
68+
topologies. Use replicas of one installation, not separate releases with
69+
different Service selectors sharing a Lease.
70+
71+
Election and routing labels are not external-action fencing or exactly-once
72+
execution. Pauses/partitions before observed cancellation, admitted work and
73+
ambiguous submissions can outlive routing changes; HTTP draining and conflict
74+
retries do not automatically adopt external operations.
75+
76+
## Metrics compatibility
77+
78+
Raw metrics remain bound to all interfaces over unauthenticated HTTP. The
79+
managed PodMonitor, when enabled, scrapes the `metrics` port directly, including
80+
when the existing auth proxy is enabled. The proxy does not protect this raw
81+
endpoint. Listener, container and proxy-upstream ports follow `metricEndpoint.port`
82+
(default 8080).
83+
84+
Managed TLS/authenticated scraping and its certificate/credential lifecycle are
85+
deferred. This preserves baseline HTTP scraping; it does not secure metrics.
86+
Do not treat the proxy's presence as authentication for the raw endpoint.
87+
88+
## Existing webhook certificate renewal limits
89+
90+
Generated webhook serving certificates last 365 days (CA: 3650 days), without
91+
continuous renewal. A generation-enabled Helm upgrade replaces CA and serving
92+
material, not necessarily without downtime. Renew before expiry and coordinate
93+
the Secret, webhook CA bundle and Pod rollout. With `webhook.certs.generate=false`,
94+
supply all three of `webhook.certs.server.tls.crt`, `webhook.certs.server.tls.key`
95+
and `webhook.certs.ca.crt` as base64-encoded PEM; the chart writes them unchanged
96+
into the Secret and webhook CA bundle, and changing them rolls the Pod.
97+
No metrics certificate reuse, issuance or automatic renewal is added.
98+
1299
## Installing the chart
13100

14101
Before installing the chart, you must first install the Koperator CustomResourceDefinition resources.
@@ -70,6 +157,7 @@ The command removes all the Kubernetes components associated with the chart and
70157
| additionalVolumes | list | `[]` | Additional volumes required for sidecars |
71158
| affinity | object | `{}` | Operator pod affinity can be set |
72159
| alertManager.enable | bool | `true` | AlertManager can be enabled |
160+
| alertManager.leaderRouting | string | `"auto"` | Route alerts only to the elected operator Pod. `auto` enables this for operator image tags after 0.32.0 and rejects unversioned tags; set `true` for other images that publish the leader label, or `false` to keep the legacy selector. |
73161
| alertManager.permissivePeerAuthentication.create | bool | `false` | Permissive PeerAuthentication (Istio resource) for AlertManager can be created |
74162
| alertManager.port | int | `9001` | AlertManager port |
75163
| certManager.enabled | bool | `false` | Operator will integrate with the cert manager |

‎charts/kafka-operator/README.md.gotmpl‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,93 @@ The [Koperator](https://github.com/adobe/koperator) is a Kubernetes operator to
99

1010
CI covers Kubernetes 1.35.0 and 1.37.0. The chart's `kubeVersion` constraint also accepts distribution-specific version suffixes.
1111

12+
## Alert routing, readiness, and election constraints
13+
14+
The chart enables leader election. Only its elected listener publishes
15+
`kafka.banzaicloud.io/alert-leader: "true"`, which the alert Service requires.
16+
Never set this label on Pod templates. Webhook-serving standbys remain ready;
17+
elected replicas require usable alert serving/routing, and enabled webhooks
18+
require local serving readiness. Liveness is independent of Kafka and election.
19+
Upgrades from label-unaware versions have an alert endpoint gap until a new
20+
replica becomes leader; plan alert retries during that handoff.
21+
22+
Leader routing needs an operator image that publishes the label. With the
23+
default `alertManager.leaderRouting: auto`, the chart enables it only for
24+
operator image tags after 0.32.0, keeps the legacy selector and listener for
25+
older tags, and rejects unversioned tags. Set `true` for an unversioned image
26+
built after 0.32.0, or `false` to keep the legacy selector.
27+
28+
The chart always supplies the operator Pod's `POD_NAMESPACE` through the
29+
downward API, and supplies `POD_NAME` and `POD_UID` only when
30+
`alertManager.enable` is true. Routing uses uncached API reads and UID-checked
31+
updates within that namespace and the exact installation selector:
32+
`control-plane=controller-manager`, `controller-tools.k8s.io=1.0`,
33+
`app.kubernetes.io/component=operator`, and matching
34+
`app.kubernetes.io/name`/`app.kubernetes.io/instance`. Existing chart RBAC grants
35+
Pod get/list/update; custom RBAC must include the operator namespace even when
36+
Kafka is watched elsewhere. Identity/read/write failures fail startup closed;
37+
transient API errors are retried with backoff within a 30-second setup bound.
38+
While serving, the leader checks its label every ten seconds, restores it if
39+
removed, and reports unready while it cannot.
40+
With leader routing, `POD_NAME` and `POD_UID` are reserved: an `additionalEnv`
41+
entry with the same downward API field is rendered once, and any other
42+
`additionalEnv` value for either name fails the render. Remove such overrides
43+
before upgrading.
44+
45+
For local/non-Helm use, absence of **both** `POD_NAME` and `POD_UID` preserves the
46+
legacy listener, including with `POD_NAMESPACE` alone. Partial identity is
47+
rejected. Opting in requires all three inputs, installation labels and matching
48+
Service selector above. With `alertManager.enable: false` the chart omits the
49+
alert Service, `POD_NAME` and `POD_UID`, so the operator keeps the legacy
50+
listener and needs no Pod access for routing.
51+
On graceful shutdown, cancellation closes admission, drains HTTP requests for
52+
up to five seconds, then removes the Pod's own label: its API calls are bounded
53+
to two seconds, plus under 1.3 seconds of conflict backoff. Cleanup failure
54+
cannot keep the listener open. Involuntary Lease loss skips graceful shutdown,
55+
so the process can exit before requests drain or the label is removed; the
56+
label stays on the Pod until the next leader clears stale labels before
57+
publishing its own. Endpoint updates are asynchronous; stale requests may fail
58+
or receive HTTP 503.
59+
60+
Election identity is unchanged. Replicas must enable election, use byte-identical
61+
raw namespace input and share the election namespace. Nonempty `--namespaces`
62+
wins over `WATCH_NAMESPACE`; order, whitespace and duplicates affect the lock
63+
ID even for equivalent watch sets. Do not normalize inputs during rolling
64+
upgrades. Election namespace defaults to the in-cluster service-account
65+
namespace, not watched namespaces or `POD_NAMESPACE`.
66+
Overlapping watch sets (including all-namespaces overlap), differently spelled
67+
equivalent sets and independent election namespaces are unsupported concurrency
68+
topologies. Use replicas of one installation, not separate releases with
69+
different Service selectors sharing a Lease.
70+
71+
Election and routing labels are not external-action fencing or exactly-once
72+
execution. Pauses/partitions before observed cancellation, admitted work and
73+
ambiguous submissions can outlive routing changes; HTTP draining and conflict
74+
retries do not automatically adopt external operations.
75+
76+
## Metrics compatibility
77+
78+
Raw metrics remain bound to all interfaces over unauthenticated HTTP. The
79+
managed PodMonitor, when enabled, scrapes the `metrics` port directly, including
80+
when the existing auth proxy is enabled. The proxy does not protect this raw
81+
endpoint. Listener, container and proxy-upstream ports follow `metricEndpoint.port`
82+
(default 8080).
83+
84+
Managed TLS/authenticated scraping and its certificate/credential lifecycle are
85+
deferred. This preserves baseline HTTP scraping; it does not secure metrics.
86+
Do not treat the proxy's presence as authentication for the raw endpoint.
87+
88+
## Existing webhook certificate renewal limits
89+
90+
Generated webhook serving certificates last 365 days (CA: 3650 days), without
91+
continuous renewal. A generation-enabled Helm upgrade replaces CA and serving
92+
material, not necessarily without downtime. Renew before expiry and coordinate
93+
the Secret, webhook CA bundle and Pod rollout. With `webhook.certs.generate=false`,
94+
supply all three of `webhook.certs.server.tls.crt`, `webhook.certs.server.tls.key`
95+
and `webhook.certs.ca.crt` as base64-encoded PEM; the chart writes them unchanged
96+
into the Secret and webhook CA bundle, and changing them rolls the Pod.
97+
No metrics certificate reuse, issuance or automatic renewal is added.
98+
1299
## Installing the chart
13100

14101
Before installing the chart, you must first install the Koperator CustomResourceDefinition resources.

‎charts/kafka-operator/templates/_helpers.tpl‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,37 @@ Sidecar volume implementation details
7070
{{- toYaml . -}}
7171
{{- end }}
7272
{{- end}}
73+
74+
{{/*
75+
Whether the operator publishes the elected Pod's alert routing label and the
76+
alert Service selects it. Renders "true" or nothing. Images up to 0.32.0 never
77+
publish the label, so "auto" enables routing only for later versioned tags.
78+
*/}}
79+
{{- define "kafka-operator.alertLeaderRouting" -}}
80+
{{- if .Values.alertManager.enable -}}
81+
{{- $mode := "auto" -}}
82+
{{- if hasKey .Values.alertManager "leaderRouting" -}}
83+
{{- $mode = toString .Values.alertManager.leaderRouting -}}
84+
{{- end -}}
85+
{{- if eq $mode "true" -}}
86+
true
87+
{{- else if eq $mode "auto" -}}
88+
{{- $tag := toString (.Values.operator.image.tag | default .Chart.AppVersion) | trimPrefix "v" -}}
89+
{{- if not (regexMatch "^[0-9]+\\.[0-9]+\\.[0-9]+(-[0-9A-Za-z.-]+)?(\\+[0-9A-Za-z.-]+)?$" $tag) -}}
90+
{{- fail (printf "alertManager.leaderRouting=auto cannot tell whether operator image tag %q publishes the alert leader label; set alertManager.leaderRouting to true for images built after 0.32.0 or false for older images" $tag) -}}
91+
{{- end -}}
92+
{{- if semverCompare ">=0.32.1-0" $tag -}}
93+
true
94+
{{- end -}}
95+
{{- else if ne $mode "false" -}}
96+
{{- fail (printf "alertManager.leaderRouting must be auto, true or false, not %q" $mode) -}}
97+
{{- end -}}
98+
{{- end -}}
99+
{{- end -}}
100+
101+
{{/*
102+
Operator metrics listener port.
103+
*/}}
104+
{{- define "kafka-operator.metricsPort" -}}
105+
{{- (.Values.metricEndpoint).port | default 8080 -}}
106+
{{- end -}}

‎charts/kafka-operator/templates/alertmanager-service.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ metadata:
1818
app.kubernetes.io/component: alertmanager
1919
spec:
2020
selector:
21+
{{- if include "kafka-operator.alertLeaderRouting" . }}
22+
kafka.banzaicloud.io/alert-leader: "true"
23+
{{- end }}
2124
control-plane: controller-manager
2225
controller-tools.k8s.io: "1.0"
2326
app.kubernetes.io/name: {{ include "kafka-operator.name" . }}

‎charts/kafka-operator/templates/operator-deployment-with-webhook.yaml‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- $metricsPort := include "kafka-operator.metricsPort" . }}
12
{{- $tlsCrt := "" }}
23
{{- $tlsKey := "" }}
34
{{- $caCrt := "" }}
@@ -157,7 +158,7 @@ spec:
157158
imagePullPolicy: {{ .Values.prometheusMetrics.authProxy.image.pullPolicy }}
158159
args:
159160
- "--secure-listen-address=0.0.0.0:8443"
160-
- "--upstream=http://127.0.0.1:8080/"
161+
- "--upstream=http://127.0.0.1:{{ $metricsPort }}/"
161162
- "--v=10"
162163
ports:
163164
- containerPort: 8443
@@ -192,9 +193,7 @@ spec:
192193
{{- if .Values.operator.developmentLogging }}
193194
- --development
194195
{{- end }}
195-
{{- if (.Values.metricEndpoint).port }}
196-
- --metrics-addr=":{{ .Values.metricEndpoint.port }}"
197-
{{- end }}
196+
- --metrics-addr=:{{ $metricsPort }}
198197
{{- if .Values.healthProbes.port }}
199198
- --health-probes-addr=:{{ .Values.healthProbes.port }}
200199
{{- end }}
@@ -216,6 +215,25 @@ spec:
216215
port: health-probes
217216
path: /readyz
218217
env:
218+
{{- if include "kafka-operator.alertLeaderRouting" . }}
219+
{{- $additionalEnv := dict }}
220+
{{- range .Values.additionalEnv }}
221+
{{- $_ := set $additionalEnv .name . }}
222+
{{- end }}
223+
{{- range $name, $field := dict "POD_NAME" "metadata.name" "POD_UID" "metadata.uid" }}
224+
{{- with get $additionalEnv $name }}
225+
{{- if ne (dig "valueFrom" "fieldRef" "fieldPath" "" .) $field }}
226+
{{- fail (printf "additionalEnv %s conflicts with the alert routing identity; remove it or use the downward API field %s" $name $field) }}
227+
{{- end }}
228+
{{- else }}
229+
- name: {{ $name }}
230+
valueFrom:
231+
fieldRef:
232+
apiVersion: v1
233+
fieldPath: {{ $field }}
234+
{{- end }}
235+
{{- end }}
236+
{{- end }}
219237
- name: POD_NAMESPACE
220238
valueFrom:
221239
fieldRef:
@@ -230,7 +248,7 @@ spec:
230248
name: webhook-server
231249
protocol: TCP
232250
{{- end}}
233-
- containerPort: {{ (.Values.metricEndpoint).port | default 8080 }}
251+
- containerPort: {{ $metricsPort }}
234252
name: metrics
235253
protocol: TCP
236254
- containerPort: {{ .Values.alertManager.port }}

‎charts/kafka-operator/templates/operator-service.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ metadata:
66
{{- if and .Values.prometheusMetrics.enabled (not .Values.prometheusMetrics.authProxy.enabled) }}
77
annotations:
88
prometheus.io/scrape: "true"
9-
prometheus.io/port: "8080"
9+
prometheus.io/port: {{ include "kafka-operator.metricsPort" . | quote }}
1010
prometheus.io/scheme: http
1111
{{- end }}
1212
labels:

‎charts/kafka-operator/values.schema.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
"enable": {
2121
"type": "boolean"
2222
},
23+
"leaderRouting": {
24+
"type": [
25+
"string",
26+
"boolean"
27+
],
28+
"enum": [
29+
"auto",
30+
true,
31+
false
32+
]
33+
},
2334
"permissivePeerAuthentication": {
2435
"type": "object",
2536
"properties": {

‎charts/kafka-operator/values.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ alertManager:
8989
enable: true
9090
# -- AlertManager port
9191
port: 9001
92+
# @schema type:[string, boolean]; enum:[auto, true, false]
93+
# -- Route alerts only to the elected operator Pod. `auto` enables this for
94+
# operator image tags after 0.32.0 and rejects unversioned tags; set `true`
95+
# for other images that publish the leader label, or `false` to keep the
96+
# legacy selector.
97+
leaderRouting: auto
9298
permissivePeerAuthentication:
9399
# -- Permissive PeerAuthentication (Istio resource) for AlertManager can be created
94100
create: false

0 commit comments

Comments
 (0)