All Policies

Check for matching VerticalPodAutoscaler (VPA)

VerticalPodAutoscaler (VPA) is useful to automatically adjust the resources assigned to Pods. It requires defining a specific target resource by kind and name. There are no built-in validation checks by the VPA controller to ensure that the target resource is associated with it. This policy ensures that the matching kind has a matching VPA.

Policy Definition

/other/check-vpa-configuration/check-vpa-configuration.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: check-vpa-configuration
 5  annotations:
 6    policies.kyverno.io/title: Check for matching VerticalPodAutoscaler (VPA)
 7    policies.kyverno.io/category: Other
 8    policies.kyverno.io/severity: medium
 9    kyverno.io/kyverno-version: 1.11.4 
10    kyverno.io/kubernetes-version: "1.27"     
11    policies.kyverno.io/subject: Deployment, StatefulSet, ReplicaSet, DaemonSet, VerticalPodAutoscaler
12    policies.kyverno.io/description: >-
13      VerticalPodAutoscaler (VPA) is useful to automatically adjust the resources assigned to Pods. 
14      It requires defining a specific target resource by kind and name. There are no built-in 
15      validation checks by the VPA controller to ensure that the target resource is associated with it. 
16      This policy ensures that the matching kind has a matching VPA. 
17spec:
18  validationFailureAction: Audit
19  background: false
20  rules:
21    - name: check-vpa-configuration
22      match:
23        any:
24        - resources:
25            kinds:
26              - Deployment
27              - StatefulSet
28              - ReplicaSet
29              - DaemonSet
30      context:
31        - name: vpas
32          apiCall:
33            urlPath: "/apis/autoscaling.k8s.io/v1/namespaces/{{request.object.metadata.namespace}}/verticalpodautoscalers"
34            jmesPath: "items[?spec.targetRef.kind=='{{ request.object.kind }}'].spec.targetRef.name"
35      validate:
36        message: >-
37          Workload '{{request.object.kind}}/{{request.object.metadata.name}}' 
38          requires a matching VerticalPodAutoscaler (VPA) in the 
39          '{{request.object.metadata.namespace}}' namespace.
40        deny:
41          conditions:
42            all:
43            - key: "{{ request.object.metadata.name }}" 
44              operator: NotIn 
45              value: "{{ vpas }}"