Tips & Tricks
These are some tips and tricks you can use when putting together your Kyverno policies.
General
- 
Need more examples or struggling to see a practical use case? Remember to check out the extensive community samples library for ideas on how to author certain types, as well as to kickstart your own needs. Very often, you may not need to start from scratch but can instead use one of the samples as a starting point to further customize. 
- 
Use kubectl explainto explain and explore the various parts and fields of a Kyverno policy. It works just like on native Kubernetes resources!1KIND: Policy 2VERSION: kyverno.io/v1 3 4RESOURCE: validate <Object> 5 6DESCRIPTION: 7 Validation is used to validate matching resources. 8 9FIELDS: 10 anyPattern <> 11 AnyPattern specifies list of validation patterns. At least one of the 12 patterns must be satisfied for the validation rule to succeed. 13 14 deny <Object> 15 Deny defines conditions used to pass or fail a validation rule. 16 17 foreach <Object> 18 ForEach applies policy rule checks to nested elements. 19 20 message <string> 21 Message specifies a custom message to be displayed on failure. 22 23 pattern <> 24 Pattern specifies an overlay-style pattern used to check resources.
- 
Organize your policies in a way which is meaningful to you, your organization, and your Kubernetes cluster design. In most cases, rules can be grouped into a single policy definition. Here are some tips when it comes to organizing rules: - Create a single ClusterPolicyfor allvalidaterules and aPolicyfor all namespacedvalidaterules.
- mutateand- generaterules should go into their own policy definition.
- Policies that cannot be written as a single rule but have highly related processing can go into their own policy definition.
- Name your rules effectively as this is a component that will be displayed to users upon enforcement for validaterules.
 
- Create a single 
- 
Ensure the resource you’re matching and the spec definition align. For example, if writing a mutaterule which matches on a Deployment, the spec of what is being mutated needs to also align to a Deployment which may be different from, for example, a Pod. When copying-and-pasting from other rules, remember to check the spec.
- 
Check Kyverno logs when designing rules if the desired result is not achieved: kubectl -n <kyverno_namespace> logs -l app=kyverno
Validate
- 
When developing your validatepolicies, it’s easiest to setvalidationFailureAction: enforceso when testing you can see the results immediately without having to look at report.
- 
Before deploying into production, ensure you have validationFailureAction: auditso the policy doesn’t have unintended consequences.
- 
validaterules have no precedence/overriding behavior, so even though a rule may be written to either allow or deny a resource/action, one cannot counteract the other. For example, a rule written to ensure all images come from registryreg.corp.comand another rule written to ensure they do not come fromreg.corp.comwill effectively render all image pulls impossible and nothing will run. Where the rule is defined is irrelevant.
- 
The choice between using a patternstatement or adenystatement depends largely on the data you need to consider;patternworks on incoming (new) objects whiledenycan additionally work on variable data such as the API operation (CREATE, UPDATE, etc.), old object data, and ConfigMap data.
Mutate
- 
When writing policies which perform cascading mutations, rule ordering matters. All rules which perform cascading mutations should be in the same policy definition and ordered top to bottom to ensure consistent results. 
- 
Need to mutate an object at a specific ordered position within an array? Use the patchesJson6902method.
Generate
- 
generaterules which trigger off the same source object should be organized in the same policy definition.
- 
Be careful with the synchronize=truebehavior as other users who may have privileges to change an object may do so to a Kyverno-protected object and see their changes wiped away during the next sync cycle.