All Policies

Require PodDisruptionBudget

PodDisruptionBudget resources are useful to ensuring minimum availability is maintained at all times. This policy checks all incoming Deployments to ensure they have a matching, preexisting PodDisruptionBudget.

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
 8    policies.kyverno.io/minversion: 1.3.6
 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
15      to ensure they have a matching, preexisting PodDisruptionBudget.      
16spec:
17  validationFailureAction: audit
18  background: false
19  rules:
20  - name: require-pdb
21    match:
22      resources:
23        kinds:
24        - Deployment
25    preconditions:
26      any:
27      - key: "{{request.operation}}"
28        operator: Equals
29        value: CREATE
30    context:
31    - name: pdb_count
32      apiCall:
33        urlPath: "/apis/policy/v1/namespaces/{{request.namespace}}/poddisruptionbudgets"
34        jmesPath: "items[?label_match(spec.selector.matchLabels, `{{request.object.spec.template.metadata.labels}}`)] | length(@)"
35    validate:
36      message: "There is no corresponding PodDisruptionBudget found for this Deployment."
37      deny:
38        conditions:
39          any:
40          - key: "{{pdb_count}}"
41            operator: LessThan
42            value: 1