-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apps-sc: add policy to reject local storage emptydir
- Loading branch information
Showing
9 changed files
with
576 additions
and
0 deletions.
There are no files selected for viewing
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
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,4 @@ | ||
opa: | ||
rejectLocalStorageEmptyDir: | ||
# In cluster api cluster autoscaler is regularly enabled, which is when we want to have this enabled | ||
enabled: true |
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
28 changes: 28 additions & 0 deletions
28
....d/charts/gatekeeper/constraints/templates/reject-local-storage-empty-dir/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,28 @@ | ||
{{- if .Values.rejectLocalStorageEmptyDir.enable -}} | ||
apiVersion: constraints.gatekeeper.sh/v1alpha1 | ||
kind: K8sRejectLocalStorageEmptyDir | ||
metadata: | ||
name: elastisys-reject-local-storage-emptydir | ||
spec: | ||
enforcementAction: {{ .Values.rejectLocalStorageEmptyDir.enforcementAction }} | ||
match: | ||
kinds: | ||
- apiGroups: [""] | ||
kinds: ["Pod", "ReplicationController"] | ||
- apiGroups: ["apps"] | ||
kinds: ["Deployment", "StatefulSet", "DaemonSet", "ReplicaSet"] | ||
- apiGroups: ["extensions"] | ||
kinds: ["Deployment", "StatefulSet", "DaemonSet", "ReplicaSet"] | ||
- apiGroups: ["batch"] | ||
kinds: ["Job", "CronJob"] | ||
excludedNamespaces: ["kube-system", "kube-public", "kube-node-lease", "calico-system"] | ||
namespaceSelector: | ||
matchExpressions: | ||
- key: owner | ||
operator: NotIn | ||
values: | ||
- operator | ||
parameters: | ||
volumeAnnotation: cluster-autoscaler.kubernetes.io/safe-to-evict-local-volumes | ||
podAnnotation: cluster-autoscaler.kubernetes.io/safe-to-evict | ||
{{- end }} |
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
90 changes: 90 additions & 0 deletions
90
helmfile.d/charts/gatekeeper/templates/policies/reject-local-storage-empty-dir.rego
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,90 @@ | ||
package k8srejectlocalstorageemptydir | ||
import future.keywords.in | ||
|
||
# violation if volume has no medium. | ||
violation[{"msg": msg}] { | ||
volume := get_volumes[_] | ||
missing(volume.emptyDir, "medium") | ||
not check_volume_in_annotation(get_metadata, volume) | ||
not check_pod_annotation(get_metadata) | ||
msg := sprintf("The volume <%v> emptyDir is using local storage emptyDir. This can prevent autoscaler from scaling down a node where this is running. Read more about this and possible solutions at https://elastisys.io/welkin/user-guide/safeguards/enforce-no-local-storage-emptydir/",[volume]) | ||
} | ||
|
||
# violation if medium is not Memory. | ||
violation[{"msg": msg}] { | ||
volume := get_volumes[_] | ||
volume.emptyDir.medium != "Memory" | ||
not check_volume_in_annotation(get_metadata, volume) | ||
not check_pod_annotation(get_metadata) | ||
msg := sprintf("The volume <%v> emptyDir is using local storage emptyDir. This can prevent autoscaler from scaling down a node where this is running. Read more about this and possible solutions at https://elastisys.io/welkin/user-guide/safeguards/enforce-no-local-storage-emptydir/",[volume]) | ||
} | ||
|
||
# Get volumes for "Pods" | ||
get_volumes = res { | ||
input.review.object.kind == "Pod" | ||
res := input.review.object.spec.volumes | ||
} | ||
|
||
# Get volumes for resources that use pod templates. | ||
get_volumes = res { | ||
kinds := [ | ||
"Deployment", | ||
"StatefulSet", | ||
"DaemonSet", | ||
"ReplicaSet", | ||
"Job", | ||
"ReplicationController" | ||
] | ||
input.review.object.kind == kinds[_] | ||
|
||
res := input.review.object.spec.template.spec.volumes | ||
} | ||
|
||
# Get volumes for "CronJobs" | ||
get_volumes = res { | ||
input.review.object.kind == "CronJob" | ||
res := input.review.object.spec.jobTemplate.spec.template.spec.volumes | ||
} | ||
|
||
# Get metadata for "Pods" | ||
get_metadata = res { | ||
input.review.object.kind == "Pod" | ||
res := input.review.object.metadata | ||
} | ||
|
||
# Get metadata for resources that use pod templates. | ||
get_metadata = res { | ||
kinds := [ | ||
"Deployment", | ||
"StatefulSet", | ||
"DaemonSet", | ||
"ReplicaSet", | ||
"Job", | ||
"ReplicationController" | ||
] | ||
input.review.object.kind == kinds[_] | ||
|
||
res := input.review.object.spec.template.metadata | ||
} | ||
|
||
# Get metadata for "CronJobs" | ||
get_metadata = res { | ||
input.review.object.kind == "CronJob" | ||
res := input.review.object.spec.jobTemplate.spec.template.metadata | ||
} | ||
|
||
# Field missing if it does not exist in the object | ||
missing(obj, field) { | ||
not obj[field] | ||
} | ||
|
||
check_volume_in_annotation(metadata, volume) { | ||
some annotation_key, annotation_value in metadata.annotations | ||
annotation_key == input.parameters.volumeAnnotation | ||
|
||
split(annotation_value, ",")[_] == volume.name | ||
} | ||
|
||
check_pod_annotation(metadata) { | ||
metadata.annotations[input.parameters.podAnnotation] == "true" | ||
} |
Oops, something went wrong.