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

TEP-0090 Matrix: Refactor Matrix FanOut() to use the Matrix Struct #6246

Merged
merged 1 commit into from
Mar 2, 2023
Merged
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
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 {
Copy link
Member

Choose a reason for hiding this comment

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

Can we please declare this function as a member function:

func (m Matrix) FanOut() Combinations {
	var combinations Combinations
	for _, parameter := range m.Params {
		combinations = combinations.fanOut(parameter)
	}
	return combinations
}

just like:

func (combinations Combinations) fanOut(param v1beta1.Param) Combinations {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/hold for now until we clarify how this works since the existing logic has matrix.FanOut()

matrixCombinations := matrix.FanOut(rpt.PipelineTask.Matrix.Params).ToMap()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Chatted with Priti and decided for now we will leave as is. The way that Matrix pkg is right now does not make it is easy to simply refactor FanOut() as a member function.
/hold cancel
/assign @pritidesai

Copy link
Member

Choose a reason for hiding this comment

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

@EmmaMunley is it because of import cycle?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. We would have to move FanOut() to where the type Matrix is defined in Pipeline_types.go, however Combinations is defined within Matrix and it doesn't make sense to have Combinations defined in the API.

Copy link
Member

Choose a reason for hiding this comment

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

thanks @EmmaMunley!

Yup, it results in cyclic imports 🙃 and bringing combinations in pkg/apis/ might not be desirable based on recent discussions of avoiding copy/paste in multiple APIs.

@jerop I am happy to merge this for now but wanted to understand this package github.com/tektoncd/pipeline/pkg/matrix. Also, the package name matrix being same as the type Matrix caused confusion in https://github.com/tektoncd/pipeline/blob/main/pkg/reconciler/pipelinerun/pipelinerun.go#L837

matrix.FanOut(rpt.PipelineTask.Matrix.Params).ToMap()

Copy link
Member

Choose a reason for hiding this comment

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

Matrix type is defined in pkg/apis/pipeline/v1beta1/ with a couple of member functions. Matrix type has two fields: Params []Param and Include []MatrixInclude

https://github.com/tektoncd/pipeline/blob/main/pkg/apis/pipeline/v1beta1/pipeline_types.go#L165

func (matrix *Matrix) MatrixHasInclude() bool {

func (matrix *Matrix) MatrixHasParams() bool {

There is a matrix package with matrix.go and matrix_types.go with a combinations defined. Combination type has two fields: MatrixID string and Params []v1beta1.Param.

Can we replace combination type with matrix struct and add MatrixID to matrix struct? This way we do not have maintain a separate struct and a package.

Copy link
Member

Choose a reason for hiding this comment

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

@pritidesai thanks for the suggestions, refactored the implementation - #6282

var combinations Combinations
for _, parameter := range params {
for _, parameter := range matrix.Params {
combinations = combinations.fanOut(parameter)
}
return combinations
Expand Down
33 changes: 21 additions & 12 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
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