Sub topic 12
Gatekeeper mutation
This chapter assumes you know how admission webhooks work, how Constraints validate objects, and how the Kyverno mutate example in the earlier chapter behaves. Gatekeeper can also modify objects at admission time, and the Kyverno +(managed-by) example has a Gatekeeper counterpart.
How mutation fits into admission
Gatekeeper runs a separate mutating webhook next to its validating webhook. Mutating admission runs before validating admission, so your Constraints check the object after Gatekeeper has changed it. Mutation is enabled by default in current Gatekeeper releases. Older releases required a startup flag.
Mutation does not use ConstraintTemplates or Rego. It uses dedicated custom resources in the mutations.gatekeeper.sh group.
| Kind | Purpose |
|---|---|
| `AssignMetadata` | Set labels or annotations only |
| `Assign` | Set any other field |
| `ModifySet` | Add or remove entries in a list, such as container args |
| `AssignImage` | Rewrite the parts of an image reference (domain, path, tag) |
Adding a label
This AssignMetadata mutator adds managed-by: gatekeeper to Pods.
apiVersion: mutations.gatekeeper.sh/v1
kind: AssignMetadata
metadata:
name: add-managed-by-label
spec:
match:
scope: Namespaced
kinds:
- apiGroups: ["*"]
kinds: ["Pod"]
location: 'metadata.labels."managed-by"'
parameters:
assign:
value: gatekeeperAssignMetadata never overwrites a value that already exists. This makes it the equivalent of Kyverno's +(managed-by) anchor.
Setting a field conditionally
Assign changes arbitrary fields and needs applyTo to name the exact group, version and kind. The location path can select list items by name. Here [name:*] means every container.
apiVersion: mutations.gatekeeper.sh/v1
kind: Assign
metadata:
name: default-pull-policy
spec:
applyTo:
- groups: [""]
kinds: ["Pod"]
versions: ["v1"]
match:
scope: Namespaced
kinds:
- apiGroups: ["*"]
kinds: ["Pod"]
namespaces: ["team-a"]
location: "spec.containers[name:*].imagePullPolicy"
parameters:
pathTests:
- subPath: "spec.containers[name:*].imagePullPolicy"
condition: MustNotExist
assign:
value: AlwaysThe pathTests entry makes the mutator set the field only when it is missing. Without it, Assign overwrites whatever the user wrote.
Common errors
- Mutating a Deployment when you meant the Pod. Mutate Pods, because Pods are created by controllers and the webhook sees each one.
- Writing an
Assignthat sets a field the Pod's schema does not allow. The mutated object then fails validation. - Writing mutators that fight each other. Gatekeeper applies mutators in order of name and re-runs them until the object stops changing. It gives up after a fixed number of iterations, so avoid circular rules.
- Expecting mutation to appear in audit results. Audit scans stored objects and does not mutate them.
Checking the result
Apply a Pod in team-a without imagePullPolicy, then read it back:
kubectl run demo --image=nginx:1.27 -n team-a
kubectl get pod demo -n team-a -o jsonpath='{.spec.containers[0].imagePullPolicy}'The second command should print Always.
Exercise
A Pod is submitted with the label managed-by: me. An AssignMetadata mutator sets managed-by: gatekeeper. What label value does the stored Pod have, and why?
Show answer
The stored Pod has managed-by: me. AssignMetadata never overwrites an existing label or annotation value. To overwrite a value you would need a different mutator, and AssignMetadata cannot do that.