Skip to main content
Kyverno is a Kubernetes-native policy engine and a practical alternative to Gatekeeper. Its major advantage: policies are authored as native Kubernetes YAML (no new language to learn). If you can write Kubernetes manifests, you can write Kyverno policies. This guide explains Kyverno’s three rule types — validate, mutate, and generate — shows common policy patterns, and helps you decide when to choose Kyverno versus Gatekeeper.
A slide titled "Learning Objectives" with three numbered points. It lists: understand Kyverno rule types (validate, mutate, generate); implement policy patterns like image registry enforcement and required labels; and compare Kyverno and Gatekeeper to choose the right policy tool.
Kyverno supports validate, mutate, and generate rules within a single ClusterPolicy or Policy resource:
  • validate — enforce structural and field constraints (expressed with Kubernetes YAML patterns).
  • mutate — modify objects before they are persisted (apply defaults, add labels, inject sidecars, set imagePullPolicy).
  • generate — create new Kubernetes resources automatically in response to events (e.g., new Namespace creation).
A presentation slide titled "Three Rule Types in One Policy" showing Kyverno policy types with three icons labeled 01 Validate, 02 Mutate, and 03. Under Mutate there are short notes about adding defaults and modifying resources before they're stored.

Quick reference: rule types

1) Validate

Validation rules examine resources and reject non-compliant objects. Kyverno uses YAML pattern matching (with wildcards) rather than a separate policy language like Rego. Example: Require container images to come from a corporate registry.
Tips:
  • validationFailureAction: Enforce blocks non-compliant resources. Start with Audit to log violations without blocking.
  • match scopes which resources the rule applies to.
  • pattern uses Kyverno’s YAML pattern matching. For simple to moderate checks this method is concise and easy to maintain.
  • If your requirements need arbitrarily complex queries or computations, consider Gatekeeper (OPA + Rego), which is more expressive.

2) Mutate

Mutate rules modify objects before they are persisted. This works via Kyverno’s mutating admission webhook and is a strong differentiation from Gatekeeper. Example: Add a default managed-by: platform-team label to Deployments and Services when absent.
Details and best practices:
  • patchStrategicMerge behaves like a Kubernetes strategic-merge patch. It will add or update the provided label.
  • To avoid overwriting an existing label value, use conditional mutation logic or JSON Patch (RFC 6902) with Kyverno’s patchesJson6902.
  • Mutations happen before validation webhooks, enabling a pattern where you apply defaults (mutate) and then assert required fields (validate).
Combining mutate + validate in a single policy (common pattern):
Behavior: On create, the mutate rule adds the label if missing; the validate rule ensures it’s present, so deliberate removal will be rejected.

3) Generate

Generate rules automatically create resources when a matching object is created. This unique Kyverno capability helps enforce secure defaults and reduce onboarding friction. Example: Create a default deny NetworkPolicy for each new Namespace, referencing the namespace name via Kyverno variables.
Common generate use cases:
  • Default NetworkPolicy to lock down new namespaces
  • ResourceQuota and LimitRange per namespace
  • Create ServiceAccount and RBAC bindings automatically for teams
A presentation slide titled "Kyverno vs Gatekeeper: When to Use Which" showing a three-column comparison of "Comparison Point", "Kyverno", and "OPA Gatekeeper." It lists differences for Complex Logic (Kyverno: limited pattern matching; Gatekeeper: full Rego), Architecture (Kyverno: single ClusterPolicy; Gatekeeper: ConstraintTemplate + Constraint), and Exam Weight (Kyverno: medium simpler YAML; Gatekeeper: high deeper Rego).
When to choose Kyverno vs Gatekeeper:
  • Use Kyverno when you want mutation (defaults, injections) and straightforward validation expressed in YAML patterns. It’s fast to author and maintain for platform policies.
  • Use Gatekeeper (OPA + Rego) when you need highly expressive, complex validation logic that requires full programmatic capability.
  • Both can run in the same cluster; evaluate per-policy which tool is the best fit.
Best practice: author policies in Audit mode first. Review PolicyReports for violations, iterate on the policy, and then switch the policy to Enforce.

Debugging Kyverno

Common commands and checks:
Troubleshooting hints:
  • READY: false for a ClusterPolicy usually indicates a YAML syntax error or invalid policy field — check the Kyverno controller logs for details.
  • If policies do not seem to target objects you expect, verify the match/exclude criteria and resource kinds.
  • If webhooks are failing, inspect the Kyverno pod logs and the webhook configuration in kubectl get validatingwebhookconfigurations / kubectl get mutatingwebhookconfigurations.
A presentation slide titled "Key Takeaways" with three numbered points. It summarizes Kyverno: it uses native Kubernetes YAML, has three rule types (validate, mutate, generate), and recommends starting in Audit mode, reviewing PolicyReports, fixing issues, then switching to Enforce.
Summary
  • Kyverno policies are native Kubernetes YAML—no separate policy language required.
  • Kyverno supports validate, mutate, and generate in a single policy resource.
  • Start in Audit mode, review PolicyReports, iterate, then move to Enforce.
  • Use Kyverno for mutation and common platform validation patterns; use Gatekeeper (Rego) for complex validation logic. They can coexist in the same cluster.
Links and references

Watch Video