any and all resource filters.
In this lesson we’ll dig into the operators that make preconditions powerful, and walk through several practical, real-world examples you can reuse in your Kyverno policies. The focus is on comparing values robustly — from exact matches to set membership, quantities, and time/duration comparisons.
Why this matters: well-crafted preconditions let you scope policy application precisely (for example, only certain service accounts or pods with small memory requests), reducing false positives and enabling safer enforcement.
Operator categories
Operators fall into four broad types. Use the category and operator that best matches the shape of the data you’re comparing.
AnyIn (set-based) example
UseAnyIn when you want a policy to apply only if a value appears in a predefined list. The example below restricts the rule to Namespace creation requests initiated by one of two service accounts.
matchselectsNamespaceresources.preconditionsuses the predefined variable{{serviceAccountName}}.AnyInverifies that the service account name is in the allowed list.
build-default or build-base, giving a simple, readable restriction to approved service accounts.
Numeric / quantity example
Kyverno’s numeric/quantity operators understand Kubernetes resource units, letting you compare values like512Mi or 1Gi directly. Use these operators to enforce policies based on resource requests or limits.
matchselectsPodresources.preconditionsreads the first container’s memory request via a JMESPath-style variable.LessThancompares the memory request to1Gi.
Numeric and quantity operators understand Kubernetes resource units such as
Ki, Mi, Gi, etc., so you can compare memory/CPU values directly.Wildcard and regex string matching
Pattern matching is useful for fields like hostnames, image names, or labels. UseMatches / NotMatches with regular expressions to include or exclude patterns.
Example: apply a policy to any Ingress that points to an external (non-company) domain.
matchselectsIngressresources.preconditionsevaluates the first rule host fromspec.rules[0].host.NotMatcheswith a regex excludes hosts matching the company domain pattern*.mycompany.com.
NotMatches and the regex represents *.mycompany.com, the precondition is true only when the host does not match the company domain — effectively targeting external hosts.
When using regex in preconditions, remember to escape special characters (for example,
. becomes \\. in YAML/regex) and validate your expression with a regex tester. Incorrect escaping can cause unexpected behavior or silent mismatches.Using message to debug failing preconditions
When policies have multiple preconditions (especially OR/AND combinations), it can be hard to know which one failed. Add a message to any precondition — if that expression evaluates to false, Kyverno prints the message in its logs. Think of this as a lightweight debug print for policy logic.
Example: a ConfigMap policy with two preconditions, each providing a clear failure message.
data.food is cheese but data.day is tuesday, Kyverno logs will include “The day is not Monday.” This quickly pinpoints the failing precondition and speeds troubleshooting.