Skip to main content
Kyverno is a Kubernetes-native alternative to Gatekeeper that expresses policy using pure Kubernetes YAML. If you can write Kubernetes manifests, you can author Kyverno policies — no new language to learn. In this guide we explain Kyverno’s three rule types (validate, mutate, generate), show concise policy examples and patterns, explain when to choose Kyverno over Gatekeeper, and cover debugging and common issues.

Overview: Kyverno rule types

A single Policy 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 NetworkPolicy when 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

Key points:
  • A single ClusterPolicy holds all rules (no templates, no Rego).
  • spec.validationFailureAction: Enforce causes non-compliant objects to be rejected. Use Audit while 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

Notes:
  • patchStrategicMerge merges labels and will not overwrite an existing label value; it adds the label only if missing.
  • Use patchJson6902 for precise add/remove/replace operations at JSON paths.

3) Combine mutate + validate in one policy (pattern)

Behavior:
  • 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

Notes:
  • The namespace field references the created Namespace using the expression {{request.object.metadata.name}}.
  • Use generate to ensure guardrails are created automatically for new namespaces.

Kyverno vs Gatekeeper — when to choose what

Links and references:
Kyverno and Gatekeeper can run in the same cluster because they register independent admission webhooks.

Debugging, status and common commands

  • kubectl get clusterpolicy shows Kyverno ClusterPolicy objects and the READY column (policy loaded and valid).
  • Kyverno emits PolicyReport and ClusterPolicyReport resources for audit-mode violations.
  • Check controller logs when webhook errors occur.
Commands quick reference:
Common issues and how to troubleshoot:
  • READY = false: usually indicates invalid policy YAML or a schema mismatch. Inspect kubectl describe clusterpolicy <name> and Kyverno logs.
  • Policies not taking effect: verify match criteria 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.
Further reading:

Watch Video