Skip to content

Commit

Permalink
feat(controller): skip resolve artifact when when evaluates to fals o…
Browse files Browse the repository at this point in the history
…ne on withsequence (#7950)

Signed-off-by: Tianchu Zhao <evantczhao@gmail.com>
  • Loading branch information
tczhao committed Feb 22, 2022
1 parent 79fc4a9 commit 412ff1c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion workflow/controller/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ func expandTask(task wfv1.DAGTask) ([]wfv1.DAGTask, error) {
expandedTasks := make([]wfv1.DAGTask, 0)
for i, item := range items {
var newTask wfv1.DAGTask
newTaskName, err := processItem(tmpl, task.Name, i, item, &newTask)
newTaskName, err := processItem(tmpl, task.Name, i, item, &newTask, task.When)
if err != nil {
return nil, err
}
Expand Down
10 changes: 10 additions & 0 deletions workflow/controller/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ spec:
artifacts:
- name: message
from: "{{tasks.generate-artifact.outputs.artifacts.hello-art}}"
- name: sequence-param
template: print-message
dependencies: [generate-artifact]
when: "false"
arguments:
artifacts:
- name: message
from: "{{tasks.generate-artifact.outputs.artifacts.hello-art}}"
withSequence:
count: "5"
- name: whalesay
container:
Expand Down
13 changes: 11 additions & 2 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2994,7 +2994,7 @@ func parseStringToDuration(durationString string) (time.Duration, error) {
return suspendDuration, nil
}

func processItem(tmpl template.Template, name string, index int, item wfv1.Item, obj interface{}) (string, error) {
func processItem(tmpl template.Template, name string, index int, item wfv1.Item, obj interface{}, whenCondition string) (string, error) {
replaceMap := make(map[string]string)
var newName string

Expand Down Expand Up @@ -3035,7 +3035,16 @@ func processItem(tmpl template.Template, name string, index int, item wfv1.Item,
default:
return "", errors.Errorf(errors.CodeBadRequest, "withItems[%d] expected string, number, list, or map. received: %v", index, item)
}
newStepStr, err := tmpl.Replace(replaceMap, false)
var newStepStr string
// If when is not parameterised and evaluated to false, we are not executing nor resolving artifact,
// we allow parameter substitution to be Unresolved
// The parameterised when will get handle by the task-expansion
proceed, err := shouldExecute(whenCondition)
if err == nil && !proceed {
newStepStr, err = tmpl.Replace(replaceMap, true)
} else {
newStepStr, err = tmpl.Replace(replaceMap, false)
}
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion workflow/controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5623,7 +5623,7 @@ func Test_processItem(t *testing.T) {

var newTask wfv1.DAGTask
tmpl, _ := template.NewTemplate(string(taskBytes))
newTaskName, err := processItem(tmpl, "task-name", 0, items[0], &newTask)
newTaskName, err := processItem(tmpl, "task-name", 0, items[0], &newTask, "")
if assert.NoError(t, err) {
assert.Equal(t, `task-name(0:json:{"list":[0,"1"],"number":2,"string":"foo"},list:[0,"1"],number:2,string:foo)`, newTaskName)
}
Expand Down
2 changes: 1 addition & 1 deletion workflow/controller/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (woc *wfOperationCtx) expandStep(step wfv1.WorkflowStep) ([]wfv1.WorkflowSt

for i, item := range items {
var newStep wfv1.WorkflowStep
newStepName, err := processItem(t, step.Name, i, item, &newStep)
newStepName, err := processItem(t, step.Name, i, item, &newStep, step.When)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 412ff1c

Please sign in to comment.