Skip to content

Commit 360149e

Browse files
committed
Added initContainers to wait for db and it's bootstrap.
1 parent 649a06a commit 360149e

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

helm-chart/eoapi/initdb-data/pgstac-setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#! /usr/bin/env python3
2+
3+
# This script is used to setup the pgstac database.
4+
# It is run as a job in the pgstacbootstrap pod.
5+
# It is important that this script and all of its steps are idempotent.
6+
17
import os
28
import psycopg
39
from psycopg import sql

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ spec:
1313
app: pgstacbootstrap
1414
spec:
1515
restartPolicy: Never
16+
initContainers:
17+
- name: wait-for-db
18+
image: busybox
19+
command:
20+
['sh', '-c', 'until nc -z eoapi-pgbouncer 5432; do echo waiting for db; sleep 10; done;']
1621
containers:
1722
- name: pgstacbootstrap
1823
image: {{ .Values.pgstacBootstrap.image.name }}:{{ .Values.pgstacBootstrap.image.tag }}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ spec:
2929
labels:
3030
app: {{ $serviceName }}-{{ $.Release.Name }}
3131
spec:
32+
serviceAccountName: {{ $serviceName }}-sa-{{ $.Release.Name }}
33+
{{- if eq $serviceName "stac" }}
34+
initContainers:
35+
- name: wait-for-pgstacbootstrap
36+
image: bitnami/kubectl:latest
37+
command:
38+
- /bin/sh
39+
- -c
40+
- |
41+
echo "Waiting for pgstacbootstrap job to complete..."
42+
while ! kubectl -n {{ $.Release.Namespace }} wait --for=condition=complete job/pgstacbootstrap --timeout=5s; do
43+
echo "pgstacbootstrap job not completed yet. Checking again in 10 seconds..."
44+
sleep 10
45+
done
46+
echo "pgstacbootstrap job completed successfully"
47+
{{- end }}
3248
containers:
3349
- image: {{ index $v "image" "name" }}:{{ index $v "image" "tag" }}
3450
name: {{ $serviceName }}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{{- range $serviceName, $v := .Values -}}
2+
{{- if has $serviceName $.Values.apiServices }}
3+
{{- if index $v "enabled" }}
4+
---
5+
apiVersion: v1
6+
kind: ServiceAccount
7+
metadata:
8+
name: {{ $serviceName }}-sa-{{ $.Release.Name }}
9+
labels:
10+
app: {{ $serviceName }}-{{ $.Release.Name }}
11+
---
12+
apiVersion: rbac.authorization.k8s.io/v1
13+
kind: Role
14+
metadata:
15+
name: {{ $serviceName }}-role-{{ $.Release.Name }}
16+
labels:
17+
app: {{ $serviceName }}-{{ $.Release.Name }}
18+
rules:
19+
- apiGroups: ["batch"]
20+
resources: ["jobs"]
21+
verbs: ["get", "list", "watch"]
22+
---
23+
apiVersion: rbac.authorization.k8s.io/v1
24+
kind: RoleBinding
25+
metadata:
26+
name: {{ $serviceName }}-rolebinding-{{ $.Release.Name }}
27+
labels:
28+
app: {{ $serviceName }}-{{ $.Release.Name }}
29+
subjects:
30+
- kind: ServiceAccount
31+
name: {{ $serviceName }}-sa-{{ $.Release.Name }}
32+
namespace: {{ $.Release.Namespace }}
33+
roleRef:
34+
kind: Role
35+
name: {{ $serviceName }}-role-{{ $.Release.Name }}
36+
apiGroup: rbac.authorization.k8s.io
37+
{{- end }}
38+
{{- end }}
39+
{{- end }}

0 commit comments

Comments
 (0)