Auto-Gen Rules for Pod Controllers

Automatically generate rules for Pod controllers.

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      any:
11      - resources:
12          kinds:
13          - Pod
14    validate:
15      message: "Images may only come from our internal enterprise registry."
16      pattern:
17        spec:
18          containers:
19          - 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.

 1spec:
 2  background: true
 3  failurePolicy: Fail
 4  rules:
 5  - match:
 6      any:
 7      - resources:
 8          kinds:
 9          - Pod
10    name: validate-registries
11    validate:
12      message: Images may only come from our internal enterprise registry.
13      pattern:
14        spec:
15          containers:
16          - image: registry.domain.com/*
17  - match:
18      any:
19      - resources:
20          kinds:
21          - DaemonSet
22          - Deployment
23          - Job
24          - StatefulSet
25    name: autogen-validate-registries
26    validate:
27      message: Images may only come from our internal enterprise registry.
28      pattern:
29        spec:
30          template:
31            spec:
32              containers:
33              - image: registry.domain.com/*
34  - match:
35      any:
36      - resources:
37          kinds:
38          - CronJob
39    name: autogen-cronjob-validate-registries
40    validate:
41      message: Images may only come from our internal enterprise registry.
42      pattern:
43        spec:
44          jobTemplate:
45            spec:
46              template:
47                spec:
48                  containers:
49                  - image: registry.domain.com/*
50  validationFailureAction: enforce

This auto-generation behavior is controlled by the pod-policies.kyverno.io/autogen-controllers annotation.

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 pod-policies.kyverno.io/autogen-controllers=Deployment.

When a name or labelSelector is specified in the match or 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 none.

Last modified February 06, 2022 at 6:32 PM PST: updates for 1.6.0 (3279396)