Skip to content
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

feat: allow usage of existing service account #363

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ helm delete --namespace test my-application
| Key | Type | Default | Description |
|-----|------|---------|-------------|
| rbac.enabled | bool | `true` | Enable RBAC. |
| rbac.existingServiceAccountName | string | `""` | Existing Service Account Name. |
| rbac.serviceAccount.enabled | bool | `false` | Deploy Service Account. |
| rbac.serviceAccount.name | string | `{{ include "application.name" $ }}` | Service Account Name. |
| rbac.serviceAccount.additionalLabels | object | `nil` | Additional labels for Service Account. |
Expand Down
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
3 changes: 3 additions & 0 deletions application/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,9 @@ rbac:
# -- (bool) Enable RBAC.
# @section -- RBAC Parameters
enabled: true
# -- (string) Existing Service Account Name.
# @section -- RBAC Parameters
existingServiceAccountName: ""
serviceAccount:
# -- (bool) Deploy Service Account.
# @section -- RBAC Parameters
Expand Down
Loading