Skip to content

Commit

Permalink
feature: support security context for pod
Browse files Browse the repository at this point in the history
  • Loading branch information
nealyip committed Sep 9, 2024
1 parent 362d6c3 commit 651008a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ helm dependency update ./helm/preview
```

<!-- how to unit test -->
### Unit test development ###
https://github.com/helm-unittest/helm-unittest/blob/main/DOCUMENT.md
### How to run unit tests

```bash
Expand Down
2 changes: 1 addition & 1 deletion cronjob/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v1
description: Helm chart with simple cronjob template
name: cronjob
version: 1.0.0
version: 1.0.1
appVersion: 1.0.0
tillerVersion: ">=2.14.3"
3 changes: 3 additions & 0 deletions cronjob/templates/_argo_cron_workflow.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
{{- if and (.Values.job) (.Values.job.timeout) }}
activeDeadlineSeconds: {{.Values.job.timeout }}
{{- end }}
{{- with .Values.securityContextForPod }}
securityContext: {{ toYaml . | nindent 6 }}
{{- end }}
metrics:
prometheus:
# Metric name (will be prepended with "argo_workflows_")
Expand Down
3 changes: 3 additions & 0 deletions cronjob/templates/_k8s_cronjob.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
serviceAccountName: {{ .Values.name }}-pod-service-account
{{- end }}
restartPolicy: Never
{{- with .Values.securityContextForPod }}
securityContext: {{ toYaml . | nindent 12 }}
{{- end }}
containers:
-
name: app
Expand Down
46 changes: 46 additions & 0 deletions cronjob/tests/securitycontextforpod_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
suite: SecurityContextForPod Test
templates:
- cronjob.yaml
tests:

- it: should render nothing for an argo workflow if securityContextForPod is not specified
values:
- ./values/cronjob/argo_minimal.yaml
asserts:
- notExists:
path: spec.workflowSpec.securityContext

- it: should render securityContext for an argo workflow if securityContextForPod is specified
values:
- ./values/cronjob/argo.yaml
asserts:
- exists:
path: spec.workflowSpec.securityContext
- equal:
path: spec.workflowSpec.securityContext
value:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000

- it: should render securityContext for an argo workflow if securityContextForPod is specified
values:
- ./values/cronjob/argo.yaml
set:
securityContextForPod:
asserts:
- notExists:
path: spec.workflowSpec.securityContext

- it: should render securityContext for a k8s cron job if securityContextForPod is specified
values:
- ./values/cronjob/k8s.yaml
asserts:
- exists:
path: spec.jobTemplate.spec.template.spec.securityContext
- equal:
path: spec.jobTemplate.spec.template.spec.securityContext
value:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
4 changes: 4 additions & 0 deletions cronjob/tests/values/cronjob/argo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ serviceaccount:
pdb:
enable: true
minAvailable: 9999
securityContextForPod:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
nodeSelector:
nodegroup: ec-eks-db-cpu-node-autoscaling-group
tolerations:
Expand Down
4 changes: 4 additions & 0 deletions cronjob/tests/values/cronjob/k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ resources:
limits:
cpu: 500m
memory: 512Mi
securityContextForPod:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
3 changes: 3 additions & 0 deletions cronjob/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ suspend: false
# container-level security attributes
securityContext: {}

# pod-level security attributes
securityContextForPod: {}

# Holds strategic merge patch to apply
# podSpecPatch: ""

Expand Down

0 comments on commit 651008a

Please sign in to comment.