---
apiVersion: batch/v1
kind: Job
metadata:
  name: update-console-link
  annotations:
    argocd.argoproj.io/hook: PostSync
  namespace: openshift-gitops
spec:
  template:
    spec:
      containers:
        - name: update-console-link
          image: quay.io/bitnami/kubectl:latest
          command:
            - /bin/sh
            - -c
            - |
              set -x

              host=$(kubectl get route -n robot-shop web -o jsonpath='{.spec.host}')
              if [ -n $host ]; then
                echo "Patching web route for robot shop to enable TLS access ..."
                kubectl patch route web -n robot-shop --type merge --patch '{"spec": {"tls": {"insecureEdgeTerminationPolicy": "Allow", "termination": "edge"}}}'

                echo "Patching console link for robot shop to update the URL ..."
                kubectl patch consolelink robot-shop  --type merge --patch '{"spec": {"href": "https://'$host'"}}'
              fi
      restartPolicy: Never
      serviceAccountName: openshift-gitops-argocd-application-controller
  backoffLimit: 1
