Extracting resource in Argo workflow using "resource" template/step with "get" action and passing to downstream steps?

11/23/2020

I'm exploring an easy way to read K8S resources in the Argo workflow. The current documentation is focusing mainly on create/patch with conditions (https://argoproj.github.io/argo/examples/#kubernetes-resources), while I'm curious if it's possible to perform "action: get", extra part of the resource state (or full resource) and pass it downstream as artifact or result output. Any ideas?

-- Oleksandr
argo-workflows
kubernetes

1 Answer

11/23/2020

action: get is not a feature available from Argo.

However, it's easy to use kubectl from within a Pod and then send the JSON output to an output parameter. This uses a BASH script to send the JSON to the result output parameter, but an explicit output parameter or an output artifact are also viable options.

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: kubectl-bash-
spec:
  entrypoint: kubectl-example
  templates:
  - name: kubectl-example
    steps:
    - - name: generate
        template: get-workflows
    - - name: print
        template: print-message
        arguments:
          parameters:
          - name: message
            value: "{{steps.generate.outputs.result}}"
  - name: get-workflows
    script:
      image: bitnami/kubectl:latest
      command: [bash]
      source: |
        some_workflow=$(kubectl get workflows -n argo | sed -n 2p | awk '{print $1;}')
        kubectl get workflow "$some_workflow" -ojson
  - name: print-message
    inputs:
      parameters:
      - name: message
    container:
      image: alpine:latest
      command: [sh, -c]
      args: ["echo result was: '{{inputs.parameters.message}}'"]

Keep in mind that kubectl will run with the permissions of the Workflow's ServiceAccount. Be sure to submit the Workflow using a ServiceAccount which has access to the resource you want to get.

-- crenshaw-dev
Source: StackOverflow