Skip to content

Commit 73ac31e

Browse files
committed
Add memory limit policy
1 parent 52b905d commit 73ac31e

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

kyverno/policies/pods/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ resources:
1313
- privileged.yaml
1414
- privilege-escalation.yaml
1515
- procMount.yaml
16+
- resources.yaml
1617
- Seccomp.yaml
1718
- SELinux.yaml
1819
- sysctls.yaml
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apiVersion: kyverno.io/v1
2+
kind: ClusterPolicy
3+
metadata:
4+
name: restrict-memory-limit
5+
annotations:
6+
policies.kyverno.io/title: Restrict Memory Limit
7+
policies.kyverno.io/category: Other
8+
policies.kyverno.io/subject: Pod
9+
policies.kyverno.io/description: >-
10+
This policy restricts containers from setting the memory limit above 24Gi.
11+
spec:
12+
validationFailureAction: Enforce
13+
background: true
14+
rules:
15+
- name: default
16+
match:
17+
resources:
18+
kinds:
19+
- Pod
20+
preconditions:
21+
all:
22+
- key: "{{ request.object.spec.containers[].resources.limits.memory || '' }}"
23+
operator: NotEquals
24+
value: ""
25+
validate:
26+
message: "Memory limit must not exceed 24Gi."
27+
pattern:
28+
spec:
29+
containers:
30+
- resources:
31+
limits:
32+
memory: "<=24Gi"

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ policies:
33
- ../hostIPC.yaml
44
- ../hostNetwork.yaml
55
- ../privilege-escalation.yaml
6+
- ../resources.yaml
67
resources:
78
- test-hostIPC.yaml
89
- test-hostNetwork.yaml
10+
- test-resources.yaml
911
- test-privilege-escalation.yaml
1012
results:
11-
# Test hostIPC
13+
# Test hostIPC
1214
- policy: disallow-host-ipc-pods
1315
rule: default
1416
resource: test-hostIPC-not-set
@@ -24,7 +26,7 @@ results:
2426
resource: test-hostIPC-set-to-true
2527
kind: Pod
2628
result: fail
27-
# Test hostNetwork
29+
# Test hostNetwork
2830
- policy: disallow-host-network-pods
2931
rule: default
3032
resource: test-hostNetwork-not-set
@@ -40,7 +42,33 @@ results:
4042
resource: test-hostNetwork-set-to-true
4143
kind: Pod
4244
result: fail
43-
# Test privilege escalation
45+
# Test Restict Memory Limit
46+
- policy: restrict-memory-limit
47+
rule: default
48+
resource: test-memory-restriction-limit-not-set
49+
kind: Pod
50+
result: skip
51+
- policy: restrict-memory-limit
52+
rule: default
53+
resource: test-memory-restriction-within-the-limit-gi
54+
kind: Pod
55+
result: pass
56+
- policy: restrict-memory-limit
57+
rule: default
58+
resource: test-memory-restriction-within-the-limit-mi
59+
kind: Pod
60+
result: pass
61+
- policy: restrict-memory-limit
62+
rule: default
63+
resource: test-memory-restriction-above-the-limit-mi
64+
kind: Pod
65+
result: fail
66+
- policy: restrict-memory-limit
67+
rule: default
68+
resource: test-memory-restriction-above-the-limit-mi
69+
kind: Pod
70+
result: fail
71+
# Test privilege escalation
4472
- policy: disallow-privilege-escalation
4573
rule: default
4674
resource: test-privilege-escalation-not-set
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: test-memory-restriction-limit-not-set
5+
spec:
6+
containers:
7+
- name: test
8+
image: test
9+
---
10+
apiVersion: v1
11+
kind: Pod
12+
metadata:
13+
name: test-memory-restriction-within-the-limit-gi
14+
spec:
15+
containers:
16+
- name: test
17+
image: test
18+
resources:
19+
limits:
20+
memory: "24Gi"
21+
---
22+
apiVersion: v1
23+
kind: Pod
24+
metadata:
25+
name: test-memory-restriction-within-the-limit-mi
26+
spec:
27+
containers:
28+
- name: test
29+
image: test
30+
resources:
31+
limits:
32+
memory: "24000Mi"
33+
---
34+
apiVersion: v1
35+
kind: Pod
36+
metadata:
37+
name: test-memory-restriction-above-the-limit-gi
38+
spec:
39+
containers:
40+
- name: test
41+
image: test
42+
resources:
43+
limits:
44+
memory: "25Gi"
45+
---
46+
apiVersion: v1
47+
kind: Pod
48+
metadata:
49+
name: test-memory-restriction-above-the-limit-mi
50+
spec:
51+
containers:
52+
- name: test
53+
image: test
54+
resources:
55+
limits:
56+
memory: "25000Mi"

0 commit comments

Comments
 (0)