All Policies
Enforce Resources as Ratio
Resource requests often need to be tailored to the type of workload in the container/Pod. With many different types of applications in a cluster, enforcing hard limits on requests or limits may not work and a ratio may be better suited instead. This policy checks every container in a Pod and ensures that memory limits are no more than 2.5x its requests.
Policy Definition
/other/enforce-resources-as-ratio/enforce-resources-as-ratio.yaml
1apiVersion : kyverno.io/v1
2kind: ClusterPolicy
3metadata:
4 name: enforce-resources-as-ratio
5 annotations:
6 policies.kyverno.io/title: Enforce Resources as Ratio
7 policies.kyverno.io/category: Other
8 policies.kyverno.io/severity: medium
9 kyverno.io/kyverno-version: 1.6.0
10 policies.kyverno.io/minversion: 1.6.0
11 kyverno.io/kubernetes-version: "1.23"
12 policies.kyverno.io/subject: Pod
13 policies.kyverno.io/description: >-
14 Resource requests often need to be tailored to the type of workload in the container/Pod.
15 With many different types of applications in a cluster, enforcing hard limits on requests
16 or limits may not work and a ratio may be better suited instead. This policy checks every
17 container in a Pod and ensures that memory limits are no more than 2.5x its requests.
18spec:
19 validationFailureAction: audit
20 rules:
21 - name: check-memory-requests-limits
22 match:
23 any:
24 - resources:
25 kinds:
26 - Pod
27 preconditions:
28 any:
29 - key: "{{ request.operation || 'BACKGROUND' }}"
30 operator: AnyIn
31 value:
32 - CREATE
33 - UPDATE
34 validate:
35 message: Limits may not exceed 2.5x the requests.
36 foreach:
37 - list: "request.object.spec.containers"
38 deny:
39 conditions:
40 any:
41 # Set resources.limits.memory equal to zero if not present and resources.requests.memory equal to 1m rather than zero
42 # to avoid undefined division error. No memory request in this case is basically the same as 1m. Kubernetes API server
43 # will automatically set requests=limits if only limits is defined.
44 - key: "{{ divide('{{ element.resources.limits.memory || '0' }}', '{{ element.resources.requests.memory || '1m' }}') }}"
45 operator: GreaterThan
46 value: 2.5