forked from timescale/helm-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait_for_example_job.yaml
93 lines (83 loc) · 3.1 KB
/
wait_for_example_job.yaml
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
89
90
91
92
93
apiVersion: batch/v1
kind: Job
metadata:
name: example
spec:
activeDeadlineSeconds: 600
template:
metadata:
name: example
spec:
restartPolicy: OnFailure
containers:
- name: waiter
image: timescale/timescaledb-ha:pg13-ts2.1-latest
resources:
limits:
cpu: 100m
memory: 24Mi
requests:
cpu: 100m
memory: 24Mi
env:
- {name: TEST_REPLICA, value: "1"}
- name: PGHOST
value: example
- name: PGUSER
value: postgres
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: example-credentials
key: PATRONI_SUPERUSER_PASSWORD
command:
- /bin/sh
- '-c'
- |
log() {
echo "$(date --iso-8601=seconds) - $1"
}
terminate() {
exit 1
}
trap terminate TERM QUIT
while sleep 10; do
# This will throw an error if the primary does not yet accept writes
psql -X -f - --set ON_ERROR_STOP=1 -c "CREATE TEMPORARY TABLE a AS SELECT 1" > /dev/null 2>&1 && break
log "Waiting for deployment \"${PGHOST}\" (primary) to become available"
done
NOW="$(date --iso-8601=seconds)"
psql -Xq -f - --set ON_ERROR_STOP=1 <<__SQL__
\x on
CREATE SCHEMA IF NOT EXISTS citest;
DROP TABLE IF EXISTS citest.demo;
CREATE TABLE citest.demo(inserted timestamptz not null);
SELECT * FROM create_hypertable('citest.demo', 'inserted');
INSERT INTO citest.demo VALUES ('${NOW}') RETURNING *;
__SQL__
EXITCODE=$?
if [ "${EXITCODE}" -ne 0 ]; then exit "${EXITCODE}"; fi
if [ "${TEST_REPLICA}" -eq 1 ]; then
export PGHOST="${PGHOST}-replica"
while sleep 3; do
# This will throw a divide by zero error as long as the record has not been replicated
psql -X -f - --set ON_ERROR_STOP=1 -c "SELECT 1/count(*) FROM citest.demo WHERE inserted = '${NOW}'" > /dev/null 2>&1 && break
log "Waiting for deployment \"${PGHOST}\" (replica) to become available"
done
psql -Xq -f - --set ON_ERROR_STOP=1 <<__SQL__
\x on
SELECT now(), * FROM pg_stat_wal_receiver;
SELECT * FROM citest.demo WHERE inserted = '${NOW}';
SELECT count(*) > 0 AS replica_ok FROM citest.demo WHERE inserted = '${NOW}'
\gset
\if :replica_ok
SELECT 'Replica caught up with primary';
\else
SELECT 'Could not find the test record on the replica';
SELECT 1/0; -- this will cause the script to exit with a failure
\endif
__SQL__
EXITCODE=$?
if [ "${EXITCODE}" -ne 0 ]; then exit "${EXITCODE}"; fi
fi
log "Everything seems fine with deployment ${PGHOST}"