comms: ensure mas db via postgres exec
This commit is contained in:
parent
72d4766d68
commit
0250de8636
@ -2,7 +2,7 @@
|
|||||||
apiVersion: batch/v1
|
apiVersion: batch/v1
|
||||||
kind: Job
|
kind: Job
|
||||||
metadata:
|
metadata:
|
||||||
name: mas-db-ensure-6
|
name: mas-db-ensure-7
|
||||||
namespace: comms
|
namespace: comms
|
||||||
spec:
|
spec:
|
||||||
backoffLimit: 0
|
backoffLimit: 0
|
||||||
@ -11,59 +11,28 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
serviceAccountName: mas-db-ensure
|
serviceAccountName: mas-db-ensure
|
||||||
restartPolicy: OnFailure
|
restartPolicy: OnFailure
|
||||||
volumes:
|
containers:
|
||||||
- name: work
|
- name: ensure
|
||||||
emptyDir: {}
|
|
||||||
initContainers:
|
|
||||||
- name: prepare
|
|
||||||
image: bitnami/kubectl:latest
|
image: bitnami/kubectl:latest
|
||||||
command: ["/bin/sh", "-c"]
|
command: ["/bin/sh", "-c"]
|
||||||
args:
|
args:
|
||||||
- |
|
- |
|
||||||
set -eu
|
set -eu
|
||||||
umask 077
|
umask 077
|
||||||
echo "ensuring postgres auth secret"
|
|
||||||
if kubectl -n postgres get secret postgres-auth >/dev/null 2>&1; then
|
|
||||||
kubectl -n postgres get secret postgres-auth -o jsonpath='{.data.POSTGRES_PASSWORD}' | base64 -d > /work/postgres_password
|
|
||||||
else
|
|
||||||
POSTGRES_PASS="$(kubectl -n postgres get statefulset postgres -o jsonpath='{.spec.template.spec.containers[0].env[?(@.name=="POSTGRES_PASSWORD")].value}')"
|
|
||||||
if [ -z "${POSTGRES_PASS}" ]; then
|
|
||||||
echo "postgres password not found in statefulset env" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
printf '%s' "${POSTGRES_PASS}" > /work/postgres_password
|
|
||||||
kubectl -n postgres create secret generic postgres-auth --from-file=POSTGRES_PASSWORD=/work/postgres_password >/dev/null
|
|
||||||
fi
|
|
||||||
echo "ensuring mas db secret"
|
|
||||||
if kubectl -n comms get secret mas-db >/dev/null 2>&1; then
|
if kubectl -n comms get secret mas-db >/dev/null 2>&1; then
|
||||||
kubectl -n comms get secret mas-db -o jsonpath='{.data.password}' | base64 -d > /work/mas_password
|
MAS_PASS="$(kubectl -n comms get secret mas-db -o jsonpath='{.data.password}' | base64 -d)"
|
||||||
else
|
else
|
||||||
head -c 32 /dev/urandom | base64 | tr -d '\n' > /work/mas_password
|
MAS_PASS="$(head -c 32 /dev/urandom | base64 | tr -d '\n')"
|
||||||
kubectl -n comms create secret generic mas-db --from-file=password=/work/mas_password >/dev/null
|
kubectl -n comms create secret generic mas-db --from-literal=password="${MAS_PASS}" >/dev/null
|
||||||
fi
|
fi
|
||||||
volumeMounts:
|
|
||||||
- name: work
|
POD_NAME="postgres-0"
|
||||||
mountPath: /work
|
if ! kubectl -n postgres get pod "${POD_NAME}" >/dev/null 2>&1; then
|
||||||
containers:
|
echo "postgres pod ${POD_NAME} not found" >&2
|
||||||
- name: ensure
|
exit 1
|
||||||
image: postgres:15
|
fi
|
||||||
env:
|
|
||||||
- name: PGHOST
|
kubectl -n postgres exec -i "${POD_NAME}" -- psql -U postgres -d postgres -v ON_ERROR_STOP=1 -v mas_pass="${MAS_PASS}" <<'SQL'
|
||||||
value: postgres-service.postgres.svc.cluster.local
|
|
||||||
- name: PGPORT
|
|
||||||
value: "5432"
|
|
||||||
- name: PGDATABASE
|
|
||||||
value: postgres
|
|
||||||
- name: PGUSER
|
|
||||||
value: postgres
|
|
||||||
command: ["/bin/sh", "-c"]
|
|
||||||
args:
|
|
||||||
- |
|
|
||||||
set -eu
|
|
||||||
export PGPASSWORD="$(cat /work/postgres_password)"
|
|
||||||
MAS_PASS="$(cat /work/mas_password)"
|
|
||||||
echo "ensuring mas role/database"
|
|
||||||
psql -v ON_ERROR_STOP=1 -v mas_pass="${MAS_PASS}" <<'SQL'
|
|
||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'mas') THEN
|
IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_roles WHERE rolname = 'mas') THEN
|
||||||
@ -81,6 +50,3 @@ spec:
|
|||||||
END
|
END
|
||||||
$$;
|
$$;
|
||||||
SQL
|
SQL
|
||||||
volumeMounts:
|
|
||||||
- name: work
|
|
||||||
mountPath: /work
|
|
||||||
|
|||||||
@ -12,12 +12,16 @@ metadata:
|
|||||||
rules:
|
rules:
|
||||||
- apiGroups: [""]
|
- apiGroups: [""]
|
||||||
resources: ["secrets"]
|
resources: ["secrets"]
|
||||||
resourceNames: ["postgres-auth", "mas-db"]
|
resourceNames: ["mas-db"]
|
||||||
verbs: ["get", "create", "patch", "update"]
|
verbs: ["get", "create", "patch", "update"]
|
||||||
- apiGroups: ["apps"]
|
- apiGroups: [""]
|
||||||
resources: ["statefulsets"]
|
resources: ["pods"]
|
||||||
resourceNames: ["postgres"]
|
resourceNames: ["postgres-0"]
|
||||||
verbs: ["get"]
|
verbs: ["get"]
|
||||||
|
- apiGroups: [""]
|
||||||
|
resources: ["pods/exec"]
|
||||||
|
resourceNames: ["postgres-0"]
|
||||||
|
verbs: ["create"]
|
||||||
---
|
---
|
||||||
apiVersion: rbac.authorization.k8s.io/v1
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
kind: ClusterRoleBinding
|
kind: ClusterRoleBinding
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user