- validate — allow or deny resources
- mutate — automatically modify resources
- generate — create resources in response to events
At a glance — Kyverno rule types
Quick links:
- Kyverno docs: https://kyverno.io
- Kyverno policy examples: https://kyverno.io/docs/writing-policies/
- OPA / Rego reference: https://www.openpolicyagent.org/docs/latest/policy-language/
Prerequisites
- A running Kubernetes cluster with Kyverno installed in the
kyvernonamespace. kubectlconfigured for the target cluster.
Verify Kyverno is running
Check Kyverno pods in thekyverno namespace:
Running, check pod logs and events to diagnose installation issues.
1) Validate rule example — restrict image registries
This example enforces that Pod container images must come fromdocker.io. The policy uses Kyverno YAML pattern overlays — no Rego required.
File: validate-registry.yaml
validationFailureAction: Enforceblocks requests that violate the policy. Switch toAuditto only record violations while allowing creation.- Kyverno uses YAML pattern overlays so policies are readable and Kubernetes-native.
quay.io:
File: test-pod-untrusted.yaml
team-dev:
docker.io:
File: test-pod-trusted.yaml
You can switch
validationFailureAction to Audit to collect violations without blocking resources.2) Mutate rule example — auto-inject labels
Use a mutate rule to automatically inject labels into Pods created in theteam-dev namespace using a strategic merge patch.
File: mutate-labels.yaml
docker.io image so the validate policy does not block it):
managed-by=kyverno and environment=dev were added automatically by the mutate rule.
3) Generate rule example — auto-create NetworkPolicy on Namespace creation
A generate rule can create a default-denyNetworkPolicy whenever a Namespace is created.
File: generate-netpol.yaml
- The
namespacefield uses the template{{request.object.metadata.name}}so the generated NetworkPolicy is created inside the Namespace that triggered the rule.
default-deny in auto-np-test.
4) Audit mode and PolicyReports
Audit mode allows resources that violate policies to be created while Kyverno records the violations in PolicyReport resources. This is useful for observing policy impact before enforcing. Patch therestrict-image-registries policy to Audit:
- The mutate rule passed and injected labels.
- The validate rule failed (image not from
docker.io), but because the policy is in Audit mode, the Pod was admitted and the violation was captured for visibility.
Use Audit mode to evaluate policy impact and blast radius. Once you are confident, switch policies back to
Enforce to block violating resources.Summary and best practices
- Kyverno policies are Kubernetes-native YAML — no Rego required.
- Use the three complementary rule types:
- validate: block or audit resources
- mutate: change resources at admission (strategic merge, JSON patches, etc.)
- generate: create resources in response to events (e.g., default-deny NetworkPolicy)
- Start in Audit mode and review PolicyReports to safely roll out policies.
- Combine mutate + validate rules to auto-fix common issues and enforce desired state.
Links and references
- Kyverno documentation: https://kyverno.io/docs/
- Kyverno policy examples: https://kyverno.io/docs/writing-policies/
- OPA Gatekeeper: https://open-policy-agent.github.io/gatekeeper/
- Rego language: https://www.openpolicyagent.org/docs/latest/policy-language/