Skip to content

securityContext override for service cronjobs #151

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 14 additions & 2 deletions charts/frontend/templates/services-cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@ spec:
cronjob: "true"
spec:
enableServiceLinks: false
{{- if or (hasKey $job "podSecurityContext") (hasKey $service "podSecurityContext") }}
securityContext:
{{- if hasKey $job "podSecurityContext" }}
{{- toYaml $job.podSecurityContext | nindent 12 }}
{{- else if hasKey $service "podSecurityContext" }}
{{- toYaml $service.podSecurityContext | nindent 12 }}
{{- end }}
{{- end }}
containers:
- name: {{ $jobName }}-cron
image: {{ $service.image | quote }}
{{- if $service.securityContext }}
{{- if or (hasKey $job "containerSecurityContext") (hasKey $service "containerSecurityContext") }}
securityContext:
{{- toYaml $service.securityContext | nindent 14 }}
{{- if hasKey $job "containerSecurityContext" }}
{{- toYaml $job.containerSecurityContext | nindent 14 }}
{{- else if hasKey $service "containerSecurityContext" }}
{{- toYaml $service.containerSecurityContext | nindent 14 }}
{{- end }}
{{- end }}
volumeMounts:
{{- if $service.mounts }}
Expand Down
126 changes: 126 additions & 0 deletions charts/frontend/tests/services-cron_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,129 @@ tests:
content:
name: TZ
value: Foo/Bar

- it: can set podSecurityContext at cron job level
template: services-cron.yaml
set:
services.foo:
image: 'bar'
cron:
foo:
command: echo "Hello world"
schedule: '1 2 3 * *'
podSecurityContext:
fsGroup: 1000
runAsUser: 1001
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.securityContext.fsGroup
value: 1000
- equal:
path: spec.jobTemplate.spec.template.spec.securityContext.runAsUser
value: 1001

- it: can set containerSecurityContext at cron job level
template: services-cron.yaml
set:
services.foo:
image: 'bar'
cron:
foo:
command: echo "Hello world"
schedule: '1 2 3 * *'
containerSecurityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation
value: false
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem
value: true

- it: inherits podSecurityContext from service level
template: services-cron.yaml
set:
services.foo:
image: 'bar'
podSecurityContext:
fsGroup: 2000
runAsUser: 2001
cron:
foo:
command: echo "Hello world"
schedule: '1 2 3 * *'
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.securityContext.fsGroup
value: 2000
- equal:
path: spec.jobTemplate.spec.template.spec.securityContext.runAsUser
value: 2001

- it: inherits containerSecurityContext from service level
template: services-cron.yaml
set:
services.foo:
image: 'bar'
containerSecurityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
cron:
foo:
command: echo "Hello world"
schedule: '1 2 3 * *'
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation
value: false
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem
value: true

- it: cronjob level podSecurityContext takes precedence over service level podSecurityContext
template: services-cron.yaml
set:
services.foo:
image: 'bar'
podSecurityContext:
fsGroup: 2000
runAsUser: 2001
cron:
foo:
command: echo "Hello world"
schedule: '1 2 3 * *'
podSecurityContext:
fsGroup: 3000
runAsUser: 3001
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.securityContext.fsGroup
value: 3000
- equal:
path: spec.jobTemplate.spec.template.spec.securityContext.runAsUser
value: 3001

- it: cronjob level containerSecurityContext takes precedence over service level containerSecurityContext
template: services-cron.yaml
set:
services.foo:
image: 'bar'
containerSecurityContext:
allowPrivilegeEscalation: true
readOnlyRootFilesystem: true
cron:
foo:
command: echo "Hello world"
schedule: '1 2 3 * *'
containerSecurityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: false
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation
value: false
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem
value: false
2 changes: 2 additions & 0 deletions charts/frontend/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@
"schedule": { "type": "string" },
"parallelism": { "type": "integer" },
"nodeSelector": { "type": "object" },
"podSecurityContext": { "type": "object" },
"containerSecurityContext": { "type": "object" },
"resources": {
"type": "object",
"additionalProperties": false,
Expand Down