Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

Workflow Fundamentals

  • Argo Workflows is a container-native workflow engine, implemented as CRDs. Each step runs as a container (pod)
  • apiVersion: argoproj.io/v1alpha1, kind is Workflow, WorkflowTemplate or ClusterWorkflowTemplate
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: hello-world-
  namespace: argo
spec:
  entrypoint: main
  templates:
  - name: main
    container:
      image: docker/whalesay
      command: [cowsay]
      args: ["hello world"]

Spec fields

  • entrypoint - template to start with
    • override at submit time: argo submit wf.yaml --entrypoint other
  • templates - the work
  • arguments - workflow-level parameters/artifacts
  • serviceAccountName - SA the pods run as
  • ttlStrategy - cleanup of finished workflows (secondsAfterCompletion, secondsAfterSuccess, secondsAfterFailure)
  • activeDeadlineSeconds - max run time for the whole workflow
  • podGC - pod garbage collection strategy
  • volumeClaimTemplates - PVCs created for the workflow
  • onExit - exit handler template
spec:
  entrypoint: main
  serviceAccountName: workflow-executor
  activeDeadlineSeconds: 3600
  ttlStrategy:
    secondsAfterCompletion: 86400
  arguments:
    parameters:
    - name: message
      value: "Hello Argo"
  templates:
  - name: main
    inputs:
      parameters:
      - name: message
    container:
      image: alpine:latest
      command: [echo]
      args: ["{{inputs.parameters.message}}"]

Phases

  • Workflow phases: Pending -> Running -> Succeeded / Failed / Error
    • Failed: a step failed (non-zero exit etc.)
    • Error: system/controller level problem
  • Skipped and Omitted are node (step) phases, not workflow phases
    • Omitted: when evaluated false / dependency omitted
  • Checking:
argo get <wf>
argo watch <wf>
argo list

Basic patterns

Sequential / parallel

  • steps is a list of lists
    • outer list = sequential
    • inner list = parallel
steps:
- - name: step-1        # runs first
    template: task
- - name: step-2a       # 2a and 2b run in parallel
    template: task
  - name: step-2b
    template: task

Conditional

  • when uses an expression
- - name: prod
    template: deploy-prod
    when: "{{workflow.parameters.environment}} == production"
  - name: dev
    template: deploy-dev
    when: "{{workflow.parameters.environment}} == development"
  • Can also check earlier results: when: "{{steps.step-1.status}} == Succeeded"

Loop

  • withItems (inline list), withParam (JSON list from a parameter), withSequence
  • Iterations run in parallel (limit with parallelism)
- - name: process
    template: process
    arguments:
      parameters:
      - name: item
        value: "{{item}}"
    withItems: [apple, banana, cherry]
  • For a list of objects use {{item.field}}

Retry

retryStrategy:
  limit: "3"
  retryPolicy: Always    # Always, OnFailure, OnError, OnTransientError
  backoff:
    duration: "10s"
    factor: 2
    maxDuration: "1m"

Variables

  • Simple tag: {{var}}; expression tag: {{=expr}} (v3.1+)

Global (workflow.*)

  • workflow.name, .namespace, .uid, .creationTimestamp, .serviceAccountName, .priority
  • workflow.status - current phase (Succeeded/Failed/Error), mostly useful in exit handlers
  • workflow.failures - failure details, exit handlers only
  • workflow.labels.<k>, workflow.annotations.<k>, workflow.parameters.<name>
    • without the key they return everything as a JSON string

Inputs / outputs

  • inputs.parameters.<name>, inputs.artifacts.<name>.path
  • outputs.parameters.<name>.path, outputs.artifacts.<name>.path
  • outputs.result - stdout of a container/script template

Steps / DAG

  • steps: steps.<step>.outputs.result, .outputs.parameters.<n>, .outputs.artifacts.<n>, .status, .exitCode, .startedAt, .finishedAt, .ip, .id
  • dag: same, but tasks.<task>.*
- name: deploy-app
  dependencies: [generate-config]
  template: deploy
  arguments:
    parameters:
    - name: config
      value: "{{tasks.generate-config.outputs.parameters.config}}"

Container/script only

  • pod.name
  • retries - current retry number
  • lastRetry.exitCode, .status, .duration, .message
    • these are strings, cast them: when: "{{=asInt(lastRetry.exitCode) >= 2}}"

Expression tags

  • {{=asInt(workflow.parameters.count) * 2}}
  • Type conversion: asInt(), asFloat(), toString()
  • JSON: toJson(), jsonpath()
  • expr-lang syntax for filter/map, plus Sprig functions
when: "{{=asInt(inputs.parameters.current) > asInt(inputs.parameters.threshold)}}"

Scoping

  • Steps can only use outputs of steps that already finished
  • DAG tasks can use outputs of tasks they depend on
  • Exit handler can use workflow-level vars (workflow.status, workflow.failures) but not steps.X

Gotchas

  • Hyphenated names: simple tags are fine ({{inputs.parameters.my-param}}), expression tags need brackets: {{=inputs.parameters['my-param']}}
  • No whitespace inside the brackets
  • lastRetry.* are strings, so asInt() before comparing

Pipeline example

steps:
- - name: extract
    template: extract-data
- - name: transform
    template: transform-data
    arguments:
      parameters:
      - name: raw-data
        value: "{{steps.extract.outputs.parameters.data}}"
      artifacts:
      - name: data-file
        from: "{{steps.extract.outputs.artifacts.dataset}}"
- - name: load
    template: load-data
    when: "{{=asInt(steps.transform.exitCode) == 0}}"

Remember