All Policies

Require Reasonable PodDisruptionBudgets

PodDisruptionBudget resources are useful to ensuring minimum availability is maintained at all times. Achieving a balance between availability and maintainability is important. This policy validates that a PodDisruptionBudget, specified as percentages, allows 50% of the replicas to be out of service in that minAvailable should be no higher than 50% and maxUnavailable should be no lower than 50%.

Policy Definition

/other/require-reasonable-pdbs/require-reasonable-pdbs.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: require-reasonable-pdbs
 5  annotations:
 6    policies.kyverno.io/title: Require Reasonable PodDisruptionBudgets
 7    policies.kyverno.io/category: Other
 8    policies.kyverno.io/subject: PodDisruptionBudget
 9    kyverno.io/kyverno-version: 1.11.4
10    kyverno.io/kubernetes-version: "1.27"
11    policies.kyverno.io/description: >-
12      PodDisruptionBudget resources are useful to ensuring minimum availability is maintained at all times.
13      Achieving a balance between availability and maintainability is important. This policy validates that a
14      PodDisruptionBudget, specified as percentages, allows 50% of the replicas to be out of service in that
15      minAvailable should be no higher than 50% and maxUnavailable should be no lower than 50%.
16spec:
17  validationFailureAction: Audit
18  background: true
19  rules:
20  # Checks if PDB fields minAvailable or maxUnavailable use percentages and, if they do,
21  # ensures that the percentage allows 50% of the replicas to be out of service.
22  - name: require-reasonable-pdb-percentage
23    match:
24      any:
25      - resources:
26          kinds:
27          - PodDisruptionBudget
28    # check if either minAvailable or maxUnavailable is a percentage
29    preconditions:
30      any:
31      - key: '{{ regex_match(''^[0-9]+%$'', ''{{ request.object.spec.minAvailable || ''''}}'') }}'
32        operator: Equals
33        value: true
34      - key: '{{ regex_match(''^[0-9]+%$'', ''{{ request.object.spec.maxUnavailable || ''''}}'') }}'
35        operator: Equals
36        value: true
37    validate:
38      message: >-
39        PodDisruptionBudget percentages should allow 50% out of service. minAvailable should be no higher than 50%
40        and maxUnavailable should be no lower than 50%.
41      # deny if minAvailable is greater than 50% or maxUnavailable is less than 50%
42      deny:
43        conditions:
44          any:
45          - key: '{{ regex_match(''^([1-9]|[1-4][0-9]|5[0])%$'', ''{{ request.object.spec.minAvailable || ''50%''}}'') }}'
46            operator: Equals
47            value: false
48          - key: '{{ regex_match(''^([5-9][0-9]|100)%$'', ''{{ request.object.spec.maxUnavailable || ''50%''}}'') }}'
49            operator: Equals
50            value: false