
Anchor fundamentals: decision points in a rule
Think of anchors as decision points — forks in the policy processing flow. When an anchor matches, Kyverno either evaluates sibling fields (sideways) or children fields (downwards), depending on the anchor type. Choosing the wrong anchor changes the direction of the logic and can break your intent. Two anchors covered here:- Conditional Anchor — looks sideways to peer/sibling elements (if X, then check peer Y).
- Equality Anchor — looks downwards to children (if parent exists, evaluate its children).
Conditional Anchor — parentheses ( )
The Conditional Anchor is the classic if-then pattern. Marked with parentheses, it defines an “if” condition inside the YAML tree and makes the rule evaluate peer elements as the “then” clause.
Use case: If an image uses the latest tag, then the imagePullPolicy must not be IfNotPresent.
Example pattern (conditional anchor on image):
- The anchor
(image)marks the image field as the if-clause. - If the container image matches
*:latest, Kyverno proceeds to evaluate peer elements of the anchored element — in this case theimagePullPolicysibling. - The rule enforces that
imagePullPolicyis notIfNotPresent(soAlwaysis typically expected).
spec to connect checks deep in spec to a sibling metadata:
- Kyverno sees
(spec): this starts the if-clause. - It drills down to
(volumes)and then(hostPath)looking for a hostPath withpath: "/var/run/docker.sock". - If that entire nested if-condition is true, Kyverno looks at peers of the anchored top-level element
(spec). metadatais a sibling ofspec, so Kyverno evaluates themetadata.labels.allow-dockerrequirement as the then-clause.
path: /var/run/docker.sock, then the pod metadata must include allow-docker: "true".
Key point: Conditional anchors connect elements sideways — they let a condition located deep in spec enforce a requirement on a sibling like metadata.
Equality Anchor — =( )
The Equality Anchor is denoted by = followed by parentheses. Its logic is different: the if-clause is the existence of the field itself, and the then-clause applies to the children of that field (the check flows downwards).
Use case: If a hostPath volume is defined, ensure its path is not /var/lib.
Example pattern (equality anchor on hostPath):
- Equality anchors on
specandvolumestell Kyverno to continue only if those fields exist. - When Kyverno reaches
=(hostPath), the presence of thehostPathfield is the if-clause. - With equality anchors, the then-clause is the children of the anchored element — so Kyverno evaluates
pathinsidehostPath. - The rule enforces that
pathmust not equal/var/run/docker.sock.

Side-by-side summary
Choose your direction before writing a rule: conditional anchors validate sibling fields (sideways), while equality anchors validate children (downwards). Picking the wrong anchor changes the meaning of the rule.
Practical tips and gotchas
- Conditional anchors connect a nested condition to requirements in sibling objects. Use them when you need cross-field/sibling enforcement (e.g., spec -> metadata).
- Equality anchors are for validating the children of a field when that field exists (e.g., hostPath -> path).
- Anchors can be combined and nested, but make sure to reason about directionality — anchors determine where Kyverno looks next.
- Test policies with representative manifests to confirm matching behavior (anchors that seem correct conceptually can still mismatch if the YAML hierarchy is different).

Using the wrong anchor breaks the intention of your rule. If your rule should enforce a sibling requirement but you used an equality anchor, the check will remain inside the same block and won’t validate the sibling field.
Quick reference and links
- Kyverno anchors: conditional
( )vs equality=( )— choose sideways vs downward validation. - Example scenarios: ensure
imagePullPolicywhenimagetag is latest; enforce labels when a hostPath volume is present.
- Kyverno documentation: https://kyverno.io/docs/
- Kubernetes volumes (hostPath): https://kubernetes.io/docs/concepts/storage/volumes/#hostpath