Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(general): Add volumeresources emptyDir sizelimit #514

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5fa81a3
dongjiang, add volumeresources size limit
dongjiang1989 Apr 24, 2024
7c1dbe0
fix by opa check --strict
dongjiang1989 Apr 24, 2024
86c4eb2
update make generate-all
dongjiang1989 Apr 24, 2024
2a80aed
update suite.yaml and add new unittest case
dongjiang1989 Apr 25, 2024
9378ac8
make generate
dongjiang1989 Apr 25, 2024
8e8e5de
fix ci
dongjiang1989 Apr 26, 2024
ddf1488
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Apr 30, 2024
2a36135
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 May 8, 2024
d9d8a24
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 May 23, 2024
70cac67
add expansion.tmpl
dongjiang1989 Jun 11, 2024
34ef369
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Jun 11, 2024
f90dce0
fix e2e casae
dongjiang1989 Jun 11, 2024
9d5da93
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Jun 12, 2024
973ebb9
keep the policy applicable to pods
dongjiang1989 Jun 17, 2024
98612c0
remove examples
dongjiang1989 Jun 18, 2024
bb4b929
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Jun 18, 2024
2fef126
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Jun 26, 2024
0054606
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Jul 4, 2024
5763421
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Aug 2, 2024
1183f3e
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Sep 2, 2024
e09214e
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Sep 12, 2024
643f57b
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 Nov 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 1.0.0
name: k8svolumeresources
displayName: Container emptyDir Volume Resources
createdAt: "2024-04-24T10:00:57Z"
description: Container emptyDir volume resources to be within the specified maximum values.
digest: cbff0bae172a3866c4097350e4c8b607b432356d1530873b011c127826792950
license: Apache-2.0
homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/volumeresources
keywords:
- gatekeeper
- open-policy-agent
- policies
readme: |-
# Container emptyDir Volume Resources
Container emptyDir volume resources to be within the specified maximum values.
install: |-
### Usage
```shell
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/general/volumeresources/1.0.0/template.yaml
```
provider:
name: Gatekeeper Library
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sVolumeRequests
metadata:
name: container-emptydir-limit
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
parameters:
volumesizelimit: 1Gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Pod
metadata:
name: allowed-pod
labels:
app: allowed-pod
spec:
containers:
- name: allowed-pod
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- mountPath: /demo
name: demo-volume
volumes:
- name: demo-volume
emptyDir:
sizeLimit: 16Mi
medium: Memory
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
name: disallowed-miss-pod
labels:
app: disallowed-miss-pod
spec:
containers:
- name: disallowed-miss-pod
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- mountPath: /demo
name: demo-volume
volumes:
- name: demo-volume
emptyDir: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: v1
kind: Pod
metadata:
name: disallowed-muti-pod
labels:
app: disallowed-muti-pod
spec:
containers:
- name: disallowed-muti-pod
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- mountPath: /demo
name: demo-volume
- mountPath: /demo-1
name: demo-volume-1
volumes:
- name: demo-volume
emptyDir:
sizeLimit: 16Mi
medium: Memory
- name: demo-volume-1
emptyDir:
sizeLimit: 2Gi
21 changes: 21 additions & 0 deletions artifacthub/library/general/volumeresources/1.0.0/suite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
metadata:
name: volumeresources
tests:
- name: volumeresources
template: template.yaml
constraint: samples/container-emptydir-limit/constraint.yaml
cases:
- name: example-allowed-pod
object: samples/container-emptydir-limit/example_allowed_pod.yaml
assertions:
- violations: no
- name: example-disallowed-miss-pod
object: samples/container-emptydir-limit/example_disallowed_miss_pod.yaml
assertions:
- violations: yes
- name: example-disallowed-muti-pod
object: samples/container-emptydir-limit/example_disallowed_muti_pod.yaml
assertions:
- violations: yes
164 changes: 164 additions & 0 deletions artifacthub/library/general/volumeresources/1.0.0/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8svolumerequests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this policy validates specifically for emptydir volume size limit, the name, description should be updated to reflect this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ritazh change name to k8semptydirvolumesizelimit?

annotations:
metadata.gatekeeper.sh/title: "Container emptyDir Volume Resources"
metadata.gatekeeper.sh/version: 1.0.0
description: >-
Container emptyDir volume resources to be within the specified maximum values.
spec:
crd:
spec:
names:
kind: K8sVolumeRequests
validation:
# Schema for the `parameters` field
openAPIV3Schema:
type: object
properties:
volumesizelimit:
description: "The maximum allowed emptyDir size limit on a volume."
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8svolumerequests

violation[{"msg": msg}] {
vols := input.review.object.spec.volumes[_]
emptydir := vols.emptyDir
not has_key(emptydir, "sizeLimit")
msg := sprintf("Volume '%v' is not allowed, do not have set sizelimit", [vols.name])
}

