Skip to content

Commit 97e22b7

Browse files
authored
Fix resource limit validation (#551)
and also add validation for initContainers
1 parent 9115deb commit 97e22b7

File tree

3 files changed

+92
-27
lines changed

3 files changed

+92
-27
lines changed

kyverno/policies/pods/resource-limits.yaml

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,42 @@ spec:
1818
resources:
1919
kinds:
2020
- Pod
21-
preconditions:
22-
all:
23-
- key: "{{ request.object.spec.containers[].resources.limits.cpu || '' }}"
24-
operator: NotEquals
25-
value: ""
2621
validate:
2722
message: "Containers must not set CPU limits over 6 cores."
28-
pattern:
29-
spec:
30-
containers:
31-
- resources:
32-
limits:
33-
cpu: "<=6"
23+
foreach:
24+
- list: "request.object.spec.initContainers"
25+
deny:
26+
conditions:
27+
any:
28+
- key: '{{ element.resources.limits.cpu || "" }}'
29+
operator: GreaterThan
30+
value: "6"
31+
- list: "request.object.spec.containers"
32+
deny:
33+
conditions:
34+
any:
35+
- key: '{{ element.resources.limits.cpu || "" }}'
36+
operator: GreaterThan
37+
value: "6"
3438
- name: memory
3539
match:
3640
resources:
3741
kinds:
3842
- Pod
39-
preconditions:
40-
all:
41-
- key: "{{ request.object.spec.containers[].resources.limits.memory || '' }}"
42-
operator: NotEquals
43-
value: ""
4443
validate:
4544
message: "Containers must not set memory limits over 24Gi."
46-
pattern:
47-
spec:
48-
containers:
49-
- resources:
50-
limits:
51-
memory: "<=24Gi"
45+
foreach:
46+
- list: "request.object.spec.initContainers"
47+
deny:
48+
conditions:
49+
any:
50+
- key: '{{ element.resources.limits.memory || "" }}'
51+
operator: GreaterThan
52+
value: "24Gi"
53+
- list: "request.object.spec.containers"
54+
deny:
55+
conditions:
56+
any:
57+
- key: '{{ element.resources.limits.memory || "" }}'
58+
operator: GreaterThan
59+
value: "24Gi"

kyverno/policies/pods/test/kyverno-test.yaml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,27 @@ results:
6363
rule: cpu
6464
resource: test-resource-limits-not-set
6565
kind: Pod
66-
result: skip
66+
result: pass
6767
- policy: restrict-resource-limits
6868
rule: memory
6969
resource: test-resource-limits-not-set
7070
kind: Pod
71-
result: skip
71+
result: pass
7272
- policy: restrict-resource-limits
7373
rule: cpu
74-
resource: test-resource-limits-both-ok
74+
resource: test-resource-limits-ok
7575
kind: Pod
7676
result: pass
7777
- policy: restrict-resource-limits
7878
rule: memory
79-
resource: test-resource-limits-both-ok
79+
resource: test-resource-limits-ok
8080
kind: Pod
8181
result: pass
82+
- policy: restrict-resource-limits
83+
rule: cpu
84+
resource: test-resource-limits-init-cpu-too-high
85+
kind: Pod
86+
result: fail
8287
- policy: restrict-resource-limits
8388
rule: cpu
8489
resource: test-resource-limits-cpu-too-high
@@ -94,6 +99,11 @@ results:
9499
resource: test-resource-limits-memory-too-high
95100
kind: Pod
96101
result: pass
102+
- policy: restrict-resource-limits
103+
rule: memory
104+
resource: test-resource-limits-init-memory-too-high
105+
kind: Pod
106+
result: fail
97107
- policy: restrict-resource-limits
98108
rule: memory
99109
resource: test-resource-limits-memory-too-high

kyverno/policies/pods/test/test-resource-limits.yaml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,35 @@ spec:
1010
apiVersion: v1
1111
kind: Pod
1212
metadata:
13-
name: test-resource-limits-both-ok
13+
name: test-resource-limits-ok
1414
spec:
15+
initContainers:
16+
- name: test
17+
image: test
18+
resources:
19+
limits:
20+
cpu: "6"
21+
memory: "24Gi"
22+
containers:
23+
- name: test
24+
image: test
25+
resources:
26+
limits:
27+
cpu: "6"
28+
memory: "24Gi"
29+
---
30+
apiVersion: v1
31+
kind: Pod
32+
metadata:
33+
name: test-resource-limits-init-cpu-too-high
34+
spec:
35+
initContainers:
36+
- name: test
37+
image: test
38+
resources:
39+
limits:
40+
cpu: "7"
41+
memory: "24Gi"
1542
containers:
1643
- name: test
1744
image: test
@@ -35,6 +62,26 @@ spec:
3562
---
3663
apiVersion: v1
3764
kind: Pod
65+
metadata:
66+
name: test-resource-limits-init-memory-too-high
67+
spec:
68+
initContainers:
69+
- name: test
70+
image: test
71+
resources:
72+
limits:
73+
cpu: "6"
74+
memory: "25Gi"
75+
containers:
76+
- name: test
77+
image: test
78+
resources:
79+
limits:
80+
cpu: "6"
81+
memory: "24Gi"
82+
---
83+
apiVersion: v1
84+
kind: Pod
3885
metadata:
3986
name: test-resource-limits-memory-too-high
4087
spec:

0 commit comments

Comments
 (0)