apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: kubectl-wait-condition
  labels:
    dagweave.io/catalog-item: kubectl-wait-condition
  annotations:
    dagweave.io/catalog-version: "1.0.0"
    dagweave.io/catalog-category: "Kubernetes"
spec:
  entrypoint: wait
  templates:
    - name: wait
      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. Waiting means
            reading the resource, and that account cannot read anything outside pods and
            workflows, so the step fails on a 403 rather than touching anything. The cluster
            you meant is one this run cannot reach.
      inputs:
        parameters:
          - name: resource
          - name: namespace
            default: "default"
          - name: condition
            default: "condition=Ready"
          - name: selector
            default: ""
          - name: timeout
            default: "300s"
      retryStrategy:
        limit: "2"
        retryPolicy: OnError
        backoff:
          duration: 10s
          factor: "2"
          maxDuration: 2m
      container:
        # cgr.dev/chainguard/kubectl:latest-dev
        image: cgr.dev/chainguard/kubectl@sha256:98f9c337808ecf5d5e80b79af4345762a3d8c803ebf8c857d2dddabf885107ba
        command: [sh, -c]
        args:
          - |
            set -eu
            if [ -n "${SELECTOR}" ]; then
              kubectl wait "${RESOURCE}" -n "${NAMESPACE}" -l "${SELECTOR}" --for="${CONDITION}" --timeout="${TIMEOUT}"
            else
              kubectl wait "${RESOURCE}" -n "${NAMESPACE}" --for="${CONDITION}" --timeout="${TIMEOUT}"
            fi
        env:
          - name: RESOURCE
            value: "{{inputs.parameters.resource}}"
          - name: NAMESPACE
            value: "{{inputs.parameters.namespace}}"
          - name: CONDITION
            value: "{{inputs.parameters.condition}}"
          - name: SELECTOR
            value: "{{inputs.parameters.selector}}"
          - name: TIMEOUT
            value: "{{inputs.parameters.timeout}}"
        resources:
          requests:
            cpu: 50m
            memory: 64Mi
          limits:
            memory: 256Mi
