Skip to content

Commit

Permalink
multi: iterate all container types
Browse files Browse the repository at this point in the history
In this commit, we ensure policies apply to `ephemeralContainers`,
`initContainers`, and `containers`.

Signed-off-by: Mohamed Awnallah <[email protected]>
  • Loading branch information
mohamedawnallah committed Jul 15, 2024
1 parent a1b8992 commit c18afd4
Show file tree
Hide file tree
Showing 26 changed files with 84 additions and 29 deletions.
2 changes: 1 addition & 1 deletion best-practices/require-probes/require-probes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ spec:
validate:
message: "Liveness, readiness, or startup probes are required for all containers."
foreach:
- list: request.object.spec.containers[]
- list: request.object.spec.[ephemeralContainers, initContainers, containers][]
deny:
conditions:
all:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec:
- Pod
mutate:
foreach:
- list: request.object.spec.containers
- list: request.object.spec.[ephemeralContainers, initContainers, containers][]
patchStrategicMerge:
spec:
containers:
Expand All @@ -43,7 +43,7 @@ spec:
- Pod
mutate:
foreach:
- list: request.object.spec.containers
- list: request.object.spec.[ephemeralContainers, initContainers, containers][]
patchStrategicMerge:
spec:
containers:
Expand Down
2 changes: 1 addition & 1 deletion other/add-certificates-volume/add-certificates-volume.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ spec:
- UPDATE
mutate:
foreach:
- list: "request.object.spec.containers"
- list: request.object.spec.[ephemeralContainers, initContainers, containers][]
patchStrategicMerge:
spec:
containers:
Expand Down
2 changes: 1 addition & 1 deletion other/add-image-as-env-var/add-image-as-env-var.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ spec:
- Pod
mutate:
foreach:
- list: request.object.spec.containers[]
- list: request.object.spec.[ephemeralContainers, initContainers, containers][]
patchesJson6902: |-
- op: add
path: /spec/containers/{{elementIndex}}/env/-
Expand Down
2 changes: 1 addition & 1 deletion other/annotate-base-images/annotate-base-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
value: DELETE
mutate:
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: imageData
imageRegistry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
validate:
message: "Images containing built-in volumes are prohibited."
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: imageData
imageRegistry:
Expand Down
2 changes: 1 addition & 1 deletion other/block-large-images/block-large-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
validate:
message: "images with size greater than 2Gi not allowed"
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: imageSize
imageRegistry:
Expand Down
2 changes: 1 addition & 1 deletion other/block-stale-images/block-stale-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ spec:
validate:
message: "Images built more than 6 months ago are prohibited."
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: imageData
imageRegistry:
Expand Down
2 changes: 1 addition & 1 deletion other/check-nvidia-gpu/check-nvidia-gpu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
validate:
message: "Images which reserve NVIDIA GPUs must be built to use them."
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: imageData
imageRegistry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ spec:
- Pod
preconditions:
all:
- key: "{{ length(request.object.spec.containers[].livenessProbe.exec.command[] || `[]`) }}"
- key: "{{ length(request.object.spec.[ephemeralContainers, initContainers, containers][].livenessProbe.exec.command[] || `[]`) }}"
operator: GreaterThan
value: 0
- key: "{{ request.operation }}"
Expand All @@ -40,12 +40,12 @@ spec:
- key:
- true
operator: AnyIn
value: "{{ request.object.spec.containers[].livenessProbe.exec.command[].regex_match('\\bjcmd\\b',@) }}"
value: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][].livenessProbe.exec.command[].regex_match('\\bjcmd\\b',@) }}"
- key:
- true
operator: AnyIn
value: "{{ request.object.spec.containers[].livenessProbe.exec.command[].regex_match('\\bps\\b',@) }}"
value: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][].livenessProbe.exec.command[].regex_match('\\bps\\b',@) }}"
- key:
- true
operator: AnyIn
value: "{{ request.object.spec.containers[].livenessProbe.exec.command[].regex_match('\\bls\\b',@) }}"
value: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][].livenessProbe.exec.command[].regex_match('\\bls\\b',@) }}"
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ spec:
validate:
message: Limits may not exceed 2.5x the requests.
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
deny:
conditions:
any:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
value: DELETE
mutate:
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: maintainer
imageRegistry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ spec:
deny:
conditions:
any:
- key: "{{request.object.spec.containers[] | length(@)}}"
- key: "{{request.object.spec.[ephemeralContainers, initContainers, containers][] | length(@)}}"
operator: GreaterThan
value: "4"
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ spec:
deny:
conditions:
any:
- key: "{{ request.object.spec.containers[?resources.requests.memory!=resources.limits.memory] | length(@) }}"
operator: NotEquals
- key: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][] | [?resources.requests.memory!=resources.limits.memory] | length(@) }}"
operator: GreaterThanOrEquals
value: 0
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
validate:
message: "Images with root user are not allowed to be pulled from any registry other than ghcr.io."
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: imageData
imageRegistry:
Expand Down
21 changes: 21 additions & 0 deletions other/prepend-image-registry/prepend-image-registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,24 @@ spec:
initContainers:
- name: "{{ element.name }}"
image: registry.io/{{ images.initContainers."{{element.name}}".path}}:{{images.initContainers."{{element.name}}".tag}}
- name: prepend-registry-ephemeralContainers
match:
any:
- resources:
kinds:
- Pod
preconditions:
all:
- key: "{{request.operation || 'BACKGROUND'}}"
operator: AnyIn
value:
- CREATE
- UPDATE
mutate:
foreach:
- list: "request.object.spec.ephemeralContainers"
patchStrategicMerge:
spec:
containers:
- name: "{{ element.name }}"
image: registry.io/{{ images.ephemeralContainers."{{element.name}}".path}}:{{images.ephemeralContainers."{{element.name}}".tag}}
2 changes: 1 addition & 1 deletion other/remove-hostpath-volumes/remove-hostpath-volumes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ spec:
patchesJson6902: |-
- path: /spec/volumes/{{elementIndex}}
op: remove
- list: request.object.spec.containers[]
- list: request.object.spec.[ephemeralContainers, initContainers, containers][]
foreach:
- list: " element.volumeMounts || `[]` "
order: Descending
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ spec:
patchesJson6902: |-
- path: /spec/volumes/{{elementIndex}}
op: remove
- list: request.object.spec.containers[]
- list: request.object.spec.[ephemeralContainers, initContainers, containers][]
foreach:
- list: element.volumeMounts
order: Descending
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,18 @@ spec:
containers:
- name: "{{ element.name }}"
image: harbor.example.com/k8s/{{imageData.repository}}:{{imageData.identifier}}
- list: request.object.spec.ephemeralContainers[]
context:
- name: imageData
imageRegistry:
reference: "{{ element.image }}"
preconditions:
any:
- key: "{{imageData.registry}}"
operator: Equals
value: index.docker.io
patchStrategicMerge:
spec:
containers:
- name: "{{ element.name }}"
image: harbor.example.com/k8s/{{imageData.repository}}:{{imageData.identifier}}
19 changes: 19 additions & 0 deletions other/replace-image-registry/replace-image-registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ spec:
initContainers:
- name: "{{ element.name }}"
image: "{{ regex_replace_all('^(localhost/|(?:[a-z0-9]+\\.)+[a-z0-9]+/)?(.*)$', '{{element.image}}', 'myregistry.corp.com/$2' )}}"
- name: replace-image-registry-pod-ephemeralContainers
match:
any:
- resources:
kinds:
- Pod
preconditions:
all:
- key: "{{ request.object.spec.ephemeralContainers[] || `[]` | length(@) }}"
operator: GreaterThanOrEquals
value: 1
mutate:
foreach:
- list: "request.object.spec.ephemeralContainers"
patchStrategicMerge:
spec:
initContainers:
- name: "{{ element.name }}"
image: "{{ regex_replace_all('^(localhost/|(?:[a-z0-9]+\\.)+[a-z0-9]+/)?(.*)$', '{{element.image}}', 'myregistry.corp.com/$2' )}}"
2 changes: 1 addition & 1 deletion other/require-base-image/require-base-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ spec:
validate:
message: "Images must specify a source/base image from which they are built."
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: imageData
imageRegistry:
Expand Down
2 changes: 1 addition & 1 deletion other/require-image-source/require-image-source.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ spec:
validate:
message: "The image source must be specified in a label or annotation."
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: imageData
imageRegistry:
Expand Down
4 changes: 2 additions & 2 deletions other/require-qos-burstable/require-qos-burstable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
all:
- key: requests
operator: AnyNotIn
value: "{{ request.object.spec.containers[].resources.keys(@)[] }}"
value: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][].resources.keys(@)[] }}"
- key: limits
operator: AnyNotIn
value: "{{ request.object.spec.containers[].resources.keys(@)[] }}"
value: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][].resources.keys(@)[] }}"
2 changes: 1 addition & 1 deletion other/require-qos-guaranteed/require-qos-guaranteed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spec:
validate:
message: "All containers must define memory and CPU requests and limits where they are equal."
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
pattern:
resources:
requests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ spec:
conditions:
# this checks uids for ALL containers in any pod of the workload
all:
- key: "{{ request.object.spec.containers[].securityContext.to_string(runAsUser) }}"
- key: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][].securityContext.to_string(runAsUser) }}"
operator: AnyIn
value: "{{ uidsAllPodsExceptSameOwnerAsRequestObject }}"
2 changes: 1 addition & 1 deletion other/resolve-image-to-digest/resolve-image-to-digest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spec:
value: DELETE
mutate:
foreach:
- list: "request.object.spec.containers"
- list: "request.object.spec.[ephemeralContainers, initContainers, containers][]"
context:
- name: resolvedRef
imageRegistry:
Expand Down

0 comments on commit c18afd4

Please sign in to comment.