Skip to content

Commit

Permalink
feat: allow usage of existing service account
Browse files Browse the repository at this point in the history
  • Loading branch information
aslafy-z authored Nov 15, 2024
1 parent 2c97270 commit 0ef820d
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 23 deletions.
15 changes: 15 additions & 0 deletions application/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@ reference:
kind: Route
name: {{ include "application.name" . }}
{{- end }}

{{- define "application.service-account-name" }}
{{- if .Values.rbac.enabled }}
{{- if and .Values.rbac.serviceAccount.enabled .Values.rbac.existingServiceAccountName }}
{{- fail "Conflict: 'rbac.existingServiceAccountName' is set, but a new service account is being created. Please disable 'rbac.serviceAccount.enabled' or unset 'rbac.existingServiceAccountName'." }}
{{- end }}
{{- if .Values.rbac.serviceAccount.enabled }}
{{- default (include "application.name" .) .Values.rbac.serviceAccount.name }}
{{- else }}
{{- default "null" .Values.rbac.existingServiceAccountName }}
{{- end }}
{{- else }}
null
{{- end }}
{{- end }}
8 changes: 1 addition & 7 deletions application/templates/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ spec:
annotations: {{ toYaml . | nindent 12 }}
{{- end }}
spec:
{{- if $.Values.rbac.enabled }}
{{- if $.Values.rbac.serviceAccount.name }}
serviceAccountName: {{ $.Values.rbac.serviceAccount.name }}
{{- else }}
serviceAccountName: {{ template "application.name" $ }}
{{- end }}
{{- end }}
serviceAccountName: {{ template "application.service-account-name" $ }}
containers:
- name: {{ $name }}
{{- $image := required (print "Undefined image repo for container '" $name "'") $job.image.repository }}
Expand Down
8 changes: 1 addition & 7 deletions application/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ spec:
]
{{- end }}
spec:
serviceAccountName: {{ template "application.service-account-name" $ }}
{{- if .Values.deployment.hostAliases }}
hostAliases:
{{ toYaml .Values.deployment.hostAliases | indent 6 }}
Expand Down Expand Up @@ -308,13 +309,6 @@ spec:
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.rbac.serviceAccount.enabled }}
{{- if .Values.rbac.serviceAccount.name }}
serviceAccountName: {{ .Values.rbac.serviceAccount.name }}
{{- else }}
serviceAccountName: {{ template "application.name" $ }}
{{- end }}
{{- end }}
{{- if .Values.deployment.hostNetwork }}
hostNetwork: {{ .Values.deployment.hostNetwork }}
{{- end }}
Expand Down
8 changes: 1 addition & 7 deletions application/templates/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ spec:
annotations: {{ toYaml . | nindent 8 }}
{{- end }}
spec:
{{- if $.Values.rbac.enabled }}
{{- if $.Values.rbac.serviceAccount.name }}
serviceAccountName: {{ $.Values.rbac.serviceAccount.name }}
{{- else }}
serviceAccountName: {{ template "application.name" $ }}
{{- end }}
{{- end }}
serviceAccountName: {{ template "application.service-account-name" $ }}
containers:
- name: {{ $name }}

Expand Down
2 changes: 1 addition & 1 deletion application/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ default (include "application.name" .) .Values.rbac.serviceAccount.name }}
name: {{ template "application.service-account-name" . }}
namespace: {{ template "application.namespace" . }}
labels:
{{- include "application.labels" $ | nindent 4 }}
Expand Down
44 changes: 44 additions & 0 deletions application/tests/cronjob_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,47 @@ tests:
- equal:
path: spec.jobTemplate.spec.template.spec.containers[0].image
value: example-image:example-tag@sha256:example-digest

- it: yields empty service account name when disabled
set:
cronJob:
enabled: true
jobs:
example:
image:
repository: example-image
rbac.serviceAccount.enabled: false
asserts:
- isNullOrEmpty:
path: spec.jobTemplate.spec.template.spec.serviceAccountName

- it: uses service account name override when present
set:
cronJob:
enabled: true
jobs:
example:
image:
repository: example-image
rbac.serviceAccount.enabled: true
rbac.serviceAccount.name: example-sa
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.serviceAccountName
value: example-sa

- it: uses a generated service account name when not given
set:
cronJob:
enabled: true
jobs:
example:
image:
repository: example-image
applicationName: example-app
rbac.serviceAccount.enabled: true
rbac.serviceAccount.name: ""
asserts:
- equal:
path: spec.jobTemplate.spec.template.spec.serviceAccountName
value: example-app
2 changes: 1 addition & 1 deletion application/tests/deployment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ tests:
set:
rbac.serviceAccount.enabled: false
asserts:
- notExists:
- isNullOrEmpty:
path: spec.template.spec.serviceAccountName

- it: uses service account name override when present
Expand Down
44 changes: 44 additions & 0 deletions application/tests/job_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,47 @@ tests:
path: spec.template.metadata.annotations
value:
helm.sh/hook: "pre-install,pre-upgrade"

- it: yields empty service account name when disabled
set:
job:
enabled: true
jobs:
example:
image:
repository: example-image
rbac.serviceAccount.enabled: false
asserts:
- isNullOrEmpty:
path: spec.template.spec.serviceAccountName

- it: uses service account name override when present
set:
job:
enabled: true
jobs:
example:
image:
repository: example-image
rbac.serviceAccount.enabled: true
rbac.serviceAccount.name: example-sa
asserts:
- equal:
path: spec.template.spec.serviceAccountName
value: example-sa

- it: uses a generated service account name when not given
set:
job:
enabled: true
jobs:
example:
image:
repository: example-image
applicationName: example-app
rbac.serviceAccount.enabled: true
rbac.serviceAccount.name: ""
asserts:
- equal:
path: spec.template.spec.serviceAccountName
value: example-app

0 comments on commit 0ef820d

Please sign in to comment.