Skip to content

Commit 1ca5d26

Browse files
ScroulLukas Sandholm Duberg
authored andcommitted
Added support for serviceAccount and allowing for serviceAccounts to be defined for each deployment/pod resource
1 parent d50fa93 commit 1ca5d26

16 files changed

+251
-8
lines changed

charts/redpanda/templates/_helpers.tpl

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,54 @@ Get the version of redpanda being used as an image
4949

5050
{{/*
5151
Create the name of the service account to use
52+
{{ include "redpanda.serviceAccountName" (list . "statefulset") }}
53+
{{ include "redpanda.serviceAccountName" (list . "kafkaProduceConsume" "tests") }}
54+
* root is the chart root .
55+
* service_id is the service as defined in values.
56+
ex: statefulset, postUpgrade, postInstallUpgrade
57+
* child is a child of serviceAccounts containing it's own serviceAccount definition.
58+
ex: tests
5259
*/}}
5360
{{- define "redpanda.serviceAccountName" -}}
54-
{{- if .Values.serviceAccount.create }}
55-
{{- default (include "redpanda.fullname" .) .Values.serviceAccount.name }}
56-
{{- else }}
57-
{{- default "default" .Values.serviceAccount.name }}
58-
{{- end }}
59-
{{- end }}
61+
{{- $root := index . 0 }}
62+
{{- $service_id := index . 1 }}
63+
{{- $child := (ternary (last .) "" (gt (len .) 2)) }}
64+
{{- $service := (fromJson (include "redpanda.serviceAccount" (list $root.Values.serviceAccount $service_id $child))) -}}
65+
{{- if $service.create }}
66+
{{- default (include "redpanda.fullname" $root) $service.name }}
67+
{{- else -}}
68+
{{- default "default" $service.name }}
69+
{{- end -}}
70+
{{- end -}}
71+
72+
{{/*
73+
Get the serviceAccount definition and if it does not exists return parent defintion
74+
{{ include "redpanda.serviceAccount" (list .Values.serviceAccount "statefulset") }}
75+
{{ include "redpanda.serviceAccountName" (list .Values.serviceAccount "kafkaProduceConsume" "tests") }}
76+
* root is the chart root .
77+
* service_id is the service as defined in values.
78+
ex: statefulset, postUpgrade, postInstallUpgrade, tests
79+
* child is a child of serviceAccounts containing it's own serviceAccount definition.
80+
ex: tests
81+
*/}}
82+
{{- define "redpanda.serviceAccount" -}}
83+
{{ $root := index . 0 }}
84+
{{ $service := index . 1 }}
85+
{{ $child := (ternary (last .) "" (gt (len .) 2)) }}
86+
{{- with $root -}}
87+
{{- if hasKey . $child -}}
88+
{{ if hasKey (get . $child) $service }}
89+
{{ toJson (get (get . $child) $service) }}
90+
{{- else }}
91+
{{ toJson (get . $child) }}
92+
{{- end }}
93+
{{- else if hasKey . $service }}
94+
{{ toJson (get . $service) }}
95+
{{- else -}}
96+
{{ toJson . }}
97+
{{- end -}}
98+
{{- end -}}
99+
{{- end -}}
60100

