apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: velero-backup
  labels:
    dagweave.io/catalog-item: velero-backup
  annotations:
    dagweave.io/catalog-version: "1.0.0"
    dagweave.io/catalog-category: "Kubernetes"
spec:
  entrypoint: backup
  templates:
    - name: backup
      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 Velero
            Backup 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: namespaces
            default: ""
          - name: ttl
            default: "720h0m0s"
          - name: storage-location
            default: ""
          - name: velero-namespace
            default: "velero"
          - name: name-prefix
            default: "backup"
          - name: timeout-seconds
            default: "1800"
      outputs:
        parameters:
          # Name of the Velero Backup object that was created.
          - name: backup-name
            valueFrom:
              path: /tmp/backup-name
      container:
        # cgr.dev/chainguard/kubectl:latest-dev
        image: cgr.dev/chainguard/kubectl@sha256:98f9c337808ecf5d5e80b79af4345762a3d8c803ebf8c857d2dddabf885107ba
        command: [sh, -c]
        args:
          - |
            set -eu
            case "${INCLUDED_NAMESPACES}${TTL}${STORAGE_LOCATION}${NAME_PREFIX}" in
              *[!A-Za-z0-9,._-]*) echo "namespaces, ttl, storage-location and name-prefix take letters, digits and , . _ - only" >&2; exit 2 ;;
            esac
            backup=$(
              {
                echo "apiVersion: velero.io/v1"
                echo "kind: Backup"
                echo "metadata:"
                echo "  generateName: ${NAME_PREFIX}-"
                echo "spec:"
                echo "  ttl: ${TTL}"
                if [ -n "${STORAGE_LOCATION}" ]; then echo "  storageLocation: ${STORAGE_LOCATION}"; fi
                if [ -n "${INCLUDED_NAMESPACES}" ]; then
                  echo "  includedNamespaces:"
                  IFS=,
                  for ns in ${INCLUDED_NAMESPACES}; do echo "    - ${ns}"; done
                fi
              } | kubectl create -n "${VELERO_NAMESPACE}" -f - -o jsonpath='{.metadata.name}'
            )
            echo "${backup}" > /tmp/backup-name
            echo "created backup ${backup}"
            end=$(( $(date +%s) + TIMEOUT_SECONDS ))
            phase=""
            while [ "$(date +%s)" -lt "${end}" ]; do
              phase=$(kubectl get backup "${backup}" -n "${VELERO_NAMESPACE}" -o jsonpath='{.status.phase}')
              case "${phase}" in
                Completed|PartiallyFailed|Failed|FailedValidation) break ;;
              esac
              sleep 10
            done
            echo "backup ${backup}: ${phase:-no status yet}"
            [ "${phase}" = "Completed" ]
        env:
          - name: INCLUDED_NAMESPACES
            value: "{{inputs.parameters.namespaces}}"
          - name: TTL
            value: "{{inputs.parameters.ttl}}"
          - name: STORAGE_LOCATION
            value: "{{inputs.parameters.storage-location}}"
          - name: VELERO_NAMESPACE
            value: "{{inputs.parameters.velero-namespace}}"
          - name: NAME_PREFIX
            value: "{{inputs.parameters.name-prefix}}"
          - name: TIMEOUT_SECONDS
            value: "{{inputs.parameters.timeout-seconds}}"
        resources:
          requests:
            cpu: 50m
            memory: 64Mi
          limits:
            memory: 256Mi
