
Overview: Kyverno rule types
A singlePolicy or ClusterPolicy resource can contain multiple rules of three types:
- validate — check resources and reject non-compliant objects (similar to Gatekeeper, but expressed in YAML pattern matching instead of Rego).
- mutate — modify resources (add defaults, labels, env vars, etc.) via a mutating admission webhook.
- generate — create related resources automatically when a matching resource is created (for example, create a default-deny
NetworkPolicywhen a Namespace is created).

Validate, Mutate, Generate — concise explanation
- Validate: Uses YAML pattern matching and wildcards to assert required fields and values. Rejections occur when the resource does not match the allowed pattern.
- Mutate: Runs as a mutating admission webhook and modifies the incoming resource before it is persisted. Useful for adding default labels, setting
imagePullPolicy, injecting environment variables, etc. - Generate: Automatically creates another resource in response to a matching resource creation (common for guardrails like
NetworkPolicy,ResourceQuota,LimitRange, or service accounts).

Examples
Below are minimal, copy-ready policy examples that illustrate common patterns.1) Validation example — restrict image registries
- A single
ClusterPolicyholds all rules (no templates, no Rego). spec.validationFailureAction: Enforcecauses non-compliant objects to be rejected. UseAuditwhile testing.
Use
validationFailureAction: Audit during rollout to collect violations without blocking resources. Review PolicyReport and ClusterPolicyReport entries before switching to Enforce.2) Mutate example — add a default label
patchStrategicMergemerges labels and will not overwrite an existing label value; it adds the label only if missing.- Use
patchJson6902for precise add/remove/replace operations at JSON paths.
3) Combine mutate + validate in one policy (pattern)
- Resources without the label will have it added automatically by the mutate rule.
- If the label is removed later, the validate rule will reject updates that do not match (subject to webhook ordering and timing).
Mutations run before validations. Be aware of webhook ordering and timing: a validate rule may rely on a prior mutate rule to add required fields. Test to ensure the intended behavior.
4) Generate example — create a default-deny NetworkPolicy per Namespace
- The
namespacefield references the created Namespace using the expression{{request.object.metadata.name}}. - Use
generateto ensure guardrails are created automatically for new namespaces.
Kyverno vs Gatekeeper — when to choose what
Links and references:

Debugging, status and common commands
kubectl get clusterpolicyshows Kyverno ClusterPolicy objects and the READY column (policy loaded and valid).- Kyverno emits
PolicyReportandClusterPolicyReportresources for audit-mode violations. - Check controller logs when webhook errors occur.
- READY = false: usually indicates invalid policy YAML or a schema mismatch. Inspect
kubectl describe clusterpolicy <name>and Kyverno logs. - Policies not taking effect: verify
matchcriteria are correct; it’s common for match scope to not include the resources you expect. - No rejections while in
Enforce: ensure the policy truly matches the resources and the pattern is correct. - Webhook errors: check Kyverno controller health and logs; confirm webhook configurations and certificates.
Key takeaways

- Kyverno uses pure Kubernetes YAML — no extra language required.
- One policy resource can include validate, mutate, and generate rules.
- Adopt a safe rollout: audit mode first, review
PolicyReports, then switch to enforce. - Choose Kyverno when you need mutation or YAML-native policies; choose Gatekeeper when Rego’s expressiveness is required.
- Kyverno and Gatekeeper can coexist in the same cluster.
- Kyverno docs: https://kyverno.io/docs/
- Gatekeeper: https://open-policy-agent.github.io/gatekeeper/