Skip to main content
This lesson demonstrates how to use anchors in Kyverno validate rules. Anchors let you express if-then style logic directly inside a policy pattern, making validation concise and declarative. We’ll cover three anchor types with short policy examples and test Pods:
  • Conditional anchors
  • Equality anchors
  • Existence anchors
Anchors are a Kyverno pattern feature that let you express conditional validation without adding an extra configuration block. failureAction: Enforce blocks resource creation when validation fails.

Quick reference: anchor types

For full Kyverno anchor pattern syntax and examples, see the Kyverno documentation: https://kyverno.io/docs/writing-policies/validation-patterns/

1) Conditional anchors

Conditional anchors implement an if-then rule. The content inside parentheses is the “if” clause; a sibling key at the same hierarchy level is the “then” clause. Policy intent:
  • If a Pod mounts /var/run/docker.sock via a hostPath volume, then the Pod must include label allow-docker=true.
  • failureAction: Enforce blocks non-compliant Pods.
Policy (ClusterPolicy):
How it works:
  • The (spec) -> (volumes) -> (hostPath) path is the “if” condition — it matches when a hostPath volume with path: /var/run/docker.sock is present.
  • The sibling metadata.labels.allow-docker is the “then” clause and must match true (string).
Apply the policy:
Compliant Pod (has required label):
Create it:
Non-compliant Pod (missing label):
Attempt to create it:
Because the “if” condition matched and the required label was missing, admission is denied.
Policies with failureAction: Enforce will block resource creation on non-compliance. Use background and failureAction carefully in production clusters.

2) Equality anchors

Equality anchors use = to indicate the existence of an object. If the object exists, equality anchors let you place constraints on its child fields. Policy intent:
  • If a hostPath volume object exists in a Pod, then its path must not be /var/run/docker.sock.
Policy (ClusterPolicy):
How it works:
  • =(spec) and =(volumes) assert that those objects exist; when they do, the child constraint applies.
  • path: "!/var/run/docker.sock" uses Kyverno’s ! operator to express “not equal”.
Apply the policy:
Non-compliant Pod (disallowed hostPath path):
Attempt to create it:
Compliant Pod (different hostPath path):
Create it:
Because the hostPath existed but its path did not match the forbidden value, the Pod passed validation.

3) Existence anchors

Existence anchors are applied to lists/arrays and assert that at least one element in the list matches a given pattern. This is ideal for requiring a Pod to include a specific sidecar or container image. Policy intent:
  • Verify that at least one container in the Pod uses the nginx image.
Behavior:
  • The policy iterates the containers list and accepts the Pod if any item has image: nginx. If none match, validation fails.
Apply the existence-anchor policy (example policy file assumed applied):
Non-compliant Pod (no nginx container):
Attempt to create it:
Compliant Pod (has an nginx container among others):
Create it:
Because one of the containers matches the required pattern (image: nginx), the policy is satisfied.

Recap

  • Conditional anchors ( ) create if-then rules where the “if” is an object pattern and the “then” is a sibling field.
  • Equality anchors = check for the existence of an object and impose constraints on its children.
  • Existence anchors operate on lists and require at least one element to match the provided pattern.
These anchors let you write compact, declarative Kyverno validate patterns for common policy intents without adding extra configuration blocks.

Watch Video

Practice Lab