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

Templates and Steps

  • Templates are the building blocks of a workflow, defined under spec.templates
  • Work templates: container, script, resource, suspend (also containerSet, http, plugin)
  • Invocators: steps, dag - call other templates and control order

Template types

container

  • Most common, runs an image
- name: whalesay
  container:
    image: docker/whalesay:latest
    command: [cowsay]
    args: ["hello workflow"]
    resources:
      requests: {memory: 64Mi, cpu: 250m}
      limits: {memory: 128Mi, cpu: 500m}
    env:
    - name: WORKFLOW_NAME
      value: "{{workflow.name}}"
  • Resources and env are set per template, and env supports interpolation

script

  • Like container but with inline source; the script is saved to a file and passed to command
  • stdout is captured as outputs.result
- name: gen
  script:
    image: python:3.9
    command: [python]
    source: |
      import random
      print(random.randint(1, 100))
  • Parameters get substituted into source before it runs: data = {{inputs.parameters.input-data}}

resource

  • Create / patch / apply / delete / replace k8s resources
  • Needs RBAC for the workflow SA on those resources
  • successCondition / failureCondition make the step wait on the resource status
- name: create-job
  resource:
    action: create
    successCondition: status.succeeded > 0
    failureCondition: status.failed > 3
    manifest: |
      apiVersion: batch/v1
      kind: Job
      metadata:
        generateName: workflow-job-
      spec:
        template:
          spec:
            containers:
            - name: worker
              image: alpine:latest
              command: [echo, done]
            restartPolicy: Never
  • action: delete just needs a manifest identifying the resource

suspend

  • Pauses until resumed (argo resume <wf>) or duration expires
- name: approval-gate
  suspend: {}
- name: wait
  suspend:
    duration: "1h"

dag

Steps

  • List of lists
    • each outer item runs sequentially
    • steps inside the same inner list run in parallel
steps:
- - name: initialize
    template: init-task
- - name: parallel-1          # these two run together
    template: worker-task
    arguments:
      parameters: [{name: id, value: "1"}]
  - name: parallel-2
    template: worker-task
    arguments:
      parameters: [{name: id, value: "2"}]
- - name: finalize
    template: final-task

Invoking templates

  • template: <name> plus arguments
  • Parameters and artifacts can be passed
- - name: create-artifact
    template: create-file
- - name: use-artifact
    template: read-file
    arguments:
      artifacts:
      - name: input-file
        from: "{{steps.create-artifact.outputs.artifacts.result}}"
  • Loops: withItems / withParam with {{item}}
  • Templates can also be referenced from a WorkflowTemplate with templateRef

Inputs and outputs

Parameters

  • Input parameter can have default (used if not passed) or value
inputs:
  parameters:
  - name: name
    default: "World"
  • Output parameters read from a file (or outputs.result)
outputs:
  parameters:
  - name: result
    valueFrom:
      path: /tmp/output.txt
  • Consumed with {{steps.generate.outputs.parameters.result}}

Artifacts

  • Input artifact can come from git/http/s3 etc, mounted at path
inputs:
  artifacts:
  - name: source-code
    path: /src
    git:
      repo: https://github.com/example/repo.git
      revision: "main"
  • Output artifacts are files/dirs uploaded to the artifact repo
    • archived as tgz by default, archive: none: {} disables it
outputs:
  artifacts:
  - name: build-output
    path: /tmp/output
    archive:
      none: {}

Retry

  • retryStrategy on a template
retryStrategy:
  limit: 5
  retryPolicy: OnFailure
  backoff:
    duration: "1s"
    factor: 2
    maxDuration: "1m"
  • Backoff: 1s, 2s, 4s, 8s, … capped at maxDuration
  • retryPolicy
    • Always - any failure or error
    • OnFailure - main container exits non-zero (default)
    • OnError - system errors (pod deleted/evicted, etc), not user code failures
    • OnTransientError - only transient errors (e.g. network/timeouts)
  • expression decides if retry happens, has access to lastRetry.*
    • if retryPolicy is also set, both must allow the retry
retryStrategy:
  limit: 3
  expression: "asInt(lastRetry.exitCode) != 2"   # don't retry exit code 2
  • Use exit codes to separate transient (1) from permanent (2) failures

Timeouts

  • Template level: timeout: "5m", applies per attempt
  • Workflow level: spec.activeDeadlineSeconds
  • Combine: timeout per attempt + retry with backoff
- name: resilient
  timeout: "2m"
  retryStrategy:
    limit: 3
    retryPolicy: OnError
    backoff:
      duration: "10s"
      factor: 2
      maxDuration: "1m"
  • Always use backoff for external calls
  • Workflow deadline should cover all steps plus a buffer

Remember

  • Same inner list = parallel, different = sequential

  • {{inputs.parameters.name}} for inputs, {{steps.<step>.outputs.parameters.<name>}} for outputs

  • Parameters are values, artifacts are files

  • Lab 02: Templates and Steps

  • Walk-through