61101
{{/*
62102
Use AppVersion if image.tag is not set

charts/redpanda/templates/post-install-upgrade-job.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ spec:
4848
{{- end }}
4949
spec:
5050
restartPolicy: Never
51+
{{- if .Values.serviceAccount }}
52+
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "postInstallUpgrade" ) }}
53+
{{- end }}
5154
containers:
5255
- name: {{ template "redpanda.name" . }}-post-install
5356
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
@@ -66,7 +69,7 @@ spec:
6669
- >
6770
{{- if .Values.auth.sasl.enabled }}
6871
{{- range $user := .Values.auth.sasl.users }}
69-
rpk acl user create {{ $user.name }} -p {{ $user.password | quote }} {{ template "rpk-common-flags" $ }}
72+
rpk acl user create {{ $user.name }} -p {{ $user.password | quote }} {{ template "rpk-common-flags" $ }}
7073
;
7174
{{- end }}
7275
{{- end }}

charts/redpanda/templates/post-upgrade.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ spec:
3232
{{- end }}
3333
spec:
3434
restartPolicy: Never
35+
{{- if .Values.serviceAccount }}
36+
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "postUpgrade" ) }}
37+
{{- end }}
3538
containers:
3639
- name: {{ template "redpanda.name" . }}-post-upgrade
3740
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}

charts/redpanda/templates/serviceaccount.yaml

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/}}
17-
---
17+
{{- if .Values.serviceAccount -}}
1818
{{- if .Values.serviceAccount.create -}}
19+
---
1920
apiVersion: v1
2021
kind: ServiceAccount
2122
metadata:
@@ -35,3 +36,54 @@ metadata:
3536
{{- toYaml . | nindent 4 }}
3637
{{- end }}
3738
{{- end }}
39+
{{- $root := . -}}
40+
{{- range $k, $v := (omit .Values.serviceAccount "create" "annotations" "name") -}}
41+
{{- if $v.create }}
42+
---
43+
apiVersion: v1
44+
kind: ServiceAccount
45+
metadata:
46+
name: {{ include "redpanda.serviceAccountName" (list $root $k) }}
47+
namespace: {{ $root.Release.Namespace | quote }}
48+
labels:
49+
helm.sh/chart: {{ template "redpanda.chart" $root }}
50+
app.kubernetes.io/name: {{ template "redpanda.name" $root }}
51+
app.kubernetes.io/instance: {{ $root.Release.Name | quote }}
52+
app.kubernetes.io/managed-by: {{ $root.Release.Service | quote }}
53+
app.kubernetes.io/component: {{ template "redpanda.name" $root }}
54+
{{- with $root.Values.commonLabels }}
55+
{{- toYaml . | nindent 4 }}
56+
{{- end }}
57+
{{- with $v.annotations }}
58+
annotations:
59+
{{- toYaml . | nindent 4 }}
60+
{{- end }}
61+
{{- end }}
62+
{{- end }}
63+
64+
{{- if .Values.serviceAccount.tests }}
65+
{{- range $k, $v := (omit .Values.serviceAccount.tests "create" "annotations" "name") }}
66+
{{- if $v.create }}
67+
---
68+
apiVersion: v1
69+
kind: ServiceAccount
70+
metadata:
71+
name: {{ include "redpanda.serviceAccountName" (list $root $k "tests") }}
72+
namespace: {{ $root.Release.Namespace | quote }}
73+
labels:
74+
helm.sh/chart: {{ template "redpanda.chart" $root }}
75+
app.kubernetes.io/name: {{ template "redpanda.name" $root }}
76+
app.kubernetes.io/instance: {{ $root.Release.Name | quote }}
77+
app.kubernetes.io/managed-by: {{ $root.Release.Service | quote }}
78+
app.kubernetes.io/component: {{ template "redpanda.name" $root }}
79+
{{- with $root.Values.commonLabels }}
80+
{{- toYaml . | nindent 4 }}
81+
{{- end }}
82+
{{- with $v.annotations }}
83+
annotations:
84+
{{- toYaml . | nindent 4 }}
85+
{{- end }}
86+
{{- end }}
87+
{{- end }}
88+
{{- end }}
89+
{{- end }}

charts/redpanda/templates/statefulset.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ spec:
5757
{{- toYaml . | nindent 8 }}
5858
{{- end }}
5959
spec:
60+
{{- if .Values.serviceAccount }}
61+
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "statefulset" ) }}
62+
{{- end }}
6063
securityContext:
6164
{{- toYaml .Values.statefulset.podSecurityContext | nindent 8 }}
6265
initContainers:

charts/redpanda/templates/tests/test-api-status.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ metadata:
3434
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
3535
spec:
3636
restartPolicy: Never
37+
{{- if .Values.serviceAccount }}
38+
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "apiStatus" "tests" ) }}
39+
{{- end }}
3740
containers:
3841
- name: {{ template "redpanda.name" . }}
3942
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}

charts/redpanda/templates/tests/test-kafka-internal-tls-status.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ metadata:
3434
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
3535
spec:
3636
restartPolicy: Never
37+
{{- if .Values.serviceAccount }}
38+
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "kafkaInternalTlsStatus" "tests" ) }}
39+
{{- end }}
3740
containers:
3841
- name: {{ template "redpanda.name" . }}
3942
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}

charts/redpanda/templates/tests/test-kafka-produce-consume.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ spec:
3939
template:
4040
spec:
4141
restartPolicy: Never
42+
{{- if .Values.serviceAccount }}
43+
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "kafkaProduceConsume" "tests" ) }}
44+
{{- end }}
4245
containers:
4346
- name: {{ template "redpanda.name" . }}
4447
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}

charts/redpanda/templates/tests/test-kafka-sasl-status.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ metadata:
3434
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
3535
spec:
3636
restartPolicy: Never
37+
{{- if .Values.serviceAccount }}
38+
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "kafkaSaslStatus" "tests" ) }}
39+
{{- end }}
3740
containers:
3841
- name: {{ template "redpanda.name" . }}
3942
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}

charts/redpanda/templates/tests/test-kafka-sasl-tls-status.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ metadata:
3434
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
3535
spec:
3636
restartPolicy: Never
37+
{{- if .Values.serviceAccount }}
38+
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "kafkaSaslTlsStatus" "tests" ) }}
39+
{{- end }}
3740
containers:
3841
- name: {{ template "redpanda.name" . }}
3942
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}

0 commit comments

Comments
 (0)