violation[{"msg": msg}] {
vols := input.review.object.spec.volumes[_]
emptydir_orig := vols.emptyDir.sizeLimit
size := canonify_size(emptydir_orig)
max_size_orig := input.parameters.volumesizelimit
max_size := canonify_size(max_size_orig)
size > max_size
msg := sprintf("volume <%v> size limit <%v> is higher than the maximum allowed of <%v>", [vols.name, emptydir_orig, max_size_orig])
}

has_key(object, key) {
type_name(object[key])
}

size_multiple("E") = 1000000000000000000000

# 10 ** 18
size_multiple("P") = 1000000000000000000

# 10 ** 15
size_multiple("T") = 1000000000000000

# 10 ** 12
size_multiple("G") = 1000000000000

# 10 ** 9
size_multiple("M") = 1000000000

# 10 ** 6
size_multiple("k") = 1000000

# 10 ** 3
size_multiple("") = 1000

# Kubernetes accepts millibyte precision when it probably shouldn't.
# https://github.com/kubernetes/kubernetes/issues/28741
# 10 ** 0
size_multiple("m") = 1

# 1000 * 2 ** 10
size_multiple("Ki") = 1024000

# 1000 * 2 ** 20
size_multiple("Mi") = 1048576000

# 1000 * 2 ** 30
size_multiple("Gi") = 1073741824000

# 1000 * 2 ** 40
size_multiple("Ti") = 1099511627776000

# 1000 * 2 ** 50
size_multiple("Pi") = 1125899906842624000

# 1000 * 2 ** 60
size_multiple("Ei") = 1152921504606846976000

canonify_size(orig) = new {
is_number(orig)
new := orig * 1000
}

get_suffix(size) = suffix {
is_string(size)
count(size) > 0
suffix := substring(size, count(size) - 1, -1)
size_multiple(suffix)
}

get_suffix(size) = suffix {
is_string(size)
count(size) > 1
suffix := substring(size, count(size) - 2, -1)
size_multiple(suffix)
}

get_suffix(size) = suffix {
is_string(size)
count(size) > 1
not size_multiple(substring(size, count(size) - 1, -1))
not size_multiple(substring(size, count(size) - 2, -1))
suffix := ""
}

get_suffix(size) = suffix {
is_string(size)
count(size) == 1
not size_multiple(substring(size, count(size) - 1, -1))
suffix := ""
}

get_suffix(size) = suffix {
is_string(size)
count(size) == 0
suffix := ""
}

canonify_size(orig) = new {
is_number(orig)
new := orig * 1000
}

canonify_size(orig) = new {
not is_number(orig)
suffix := get_suffix(orig)
raw := replace(orig, suffix, "")
regex.match("^[0-9]+(\\.[0-9]+)?$", raw)
new := to_number(raw) * size_multiple(suffix)
}
libs:
- |
package lib.exempt_container

is_exempt(container) {
Copy link
Member

@ritazh ritazh Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is is_exempt used? might be good to also add an exemptImage example to the allowed test case

Copy link
Author

@dongjiang1989 dongjiang1989 Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks.

exempt_images := object.get(object.get(input, "parameters", {}), "exemptImages", [])
img := container.image
exemption := exempt_images[_]
_matches_exemption(img, exemption)
}

_matches_exemption(img, exemption) {
not endswith(exemption, "*")
exemption == img
}

_matches_exemption(img, exemption) {
endswith(exemption, "*")
prefix := trim_suffix(exemption, "*")
startswith(img, prefix)
}
1 change: 1 addition & 0 deletions library/general/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ resources:
- uniqueserviceselector
- verifydeprecatedapi
- storageclass
- volumeresources
2 changes: 2 additions & 0 deletions library/general/volumeresources/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sVolumeRequests
metadata:
name: container-emptydir-limit
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
JaydipGabani marked this conversation as resolved.
Show resolved Hide resolved
parameters:
volumesizelimit: 1Gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v1
kind: Pod
metadata:
name: allowed-pod
labels:
app: allowed-pod
spec:
containers:
- name: allowed-pod
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- mountPath: /demo
name: demo-volume
volumes:
- name: demo-volume
emptyDir:
sizeLimit: 16Mi
medium: Memory
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
name: disallowed-miss-pod
labels:
app: disallowed-miss-pod
spec:
containers:
- name: disallowed-miss-pod
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- mountPath: /demo
name: demo-volume
volumes:
- name: demo-volume
emptyDir: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: v1
kind: Pod
metadata:
name: disallowed-muti-pod
labels:
app: disallowed-muti-pod
spec:
containers:
- name: disallowed-muti-pod
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- mountPath: /demo
name: demo-volume
- mountPath: /demo-1
name: demo-volume-1
volumes:
- name: demo-volume
emptyDir:
sizeLimit: 16Mi
medium: Memory
- name: demo-volume-1
emptyDir:
sizeLimit: 2Gi
Loading
Loading