All Policies

Require PodDisruptionBudget

PodDisruptionBudget resources are useful to ensuring minimum availability is maintained at all times. This policy checks all incoming Deployments and StatefulSets to ensure they have a matching, preexisting PodDisruptionBudget. Note: This policy must be run in `enforce` mode to ensure accuracy.

Policy Definition

/other/require-pdb/require-pdb.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: require-pdb
 5  annotations:
 6    policies.kyverno.io/title: Require PodDisruptionBudget
 7    policies.kyverno.io/category: Sample, EKS Best Practices
 8    policies.kyverno.io/minversion: 1.6.0
 9    kyverno.io/kyverno-version: 1.6.2
10    kyverno.io/kubernetes-version: "1.23"
11    policies.kyverno.io/subject: Deployment, PodDisruptionBudget
12    policies.kyverno.io/description: >-
13      PodDisruptionBudget resources are useful to ensuring minimum availability
14      is maintained at all times. This policy checks all incoming Deployments and StatefulSets
15      to ensure they have a matching, preexisting PodDisruptionBudget.
16      Note: This policy must be run in `enforce` mode to ensure accuracy.      
17spec:
18  validationFailureAction: audit
19  background: false
20  rules:
21    - name: require-pdb
22      match:
23        any:
24          - resources:
25              kinds:
26                - Deployment
27                - StatefulSet
28      preconditions:
29        all:
30          - key: "{{request.operation || 'BACKGROUND'}}"
31            operator: Equals
32            value: CREATE
33          - key: "{{ request.object.spec.replicas }}"
34            operator: GreaterThanOrEquals
35            value: 3
36      context:
37        - name: pdb_count
38          apiCall:
39            urlPath: "/apis/policy/v1/namespaces/{{request.namespace}}/poddisruptionbudgets"
40            jmesPath: "items[?label_match(spec.selector.matchLabels, `{{request.object.spec.template.metadata.labels}}`)] | length(@)"
41      validate:
42        message: "There is no corresponding PodDisruptionBudget found for this Deployment."
43        deny:
44          conditions:
45            any:
46              - key: "{{pdb_count}}"
47                operator: LessThan
48                value: 1