From 0a21cff752350bb187547db8e86470b5c2d23b7c Mon Sep 17 00:00:00 2001 From: Marcin Jasion Date: Fri, 13 Jan 2023 21:28:43 +0100 Subject: [PATCH] Add Job for running migrations Closes #35 --- charts/studio/templates/_helpers.tpl | 2 + charts/studio/templates/configmap.yaml | 3 ++ .../templates/deployment-studio-beat.yaml | 5 -- .../templates/deployment-studio-worker.yaml | 5 -- charts/studio/templates/job_db_migration.yaml | 54 +++++++++++++++++++ .../templates/secret-migration-job.yaml | 6 +++ 6 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 charts/studio/templates/job_db_migration.yaml create mode 100644 charts/studio/templates/secret-migration-job.yaml diff --git a/charts/studio/templates/_helpers.tpl b/charts/studio/templates/_helpers.tpl index 4babcb5c..8d85b506 100644 --- a/charts/studio/templates/_helpers.tpl +++ b/charts/studio/templates/_helpers.tpl @@ -122,4 +122,6 @@ Create the name of the service account to use {{- define "studio.checkSum" }} checksum/studio-cm: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }} checksum/studio-ca-certificates-cm: {{ include (print $.Template.BasePath "/configmap-ca-cert.yaml") . | sha256sum }} +checksum/studio-migration-job-secret: {{ include (print $.Template.BasePath "/secret-migration-job.yaml") . | sha256sum }} +checksum/studio-secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }} {{- end}} diff --git a/charts/studio/templates/configmap.yaml b/charts/studio/templates/configmap.yaml index 960ec7ae..50f35dd7 100644 --- a/charts/studio/templates/configmap.yaml +++ b/charts/studio/templates/configmap.yaml @@ -3,7 +3,10 @@ kind: ConfigMap metadata: name: studio data: + NO_MIGRATE_DB: "1" + WAIT_FOR_MIGRATIONS: "1" ALLOWED_HOSTS: "*" + {{/* {{- if .Values.studioBackend.ingress.enabled }}*/}} {{/* {{- range $host := .Values.studioBackend.ingress.hosts }}*/}} {{/* {{- range .paths }}*/}} diff --git a/charts/studio/templates/deployment-studio-beat.yaml b/charts/studio/templates/deployment-studio-beat.yaml index 13904891..d32e4f4f 100644 --- a/charts/studio/templates/deployment-studio-beat.yaml +++ b/charts/studio/templates/deployment-studio-beat.yaml @@ -41,11 +41,6 @@ spec: protocol: TCP resources: {{- toYaml .Values.studioBeat.resources | nindent 12 }} - env: - - name: "NO_MIGRATE_DB" - value: "1" - - name: "WAIT_FOR_MIGRATIONS" - value: "1" envFrom: - configMapRef: name: studio diff --git a/charts/studio/templates/deployment-studio-worker.yaml b/charts/studio/templates/deployment-studio-worker.yaml index cccbfc2b..89cee4d3 100644 --- a/charts/studio/templates/deployment-studio-worker.yaml +++ b/charts/studio/templates/deployment-studio-worker.yaml @@ -41,11 +41,6 @@ spec: protocol: TCP resources: {{- toYaml .Values.studioWorker.resources | nindent 12 }} - env: - - name: "NO_MIGRATE_DB" - value: "1" - - name: "WAIT_FOR_MIGRATIONS" - value: "1" envFrom: - configMapRef: name: studio diff --git a/charts/studio/templates/job_db_migration.yaml b/charts/studio/templates/job_db_migration.yaml new file mode 100644 index 00000000..47f0b1b1 --- /dev/null +++ b/charts/studio/templates/job_db_migration.yaml @@ -0,0 +1,54 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: studio-database-migration + labels: + {{- include "studio-backend.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": pre-rollback,pre-upgrade,post-install + "helm.sh/hook-weight": "10" + "helm.sh/hook-delete-policy": hook-succeeded +spec: + template: + metadata: + annotations: + {{- with .Values.studioBackend.podAnnotations }} + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "studio-backend.selectorLabels" . | nindent 8 }} + spec: + restartPolicy: OnFailure + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "studio.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.studioBackend.podSecurityContext | nindent 8 }} + containers: + - name: studio-backend + securityContext: + {{- toYaml .Values.studioBackend.securityContext | nindent 12 }} + image: "{{ .Values.studioBackend.image.repository }}:{{ .Values.studioBackend.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.studioBackend.image.pullPolicy }} + command: [ "/usr/local/bin/python" ] + args: [ "manage.py", "migrate", "--noinput" ] + resources: + {{- toYaml .Values.studioBackend.resources | nindent 12 }} + envFrom: + - secretRef: + name: studio-migration-job + {{- with .Values.studioBackend.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.studioBackend.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.studioBackend.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} + diff --git a/charts/studio/templates/secret-migration-job.yaml b/charts/studio/templates/secret-migration-job.yaml new file mode 100644 index 00000000..fd168752 --- /dev/null +++ b/charts/studio/templates/secret-migration-job.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: Secret +metadata: + name: studio-migration-job +data: + DATABASE_URL: "psql://{{ .Values.global.postgres.databaseUser}}:{{ .Values.global.postgres.databasePassword }}@{{ .Values.global.postgres.databaseUrl }}"