← Back to Pull requests

fix(webhook): make webhook template cache race-safe across rollouts #995

Open
furykerry opened this pull request with 470 lines diff across 2 files
View on GitHub
furykerry furykerry commented on Sep 21, 2026 09:45 UTC
Author

Ⅰ. Describe what this PR does

The controller persists the authoritative webhook definitions in the
template annotation on the mutating/validating webhook configurations so the
self-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.template under server-side apply, so helm upgrade
failed 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:

  • The chart owns agents.kruise.io/webhook-template (canonical desired webhook

  • JSON) and agents.kruise.io/webhook-template-revision (its SHA-256). The
    controller keeps owning the runtime template cache plus a private
    agents.kruise.io/webhook-template-cache-revision.
  • When a chart-owned desired template is present, the controller validates the

  • 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.
  • Kubernetes API defaults are applied to the decoded webhooks before caching so

  • reconciliation does not hot-loop on server-defaulted fields.
  • Configuration writes now use resourceVersion-guarded strategic-merge patches

  • scoped to controller-owned fields, so the controller never claims the
    chart-owned source annotations.

    Legacy, non-Helm deployments that carry only the runtime template cache keep
    working unchanged.

    Ⅱ. Does this pull request fix one issue?

    NONE

    Ⅲ. Describe how to verify it

    1. go test ./pkg/utils/webhookutils/configuration/... -count=1
    2. 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

  • The paired Helm chart change (which emits agents.kruise.io/webhook-template

  • and its checksum, derived from the same rendered webhook list) lives in the
    openkruise/charts repository and is submitted separately.
  • Webhook definitions decoded from annotations are normalized with the upstream

  • k8s.io/kubernetes/pkg/apis/admissionregistration/v1 defaulters before
    comparison/caching to keep reconciliation idempotent.
  • Empty/malformed desired templates, mismatched hashes, and empty/malformed

  • legacy caches are all rejected rather than silently applied.

    Diff: 470 lines (+443 / -27) across 2 files Reviewer: harshrajdebug Review duration: 74471m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.48) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 470 lines diff across 2 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.83 · 2 substantive comment(s)
    0.83
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    84%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.368
    Real PR Conversation Timeline
    4 items
    kruise-bot kruise-bot commented on Sep 21, 2026 09:45 UTC
    Bot

    [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:

  • OWNERS
  • 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[bot] codecov[bot] commented on Sep 21, 2026 10:01 UTC
    Bot

    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>

  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

  • </details>

    harshrajdebug harshrajdebug commented on Sep 26, 2026 09:12 UTC
    Reviewer

    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:

  • repeated helm upgrade runs succeed

  • the injected caBundle survives each upgrade

  • the cache revision follows the chart revision after the next reconcile

  • a second reconcile leaves resourceVersion unchanged

  • in managedFields the annotations split cleanly, with Helm owning the two webhook-template annotations and sandbox-controller owning template and the cache revision
  • One 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.

    Reviewers
    Assignees
    furykerry furykerry
    Labels
    risk: low confidence: 84.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.3% (MEDIUM) · Higher = larger blast radius
    48.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 84.8% · Lower = rushed or shallow review
    84.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.3% · Safe to merge
    7.3%