Skip to content

Commit

Permalink
Added support for serviceAccount and allowing for serviceAccounts to …
Browse files Browse the repository at this point in the history
…be defined for each deployment/pod resource
  • Loading branch information
Scroul authored and Lukas Sandholm Duberg committed Dec 2, 2022
1 parent d50fa93 commit 1ca5d26
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 8 deletions.
52 changes: 46 additions & 6 deletions charts/redpanda/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,54 @@ Get the version of redpanda being used as an image

{{/*
Create the name of the service account to use
{{ include "redpanda.serviceAccountName" (list . "statefulset") }}
{{ include "redpanda.serviceAccountName" (list . "kafkaProduceConsume" "tests") }}
* root is the chart root .
* service_id is the service as defined in values.
ex: statefulset, postUpgrade, postInstallUpgrade
* child is a child of serviceAccounts containing it's own serviceAccount definition.
ex: tests
*/}}
{{- define "redpanda.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "redpanda.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
{{- $root := index . 0 }}
{{- $service_id := index . 1 }}
{{- $child := (ternary (last .) "" (gt (len .) 2)) }}
{{- $service := (fromJson (include "redpanda.serviceAccount" (list $root.Values.serviceAccount $service_id $child))) -}}
{{- if $service.create }}
{{- default (include "redpanda.fullname" $root) $service.name }}
{{- else -}}
{{- default "default" $service.name }}
{{- end -}}
{{- end -}}

{{/*
Get the serviceAccount definition and if it does not exists return parent defintion
{{ include "redpanda.serviceAccount" (list .Values.serviceAccount "statefulset") }}
{{ include "redpanda.serviceAccountName" (list .Values.serviceAccount "kafkaProduceConsume" "tests") }}
* root is the chart root .
* service_id is the service as defined in values.
ex: statefulset, postUpgrade, postInstallUpgrade, tests
* child is a child of serviceAccounts containing it's own serviceAccount definition.
ex: tests
*/}}
{{- define "redpanda.serviceAccount" -}}
{{ $root := index . 0 }}
{{ $service := index . 1 }}
{{ $child := (ternary (last .) "" (gt (len .) 2)) }}
{{- with $root -}}
{{- if hasKey . $child -}}
{{ if hasKey (get . $child) $service }}
{{ toJson (get (get . $child) $service) }}
{{- else }}
{{ toJson (get . $child) }}
{{- end }}
{{- else if hasKey . $service }}
{{ toJson (get . $service) }}
{{- else -}}
{{ toJson . }}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Use AppVersion if image.tag is not set
Expand Down
5 changes: 4 additions & 1 deletion charts/redpanda/templates/post-install-upgrade-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ spec:
{{- end }}
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "postInstallUpgrade" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}-post-install
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand All @@ -66,7 +69,7 @@ spec:
- >
{{- if .Values.auth.sasl.enabled }}
{{- range $user := .Values.auth.sasl.users }}
rpk acl user create {{ $user.name }} -p {{ $user.password | quote }} {{ template "rpk-common-flags" $ }}
rpk acl user create {{ $user.name }} -p {{ $user.password | quote }} {{ template "rpk-common-flags" $ }}
;
{{- end }}
{{- end }}
Expand Down
3 changes: 3 additions & 0 deletions charts/redpanda/templates/post-upgrade.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ spec:
{{- end }}
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "postUpgrade" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}-post-upgrade
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
54 changes: 53 additions & 1 deletion charts/redpanda/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
---
{{- if .Values.serviceAccount -}}
{{- if .Values.serviceAccount.create -}}
---
apiVersion: v1
kind: ServiceAccount
metadata:
Expand All @@ -35,3 +36,54 @@ metadata:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
{{- $root := . -}}
{{- range $k, $v := (omit .Values.serviceAccount "create" "annotations" "name") -}}
{{- if $v.create }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "redpanda.serviceAccountName" (list $root $k) }}
namespace: {{ $root.Release.Namespace | quote }}
labels:
helm.sh/chart: {{ template "redpanda.chart" $root }}
app.kubernetes.io/name: {{ template "redpanda.name" $root }}
app.kubernetes.io/instance: {{ $root.Release.Name | quote }}
app.kubernetes.io/managed-by: {{ $root.Release.Service | quote }}
app.kubernetes.io/component: {{ template "redpanda.name" $root }}
{{- with $root.Values.commonLabels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with $v.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}

{{- if .Values.serviceAccount.tests }}
{{- range $k, $v := (omit .Values.serviceAccount.tests "create" "annotations" "name") }}
{{- if $v.create }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "redpanda.serviceAccountName" (list $root $k "tests") }}
namespace: {{ $root.Release.Namespace | quote }}
labels:
helm.sh/chart: {{ template "redpanda.chart" $root }}
app.kubernetes.io/name: {{ template "redpanda.name" $root }}
app.kubernetes.io/instance: {{ $root.Release.Name | quote }}
app.kubernetes.io/managed-by: {{ $root.Release.Service | quote }}
app.kubernetes.io/component: {{ template "redpanda.name" $root }}
{{- with $root.Values.commonLabels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with $v.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
3 changes: 3 additions & 0 deletions charts/redpanda/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ spec:
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "statefulset" ) }}
{{- end }}
securityContext:
{{- toYaml .Values.statefulset.podSecurityContext | nindent 8 }}
initContainers:
Expand Down
3 changes: 3 additions & 0 deletions charts/redpanda/templates/tests/test-api-status.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ metadata:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "apiStatus" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ metadata:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "kafkaInternalTlsStatus" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ spec:
template:
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "kafkaProduceConsume" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
3 changes: 3 additions & 0 deletions charts/redpanda/templates/tests/test-kafka-sasl-status.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ metadata:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "kafkaSaslStatus" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ metadata:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "kafkaSaslTlsStatus" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ metadata:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "pandaProxyInternalTlsStatus" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
3 changes: 3 additions & 0 deletions charts/redpanda/templates/tests/test-pandaproxy-status.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ metadata:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "pandaProxyStatus" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ metadata:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "schemaRegistryInternalTlsStatus" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ metadata:
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
restartPolicy: Never
{{- if .Values.serviceAccount }}
serviceAccountName: {{ include "redpanda.serviceAccountName" (list . "schemaRegistryStatus" "tests" ) }}
{{- end }}
containers:
- name: {{ template "redpanda.name" . }}
image: {{ .Values.image.repository}}:{{ template "redpanda.tag" . }}
Expand Down
56 changes: 56 additions & 0 deletions charts/redpanda/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,40 @@
},
"name": {
"type": "string"
},
"statefulset": {
"$ref": "#/$defs/serviceAccount"
},
"postUpgradeJob": {
"$ref": "#/$defs/serviceAccount"
},
"postInstallJob": {
"$ref": "#/$defs/serviceAccount"
},
"tests": {
"type": "object",
"required": [
"create",
"annotations",
"name"
],
"propertyNames": {
"pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
},
"properties": {
"create": {
"type": "boolean"
},
"annotations": {
"type": "object"
},
"name": {
"type": "string"
}
},
"additionalProperties": {
"$ref": "#/$defs/serviceAccount"
}
}
}
},
Expand Down Expand Up @@ -896,5 +930,27 @@
}
}
}
},
"$defs": {
"serviceAccount": {
"type": "object",
"required": [
"create",
"annotations",
"name"
],
"properties": {
"create": {
"type": "boolean"
},
"annotations": {
"type": "object"
},
"name": {
"type": "string"
}
},
"additionalProperties": false
}
}
}
59 changes: 59 additions & 0 deletions charts/redpanda/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,65 @@ serviceAccount:
# The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template
name: ""
# All config bellow this comment allows for setting specific service accounts
# for parts of the helm. Any services with no additional config will use the
# service account defined in the top level serviceAccount definition.
# Create service account specific to statefulset and tests
#statefulset:
# create: true
# annotations: {}
# name: "test"
# postUpgrade:
# create: false
# annotations: {}
# name: ""
# postInstallUpgrade:
# create: false
# annotations: {}
# name: ""
# Test specific service account
# Setting values for the tests object will apply the same service account to
# all tests.
# tests:
# create: false
# annotations: {}
# name: ""
# apiStatus:
# create:false
# annotations: {}
# name: ""
# kafkaInternalTlsStatus:
# create: false
# annotations: {}
# name: ""
# kafkaProduceConsume:
# create: false
# annotations: {}
# name: ""
# kafkaSaslStatus:
# create: false
# annotations: {}
# name: ""
# kafkaSaslTlsStatus:
# create: false
# annotations: {}
# name: ""
# pandaProxyInternalTlsStatus:
# create: false
# annotations: {}
# name: ""
# pandaProxyStatus:
# create: false
# annotations: {}
# name: ""
# schemaRegistryInternalTlsStatus:
# create: false
# annotations: {}
# name: ""
# schemaRegistryStatus:
# create: false
# annotations: {}
# name: ""

tuning: {}
# This section contains Redpanda tuning parameters.
Expand Down

0 comments on commit 1ca5d26

Please sign in to comment.