Skip to main content
OPA Gatekeeper is a powerful engine, but it requires learning the Rego policy language. Kyverno takes a different approach: policies are written in Kubernetes-native YAML, so teams can author policies without learning a new language. Kyverno supports three primary rule types in a single ClusterPolicy: validate, mutate, and generate.
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)
Below we first confirm Kyverno is running, then demonstrate one example of each rule type (validate, mutate, generate). Finally, we show how to switch to audit mode and inspect PolicyReports.

Verify Kyverno is running

List Kyverno pods to confirm the controllers and admission webhook are active:
Example output:
If the admission-controller Pod is not Running, Kyverno won’t be able to validate or mutate requests.

Quick reference: policy files and purposes

1) Validate rule (deny or audit)

This example enforces that Pod container images must originate from docker.io. The policy uses Kyverno’s YAML pattern matching—no Rego required. ClusterPolicy (validate-registry.yaml):
Apply the policy:
Test the policy using an untrusted Pod that pulls from quay.io (test-pod-untrusted.yaml):
Attempt to create it in the team-dev namespace:
Kyverno will block the request and return a validation error:
Create a trusted Pod that uses docker.io (test-pod-trusted.yaml):
Apply it:
Result:
Note: Kyverno’s 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 the team-dev namespace using patchStrategicMerge. ClusterPolicy (mutate-labels.yaml):
Apply the policy and create a pod to test the mutation:
Verify the Pod’s labels:
Example output:
Kyverno injected 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-deny NetworkPolicy when a Namespace is created. ClusterPolicy (generate-netpol.yaml):
Note: The 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:
Verify the NetworkPolicy in the new namespace:
Example output:
Kyverno created the 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’s validationFailureAction to Audit to record violations without blocking resource creation. Patch the existing restrict-image-registries policy to audit mode:
Now apply the previously failing untrusted pod:
The pod will be admitted, but Kyverno records the violation in PolicyReports. List PolicyReports in the namespace:
Example output:
Describe a PolicyReport to inspect individual results (replace the name and namespace as needed):
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 validationFailureAction to Enforce or Audit).
  • 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 NetworkPolicy when a Namespace is created).
  • Use audit mode and PolicyReports to assess impact and reduce blast radius before switching policies to Enforce.

Watch Video

Practice Lab