What Kyverno's Autogen Did to a Job Template — and the Annotation That Stopped It

Kyverno helpfully generates rule variants for controllers — and its autogen rewrote an immutable Job template, wedging the GitLab HelmRelease with 'spec.template: field is immutable'. The fix is one annotation on every rewrite policy.

What Kyverno's Autogen Did to a Job Template — and the Annotation That Stopped It

Kyverno has a feature that is almost always what you want: autogen. Write a policy for Pods, and Kyverno generates equivalent rules for the controllers that create them — Deployments, StatefulSets, DaemonSets, Jobs — by transforming the rule to match the pod template inside each. One policy, whole-fleet coverage.

Then one day the GitLab HelmRelease wedged with:

spec.template: field is immutable

The mechanics of the wedge

A GitLab backup Job existed in the cluster. Autogen generated a rule variant targeting Job pod templates, the mutation tried to rewrite the Job's spec.template — and Kubernetes rejects any change to a Job's template, ever. The HelmRelease could not apply, the release stuck, and the error named a field nobody had edited.

The fix is one annotation on every rewrite policy:

annotations:
  pod-policies.kyverno.io/autogen-controllers: none

Now the mutation lands on the Pod at admission time and never on the workload template — which is what the registry-routing design wants anyway.

One annotation between a fleet rewrite and an immutable-field wedge
One annotation between a fleet rewrite and an immutable-field wedge

Autogen generates controller variants from a pod-targeting policy; Jobs are the ones whose templates cannot change.

The consequence the fix carries

With autogen off, the mutation is invisible to anything reading the Deployment's template — including Trivy Operator, which takes the image from the pod template when it writes VulnerabilityReports. The report keeps naming the upstream registry while the kubelet pulls through Harbor. That reporting skew is the subject of the next post; no CVE count was ever affected, only attribution.

The general lesson survives the specifics: a policy engine's convenience feature is still a code path that touches your objects, and "helpfully applying to more things" includes applying to the one thing where mutation is forbidden.

Reading the error backwards

The wedge presented as a GitLab HelmRelease stuck in upgrade, and the error — spec.template: field is immutable — named a field nobody's MR touched. The debugging path that finds the cause is short once you know the mechanism: the error's field path (spec.template) points at a Job, not a Deployment; Jobs are not in the Helm chart's render for this change; so the mutation did not come from the chart at all. It came from admission. The policy diff, and the autogen behavior, completed the picture.

This is the general reading technique for admission-side surprises: when the error's field is not in your diff, ask what else can write that field — mutating webhooks are the usual answer, and autogen is the specific one that writes fields you did not know your policy targeted.

What the annotation does and does not disable

pod-policies.kyverno.io/autogen-controllers: none disables autogen for that policy — the rule then applies only where it is literally written to apply (Pods). It does not weaken the policy for running workloads, because the rewrite design wants pod-time mutation: the kubelet reads the image from the Pod, not the template, so rewriting at pod admission is the point of interception.

The cost is confined to observability, and it is precisely the cost the registry-label-lies post quantifies: trivy-operator reads the template, so the report and the Pod disagree about the registry. The design accepted that cost knowingly — and then paid it down in the exporter, by joining on digest. The alternative (leaving autogen on, mutating templates where mutable) was rejected because the failure it buys — wedged HelmReleases on immutable Jobs — is worse than an attribution column that needs a join.

The checklist line it produced

The incident's permanent residue is a line in the procedure for adding any new Kyverno policy: if the policy targets Pods, add the autogen annotation and say in the MR description whether anything reads the template field being mutated. One line, one incident, and — like the other load-bearing comments — it lives where the next policy author will actually see it.

Next: the registry label lies — reading dashboards when the kubelet and the scanner disagree.