DAG and Parallel Execution
dagtemplate: tasks with explicitdependencies- Tasks with no dependencies start immediately (in parallel)
- A task starts once all its dependencies finish successfully
- name: main-dag
dag:
tasks:
- name: task-a
template: worker
- name: task-b
template: worker
dependencies: [task-a]
- name: task-c
template: worker
dependencies: [task-a]
- name: task-d
template: worker
dependencies: [task-b, task-c]
- a runs first, b and c in parallel, d waits for both
DAG vs steps
- steps: list of lists, order implied by position, simpler for linear flows
- dag: explicit dependencies, better for complex graphs with many parallel paths, nicer visualization
- Task outputs:
{{tasks.<name>.outputs.parameters.<p>}}(steps usessteps.)
Dependencies
dependencies: [a, b]- needs all of them to succeed- If a dependency fails, dependent tasks do not run
continueOnon the task proceeds anyway:
- name: cleanup
dependencies: [test]
continueOn:
failed: true
depends- boolean expression on task results, replacesdependencies(can’t use both on one task)- results:
.Succeeded,.Failed,.Errored,.Skipped,.Completed,.Daemoned - operators:
&&,||,! - plain
depends: "task"is same astask.Succeeded
- results:
- name: deploy-prod
depends: "test.Succeeded"
template: deploy
- name: notify-failure
depends: "test.Failed"
template: send-alert
whenis still available for conditions on values, e.g.when: "{{tasks.quality-gate.outputs.parameters.passed}} == true"- it only evaluates after dependencies succeed, so it can’t be used to catch failures. Use
dependsfor that
- it only evaluates after dependencies succeed, so it can’t be used to catch failures. Use
Parallelism
- All independent tasks run at once
- Limit with
parallelism- workflow level:
spec.parallelism: 2 - template level:
parallelismon the dag/steps template
- workflow level:
spec:
entrypoint: main
parallelism: 2 # max 2 pods at once
- Controller-wide defaults can be set in
workflow-controller-configmap
Dynamic parallelism
withItems- static listwithParam- JSON list, usually from another task’s outputwithSequence- number range
- name: generate-list
template: generate-items # script printing json.dumps([...])
- name: process-items
template: processor
dependencies: [generate-list]
arguments:
parameters:
- name: item
value: "{{item}}"
withParam: "{{tasks.generate-list.outputs.result}}"
- List of objects:
{{item.id}},{{item.data}}
Fan-out / fan-in
- fan-out: one task creates work, many tasks process it in parallel
- fan-in: a task depending on all of them aggregates
- Static: list each
process-Ntask withdependencies: [prepare-work], andaggregatewithdependencies: [process-1, process-2, process-3] - Dynamic:
withParamon the processing task, then
- name: aggregate-results
template: aggregator
dependencies: [process-tasks] # waits for every expanded task
- To collect outputs of all expanded tasks use
{{tasks.process-tasks.outputs.parameters}}(JSON list of all outputs)
CI/CD style DAG
- checkout -> (build-backend, build-frontend, security-scan) in parallel -> tests -> quality-gate -> deploy -> smoke-tests -> notify
- name: quality-gate
template: check-quality
dependencies: [test-backend, test-frontend, integration-tests, security-scan]
- name: deploy-to-staging
template: deploy
dependencies: [quality-gate]
when: "{{tasks.quality-gate.outputs.parameters.passed}} == true"
Remember
-
dependencies/dependsfor ordering,whenfor conditions -
Failed dependency blocks dependents unless
continueOnordependswith.Failed/.Completed -
parallelismcaps concurrency -
withItemsstatic,withParamdynamic