apiVersion: batch/v1
kind: Job
metadata:
  name: tavros-schema-init
  namespace: postgresql
spec:
  template:
    spec:
      containers:
        - name: schema-init
          image: docker.io/bitnami/postgresql:11.9.0-debian-10-r73
          command:
            - "bin/bash"
            - "-c"
            - |
              until pg_isready -h $PG_HOST -p 5432 -U postgres
              do
                echo "Waiting for postgres at: $PG_HOST"
                sleep 5;
              done
              echo 'Running PSQL schema-init/script.sql'
              psql -h $PG_HOST -U postgres -f /tmp/schema-init/script.sql || { echo 'PSQL Setup failure' ; exit 1; }
          env:
            - name: PG_HOST
              value: tavros-postgresql.postgresql.svc.cluster.local
            - name: PGPASSWORD
              valueFrom:
                secretKeyRef:
                  name: tavros-pg-creds
                  key: postgresql-password
          volumeMounts:
            - name: schema-init-script
              mountPath: /tmp/schema-init
      volumes:
      - name: schema-init-script
        secret:
          secretName: tavros-schema-init-script
      restartPolicy: OnFailure
  backoffLimit: 4
