Kyverno rule types:
- validate: allow or deny resources (enforce or audit)
- mutate: automatically modify resources (patch merge)
- generate: create resources in response to events (for example, when a Namespace is created)
Verify Kyverno is running
List Kyverno pods to confirm the controllers and admission webhook are active:Quick reference: policy files and purposes
1) Validate rule (deny or audit)
This example enforces that Pod container images must originate fromdocker.io. The policy uses Kyverno’s YAML pattern matching—no Rego required.
ClusterPolicy (validate-registry.yaml):
quay.io (test-pod-untrusted.yaml):
team-dev namespace:
docker.io (test-pod-trusted.yaml):
pattern acts like a YAML overlay pattern. The wildcard "docker.io/*" ensures the image string begins with docker.io/.
2) Mutate rule (automatic changes)
Mutate rules can automatically patch resources as they are created or updated. This example injects labels into Pods created in theteam-dev namespace using patchStrategicMerge.
ClusterPolicy (mutate-labels.yaml):
managed-by=kyverno and environment=dev into the Pod metadata using a strategic merge patch.
Mutations modify resources as they are admitted. Choose the correct patch type (
patchStrategicMerge, patchJson6902, or overlay) for the structure you target to avoid unintentional overwrites.3) Generate rule (create resources automatically)
Generate rules create resources in response to events. This example automatically creates a default-denyNetworkPolicy when a Namespace is created.
ClusterPolicy (generate-netpol.yaml):
namespace field uses the Namespace name that triggered the rule via {{request.object.metadata.name}}.
Apply the generate policy and create a Namespace for testing:
default-deny NetworkPolicy inside auto-np-test automatically when the Namespace was created.
Audit mode and PolicyReports
Kyverno supports an audit mode similar to Gatekeeper’s dry-run. Set a policy’svalidationFailureAction to Audit to record violations without blocking resource creation.
Patch the existing restrict-image-registries policy to audit mode:
Use audit mode when you want to evaluate policy impact before enforcing them. PolicyReports provide a searchable, reviewable record of policy evaluations.
Summary and best practices
- Kyverno uses Kubernetes-native YAML and supports three rule types: validate, mutate, and generate.
- validate enforces or audits resource compliance (set
validationFailureActiontoEnforceorAudit). - mutate modifies resources using patches (use the appropriate patch type for your target).
- generate creates resources in response to events (e.g., create a default-deny
NetworkPolicywhen a Namespace is created). - Use audit mode and PolicyReports to assess impact and reduce blast radius before switching policies to
Enforce.
Links and references
- Kyverno project: https://kyverno.io/
- Kyverno PolicyReports: https://kyverno.io/docs/usage/policy-reports/
- OPA Gatekeeper: https://open-policy-agent.github.io/gatekeeper/
- Rego policy language: https://www.openpolicyagent.org/docs/latest/policy-language/