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

              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 instana-core -n instana-core
                if [[ $? != 0 ]]; then
                  echo 'Serviceaccount "instana-core" not found in namespace instana-core'
                  sleep 10s
                else 
                  break
                fi
              done
              oc adm policy add-scc-to-user anyuid -z instana-core -n instana-core
              result=$?
              if [[ ${result} != 0 ]]; then 
                exit "${result}"
              fi

              while : ; do
                oc get sa default -n instana-core
                if [[ $? != 0 ]]; then
                  echo 'Serviceaccount "default" not found in namespace instana-core'
                  sleep 10s
                else 
                  break
                fi
              done
              oc adm policy add-scc-to-user anyuid -z default -n instana-core
              result=$?
              if [[ ${result} != 0 ]]; then 
                exit "${result}"
              fi

              while : ; do
                oc get sa default -n instana-units
                if [[ $? != 0 ]]; then
                  echo 'Serviceaccount "default" not found in namespace instana-units'
                  sleep 10s
                else 
                  break
                fi
              done
              oc adm policy add-scc-to-user anyuid -z default -n instana-units
              result=$?
              if [[ ${result} != 0 ]]; then 
                exit "${result}"
              fi

              base=`oc get ingresses.config/cluster -o jsonpath={.spec.domain}`
              echo ${base}

              while : ; do
                oc get ns instana-core
                if [[ $? != 0 ]]; then
                  echo 'Namespace "instana-core" not found'
                  sleep 10s
                else 
                  break
                fi
              done

              cat << EOF | oc apply -f -
              apiVersion: route.openshift.io/v1
              kind: Route
              metadata:
                name: acceptor-svc
                namespace: instana-core
              spec:
                host: agent.instana.${base}
                port:
                  targetPort: 8600
                tls:
                  insecureEdgeTerminationPolicy: Redirect
                  termination: passthrough
                to:
                  kind: Service
                  name: acceptor
              EOF
              result=$?
              if [[ ${result} != 0 ]]; then 
                exit "${result}"
              fi              

              cat << EOF | oc apply -f -
              apiVersion: route.openshift.io/v1
              kind: Route
              metadata:
                name: ingress-core-svc
                namespace: instana-core
              spec:
                host: instana.${base}
                port:
                  targetPort: 8443
                tls:
                  insecureEdgeTerminationPolicy: Redirect
                  termination: passthrough
                to:
                  kind: Service
                  name: ingress-core
                  weight: 100
                wildcardPolicy: None
              EOF
              result=$?
              if [[ ${result} != 0 ]]; then 
                exit "${result}"
              fi                            

              while : ; do
                oc get ns instana-units
                if [[ $? != 0 ]]; then
                  echo 'Namespace "instana-units" not found'
                  sleep 10s
                else 
                  break
                fi
              done
              cat << EOF | oc apply -f -
              apiVersion: route.openshift.io/v1
              kind: Route
              metadata:
                name: unit-ingress-svc
                namespace: instana-units
              spec:
                host: prod-instana.instana.${base}
                port:
                  targetPort: 8443
                tls:
                  insecureEdgeTerminationPolicy: Redirect
                  termination: passthrough
                to:
                  kind: Service
                  name: ingress
              EOF
              result=$?
              if [[ ${result} != 0 ]]; then 
                exit "${result}"
              fi                            

              exit "${result}"
          volumeMounts:
          - mountPath: /root/.kube
            name: kubeconfig              
      restartPolicy: Never
      serviceAccountName: crossplane
      volumes:
      - name: kubeconfig
        secret:
          defaultMode: 420
          items:
          - key: credentials
            path: config
          secretName: k8s-kubeconfig