Auto-Gen Rules for Pod Controllers
NoteThe auto-gen feature is only supported for validation rules with patterns and mutation rules with overlay or patch strategic merge. Deny rules and generate rules are not supported.
Pods are one of the most common object types in Kubernetes and as such are the focus of most types of validation rules. But creation of Pods directly is almost never done as it is considered an anti-pattern. Instead, Kubernetes has many higher-level controllers that directly or indirectly manage Pods, namely the Deployment, DaemonSet, StatefulSet, Job, and CronJob resources. Writing policy that targets Pods but must be written for every one of these controllers would be tedious and inefficient. Kyverno solves this issue by supporting automatic generation of policy rules for higher-level controllers from a rule written for a Pod.
For example, when creating a validation policy like below which checks that all images come from an internal, trusted registry, the policy applies to all resources capable of generating Pods.
1apiVersion : kyverno.io/v1 2kind: ClusterPolicy 3metadata: 4 name: restrict-image-registries 5spec: 6 validationFailureAction: enforce 7 rules: 8 - name: validate-registries 9 match: 10 resources: 11 kinds: 12 - Pod 13 validate: 14 message: "Images may only come from our internal enterprise registry." 15 pattern: 16 spec: 17 containers: 18 - image: "registry.domain.com/*"
Once the policy is created, these other resources can be shown in auto-generated rules which Kyverno adds to the policy.
1 Match: 2 Resources: 3 Kinds: 4 DaemonSet 5 Deployment 6 Job 7 StatefulSet 8 Name: autogen-validate-registries 9 Validate: 10 Message: Images may only come from our internal enterprise registry. 11 Pattern: 12 Spec: 13 Template: 14 Spec: 15 Containers: 16 Image: registry.domain.com/* 17 Match: 18 Resources: 19 Kinds: 20 CronJob 21 Name: autogen-cronjob-validate-registries 22 Validate: 23 Message: Images may only come from our internal enterprise registry. 24 Pattern: 25 Spec: 26 Job Template: 27 Spec: 28 Template: 29 Spec: 30 Containers: 31 Image: registry.domain.com/* 32 Validation Failure Action: enforce
This auto-generation behavior is controlled by the
By default, Kyverno inserts an annotation
pod-policies.kyverno.io/autogen-controllers=DaemonSet,Deployment,Job,StatefulSet,CronJob, to generate additional rules that are applied to these controllers.
You can change the annotation
pod-policies.kyverno.io/autogen-controllers to customize the target Pod controllers for the auto-generated rules. For example, Kyverno generates a rule for a
Deployment if the annotation of policy is defined as
labelSelector is specified in the
exclude block, Kyverno skips generating Pod controller rules as these filters may not be applicable to Pod controllers.
To disable auto-generating rules for Pod controllers set
pod-policies.kyverno.io/autogen-controllers to the value