-
Notifications
You must be signed in to change notification settings - Fork 321
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
dongjiang1989
wants to merge
22
commits into
open-policy-agent:master
Choose a base branch
from
dongjiang1989:add-disallowduplicatedaemonset
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 7c1dbe0
fix by opa check --strict
dongjiang1989 86c4eb2
update make generate-all
dongjiang1989 2a80aed
update suite.yaml and add new unittest case
dongjiang1989 9378ac8
make generate
dongjiang1989 8e8e5de
fix ci
dongjiang1989 ddf1488
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 2a36135
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 d9d8a24
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 70cac67
add expansion.tmpl
dongjiang1989 34ef369
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 f90dce0
fix e2e casae
dongjiang1989 9d5da93
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 973ebb9
keep the policy applicable to pods
dongjiang1989 98612c0
remove examples
dongjiang1989 bb4b929
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 2fef126
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 0054606
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 5763421
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 1183f3e
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 e09214e
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 643f57b
Merge branch 'master' into add-disallowduplicatedaemonset
dongjiang1989 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
artifacthub/library/general/volumeresources/1.0.0/artifacthub-pkg.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
2 changes: 2 additions & 0 deletions
2
artifacthub/library/general/volumeresources/1.0.0/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
resources: | ||
- template.yaml |
12 changes: 12 additions & 0 deletions
12
...ub/library/general/volumeresources/1.0.0/samples/container-emptydir-limit/constraint.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
20 changes: 20 additions & 0 deletions
20
...y/general/volumeresources/1.0.0/samples/container-emptydir-limit/example_allowed_pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
18 changes: 18 additions & 0 deletions
18
...l/volumeresources/1.0.0/samples/container-emptydir-limit/example_disallowed_miss_pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
25 changes: 25 additions & 0 deletions
25
...l/volumeresources/1.0.0/samples/container-emptydir-limit/example_disallowed_muti_pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
artifacthub/library/general/volumeresources/1.0.0/suite.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
164
artifacthub/library/general/volumeresources/1.0.0/template.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
apiVersion: templates.gatekeeper.sh/v1 | ||
kind: ConstraintTemplate | ||
metadata: | ||
name: k8svolumerequests | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ resources: | |
- uniqueserviceselector | ||
- verifydeprecatedapi | ||
- storageclass | ||
- volumeresources |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
resources: | ||
- template.yaml |
12 changes: 12 additions & 0 deletions
12
library/general/volumeresources/samples/container-emptydir-limit/constraint.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
20 changes: 20 additions & 0 deletions
20
library/general/volumeresources/samples/container-emptydir-limit/example_allowed_pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
18 changes: 18 additions & 0 deletions
18
...general/volumeresources/samples/container-emptydir-limit/example_disallowed_miss_pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} |
25 changes: 25 additions & 0 deletions
25
...general/volumeresources/samples/container-emptydir-limit/example_disallowed_muti_pod.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?