-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmigrate-db.job.yaml
More file actions
88 lines (88 loc) · 3.7 KB
/
Copy pathmigrate-db.job.yaml
File metadata and controls
88 lines (88 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "runtime-api.fullname" . }}-migrate-db
labels:
{{- include "runtime-api.labels" . | nindent 4 }}
annotations:
{{- if not (or .Values.postgresql.enabled .Values.postgresql.postMigrate) }}
"helm.sh/hook": "pre-install,pre-upgrade"
{{- else }}
"helm.sh/hook": "post-install,post-upgrade"
{{- end }}
"helm.sh/hook-weight": "-5"
{{- if .Values.postgresql.isFeatureDeploy }}
"helm.sh/hook-delete-policy": "hook-succeeded"
{{- else }}
"helm.sh/hook-delete-policy": "before-hook-creation"
{{- end }}
spec:
activeDeadlineSeconds: {{ .Values.jobSettings.activeDeadlineSeconds }}
template:
metadata:
name: {{ include "runtime-api.fullname" . }}-migrate-db
labels:
{{- include "runtime-api.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.securityContext }}
securityContext:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ .Values.serviceAccount.name }}-hook
restartPolicy: OnFailure
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- if .Values.ingressBase.enabled }}
volumes:
- name: ingress-base-config
configMap:
name: {{ include "runtime-api.fullname" . }}-ingress-base
{{- end }}
{{- if or .Values.externalDatabase.enabled .Values.postgresql.postMigrate }}
initContainers:
- name: wait-for-postgres
image: "bitnamilegacy/postgresql:latest"
command: ['sh', '-c']
args:
- |
echo "Waiting for PostgreSQL at $DB_HOST to be ready..."
until PGPASSWORD=$DB_PASS psql -h $DB_HOST -p 5432 -U $DB_USER -d postgres -c '\q' > /dev/null 2>&1; do
echo "PostgreSQL is unavailable - sleeping for 2 seconds"
sleep 2
done
echo "PostgreSQL is up and running!"
{{- if .Values.externalDatabase.enabled }}
echo "Checking if the DB \"$DB_NAME\" and user \"$DB_USER\" exist..."
export PGPASSWORD=$DB_ADMIN_PASSWORD
# Create the database if it doesn't exist
psql -h $DB_HOST -p 5432 -U $DB_ADMIN_USER -d postgres -tc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" | grep -q 1 || \
psql -h $DB_HOST -p 5432 -U $DB_ADMIN_USER -d postgres -c "CREATE DATABASE $DB_NAME;"
# Create the user if it doesn't exist
psql -h $DB_HOST -p 5432 -U $DB_ADMIN_USER -d $DB_NAME -tc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'" | grep -q 1 || \
(psql -h $DB_HOST -p 5432 -U $DB_ADMIN_USER -d $DB_NAME -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS'; GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;")
{{- end }}
env:
{{- if .Values.externalDatabase.enabled }}
- name: DB_ADMIN_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.externalDatabase.existingSecret }}
key: admin_password
{{- end }}
{{- include "runtime-api.env" . | nindent 12 }}
{{- end }}
containers:
- name: migrate-db
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
command: ["alembic", "upgrade", "head"]
env:
{{- include "runtime-api.env" . | nindent 12 }}
{{- if .Values.ingressBase.enabled }}
volumeMounts:
- name: ingress-base-config
mountPath: /ingress-base
{{- end }}
resources:
{{- toYaml .Values.jobSettings.resources | nindent 12 }}