Skip to content

✨ feat(chart,manager): authorize the metrics endpoint and make leader election survivable - #294

Merged
konih merged 5 commits into
mainfrom
lane/perf-fix-01-02
Aug 16, 2026
Merged

konih merged 5 commits into
mainfrom
lane/perf-fix-01-02

Conversation

@konih

@konih konih commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator

Two P1 findings from a perftest run on the kumulus 3-node Talos lab (2026-08-16). Both blocked real measurement; the second also caused the data lag that made a 10k inventory look short.

PERF-FIX-01 — the chart shipped no way to read its own metrics

metrics.secure defaults to true, so /metrics authorizes every request against RBAC, and metrics.serviceMonitor.enabled advertises a scrape path. But the chart rendered nothing granting that permission to anyone — not even the operator's own ServiceAccount — so a default install answers 403 to everything. The role existed only at config/rbac/metrics_reader_role.yaml, the kustomize path a Helm user never receives.

Measured, not inferred: /metrics returned 403 to the operator's own SA, and 200 only after hand-applying a ClusterRole. That is why export_p99 sat at unmeasured for a day and held the L-10k / LT-S14 gate open.

  • New clusterrole-metrics-reader.yaml (ClusterRole + binding), gated on metrics.enabled && metrics.secure && readerRole.create, and — per review — on rbac.create and not tenantMode like every sibling RBAC template.
  • metrics.readerRole.extraSubjects for the common case of a Prometheus under its own ServiceAccount.
  • The kustomize path shipped that role bound to nobody; added the missing binding so the two install paths stop diverging.

PERF-FIX-02 — a 10s API blip killed the operator

cmd/main.go set LeaderElection/LeaderElectionID and nothing else, so controller-runtime defaults applied (15s lease / 10s renew / 2s retry). Roughly ten seconds of API unreachability terminated the process.

Observed under load: 18 restarts, each leader election lost → exit 1 after dial tcp 10.96.0.1:443: connect: operation not permitted. Every exit interrupted in-flight exports.

  • LeaseDuration/RenewDeadline/RetryPeriod are now flags wired into ctrl.Options and surfaced in the chart.
  • Defaults 60s/40s/5s, deliberately more patient than controller-runtime's, because the shipped topology is a single replica: there is no standby, so exiting fast buys no failover speed and only turns a blip into downtime.
  • LeaderElectionReleaseOnCancel = true is the counterweight — without it the longer lease would make every rolling upgrade cost a 60s reconciliation gap. Safe here: main() does no work after mgr.Start returns.

Review

Two rounds of independent review (fresh reviewer, diff-only). Round 1 returned REQUEST CHANGES with 3 P1s — including a tautological helm assertion the reviewer mutation-proved could never fail, and the fact that the ctrl.Options wiring had no test (deleting it left everything green). Round 2 re-verified every fix by execution and returned APPROVE.

Mutation-verified in this branch: removing any applyLeaderElection assignment turns go test ./cmd/ red; enabling a leader-election timing turns the helm suite red.

Gates

helm-test 40/40 · verify · lint · arch-lint · go test · audit:rbac — all green.

format:check and docs:verify fail identically on clean main (gitignored references/, and a stale ui-removal assertion) — pre-existing and unrelated; confirmed independently by the reviewer.

konih added 4 commits August 16, 2026 01:35
… election survivable

Two P1 findings from the 2026-08-16 perftest run on the kumulus Talos lab. Both
blocked real measurement; the second also caused the data lag that made a 10k
inventory look short.

**PERF-FIX-01 — the chart ships no way to read its own metrics.**
metrics.secure defaults to true, so /metrics authorizes every request against
RBAC, and metrics.serviceMonitor.enabled advertises a scrape path. But the chart
rendered nothing granting that permission to anyone — not even the operator's own
ServiceAccount — so a default install answers 403 to everything. The role existed
only at config/rbac/metrics_reader_role.yaml, which is the kustomize path a Helm
user never receives.

Measured, not inferred: /metrics returned 403 to the operator's own SA and 200
only after hand-applying a ClusterRole. That is why export_p99 sat at
'unmeasured' for a day and held the LT-S14 gate open.

Adds clusterrole-metrics-reader.yaml (ClusterRole + binding), gated on
metrics.enabled AND metrics.secure so nothing is granted when there is nothing to
authorize, plus metrics.readerRole.extraSubjects for the common case of a
Prometheus running under its own ServiceAccount elsewhere.

**PERF-FIX-02 — a 10s API blip killed the operator.**
cmd/main.go set LeaderElection and LeaderElectionID and nothing else, so
controller-runtime's defaults applied: 15s lease, 10s renew, 2s retry. Roughly
ten seconds of API unreachability terminated the process.

Observed under load: 18 restarts, each 'leader election lost' -> exit 1 after
'dial tcp 10.96.0.1:443: connect: operation not permitted'. Every exit
interrupted in-flight exports.

LeaseDuration/RenewDeadline/RetryPeriod are now flags wired into ctrl.Options and
surfaced in the chart. Defaults (60s/40s/5s) are deliberately more patient than
controller-runtime's because the shipped topology is a SINGLE REPLICA: there is
no standby to fail over to, so exiting fast buys no failover speed and only turns
a blip into downtime. Operators running multiple replicas can tighten all three.

