fix(webhook): make webhook template cache race-safe across rollouts #995
[APPROVALNOTIFIER] This PR is NOT APPROVED
This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from furykerry by writing /assign @furykerry in a comment. For more information see:The Kubernetes Code Review Process.
The full list of commands accepted by this bot can be found here.
<details open>
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
</details>
<!-- META={"approvers":["furykerry"]} -->
Codecov Report
:x: Patch coverage is
64.89362% with 33 lines in your changes missing coverage. Please review.:white_check_mark: Project coverage is 83.42%. Comparing base (
5f518fc) to head (9baaf4a).| Files with missing lines | Patch % | Lines |
|---|---|---|
| .../utils/webhookutils/configuration/configuration.go | 64.89% | 25 Missing and 8 partials :warning: |
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## master #995 +/- ##
==========================================
+ Coverage 83.40% 83.42% +0.01%
==========================================
Files 260 260
Lines 22909 22977 +68
==========================================
+ Hits 19108 19169 +61
+ Misses 3060 3058 -2
- Partials 741 750 +9
| Flag | Coverage Δ | |
|---|---|---|
| unittests | 83.42% <64.89%> (+0.01%) | :arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
I ran this against a real API server (kind, Kubernetes v1.36.1) with Helm v4 using server-side apply, calling Ensure from this branch and from master, against a small chart that renders both configurations. In the old style the chart ships an empty template annotation, and in the new style it ships agents.kruise.io/webhook-template plus its sha256 revision.
On master, helm upgrade fails with a conflict with the controller on the .metadata.annotations.template field, which matches the description. With this PR and the new-style chart:
helm upgrade runs succeedcaBundle survives each upgraderesourceVersion unchangedmanagedFields the annotations split cleanly, with Helm owning the two webhook-template annotations and sandbox-controller owning template and the cache revisionOne case still ends in a conflict, and it is the rollout the description covers. The webhook controller is started from SetupWithManager in a goroutine on every replica and re-runs Ensure on every update to the configurations. So during the upgrade that moves an existing install onto the new chart, the old pods react to Helm's change and write their cached webhooks back. I reproduced it in this order:
1. Install with the old-style chart and reconcile with master's Ensure
2. helm upgrade to the new-style chart with timeoutSeconds changed from 10 to 12
3. Reconcile with master's Ensure (an old replica), which sets it back to 10 from its cache
4. Reconcile with this PR's Ensure code, which restores 12 as intended
5. Run helm upgrade with timeoutSeconds set to 15
Step 5 fails with a conflict with "sandbox-controller" on .webhooks[name="mutate.sandbox.kb.io"].timeoutSeconds and the same field on the validating webhook. The patch in step 4 corrects the value, but it also takes ownership of the chart's field. If step 4 is skipped, step 5 conflicts with the old replica's field manager instead, so the ownership comes from the old pods' full-object update, and this PR only moves it. Without step 3, step 5 succeeds.
It only affects the upgrade onto this chart, and only when that release also changes a webhook field, because later rollouts run new code that reads the chart's annotation. Running helm upgrade --force-conflicts once clears it, and later upgrades succeed normally with the caBundle kept. It may be worth noting that in the upgrade notes for this change.
harshrajdebug
furykerry
Ⅰ. Describe what this PR does
The controller persists the authoritative webhook definitions in the
templateannotation on the mutating/validating webhook configurations so theself-signed CA bundle it injects survives manifest re-applies. Shipping that
annotation from the Helm chart as an empty placeholder made the chart and the
controller (its own field manager) contend for ownership of
.metadata.annotations.templateunder server-side apply, sohelm upgradefailed with a field-ownership conflict.
This PR splits ownership so the two managers never collide, and makes cache
recovery safe during a mixed-version rollout:
agents.kruise.io/webhook-template(canonical desired webhookJSON) and
agents.kruise.io/webhook-template-revision(its SHA-256). Thecontroller keeps owning the runtime
templatecache plus a privateagents.kruise.io/webhook-template-cache-revision.revision hash, treats the desired template as authoritative, and rebuilds its
own cache from it. A stale cache re-applied by an old replica during rollout
is therefore always corrected by a new replica.
reconciliation does not hot-loop on server-defaulted fields.
scoped to controller-owned fields, so the controller never claims the
chart-owned source annotations.
Legacy, non-Helm deployments that carry only the runtime
templatecache keepworking unchanged.
Ⅱ. Does this pull request fix one issue?
NONE
Ⅲ. Describe how to verify it
1.
go test ./pkg/utils/webhookutils/configuration/... -count=12. Confirm a chart-owned desired template with a matching hash is adopted as
authoritative and overwrites a stale controller cache, while a mismatched
hash is rejected.
3. Confirm a second reconciliation over an API-defaulted configuration produces
no patch (no hot loop).
4. Confirm the generated patch is resourceVersion-guarded and never includes the
chart-owned
agents.kruise.io/webhook-template*source annotations.Ⅳ. Special notes for reviews
agents.kruise.io/webhook-templateand its checksum, derived from the same rendered webhook list) lives in the
openkruise/charts repository and is submitted separately.
k8s.io/kubernetes/pkg/apis/admissionregistration/v1defaulters beforecomparison/caching to keep reconciliation idempotent.
legacy caches are all rejected rather than silently applied.