apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Values.postgresCluster.jobName }}
  annotations:
    "helm.sh/hook": post-install,post-upgrade
spec:
  backoffLimit: 10
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "false"
    spec:
      serviceAccountName: jenkins
      restartPolicy: Never
      initContainers:
        - name: wait-for-postgres-cluster
          image: {{ template "imageRegistry" . -}} curlimages/curl
          command:
            - sh
            - '-c'
            - >-
              get_postgres_cluster_ststus () {
                operational_curl=$(curl -k https://operational-pods:8008/cluster 2>&1 | grep -o 'state.\{0,12\}')
                if [[ ! "$operational_curl" ]]; then
                  echo 'EMPTY'
                else
                  operational_status=$(curl -k https://operational-pods:8008/cluster 2>&1 | grep -o 'state.\{0,12\}' | cut -d'"' -f3 | grep -v 'running' | wc -l)
                fi
                echo $operational_status;

                analytical_curl=$(curl -k https://analytical-pods:8008/cluster 2>&1 | grep -o 'state.\{0,12\}')
                if [[ ! "$analytical_curl" ]]; then
                  echo -n 'EMPTY'
                else
                  analytical_status=$(curl -k https://analytical-pods:8008/cluster 2>&1 | grep -o 'state.\{0,12\}' | cut -d'"' -f3 | grep -v 'running' | wc -l)
                fi
                echo $analytical_status;

                if [[ "$operational_status" == "0" && "$analytical_status" == "0" ]]; then
                  return 0
                else
                  return 1
                fi
                }
              while ! get_postgres_cluster_ststus; do echo waiting for postgres clusters...; sleep 10; done;
              echo postgres clusters are ready!;
      containers:
      - name: {{ .Values.postgresCluster.jobName }}
        image: {{ .Values.image.name  }}:{{ .Values.image.version }}
        imagePullPolicy: IfNotPresent
        workingDir: /liquibase/platform-db
        command:
          - sh
          - '-c'
          - >-
            bash update.sh
        env:
        {{- $rolesSecret := .Values.postgresCluster.secrets.citusSecrets.rolesSecret }}
        {{- range $k,$v := .Values.postgresCluster.secrets.citusRolesSecrets }}
          {{- $keyName := (print $k "Name") }}
          - name: {{ $keyName }}
            valueFrom:
              secretKeyRef:
                name: {{ $rolesSecret }}
                key: {{ $keyName }}
        {{- end }}
        {{- range $k,$v := .Values.postgresCluster.secrets.citusRolesSecrets }}
          {{- $keyPass := (print $k "Pass") }}
          - name: {{ $keyPass }}
            valueFrom:
              secretKeyRef:
                name: {{ $rolesSecret }}
                key: {{ $keyPass }}
        {{- end }}
          - name: DB_NAME_OP
            valueFrom:
              secretKeyRef:
                name: {{ .Values.postgresCluster.operationalClusterSecret.name }}
                key: user
          - name: DB_PASS_OP
            valueFrom:
              secretKeyRef:
                name: {{ .Values.postgresCluster.operationalClusterSecret.name }}
                key: password
          - name: DB_NAME_AN
            valueFrom:
              secretKeyRef:
                name: {{ .Values.postgresCluster.analyticalClusterSecret.name }}
                key: user
          - name: DB_PASS_AN
            valueFrom:
              secretKeyRef:
                name: {{ .Values.postgresCluster.analyticalClusterSecret.name }}
                key: password
          - name: masterDBurl
            value: jdbc:postgresql://operational-primary:5432
          - name: replicaDBurl
            value: jdbc:postgresql://analytical-primary:5432
          - name: dbName
            value: {{ .Values.postgresCluster.dbName }}
          - name: archiveSchema
            value: {{ .Values.postgresCluster.archiveSchema }}
          - name: pubHost
            value: {{ .Values.postgresCluster.pubHost }}
          - name: pubUser
            value: {{ .Values.postgresCluster.pubUser }}
          - name: pubPort
            value: {{ .Values.postgresCluster.pubPort | quote }}
