Skip to content

Commit

Permalink
TEP-0090 Matrix: Refactor Matrix FanOut() to use the Matrix Struct
Browse files Browse the repository at this point in the history
[TEP-0090: Matrix] introduced `Matrix` to the `PipelineTask` specification such that the `PipelineTask` executes a list of `TaskRuns` or `Runs` in parallel with the specified list of inputs for a `Parameter` or with different combinations of the inputs for a set of `Parameters`.

This PR refactors the FanOut() function to use the Matrix struct instead of the old []Params.
  • Loading branch information
EmmaMunley committed Feb 28, 2023
1 parent 5cc8bf2 commit 1ca42af
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pkg/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
)

// FanOut produces combinations of Parameters of type String from a slice of Parameters of type Array.
func FanOut(params []v1beta1.Param) Combinations {
func FanOut(matrix v1beta1.Matrix) Combinations {
var combinations Combinations
for _, parameter := range params {
for _, parameter := range matrix.Params {
combinations = combinations.fanOut(parameter)
}
return combinations
Expand Down
35 changes: 22 additions & 13 deletions pkg/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ import (
func Test_FanOut(t *testing.T) {
tests := []struct {
name string
matrix []v1beta1.Param
matrix *v1beta1.Matrix
wantCombinations Combinations
}{{
name: "matrix with no params",
matrix: &v1beta1.Matrix{
Params: []v1beta1.Param{},
},
wantCombinations: nil,
}, {
name: "single array in matrix",
matrix: []v1beta1.Param{{
Name: "platform",
Value: v1beta1.ParamValue{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"linux", "mac", "windows"}},
}},
matrix: &v1beta1.Matrix{
Params: []v1beta1.Param{{
Name: "platform",
Value: v1beta1.ParamValue{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"linux", "mac", "windows"}},
}},
},
wantCombinations: Combinations{{
MatrixID: "0",
Params: []v1beta1.Param{{
Expand All @@ -52,13 +60,14 @@ func Test_FanOut(t *testing.T) {
}},
}, {
name: "multiple arrays in matrix",
matrix: []v1beta1.Param{{
Name: "platform",
Value: v1beta1.ParamValue{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"linux", "mac", "windows"}},
}, {
Name: "browser",
Value: v1beta1.ParamValue{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"chrome", "safari", "firefox"}},
}},
matrix: &v1beta1.Matrix{
Params: []v1beta1.Param{{
Name: "platform",
Value: v1beta1.ParamValue{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"linux", "mac", "windows"}},
}, {
Name: "browser",
Value: v1beta1.ParamValue{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"chrome", "safari", "firefox"}},
}}},
wantCombinations: Combinations{{
MatrixID: "0",
Params: []v1beta1.Param{{
Expand Down Expand Up @@ -144,7 +153,7 @@ func Test_FanOut(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotCombinations := FanOut(tt.matrix)
gotCombinations := FanOut(*tt.matrix)
if d := cmp.Diff(tt.wantCombinations, gotCombinations); d != "" {
t.Errorf("Combinations of Parameters did not match the expected Combinations: %s", d)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ func (c *Reconciler) createTaskRuns(ctx context.Context, rpt *resources.Resolved
ctx, span := c.tracerProvider.Tracer(TracerName).Start(ctx, "createTaskRuns")
defer span.End()
var taskRuns []*v1beta1.TaskRun
matrixCombinations := matrix.FanOut(rpt.PipelineTask.Matrix.Params).ToMap()
matrixCombinations := matrix.FanOut(*rpt.PipelineTask.Matrix).ToMap()
for i, taskRunName := range rpt.TaskRunNames {
params := matrixCombinations[strconv.Itoa(i)]
taskRun, err := c.createTaskRun(ctx, taskRunName, params, rpt, pr, storageBasePath)
Expand Down Expand Up @@ -909,7 +909,7 @@ func (c *Reconciler) createRunObjects(ctx context.Context, rpt *resources.Resolv
var runObjects []v1beta1.RunObject
ctx, span := c.tracerProvider.Tracer(TracerName).Start(ctx, "createRunObjects")
defer span.End()
matrixCombinations := matrix.FanOut(rpt.PipelineTask.Matrix.Params).ToMap()
matrixCombinations := matrix.FanOut(*rpt.PipelineTask.Matrix).ToMap()
for i, runObjectName := range rpt.RunObjectNames {
params := matrixCombinations[strconv.Itoa(i)]
runObject, err := c.createRunObject(ctx, runObjectName, params, rpt, pr)
Expand Down

0 comments on commit 1ca42af

Please sign in to comment.