All Policies

Enforce pod duration

This validation is valuable when annotations are used to define durations, such as to ensure a Pod lifetime annotation does not exceed some site specific max threshold. Pod lifetime annotation can be no greater than 8 hours.

Policy Definition

/other/enforce-pod-duration/enforce-pod-duration.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: pod-lifetime
 5  annotations:
 6    policies.kyverno.io/title: Enforce pod duration
 7    policies.kyverno.io/category: Sample
 8    policies.kyverno.io/minversion: 1.6.0
 9    policies.kyverno.io/subject: Pod
10    policies.kyverno.io/description: >-
11      This validation is valuable when annotations are used to define durations,
12      such as to ensure a Pod lifetime annotation does not exceed some site specific max threshold.
13      Pod lifetime annotation can be no greater than 8 hours.
14spec:
15  validationFailureAction: Audit
16  background: true
17  rules:
18  - name: pods-lifetime
19    match:
20      any:
21      - resources:
22          kinds:
23          - Pod
24    validate:
25      message: "Pod lifetime exceeds limit of 8h"
26      deny:
27        conditions:
28          any:
29          - key: "{{ request.object.metadata.annotations.\"pod.kubernetes.io/lifetime\" || '0s' }}"
30            operator: GreaterThan
31            value: "8h"