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: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:
--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 aConfigMap 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):
- Use
--resourceto pass the trigger (theConfigMapmanifest). - Use
--target-resourceto pass the current state of theSecretthat Kyverno should mutate.
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, aNetworkPolicy 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
mutatepolicies by rendering the full mutated resource YAML. - For
generaterules 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-resourceso the CLI can compute the mutation precisely.
