Skip to content

Commit 5764761

Browse files
Refactor pgstacbootstrap job and ConfigMaps to use Helm hooks for execution order
1 parent f9ca4d4 commit 5764761

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

helm-chart/eoapi/templates/pgstacboostrap/configmap.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
{{- if .Values.pgstacBootstrap.enabled }}
22
---
3+
# These ConfigMaps provide the necessary data and scripts for the pgstacbootstrap job.
4+
# They use Helm hooks with a weight of "-6" (lower than the job's "-5") to ensure
5+
# they are created before the job that depends on them.
36
apiVersion: v1
47
kind: ConfigMap
58
metadata:
69
name: initdb-sql-config-{{ $.Release.Name }}
10+
annotations:
11+
helm.sh/hook: "post-install,post-upgrade"
12+
helm.sh/hook-weight: "-6"
13+
helm.sh/hook-delete-policy: "before-hook-creation,hook-succeeded"
714
data:
815
initdb.sql: |
916
{{- range $path, $bytes := $.Files.Glob "initdb-data/*.sql" -}}
@@ -14,6 +21,10 @@ apiVersion: v1
1421
kind: ConfigMap
1522
metadata:
1623
name: initdb-json-config-{{ $.Release.Name }}
24+
annotations:
25+
helm.sh/hook: "post-install,post-upgrade"
26+
helm.sh/hook-weight: "-6"
27+
helm.sh/hook-delete-policy: "before-hook-creation,hook-succeeded"
1728
data:
1829
{{- range $path, $bytes := $.Files.Glob "initdb-data/*.json" -}}
1930
{{- base $path | nindent 2 -}}: | {{- $.Files.Get $path | nindent 4 -}}
@@ -23,6 +34,10 @@ apiVersion: v1
2334
kind: ConfigMap
2435
metadata:
2536
name: pgstac-setup-config-{{ $.Release.Name }}
37+
annotations:
38+
helm.sh/hook: "post-install,post-upgrade"
39+
helm.sh/hook-weight: "-6"
40+
helm.sh/hook-delete-policy: "before-hook-creation,hook-succeeded"
2641
data:
2742
pgstac-migrate.py: |
2843
{{- range $path, $bytes := $.Files.Glob "initdb-data/*.py" -}}
@@ -33,6 +48,10 @@ apiVersion: v1
3348
kind: ConfigMap
3449
metadata:
3550
name: initdb-sh-config-{{ $.Release.Name }}
51+
annotations:
52+
helm.sh/hook: "post-install,post-upgrade"
53+
helm.sh/hook-weight: "-6"
54+
helm.sh/hook-delete-policy: "before-hook-creation,hook-succeeded"
3655
data:
3756
run-forever.sh: |
3857
#!/bin/bash

helm-chart/eoapi/templates/pgstacboostrap/job.yaml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
{{- if .Values.pgstacBootstrap.enabled }}
22
---
3+
# This job initializes the PostgreSQL database with the necessary schema and data.
4+
# Instead of using an initContainer in the deployment to wait for this job to complete,
5+
# we use Helm hooks to control the execution order:
6+
#
7+
# 1. The "post-install,post-upgrade" hook ensures this job runs after the postgres
8+
# dependency is installed but before other resources (like deployments)
9+
# 2. The "-5" hook-weight ensures this job runs after its ConfigMaps (weight -6)
10+
# 3. The "before-hook-creation,hook-succeeded" delete policy ensures the job is recreated
11+
# on each helm install/upgrade and is cleaned up after successful completion
12+
#
313
apiVersion: batch/v1
414
kind: Job
515
metadata:
616
name: pgstacbootstrap
717
labels:
818
app: pgstacbootstrap
19+
annotations:
20+
helm.sh/hook: "post-install,post-upgrade"
21+
helm.sh/hook-weight: "-5"
22+
helm.sh/hook-delete-policy: "before-hook-creation,hook-succeeded"
923
spec:
1024
template:
1125
metadata:
1226
labels:
1327
app: pgstacbootstrap
1428
spec:
1529
restartPolicy: Never
16-
initContainers:
17-
- name: wait-for-db
18-
image: busybox
19-
command:
20-
{{ if .Values.testing }}
21-
['sh', '-c', 'until nc -z {{ $.Release.Name }}-pgbouncer 5432; do echo waiting for db; sleep 10; done;']
22-
{{ else }}
23-
['sh', '-c', 'until nc -z eoapi-pgbouncer 5432; do echo waiting for db; sleep 10; done;']
24-
{{ end }}
2530
containers:
2631
- name: pgstacbootstrap
2732
image: {{ .Values.pgstacBootstrap.image.name }}:{{ .Values.pgstacBootstrap.image.tag }}

helm-chart/eoapi/templates/services/deployment.yaml

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ metadata:
99
app: {{ $serviceName }}-{{ $.Release.Name }}
1010
gitsha: {{ $.Values.gitSha }}
1111
name: {{ $serviceName }}-{{ $.Release.Name }}
12-
{{- with index $v "annotations" }}
1312
annotations:
13+
# These hooks ensure the deployment runs after the pgstacbootstrap job (which has weight -5)
14+
# No initContainer is needed anymore since Helm will handle the execution order
15+
# helm.sh/hook: "post-install,post-upgrade"
16+
# helm.sh/hook-weight: "10"
17+
{{- with index $v "annotations" }}
1418
{{- toYaml . | nindent 4 }}
15-
{{- end }}
19+
{{- end }}
1620
spec:
1721
progressDeadlineSeconds: 600
1822
revisionHistoryLimit: 5
@@ -30,19 +34,6 @@ spec:
3034
app: {{ $serviceName }}-{{ $.Release.Name }}
3135
spec:
3236
serviceAccountName: eoapi-sa-{{ $.Release.Name }}
33-
initContainers:
34-
- name: wait-for-pgstacbootstrap
35-
image: bitnami/kubectl:latest
36-
command:
37-
- /bin/sh
38-
- -c
39-
- |
40-
echo "Waiting for pgstacbootstrap job to complete..."
41-
while ! kubectl -n {{ $.Release.Namespace }} wait --for=condition=complete job/pgstacbootstrap --timeout=5s; do
42-
echo "pgstacbootstrap job not completed yet. Checking again in 10 seconds..."
43-
sleep 10
44-
done
45-
echo "pgstacbootstrap job completed successfully"
4637
containers:
4738
- image: {{ index $v "image" "name" }}:{{ index $v "image" "tag" }}
4839
name: {{ $serviceName }}

0 commit comments

Comments
 (0)