---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: sre-build-test
  namespace: openshift-build-test
spec:
  failedJobsHistoryLimit: 5
  successfulJobsHistoryLimit: 3
  concurrencyPolicy: Forbid
  schedule: "11 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                - matchExpressions:
                  - key: "node-role.kubernetes.io"
                    operator: In
                    values:
                      - infra
          tolerations:
          - operator: Equal
            key: node-role.kubernetes.io
            value: infra
            effect: NoSchedule
          serviceAccountName: sre-build-test
          restartPolicy: Never
          containers:
          - name: sre-build-test
            image: image-registry.openshift-image-registry.svc:5000/openshift/cli:latest
            imagePullPolicy: Always
            env:
              - name: POD_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.name
              - name: POD_NS
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.namespace
            command:
            - /bin/bash
            - -c
            - |
              # ensure we fail if something exits non-zero
              set -o errexit
              set -o nounset
              set -o pipefail

              # cleanup traps on exiting, so these always run
              cleanup () {
                echo "$(date): Done, deleting build NS=$NS"
                oc delete ns "${NS}"
              }
              trap "cleanup" EXIT SIGINT

              # set NS to include job name, to ease linking namespace to a specific job
              JOB_PREFIX=sre-build-test
              NS="${POD_NS}-${POD_NAME##${JOB_PREFIX}-}"
              JOB_NAME=$(echo "${POD_NAME}" | rev | cut -d- -f2- | rev)

              oc create ns "${NS}"

              # run build
              oc -n "${NS}" new-build nodejs~https://github.com/openshift/nodejs-ex --name="${JOB_PREFIX}"
              echo "$(date): Waiting for build to complete."
              while :
              do
                ST=$(oc -n "${NS}" get build -o custom-columns=STATUS:.status.phase --no-headers)
                case ${ST} in
                  "")
                    # if build status is blank, assume we are still starting the build
                    ST="Starting"
                    ;;
                  Failed)
                    echo "$(date): Build Failed" >&2
                    exit 1
                    ;;
                  Cancelled)
                    echo "$(date): Build was Cancelled" >&2
                    exit 1
                    ;;
                  Complete)
                    echo "$(date): Build Complete"
                    # Get all job names that have exceeded failed retries
                    JOBS_TO_DELETE=$(oc -n "${POD_NS}" get job -o \
                                       jsonpath='{.items[?(@.status.failed==@.spec.backoffLimit)].metadata.name}' || true)
                    if [[ -n "${JOBS_TO_DELETE}" ]] ; then
                      echo "$(date): Selected jobs for deletion: ${JOBS_TO_DELETE}"
                      echo "${JOBS_TO_DELETE}" | xargs oc -n "${POD_NS}" delete job
                    fi
                    break
                    ;;
                esac
                echo "$(date): Build is ${ST}; checking build again in 15 seconds, NS=${NS}"
                sleep 15
              done
              exit 0
