Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for array task results as matrix parameters #6056

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions docs/matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,40 +166,28 @@ Consuming `Results` from previous `TaskRuns` or `Runs` in a `Matrix`, which woul
`TaskRuns` or `Runs` from the fanned out `PipelineTask`, is supported. Producing `Results` in from a
`PipelineTask` with a `Matrix` is not yet supported - see [further details](#results-from-fanned-out-pipelinetasks).

`Matrix` supports Results of type String that are passed in individually:
`Matrix` supports Results of type String that are passed in individually, and of type Array, either individual
values by index or the entire array:

```yaml
tasks:
...
- name: task-4
- name: task-5
taskRef:
name: task-4
name: task-5
matrix:
params:
- name: values
value:
- (tasks.task-1.results.foo) # string
- (tasks.task-2.results.bar) # string
- (tasks.task-3.results.rad) # string
- $(tasks.task-1.results.foo) # string
- $(tasks.task-2.results.bar) # string
- $(tasks.task-3.results.rad[0]) # array index
- name: otherValues
value: $(tasks.task-4.results.someArray[*])
```

For further information, see the example in [`PipelineRun` with `Matrix` and `Results`][pr-with-matrix-and-results].

When we support `Results` of type Array at the `Pipeline` level, we will support passing Results into the `Matrix`.
> Note: Results of type Array are not yet supported in the Pipeline level.

```yaml
tasks:
...
- name: task-5
taskRef:
name: task-5
matrix:
params:
- name: values
value: (tasks.task-4.results.foo) # array
```

#### Results from fanned out PipelineTasks

Consuming `Results` from fanned out `PipelineTasks` will not be in the supported in the initial iteration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ spec:
- name: get-browsers
taskSpec:
results:
- name: one
- name: two
- name: three
- name: browsers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will need an array type, right? Default type of a param is string.

steps:
- name: echo
image: alpine
script: |
printf chrome | tee /tekton/results/one
printf safari | tee /tekton/results/two
printf firefox | tee /tekton/results/three
#!/usr/bin/env bash
echo -n "[\"chrome\",\"safari\",\"firefox\"]" | tee $(results.browsers.path)
- name: platforms-and-browsers-dag
matrix:
params:
Expand All @@ -58,8 +55,6 @@ spec:
- $(tasks.get-platforms.results.two)
- $(tasks.get-platforms.results.three)
- name: browser
value:
- $(tasks.get-browsers.results.one)
- $(tasks.get-browsers.results.two)
value: $(tasks.get-browsers.results.browsers[*])
taskRef:
name: platform-browsers
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/param_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func validatePipelineParametersVariablesInMatrixParameters(matrix []Param, prefi
func validateParametersInTaskMatrix(matrix *Matrix) (errs *apis.FieldError) {
if matrix != nil {
for _, param := range matrix.Params {
if param.Value.Type != ParamTypeArray {
if param.Value.Type == ParamTypeObject || (param.Value.Type == ParamTypeString && !fullArrayVariableSubstitutionRegex.MatchString(param.Value.StringVal)) {
errs = errs.Also(apis.ErrInvalidValue("parameters of type array only are allowed in matrix", "").ViaFieldKey("matrix", param.Name))
}
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/apis/pipeline/v1beta1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3396,7 +3396,7 @@ func Test_validateMatrix(t *testing.T) {
}},
}},
}, {
name: "parameters in matrix are strings",
name: "parameters in matrix are strings or full array parameters",
tasks: PipelineTaskList{{
Name: "a-task",
TaskRef: &TaskRef{Name: "a-task"},
Expand All @@ -3413,10 +3413,17 @@ func Test_validateMatrix(t *testing.T) {
Params: []Param{{
Name: "baz", Value: ParamValue{Type: ParamTypeString, StringVal: "baz"},
}}},
}, {
Name: "c-task",
TaskRef: &TaskRef{Name: "c-task"},
Matrix: &Matrix{
Params: []Param{{
Name: "faz", Value: ParamValue{Type: ParamTypeString, StringVal: "$(params.apple[1])"},
}}},
}},
wantErrs: &apis.FieldError{
Message: "invalid value: parameters of type array only are allowed in matrix",
Paths: []string{"[0].matrix[foo]", "[0].matrix[bar]", "[1].matrix[baz]"},
Paths: []string{"[0].matrix[foo]", "[0].matrix[bar]", "[1].matrix[baz]", "[2].matrix[faz]"},
},
}, {
name: "parameters in matrix are arrays",
Expand Down Expand Up @@ -3446,6 +3453,13 @@ func Test_validateMatrix(t *testing.T) {
Params: []Param{{
Name: "b-param", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{"$(tasks.bar-task.results.b-result)"}},
}}},
}, {
Name: "c-task",
TaskRef: &TaskRef{Name: "c-task"},
Matrix: &Matrix{
Params: []Param{{
Name: "c-param", Value: ParamValue{Type: ParamTypeArray, ArrayVal: []string{"$(tasks.bar-task.results.c-result[*])"}},
}}},
}},
}}
for _, tt := range tests {
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/pipeline/v1beta1/resultref.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
// TODO(#2462) use one regex across all substitutions
// variableSubstitutionFormat matches format like $result.resultname, $result.resultname[int] and $result.resultname[*]
variableSubstitutionFormat = `\$\([_a-zA-Z0-9.-]+(\.[_a-zA-Z0-9.-]+)*(\[([0-9]+|\*)\])?\)`
// fullArrayVariableSubstitutionFormat matches the array replacement format $result.resultname[*]
fullArrayVariableSubstitutionFormat = `\$\([_a-zA-Z0-9.-]+(\.[_a-zA-Z0-9.-]+)*(\[\*\])\)`
// exactVariableSubstitutionFormat matches strings that only contain a single reference to result or param variables, but nothing else
// i.e. `$(result.resultname)` is a match, but `foo $(result.resultname)` is not.
exactVariableSubstitutionFormat = `^\$\([_a-zA-Z0-9.-]+(\.[_a-zA-Z0-9.-]+)*(\[([0-9]+|\*)\])?\)$`
Expand All @@ -57,6 +59,7 @@ const (

// VariableSubstitutionRegex is a regex to find all result matching substitutions
var VariableSubstitutionRegex = regexp.MustCompile(variableSubstitutionFormat)
var fullArrayVariableSubstitutionRegex = regexp.MustCompile(fullArrayVariableSubstitutionFormat)
var exactVariableSubstitutionRegex = regexp.MustCompile(exactVariableSubstitutionFormat)
var resultNameFormatRegex = regexp.MustCompile(ResultNameFormat)

Expand Down
37 changes: 17 additions & 20 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9032,20 +9032,18 @@ spec:
- name: platform-1
- name: platform-2
- name: platform-3
- name: browser-1
- name: browser-2
- name: browser-3
- name: browsers
type: array
- name: version
steps:
- name: echo
image: alpine
script: |
#!/usr/bin/env bash
printf linux | tee /tekton/results/platform-1
printf mac | tee /tekton/results/platform-2
printf windows | tee /tekton/results/platform-3
printf chrome | tee /tekton/results/browser-1
printf safari | tee /tekton/results/browser-2
printf firefox | tee /tekton/results/browser-3
echo -n "[\"chrome\",\"safari\",\"firefox\"]" | tee $(results.browsers.path)
printf v0.33.0 | tee /tekton/results/version
`)

Expand Down Expand Up @@ -9254,10 +9252,7 @@ spec:
- $(tasks.pt-with-result.results.platform-2)
- $(tasks.pt-with-result.results.platform-3)
- name: browser
value:
- $(tasks.pt-with-result.results.browser-1)
- $(tasks.pt-with-result.results.browser-2)
- $(tasks.pt-with-result.results.browser-3)
value: $(tasks.pt-with-result.results.browsers[*])
params:
- name: version
value: $(tasks.pt-with-result.results.version)
Expand Down Expand Up @@ -9285,12 +9280,8 @@ status:
value: mac
- name: platform-3
value: windows
- name: browser-1
value: chrome
- name: browser-2
value: safari
- name: browser-3
value: firefox
- name: browsers
value: chrome,safari,firefox
- name: version
value: v0.33.0
`),
Expand All @@ -9307,6 +9298,9 @@ spec:
name: p-dag
status:
pipelineSpec:
params:
- name: browsers
type: array
tasks:
- name: pt-with-result
params:
Expand All @@ -9331,10 +9325,7 @@ status:
- $(tasks.pt-with-result.results.platform-2)
- $(tasks.pt-with-result.results.platform-3)
- name: browser
value:
- $(tasks.pt-with-result.results.browser-1)
- $(tasks.pt-with-result.results.browser-2)
- $(tasks.pt-with-result.results.browser-3)
value: $(tasks.pt-with-result.results.browsers[*])
params:
- name: version
value: $(tasks.pt-with-result.results.version)
Expand Down Expand Up @@ -9563,6 +9554,12 @@ metadata:
name: pr
namespace: foo
spec:
params:
- name: browsers
value:
- chrome
- safari
- firefox
serviceAccountName: test-sa
pipelineRef:
name: %s
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/pipelinerun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func ApplyTaskResults(targets PipelineRunState, resolvedResultRefs ResolvedResul
pipelineTask := resolvedPipelineRunTask.PipelineTask.DeepCopy()
pipelineTask.Params = replaceParamValues(pipelineTask.Params, stringReplacements, arrayReplacements, objectReplacements)
if pipelineTask.IsMatrixed() {
pipelineTask.Matrix.Params = replaceParamValues(pipelineTask.Matrix.Params, stringReplacements, nil, nil)
pipelineTask.Matrix.Params = replaceParamValues(pipelineTask.Matrix.Params, stringReplacements, arrayReplacements, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to handle object replacements too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if objects actually make sense in a matrix params context - thoughts, @jerop?

Copy link
Member

@jerop jerop Jan 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't think we should handle object results - unless we're speaking of getting a particular value from the object as an item in the matrix array, but even then I'd like to leave that out for when we have use cases and explore it in detail

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, never mind, it was a silly comment.
We might support objects in future there, to make it easier for a task to trigger a specific set of combinations of parameters through results, but that would be a new feature.

}
pipelineTask.WhenExpressions = pipelineTask.WhenExpressions.ReplaceWhenExpressionsVariables(stringReplacements, arrayReplacements)
if pipelineTask.TaskRef != nil && pipelineTask.TaskRef.Params != nil {
Expand Down
32 changes: 32 additions & 0 deletions pkg/reconciler/pipelinerun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,38 @@ func TestApplyTaskResults_MinimalExpression(t *testing.T) {
}}},
},
}},
}, {
name: "Test array result substitution on minimal variable substitution expression - matrix",
lbernick marked this conversation as resolved.
Show resolved Hide resolved
resolvedResultRefs: ResolvedResultRefs{{
Value: *v1beta1.NewStructuredValues("arrayResultValueOne", "arrayResultValueTwo"),
ResultReference: v1beta1.ResultRef{
PipelineTask: "aTask",
Result: "aResult",
},
FromTaskRun: "aTaskRun",
}},
targets: PipelineRunState{{
PipelineTask: &v1beta1.PipelineTask{
Name: "bTask",
TaskRef: &v1beta1.TaskRef{Name: "bTask"},
Matrix: &v1beta1.Matrix{
Params: []v1beta1.Param{{
Name: "bParam",
Value: *v1beta1.NewStructuredValues(`$(tasks.aTask.results.aResult[*])`),
}}},
},
}},
want: PipelineRunState{{
PipelineTask: &v1beta1.PipelineTask{
Name: "bTask",
TaskRef: &v1beta1.TaskRef{Name: "bTask"},
Matrix: &v1beta1.Matrix{
Params: []v1beta1.Param{{
Name: "bParam",
Value: *v1beta1.NewStructuredValues("arrayResultValueOne", "arrayResultValueTwo"),
}}},
},
}},
}, {
name: "Test array result substitution on minimal variable substitution expression - when expressions",
resolvedResultRefs: ResolvedResultRefs{{
Expand Down