
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).

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.validationFailureAction: Enforceblocks non-compliant resources. Start withAuditto log violations without blocking.matchscopes which resources the rule applies to.patternuses 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 defaultmanaged-by: platform-team label to Deployments and Services when absent.
patchStrategicMergebehaves 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).
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.- Default
NetworkPolicyto lock down new namespaces ResourceQuotaandLimitRangeper namespace- Create
ServiceAccountand RBAC bindings automatically for teams

- 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:READY: falsefor 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/excludecriteria and resourcekinds. - If webhooks are failing, inspect the Kyverno pod logs and the webhook configuration in
kubectl get validatingwebhookconfigurations/kubectl get mutatingwebhookconfigurations.

- Kyverno policies are native Kubernetes YAML—no separate policy language required.
- Kyverno supports validate, mutate, and generate in a single policy resource.
- Start in
Auditmode, review PolicyReports, iterate, then move toEnforce. - Use Kyverno for mutation and common platform validation patterns; use Gatekeeper (Rego) for complex validation logic. They can coexist in the same cluster.
- Kyverno official docs: https://kyverno.io/
- Gatekeeper (OPA) docs: https://open-policy-agent.github.io/gatekeeper/
- Rego policy language: https://www.openpolicyagent.org/docs/latest/policy-language/rego/
- Kubernetes admission controllers: https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/
- Kyverno Best Practices: https://kyverno.io/docs/best-practices/