apiVersion: batch/v1
kind: Job
metadata:
  annotations:
    argocd.argoproj.io/sync-wave: "260"
  name: create-cluster-init-bundle
spec:
  template:
    spec:
      containers:
        - image: image-registry.openshift-image-registry.svc:5000/openshift/cli:latest
          env:
            - name: PASSWORD
              valueFrom:
                secretKeyRef:
                  name: central-htpasswd
                  key: password
          command:
            - /bin/bash
            - -c
            - |
              #!/usr/bin/env bash
              if kubectl get secret/sensor-tls &> /dev/null; then
                echo "cluster-init bundle has already been configured, doing nothing"
                exit 0
              else
                # Wait for central to be ready
                attempt_counter=0
                max_attempts=20
                echo "Waiting for central to be available..."
                until $(curl -k --output /dev/null --silent --head --fail https://central); do
                    if [ ${attempt_counter} -eq ${max_attempts} ];then
                      echo "Max attempts reached"
                      exit 1
                    fi
                    printf '.'
                    attempt_counter=$(($attempt_counter+1))
                    echo "Made attempt $attempt_counter, waiting..."
                    sleep 5
                done
                echo "Configuring cluster-init bundle"
                export DATA={\"name\":\"local-cluster\"}
                curl -k -o /tmp/bundle.json -X POST -u "admin:$PASSWORD" -H "Content-Type: application/json" --data $DATA https://central/v1/cluster-init/init-bundles
                echo "Bundle received"
                cat /tmp/bundle.json
                echo "Applying bundle"
                # No jq in container, python to the rescue
                cat /tmp/bundle.json | python3 -c "import sys, json; print(json.load(sys.stdin)['kubectlBundle'])" | base64 -d | oc apply -f -
                # Touch SecuredCluster to force operator to reconcile
                oc label SecuredCluster local-cluster cluster-init-job-status=created
                echo "ACS Cluster init bundle generated and applied"
              fi
          imagePullPolicy: Always
          name: create-cluster-init-bundle
      dnsPolicy: ClusterFirst
      restartPolicy: Never
      serviceAccount: create-cluster-init
      serviceAccountName: create-cluster-init
      terminationGracePeriodSeconds: 30