Skip to content

Commit f23f8c5

Browse files
committed
Added helm hooks for database schema upgrades.
1 parent 35ea158 commit f23f8c5

11 files changed

+253
-36
lines changed

templates/default/configmap.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ data:
1212
CLOUD_STORAGE_BUCKET: {{ .Values.diffgramSettings.CLOUD_STORAGE_BUCKET }}
1313
ML__CLOUD_STORAGE_BUCKET: {{ .Values.diffgramSettings.ML__CLOUD_STORAGE_BUCKET }}
1414
URL_BASE: {{ .Values.diffgramDomain }}
15-
PYTHONPATH: /app
15+
SERVICE_ACCOUNT_FULL_PATH: {{ .Values.diffgramSettings.SERVICE_ACCOUNT_FULL_PATH }}
16+
PYTHONPATH: "/app:/app/shared:/"

templates/default/deployment.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ spec:
3838
{{ if eq .Values.dbSettings.dbProvider "azure"}}
3939
command: ['sh', '-c', 'until pg_isready -h postgres-azure-service -p 5432; do echo waiting for database; sleep 2; done;']
4040
{{ end }}
41-
- name: run-migrations
42-
imagePullPolicy: Always
43-
image: gcr.io/diffgram-enterprise/default:{{ .Values.diffgramVersion }}
44-
envFrom:
45-
- configMapRef:
46-
name: diffgram-default-configmap
47-
- secretRef:
48-
name: diffgram-default-secrets
49-
command: ["sh","-c", "cd shared; export PYTHONPATH=/app; pip install sqlalchemy-utils==0.36.6;python /app/testing_and_other/create_database.py; alembic upgrade head"]
50-
5141
containers:
5242
- image: gcr.io/diffgram-enterprise/default:{{ .Values.diffgramVersion }}
5343
imagePullPolicy: Always
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
annotations:
5+
"helm.sh/hook": pre-install,pre-upgrade, pre-rollback
6+
"helm.sh/hook-weight": "1" # we use a smaller weight so it's created before the job
7+
"helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
8+
name: db-migrations-configmap
9+
data:
10+
USERDOMAIN: {{ .Values.diffgramSettings.USERDOMAIN }}
11+
DIFFGRAM_SYSTEM_MODE: {{ .Values.diffgramSettings.DIFFGRAM_SYSTEM_MODE }}
12+
DIFFGRAM_STATIC_STORAGE_PROVIDER: {{ .Values.diffgramSettings.DIFFGRAM_STATIC_STORAGE_PROVIDER }}
13+
DIFFGRAM_S3_BUCKET_NAME: {{ .Values.diffgramSettings.DIFFGRAM_S3_BUCKET_NAME }}
14+
ML__DIFFGRAM_S3_BUCKET_NAME: {{ .Values.diffgramSettings.ML__DIFFGRAM_S3_BUCKET_NAME }}
15+
GOOGLE_APPLICATION_CREDENTIALS: /etc/gcp/sa_credentials.json # Check the volume in deployment.yaml and service_account_secret.yaml
16+
CLOUD_STORAGE_BUCKET: {{ .Values.diffgramSettings.CLOUD_STORAGE_BUCKET }}
17+
ML__CLOUD_STORAGE_BUCKET: {{ .Values.diffgramSettings.ML__CLOUD_STORAGE_BUCKET }}
18+
URL_BASE: {{ .Values.diffgramDomain }}
19+
SERVICE_ACCOUNT_FULL_PATH: {{ .Values.diffgramSettings.SERVICE_ACCOUNT_FULL_PATH }}
20+
PYTHONPATH: /app
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: "{{ .Release.Name }}-pre-install"
5+
labels:
6+
app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
7+
app.kubernetes.io/instance: {{ .Release.Name | quote }}
8+
app.kubernetes.io/version: {{ .Chart.AppVersion }}
9+
helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
10+
annotations:
11+
# This is what defines this resource as a hook. Without this line, the
12+
# job is considered part of the release.
13+
"helm.sh/hook": pre-install
14+
"helm.sh/hook-weight": "2"
15+
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
16+
spec:
17+
template:
18+
metadata:
19+
name: "{{ .Release.Name }}"
20+
labels:
21+
app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
22+
app.kubernetes.io/instance: {{ .Release.Name | quote }}
23+
helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
24+
spec:
25+
restartPolicy: Never
26+
imagePullSecrets:
27+
- name: db-migrations-pull-secret
28+
volumes:
29+
- name: service-account-credentials-volume-hook
30+
secret:
31+
secretName: gcp-service-account-credentials-hook
32+
items:
33+
- key: sa_json
34+
path: sa_credentials.json
35+
containers:
36+
- image: gcr.io/diffgram-enterprise/default:{{ .Values.diffgramVersion }}
37+
imagePullPolicy: Always
38+
name: pre-upgrade-alembic-hook
39+
volumeMounts:
40+
- name: service-account-credentials-volume-hook
41+
mountPath: /etc/gcp
42+
readOnly: true
43+
envFrom:
44+
- configMapRef:
45+
name: db-migrations-configmap
46+
- secretRef:
47+
name: db-migrations-secret
48+
# The actual migrations command
49+
command: ["sh","-c", "cd shared; export PYTHONPATH=/app; pip install sqlalchemy-utils==0.36.6;python /app/testing_and_other/create_database.py; alembic upgrade head"]
Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: batch/v1
22
kind: Job
33
metadata:
4-
name: "{{ .Release.Name }}"
4+
name: "{{ .Release.Name }}-pre-upgrade"
55
labels:
66
app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
77
app.kubernetes.io/instance: {{ .Release.Name | quote }}
@@ -11,7 +11,7 @@ metadata:
1111
# This is what defines this resource as a hook. Without this line, the
1212
# job is considered part of the release.
1313
"helm.sh/hook": pre-upgrade
14-
"helm.sh/hook-weight": "-5"
14+
"helm.sh/hook-weight": "2"
1515
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
1616
spec:
1717
template:
@@ -26,9 +26,9 @@ spec:
2626
imagePullSecrets:
2727
- name: diffgramsecret
2828
volumes:
29-
- name: service-account-credentials-volume
29+
- name: service-account-credentials-volume-hook
3030
secret:
31-
secretName: gcp-service-account-credentials
31+
secretName: gcp-service-account-credentials-hook
3232
items:
3333
- key: sa_json
3434
path: sa_credentials.json
@@ -37,27 +37,13 @@ spec:
3737
imagePullPolicy: Always
3838
name: pre-upgrade-alembic-hook
3939
volumeMounts:
40-
- name: service-account-credentials-volume
40+
- name: service-account-credentials-volume-hook
4141
mountPath: /etc/gcp
4242
readOnly: true
4343
envFrom:
4444
- configMapRef:
45-
name: diffgram-default-configmap
45+
name: db-migrations-configmap
4646
- secretRef:
47-
name: diffgram-default-secrets
48-
initContainers:
49-
- name: check-db-ready
50-
image: postgres:9.6.5
51-
{{ if eq .Values.dbSettings.dbProvider "local"}}
52-
command: ['sh', '-c',
53-
'until pg_isready -h diffgram-postgres -p 5432;
54-
do echo waiting for database; sleep 2; done;']
55-
{{ end }}
56-
{{ if eq .Values.dbSettings.dbProvider "rds"}}
57-
command: ['sh', '-c', 'until pg_isready -h postgres-rds-service -p 5432; do echo waiting for database; sleep 2; done;']
58-
{{ end }}
59-
{{ if eq .Values.dbSettings.dbProvider "azure"}}
60-
command: ['sh', '-c', 'until pg_isready -h postgres-azure-service -p 5432; do echo waiting for database; sleep 2; done;']
61-
{{ end }}
47+
name: db-migrations-secret
6248
# The actual migrations command
63-
command: ["sh","-c", "cd shared; export PYTHONPATH=/app; pip install sqlalchemy-utils==0.36.6;python /app/testing_and_other/create_database.py; alembic upgrade head"]
49+
command: ["sh","-c", "cd shared; export PYTHONPATH=/app; pip install sqlalchemy-utils==0.36.6;python /app/testing_and_other/create_database.py;"]
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{{ if eq .Values.dbSettings.dbProvider "azure"}}
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
annotations:
6+
# This is what defines this resource as a hook. Without this line, the
7+
# job is considered part of the release.
8+
"helm.sh/hook": pre-install, pre-upgrade, pre-rollback
9+
"helm.sh/hook-weight": "0"
10+
"helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
11+
labels:
12+
app: postgres-azure-service-hook
13+
name: postgres-azure-service-hook
14+
spec:
15+
externalName: {{ .Values.dbSettings.azureSqlEndpoint }}
16+
selector:
17+
app: postgres-azure-service-hook
18+
type: ExternalName
19+
status:
20+
loadBalancer: {}
21+
{{ end }}
22+
{{ if eq .Values.dbSettings.dbProvider "rds"}}
23+
apiVersion: v1
24+
kind: Service
25+
metadata:
26+
annotations:
27+
# This is what defines this resource as a hook. Without this line, the
28+
# job is considered part of the release.
29+
"helm.sh/hook": pre-install, pre-upgrade, pre-rollback
30+
"helm.sh/hook-weight": "0"
31+
"helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
32+
labels:
33+
app: postgres-rds-service-hook
34+
name: postgres-rds-service-hook
35+
spec:
36+
externalName: {{ .Values.dbSettings.rdsEndpoint }}
37+
selector:
38+
app: ppostgres-rds-service-hook
39+
type: ExternalName
40+
status:
41+
loadBalancer: {}
42+
{{ end }}
43+
44+
{{ if eq .Values.dbSettings.dbProvider "local"}}
45+
apiVersion: "apps/v1"
46+
kind: "Deployment"
47+
metadata:
48+
annotations:
49+
# This is what defines this resource as a hook. Without this line, the
50+
# job is considered part of the release.
51+
"helm.sh/hook": pre-install, pre-upgrade, pre-rollback
52+
"helm.sh/hook-weight": "0"
53+
"helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
54+
name: "postgres-hook"
55+
namespace: "default"
56+
labels:
57+
app: "postgres-hook"
58+
spec:
59+
replicas: 1
60+
selector:
61+
matchLabels:
62+
app: "postgres"
63+
template:
64+
metadata:
65+
labels:
66+
app: "postgres"
67+
spec:
68+
containers:
69+
- name: "postgres"
70+
image: "postgres:9.6.2"
71+
env:
72+
- name: "POSTGRES_DB"
73+
value: {{ .Values.dbSettings.dbName }}
74+
- name: "POSTGRES_USER"
75+
value: {{ .Values.dbSettings.dbUser }}
76+
- name: "POSTGRES_PASSWORD"
77+
value: {{ .Values.dbSettings.dbPassword }}
78+
ports:
79+
- containerPort: 5432
80+
name: postgres
81+
volumeMounts:
82+
- name: postgres-storage
83+
mountPath: /var/lib/postgresql/db-data
84+
volumes:
85+
- name: postgres-storage
86+
persistentVolumeClaim:
87+
claimName: postgres-pv-claim
88+
{{ end }}
89+
{{ if eq .Values.dbSettings.dbProvider "local"}}
90+
apiVersion: v1
91+
kind: Service
92+
metadata:
93+
annotations:
94+
# This is what defines this resource as a hook. Without this line, the
95+
# job is considered part of the release.
96+
"helm.sh/hook": pre-install, pre-upgrade, pre-rollback
97+
"helm.sh/hook-weight": "0"
98+
"helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
99+
name: diffgram-postgres-hook
100+
spec:
101+
ports:
102+
- port: 5432
103+
selector:
104+
app: diffgram-postgres-hook
105+
{{ end }}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: gcp-service-account-credentials-hook
5+
annotations:
6+
"helm.sh/hook": pre-install,pre-upgrade, pre-rollback
7+
"helm.sh/hook-weight": "1" # we use a smaller weight so it's created before the job
8+
"helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
9+
type: Opaque
10+
data:
11+
# This is the JSON file encoded in base64. It will be mounted as a volume on the container.
12+
sa_json: {{ .Values.diffgramSecrets.SERVICE_ACCOUNT_JSON_B64 }}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: v1
2+
data:
3+
.dockerconfigjson: {{ .Values.imagePullCredentials.gcrCredentials }}
4+
kind: Secret
5+
6+
metadata:
7+
annotations:
8+
"helm.sh/hook": pre-install,pre-upgrade, pre-rollback
9+
"helm.sh/hook-weight": "1"
10+
"helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
11+
managedFields:
12+
- apiVersion: v1
13+
fieldsType: FieldsV1
14+
fieldsV1:
15+
f:data:
16+
.: {}
17+
f:.dockerconfigjson: {}
18+
f:type: {}
19+
manager: kubectl
20+
operation: Update
21+
name: db-migrations-pull-secret
22+
namespace: default
23+
type: kubernetes.io/dockerconfigjson
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: v1
2+
kind: Secret
3+
4+
metadata:
5+
annotations:
6+
"helm.sh/hook": pre-install, pre-upgrade, pre-rollback
7+
"helm.sh/hook-weight": "1"
8+
"helm.sh/hook-delete-policy": before-hook-creation, hook-succeeded
9+
name: db-migrations-secret
10+
type: Opaque
11+
stringData:
12+
STRIPE_API_KEY: {{ .Values.diffgramSecrets.STRIPE_API_KEY }}
13+
DIFFGRAM_AWS_ACCESS_KEY_SECRET: {{ .Values.diffgramSecrets.DIFFGRAM_AWS_ACCESS_KEY_SECRET }}
14+
_ANALYTICS_WRITE_KEY: {{ .Values.diffgramSecrets._ANALYTICS_WRITE_KEY }}
15+
MAILGUN_KEY: {{ .Values.diffgramSecrets.MAILGUN_KEY }}
16+
HUB_SPOT_KEY: {{ .Values.diffgramSecrets.HUB_SPOT_KEY }}
17+
SECRET_KEY: {{ .Values.diffgramSecrets.SECRET_KEY }}
18+
INTER_SERVICE_SECRET: {{ .Values.diffgramSecrets.INTER_SERVICE_SECRET }}
19+
{{ if eq .Values.dbSettings.dbProvider "local"}}
20+
DATABASE_URL: "postgresql+psycopg2://{{ .Values.dbSettings.dbUser }}:{{ .Values.dbSettings.dbPassword }}@diffgram-postgres/{{ .Values.dbSettings.dbName }}"
21+
{{ end }}
22+
{{ if eq .Values.dbSettings.dbProvider "rds"}}
23+
DATABASE_URL: "postgresql+psycopg2://{{ .Values.dbSettings.dbUser }}:{{ .Values.dbSettings.dbPassword }}@{{ .Values.dbSettings.rdsEndpoint }}/{{ .Values.dbSettings.dbName }}"
24+
{{ end }}
25+
{{ if eq .Values.dbSettings.dbProvider "azure"}}
26+
DATABASE_URL: "postgresql+psycopg2://{{ .Values.dbSettings.dbUser }}:{{ .Values.dbSettings.dbPassword }}@{{ .Values.dbSettings.azureSqlEndpoint }}/{{ .Values.dbSettings.dbName }}"
27+
{{ end }}
28+
USER_PASSWORDS_SECRET: {{ .Values.diffgramSecrets.USER_PASSWORDS_SECRET }}

templates/walrus/configmap.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ data:
1212
CLOUD_STORAGE_BUCKET: {{ .Values.diffgramSettings.CLOUD_STORAGE_BUCKET }}
1313
ML__CLOUD_STORAGE_BUCKET: {{ .Values.diffgramSettings.ML__CLOUD_STORAGE_BUCKET }}
1414
PYTHONPATH: /app
15-
URL_BASE: {{ .Values.diffgramDomain }}
15+
URL_BASE: {{ .Values.diffgramDomain }}
16+
SERVICE_ACCOUNT_FULL_PATH: {{ .Values.diffgramSettings.SERVICE_ACCOUNT_FULL_PATH }}

0 commit comments

Comments
 (0)