Tests assert the client-go invariant (retry < renew < lease) so a future tuning
cannot ship a config that refuses to boot, and that empty chart values emit no
flags rather than silently pinning a timing nobody chose.

Gates: helm-unittest 38/38, helm-lint, helm-docs, verify, lint, arch-lint,
go test ./... — all green. (format:check fails identically on clean main from
gitignored references/; unrelated and pre-existing.)
…r and leader-election lane

Seven findings from the independent review. Three were P1.

**F1 (P1) — the reader role broke the documented tenantMode install.** The new
template ignored `rbac.create` and `tenantMode`, unlike every other RBAC
template here, so a team-namespace install rendered a ClusterRole and failed
`helm install` Forbidden for a user who is by definition not cluster-admin. Now
gated like its siblings, with tests asserting zero documents in both cases.
There is no namespaced fallback to offer: `nonResourceURLs` cannot be expressed
in a Role because /metrics is not a namespaced resource, so those installs need a
platform admin to apply the grant — documented rather than faked.

**F2 (P1) — the wiring that made the flags matter had no test.** The three
ctrl.Options assignments sat inline in main(), where deleting them left every
test green: flags parsed, docs existed, the chart rendered them, and nothing
reached the manager. Extracted to applyLeaderElection() and covered.
Mutation-verified: removing the LeaseDuration assignment now fails the test.

**F3 (P1) — the docs still pointed Helm users at the kustomize file.**
metrics.md told readers to see config/rbac/metrics_reader_role.yaml, which a Helm
user never receives. That misdirection *is* the defect this lane exists to fix,
and the lane had left it in place.

**F4 (P2) — a tautological assertion, mutation-proven by the reviewer.**
`notContains: --leader-elect-lease-duration=` can never match, because
helm-unittest compares whole elements and the real arg carries a value. With the
flag deliberately enabled the suite still passed. Replaced with an exact
arg-list equality that fails when the flag appears.

**F5 (P2) — a cost the lane had not acknowledged.** Raising the lease 15s->60s
quadruples how long a SUCCESSOR waits after a restart. The mitigation was five
lines below the change, commented out: LeaderElectionReleaseOnCancel. It is safe
here (main() does no work after mgr.Start returns), so a clean shutdown now hands
over immediately and the long lease costs nothing except after a hard kill —
which is exactly when waiting is correct.

**F6 (P2) — kustomize shipped a role bound to nobody.** metrics_reader_role.yaml
had no binding and none was listed in kustomization.yaml, so that path returned
403 exactly as if the role did not exist. Added, keeping the two install paths
from diverging.

**F7 (P2)** — curated values tables in docs/operator-manual/helm-values.md now
list the six new keys, not just the generated chart README.

Gates: helm-test 40/40, verify, lint, arch-lint, go test — green.
(format:check and docs:verify fail identically on clean main; both pre-existing
and unrelated — gitignored references/ and a stale ui-removal doc assertion.)
…claiming comments

Follow-ups from the re-review. All were non-blocking notes; three were worth
fixing now because each one misleads a reader.

**P2-2 — the advertised monitoring overlay produced a silent 403.**
charts/kollect/ci/monitoring-values.yaml enabled serviceMonitor + secure with no
extraSubjects. Prometheus scrapes with its OWN ServiceAccount, not the
operator's, so that overlay configures a perfect ServiceMonitor against an
endpoint that rejects it — with no symptom other than absent metrics. The
overlay now binds prometheus-k8s, and NOTES.txt warns at install time when a
ServiceMonitor is enabled with nothing bound. Verified: the warning fires
unbound, and is silent both when bound and when no ServiceMonitor is requested.

**P2-3** — the chart's Artifact-Hub-facing README still said "bind a
metrics-reader ClusterRole to your Prometheus service account" as if by hand,
never mentioning the values key this lane added.

**P3-3 — a comment asserting something untrue.** The template claimed the
tenantMode grant was "documented in values-minimal-rbac.yaml"; it is not, it is
in metrics.md. Corrected rather than left as a confident pointer to nothing.

**P2-5** — the tenantMode admonition implied a cluster admin applying the reader
grant is sufficient. It is not: tenantMode's namespaced Role grants only
selfsubjectaccessreviews, so delegated TokenReview/SAR cannot succeed at all
regardless of who holds the reader role. That gap pre-dates this lane; the docs
now say so instead of implying it is solved.

Gates: helm-test 40/40, helm-docs regenerated.
… block

markdownlint treats a 4-space-indented paragraph after a blank line as an
indented code block, so the second paragraph of the tenantMode admonition failed
MD046/code-block-style and took out both `preflight` (a required check) and
`Verify and build docs`.

Promoted to a real subsection instead of an admonition continuation — same
content, no indentation to misread.
@codecov

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@sonarqubecloud

Copy link
Copy Markdown

@konih
konih merged commit 4ccba73 into main Aug 16, 2026
34 of 35 checks passed
@konih
konih deleted the lane/perf-fix-01-02 branch August 16, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant