apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: kubectl-run-job
  labels:
    dagweave.io/catalog-item: kubectl-run-job
  annotations:
    dagweave.io/catalog-version: "1.0.0"
    dagweave.io/catalog-category: "Kubernetes"
spec:
  entrypoint: run
  templates:
    - name: run
      metadata:
        annotations:
          dagweave.io/local-support: unsupported
          dagweave.io/local-support-reason: >-
            This step runs kubectl with whatever identity the pod is given. On Dagweave
            Local that is the default service account of the hidden cluster's argo
            namespace, which Argo's quick-start install grants create, get and patch on pods
            and create and get on workflows, and nothing anywhere else. Creating a Job is
            outside what that account may do, so the step fails on a 403 rather than
            touching anything. The cluster you meant is one this run cannot reach.
      inputs:
        parameters:
          - name: name
          - name: image
          - name: command
            default: ""
          - name: namespace
            default: "default"
          - name: service-account
            default: ""
          - name: timeout-seconds
            default: "600"
          - name: cleanup
            default: "true"
      container:
        # cgr.dev/chainguard/kubectl:latest-dev
        image: cgr.dev/chainguard/kubectl@sha256:98f9c337808ecf5d5e80b79af4345762a3d8c803ebf8c857d2dddabf885107ba
        command: [sh, -c]
        args:
          - |
            set -eu
            set -- "${NAME}" -n "${NAMESPACE}" --image="${IMAGE}" --dry-run=client -o json
            if [ -n "${COMMAND}" ]; then set -- "$@" -- sh -c "${COMMAND}"; fi
            kubectl create job "$@" \
              | kubectl patch --local -f - --type=merge -p '{"spec":{"backoffLimit":0}}' -o json > /tmp/job.json
            if [ -n "${SERVICE_ACCOUNT}" ]; then
              kubectl set serviceaccount --local -f /tmp/job.json "${SERVICE_ACCOUNT}" -o json > /tmp/job-sa.json
              cp /tmp/job-sa.json /tmp/job.json
            fi
            kubectl create -f /tmp/job.json
            status=timeout
            end=$(( $(date +%s) + TIMEOUT_SECONDS ))
            while [ "$(date +%s)" -lt "${end}" ]; do
              if [ "$(kubectl get job "${NAME}" -n "${NAMESPACE}" -o jsonpath='{.status.conditions[?(@.type=="Complete")].status}')" = "True" ]; then status=complete; break; fi
              if [ "$(kubectl get job "${NAME}" -n "${NAMESPACE}" -o jsonpath='{.status.conditions[?(@.type=="Failed")].status}')" = "True" ]; then status=failed; break; fi
              sleep 5
            done
            kubectl logs "job/${NAME}" -n "${NAMESPACE}" --all-containers || true
            if [ "${CLEANUP}" = "true" ]; then kubectl delete job "${NAME}" -n "${NAMESPACE}" --wait=false; fi
            echo "job ${NAME}: ${status}"
            [ "${status}" = "complete" ]
        env:
          - name: NAME
            value: "{{inputs.parameters.name}}"
          - name: IMAGE
            value: "{{inputs.parameters.image}}"
          - name: COMMAND
            value: "{{inputs.parameters.command}}"
          - name: NAMESPACE
            value: "{{inputs.parameters.namespace}}"
          - name: SERVICE_ACCOUNT
            value: "{{inputs.parameters.service-account}}"
          - name: TIMEOUT_SECONDS
            value: "{{inputs.parameters.timeout-seconds}}"
          - name: CLEANUP
            value: "{{inputs.parameters.cleanup}}"
        resources:
          requests:
            cpu: 50m
            memory: 64Mi
          limits:
            memory: 256Mi
