Skip to content

Commit

Permalink
feat: add parameters to allow migrate job to use its own database acc…
Browse files Browse the repository at this point in the history
…ount and service account

Signed-off-by: Evan Anderson <[email protected]>
  • Loading branch information
evankanderson committed Jan 10, 2024
1 parent e4377ef commit 471639f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
22 changes: 22 additions & 0 deletions charts/openfga/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ Create the name of the service account to use
{{- end }}
{{- end }}

{{/*
Create the name of the service account to use for the migration job
*/}}
{{- define "openfga.migrationServiceAccountName" -}}
{{- if .Values.migrate.serviceAccount.name }}
{{- default "default" .Values.serviceAccount.name }}
{{- else if .Values.migrate.serviceAccount.create }}
{{- default (printf "%s-%s" (include "openfga.fullname" .) "migrate") .Values.migrate.serviceAccount.name }}
{{- else }}
{{- include "openfga.serviceAccountName" . }}
{{- end }}
{{- end }}

{{/*
Return true if migration job is enabled
*/}}
{{- define "openfga.haveMigration" -}}
{{- if and (has .Values.datastore.engine (list "postgres" "mysql")) .Values.datastore.applyMigrations }}
{{- true -}}
{{- end -}}
{{- end -}}

{{/*
Return true if a secret object should be created
*/}}
Expand Down
28 changes: 25 additions & 3 deletions charts/openfga/templates/job.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- if and (has .Values.datastore.engine (list "postgres" "mysql")) .Values.datastore.applyMigrations -}}
{{- if (include "openfga.haveMigration" .) -}}
apiVersion: batch/v1
kind: Job
metadata:
Expand All @@ -24,7 +24,7 @@ spec:
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
serviceAccountName: {{ include "openfga.serviceAccountName" . }}
serviceAccountName: {{ include "openfga.migrationServiceAccountName" . }}
containers:
- name: migrate-database
securityContext:
Expand All @@ -37,7 +37,16 @@ spec:
value: "{{ .Values.datastore.engine }}"
{{- end }}

{{- if .Values.datastore.uri }}
{{- if .Values.datastore.migrations.uri}}
- name: OPENFGA_DATASTORE_URI
value: "{{ .Values.datastore.migrations.uri }}"
{{- else if .Values.datastore.migrations.uriSecret }}
- name: OPENFGA_DATASTORE_URI
valueFrom:
secretKeyRef:
name: "{{ .Values.datastore.migrations.uriSecret }}"
key: "uri"
{{- else if .Values.datastore.uri }}
- name: OPENFGA_DATASTORE_URI
value: "{{ .Values.datastore.uri }}"
{{- else if .Values.datastore.uriSecret }}
Expand All @@ -47,12 +56,25 @@ spec:
name: "{{ .Values.datastore.uriSecret }}"
key: "uri"
{{- end }}
{{- if .Values.migrate.extraEnvVars }}
{{- toYaml .Values.migrate.extraEnvVars | nindent 12 }}
{{- end }}
resources:
{{- toYaml .Values.datastore.migrations.resources | nindent 12 }}

{{- with .Values.migrate.extraVolumeMounts }}
volumeMounts:
{{- toYaml . | nindent 12 }}
{{- end }}
{{- if .Values.migrate.sidecars }}
{{- include "common.tplvalues.render" ( dict "value" .Values.migrate.sidecars "context" $) | nindent 8 }}
{{- end }}

restartPolicy: Never
{{- with .Values.migrate.extraVolumes }}
volumes:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
Expand Down
20 changes: 20 additions & 0 deletions charts/openfga/templates/serviceaccount.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,23 @@ metadata:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
---
{{- if and (include "openfga.haveMigration" .) .Values.migrate.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "openfga.migrationServiceAccountName" . }}
labels:
{{- include "openfga.labels" . | nindent 4 }}
{{- if .Values.migrate.serviceAccount.annotations }}
{{- with .Values.migrate.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- else if .Values.serviceAccount.annotations -}}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}
19 changes: 19 additions & 0 deletions charts/openfga/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ telemetry:
datastore:
engine: memory
uri:
# A secret containing a "uri" key with the database URI to use. Use this if you have a
# password in the URI (for example)
uriSecret:
maxCacheSize:
maxOpenConns:
Expand All @@ -145,6 +147,11 @@ datastore:
repository: groundnuty/k8s-wait-for
pullPolicy: Always
tag: "v2.0"
# The database URI to use for migrations. If unset, the URI from datastore.uri is used.
uri:
# A secret containing a "uri" key with the database URI to use for migrations. Use this
# if you have a password in the URI (for example).
uriSecret:

postgres:
## @param postgres.enabled enable the bitnami/postgresql subchart and deploy Postgres
Expand Down Expand Up @@ -262,3 +269,15 @@ migrate:
helm.sh/hook-weight: "-5"
helm.sh/hook-delete-policy: "before-hook-creation"
labels: {}
extraEnvVars: []
extraVolumes: []
extraVolumeMounts: []
serviceAccount:
# Specifies whether a separate migration service account should be created
create: false
# Annotations to add to the migration service account. If unset, the annotations
# from serviceAccount.annotations are used.
annotations: {}
# The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template
name: ""

0 comments on commit 471639f

Please sign in to comment.