---
apiVersion: v1
kind: Pod
metadata:
  name: ${K8S_NAMESPACE}-${CONTEXT}-${BRANCH_HASH}
  labels:
    app: ${K8S_NAMESPACE}-${CONTEXT}-${BRANCH_HASH}
    git/branch: ${BRANCH_NAME}
  annotations:
    git/commit: ${COMMIT}
    gitlab/job: "${JOB_ID}"
spec:
  containers:
    - image: ${IMAGE_NAME}:${IMAGE_TAG}
      name: ${K8S_NAMESPACE}-${CONTEXT}
      env:
        - name: ELASTICSEARCH_URL
          value: "${ELASTICSEARCH_URL}"
  restartPolicy: Never
  initContainers:
    - name: wait-for-elasticsearch
      image: alpine
      imagePullPolicy: Always
      command:
        - sh
        - -c
        - |
          apk --no-cache --update add curl;
          sleep 10s # delay wait for elastic to be sure elastic had been removed first
          retry=120; # 5s * (12 * 10) = 10min
          while
            ! curl -sS "${ELASTICSEARCH_URL}/_cat/health?h=status" &&
            [[ $(( retry-- )) -gt 0 ]];
          do echo "Waiting for Elasticsearch to go Green ($(( retry )))" ; sleep 5s ; done ;

          [[ $(( retry )) -lt 1 ]] && exit 128;

          echo Ready;
