---
apiVersion: batch/v1
kind: Job
metadata:
  name: scc-instana-job
  annotations:
    argocd.argoproj.io/hook: PostSync
spec:
  template:
    spec:
      containers:
        - name: scc-policy
          image: quay.io/openshift/origin-cli:latest
          resources:
            requests:
              memory: "64Mi"
              cpu: "250m"
            limits:
              memory: "128Mi"
              cpu: "500m"
          command:
            - /bin/sh
            - -c
            - |
              set -o pipefail
              set -x

              version=`oc version |grep Server |awk '{print $3}' |tr -d ' '`
              if [ ${#version} -gt 1 ] ; then
                echo "ocp version ${version} "
              else 
                echo "this is an k8s , no need to execute following commands"
                exit 0
              fi
              while : ; do
                oc get sa default -n crossplane-system
                if [[ $? != 0 ]]; then
                  echo 'Serviceaccount "default" not found in namespace crossplane-system'
                  sleep 10s
                else 
                  break
                fi
              done
              oc adm policy add-scc-to-user anyuid -z default -n crossplane-system
              result=$?
              if [[ ${result} != 0 ]]; then 
                exit "${result}"
              fi

              cat << EOF | oc apply -f -
              apiVersion: v1
              kind: ServiceAccount
              metadata:
                name: crossplane
                namespace: crossplane-system
              EOF
              result=$?
              if [[ ${result} != 0 ]]; then 
                exit "${result}"
              fi              
              exit 0
      restartPolicy: Never
      serviceAccountName: crossplane