apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: kubectl-get-json
  labels:
    dagweave.io/catalog-item: kubectl-get-json
  annotations:
    dagweave.io/catalog-version: "1.0.0"
    dagweave.io/catalog-category: "Kubernetes"
spec:
  entrypoint: get
  templates:
    - name: get
      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. Reading a Deployment
            or a Node 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: resource
          - name: namespace
            default: "default"
          - name: selector
            default: ""
      outputs:
        parameters:
          # The resource as JSON.
          - name: json
            valueFrom:
              path: /tmp/output.json
      retryStrategy:
        limit: "2"
        retryPolicy: OnFailure
        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 get "${RESOURCE}" -n "${NAMESPACE}" -l "${SELECTOR}" -o json > /tmp/output.json
            else
              kubectl get "${RESOURCE}" -n "${NAMESPACE}" -o json > /tmp/output.json
            fi
            cat /tmp/output.json
        env:
          - name: RESOURCE
            value: "{{inputs.parameters.resource}}"
          - name: NAMESPACE
            value: "{{inputs.parameters.namespace}}"
          - name: SELECTOR
            value: "{{inputs.parameters.selector}}"
        resources:
          requests:
            cpu: 50m
            memory: 64Mi
          limits:
            memory: 256Mi
