argoproj / argoproj/argo-workflows
Specify conditions for breaking out of a step loop
- Dominant language
- Go
- Stars
- 17k
- Forks
- 3.7k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 138
Description
# Summary
There does not appear to be an ability to break out of a `withItems`/`withParam` loop in a workflow step without executing all iterations. While the `when` property allows for specifying a condition to run or skip an entire step, it does not appear that it can be used for breaking out of a loop after some iterations.
# Motivation
Using the documented example below for looping over a set of container images, let's say that I want to try to execute each image with the same set of inputs, and upon **the first container that exits with an exit code of 0** (producing usable output artifacts or parameters), I want to break out of the loop and move on to the next step.
```yaml
steps:
- - name: test-linux
template: cat-os-release
arguments:
parameters:
- name: image
value: "{{item.image}}"
- name: tag
value: "{{item.tag}}"
withItems:
- { image: 'debian', tag: '9.1' }
- { image: 'debian', tag: '8.9' }
- { image: 'alpine', tag: '3.6' }
- { image: 'ubuntu', tag: '17.10' }
# proposed pre-loop and post-loop exit conditions
while: "{{item.$index}} >= 3" # 0-based index
until: "{{steps.$self.exitCode}} == 0"
```
I extended the stock example above by adding `while` and `until` properties with my desired exit conditions.
# Proposal
There are two places where a break condition can be evaluated _($keywords I invented as possible proposals)_:
1. **Start of iteration** (while-do): evaluate condition using `item.key` or `item.$index`, especially if looping over a dynamically generated sequence using `withParam`.
2. **End of iteration** (do-until): evaluate condition using `steps.$self.exitCode` or `steps.$self.outputs.parameters.foo`.
---
**Message from the maintainers**:
If you wish to see this enhancement implemented please add a 👍 reaction to this issue! We often sort issues this way to know what to prioritize.
Contributor guide
Research direction
The issue proposes loop control for workflow steps using withItems or withParam, with conditions evaluated before or after iterations. Start by locating the workflow step-loop execution and condition evaluation entry points, then define tests for while and until behavior, including exit codes and outputs; done means the supported loop semantics are implemented and covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, kubernetes, yaml
- Domain
- backend, devops, distributed-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100