Skip to main content
Earlier we looked at how the Kyverno apply command validates resources with a clear pass/fail result. In this lesson we’ll focus on how the Kyverno CLI previews the exact effects of mutate and generate rules so you can see the concrete changes Kyverno would perform in a real cluster.

Previewing mutate rules

Start with a mutate policy that injects default resource requests into containers that lack them:
This policy uses foreach to iterate over each container in a Pod. The +(memory) and +(cpu) anchors indicate “add only if not present” — existing resources.requests remain untouched. Here’s a simple NGINX Pod manifest that lacks a resources block:
When Kyverno runs in a cluster this policy will mutate the Pod prior to creation so the container receives the default requests. Locally, the Kyverno CLI can render exactly that mutated YAML so you can inspect changes before applying them. Run:
Example CLI output (truncated for clarity):
To save the mutated manifest to a file (handy for testing, CI, or debugging), add --output (or -o):

Advanced mutate: target vs trigger resources

Some policies are evaluated against one resource (the trigger) but mutate another resource (the target). For example, a policy that reacts to a ConfigMap create event and mutates an existing Secret. In this case provide both manifests to the CLI so Kyverno can compute the mutation. Example policy (watches ConfigMap, mutates an existing Secret):
When testing locally:
  • Use --resource to pass the trigger (the ConfigMap manifest).
  • Use --target-resource to pass the current state of the Secret that Kyverno should mutate.
Example CLI invocation:
Use --target-resource when the object to be mutated is not the same as the trigger resource. This lets the CLI load the target’s current state and show the exact mutation that would be performed.

Previewing generate rules

Generate rules create new resources (for example, a NetworkPolicy when a Namespace is created). To preview a generate rule, pass the generate policy and the triggering resource with --resource. Kyverno CLI will print the full YAML of the generated resource (not a modified version of the trigger). This output helps you verify template variable substitution, metadata, and the final manifest. You can also save the generated YAML using --output.

Quick reference: common kyverno apply flags

Recap

  • The Kyverno CLI previews the exact outcome of mutate policies by rendering the full mutated resource YAML.
  • For generate rules the CLI prints the generated resource YAML so you can validate the final manifest.
  • Use --output (or -o) to write results to a file for testing or pipelines.
  • When the mutated object differs from the trigger, pass its current state with --target-resource so the CLI can compute the mutation precisely.
The image is a summary with a gradient background, detailing four points related to CLI operations: preview mutations, preview generations, save results, and test mutate existing. Each point includes a brief description and is numbered with colorful labels.
This is it for this lesson.

Watch Video

Practice Lab