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

Lab 02: Templates and Steps

Template types, steps (sequential/parallel), parameters, loops, conditionals. All manifests are submitted with:

argo submit -n argo <file>.yaml --watch
argo logs -n argo @latest

Container template

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: container-env-
spec:
  serviceAccountName: argo
  entrypoint: env-demo
  arguments:
    parameters:
    - name: environment
      value: production
    - name: app-version
      value: v1.2.3
  templates:
  - name: env-demo
    inputs:
      parameters:
      - name: environment
      - name: app-version
    container:
      image: alpine:3.20
      command: [sh, -c]
      args: ["env | grep -E '^(APP_|ENV_|WORKFLOW_)' | sort"]
      env:
      - name: APP_VERSION
        value: "{{inputs.parameters.app-version}}"
      - name: ENV_NAME
        value: "{{inputs.parameters.environment}}"
      - name: WORKFLOW_NAME
        value: "{{workflow.name}}"
      - name: WORKFLOW_NAMESPACE
        value: "{{workflow.namespace}}"
      resources:
        requests: {cpu: 100m, memory: 64Mi}
        limits: {cpu: 200m, memory: 128Mi}
argo submit -n argo container-env.yaml -p environment=staging -p app-version=v2.0.0 --watch
  • container = standard k8s container spec, most common template type
  • {{inputs.parameters.x}} and {{workflow.*}} are substituted before the pod is created
  • resources is plain k8s requests/limits, check with kubectl get pod -n argo <pod> -o jsonpath='{.spec.containers[0].resources}'

Script template

  templates:
  - name: python-script
    inputs:
      parameters:
      - name: numbers
    script:
      image: python:3.12-slim
      command: [python]
      source: |
        import statistics
        numbers = {{inputs.parameters.numbers}}
        print(sum(numbers), statistics.mean(numbers), statistics.median(numbers))
  • run with -p numbers="[10, 20, 30]"
  • script = container + inline source, which is written to a file and passed as the last arg to command
  • stdout of a script is available as outputs.result
  • other languages just change image/command (bash, node)

Output parameters

spec:
  entrypoint: main
  templates:
  - name: main
    steps:
    - - name: generate
        template: generate-value
    - - name: display
        template: display-value
        arguments:
          parameters:
          - name: value
            value: "{{steps.generate.outputs.parameters.random-number}}"

  - name: generate-value
    script:
      image: python:3.12-slim
      command: [python]
      source: |
        import random
        n = random.randint(1, 100)
        print(f"Generated: {n}")
        open('/tmp/number.txt', 'w').write(str(n))
    outputs:
      parameters:
      - name: random-number
        valueFrom:
          path: /tmp/number.txt

  - name: display-value
    inputs:
      parameters:
      - name: value
    container:
      image: alpine:3.20
      command: [echo]
      args: ["Received {{inputs.parameters.value}}"]
  • outputs.parameters[].valueFrom.path reads a file from the container
  • reference as {{steps.<step>.outputs.parameters.<name>}}
  • stdout alternative: {{steps.generate.outputs.result}}
  • JSON data works the same way, parse it in the next script with json.loads('''{{inputs.parameters.x}}''')

Steps: sequential and parallel

spec:
  entrypoint: ci
  templates:
  - name: ci
    steps:
    - - name: prepare              # runs first
        template: log
        arguments: {parameters: [{name: msg, value: prepare}]}
    - - name: build-frontend       # these two run in parallel
        template: build
        arguments: {parameters: [{name: component, value: frontend}]}
      - name: build-backend
        template: build
        arguments: {parameters: [{name: component, value: backend}]}
    - - name: deploy               # waits for both builds
        template: log
        arguments: {parameters: [{name: msg, value: deploy}]}

  - name: log
    inputs:
      parameters:
      - name: msg
    container:
      image: alpine:3.20
      command: [echo]
      args: ["{{inputs.parameters.msg}}"]

  - name: build
    inputs:
      parameters:
      - name: component
    container:
      image: alpine:3.20
      command: [sh, -c]
      args: ["echo building {{inputs.parameters.component}}; sleep 3"]
  • outer list = sequential stages, inner list = parallel steps
  • common mistake: - - a / - - b is sequential, - - a / - b is parallel
  • argo get @latest shows the tree, check the timestamps to see parallelism

Loops

    steps:
    - - name: process
        template: process-item
        arguments:
          parameters:
          - name: item
            value: "{{item}}"
        withItems:
        - {name: apple, price: 1.20}
        - {name: banana, price: 0.80}
  • one pod per item, run in parallel (limit with parallelism)
  • map items are passed as a JSON string in {{item}}
  • also withParam (JSON list from a previous output) and withSequence

Conditionals

  arguments:
    parameters:
    - name: environment
      value: production
  ...
    steps:
    - - name: deploy-prod
        template: log
        arguments: {parameters: [{name: msg, value: deploy-prod}]}
        when: "{{workflow.parameters.environment}} == production"
    - - name: deploy-staging
        template: log
        arguments: {parameters: [{name: msg, value: deploy-staging}]}
        when: "{{workflow.parameters.environment}} == staging"
argo submit -n argo cond.yaml -p environment=staging --watch
  • when is an expression, skipped steps show as Skipped
  • a step output can be used too: when: "{{steps.test.outputs.result}} == passed"

Resource template

  - name: create-configmap
    resource:
      action: create
      manifest: |
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: workflow-config
        data:
          app.version: "1.0.0"
  - name: delete-configmap
    resource:
      action: delete
      manifest: |
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: workflow-config
  • manages any k8s object from a workflow (create, patch, apply, delete, get)
  • the pod’s SA needs RBAC for that object, otherwise configmaps is forbidden:
kubectl create role wf-configmaps -n argo --verb=create,get,list,delete --resource=configmaps
kubectl create rolebinding wf-configmaps -n argo --role=wf-configmaps --serviceaccount=argo:argo

Cleanup

argo delete -n argo --all
kubectl delete role,rolebinding wf-configmaps -n argo --ignore-not-found

Next: Lab 03