liveBook · Manning

When clause

As explained in Distributed Machine Learning Patterns

A when clause is an Argo Workflows expression that specifies a condition for executing a workflow step.

A when clause conditionally executes an Argo Workflows step based on an expression. It connects a prior step's output to the next step's execution path. section 8.4.1

Branching on a coin flip

Run flip-coin first, then select heads or tails by comparing its result: when: "{{steps.flip-coin.outputs.result}} == heads". Only the matching step runs, producing two Pods for the workflow. section 8.4.1

Listing 8.52 Coin-flip example
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
 generateName: coinflip-
spec:
 serviceAccountName: argo
 entrypoint: coinflip
 templates:
 - name: coinflip
   steps:
   - - name: flip-coin
        template: flip-coin
   - - name: heads
        template: heads
        when: "{{steps.flip-coin.outputs.result}} == heads"
     - name: tails
       template: tails
       when: "{{steps.flip-coin.outputs.result}} == tails"

 - name: flip-coin
   script:
     image: python:alpine3.6
     command: [python]
     source: |
       import random
       result = "heads" if random.randint(0,1) == 0 else "tails"
       print(result)

 - name: heads
   container:
     image: alpine:3.6
     command: [sh, -c]
     args: ["echo \"it was heads\""]

 - name: tails
   container:
     image: alpine:3.6
     command: [sh, -c]
     args: ["echo \"it was tails\""]

Inspect a complete Argo workflow showing paired conditional steps, templates, output interpolation, and the preceding script step. section 8.4.1

How Distributed Machine Learning Patterns covers it

How deep it goes
Demonstrates basic conditional syntax and output comparison, but defers broader expression semantics and complex branching.
Examples
Uses a Python coin flip to select heads or tails and verifies the selected branch through Pods and logs.
What it assumes
Assumes basic Argo workflow steps and templates while introducing output interpolation and conditional execution.
What you can do after
Configure conditional Argo workflow branches using a prior step's output.
How it connects
Connects workflow steps, templates, output parameters, scripts, and conditional branching.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Distributed Machine Learning Patterns. The section links point to the parts of the book it draws on.