Skip to content

Commit

Permalink
feat: optionally run migrations as an init contianer (#157)
Browse files Browse the repository at this point in the history
avoid the deadlock when using migrations with helm --wait and ArgoCD

Co-authored-by: Raghd Hamzeh <[email protected]>
  • Loading branch information
jagregory and rhamzeh authored Oct 28, 2024
1 parent 20c3505 commit cbf79dc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
40 changes: 39 additions & 1 deletion charts/openfga/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ spec:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
{{ if or (and (has .Values.datastore.engine (list "postgres" "mysql")) .Values.datastore.applyMigrations .Values.datastore.waitForMigrations) .Values.extraInitContainers }}
initContainers:
{{- if and (has .Values.datastore.engine (list "postgres" "mysql")) .Values.datastore.applyMigrations .Values.datastore.waitForMigrations }}
{{- if and (has .Values.datastore.engine (list "postgres" "mysql")) .Values.datastore.applyMigrations .Values.datastore.waitForMigrations (eq .Values.datastore.migrationType "job") }}
- name: wait-for-migration
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
Expand All @@ -46,6 +46,44 @@ spec:
resources:
{{- toYaml .Values.datastore.migrations.resources | nindent 12 }}
{{- end }}
{{- if and (has .Values.datastore.engine (list "postgres" "mysql")) (eq .Values.datastore.migrationType "initContainer") }}
- name: migrate-database
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
args: [ "migrate" ]
env:
{{- if .Values.datastore.engine }}
- name: OPENFGA_DATASTORE_ENGINE
value: "{{ .Values.datastore.engine }}"
{{- end }}
{{- if .Values.datastore.uri }}
- name: OPENFGA_DATASTORE_URI
value: "{{ .Values.datastore.uri }}"
{{- else if .Values.datastore.uriSecret }}
- name: OPENFGA_DATASTORE_URI
valueFrom:
secretKeyRef:
name: "{{ .Values.datastore.uriSecret }}"
key: "uri"
{{- end }}
{{- if .Values.migrate.timeout }}
- name: OPENFGA_TIMEOUT
value: "{{ .Values.migrate.timeout }}"
{{- end }}
{{- with .Values.extraEnvVars }}
{{- toYaml . | 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 }}
{{- end }}
{{- with .Values.extraInitContainers }}
{{- toYaml . | nindent 8 }}
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion 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 and (has .Values.datastore.engine (list "postgres" "mysql")) .Values.datastore.applyMigrations (eq .Values.datastore.migrationType "job") -}}
apiVersion: batch/v1
kind: Job
metadata:
Expand Down
14 changes: 13 additions & 1 deletion charts/openfga/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,21 @@
},
"applyMigrations": {
"type": "boolean",
"description": "enable/disable the job that runs migrations in the datastore",
"description": "enable/disable the running of migrations in the datastore",
"default": true
},
"migrationType": {
"type": [
"string",
"null"
],
"description": "how the migrations will be run",
"default": "job",
"enum": [
"job",
"initContainer"
]
},
"waitForMigrations": {
"type": "boolean",
"description": "wait for migrations to complete before starting the server",
Expand Down

0 comments on commit cbf79dc

Please sign in to comment.