diff --git a/pkg/apis/pipeline/v1/matrix_types.go b/pkg/apis/pipeline/v1/matrix_types.go index f51a0ac4af4..17cdc65b29f 100644 --- a/pkg/apis/pipeline/v1/matrix_types.go +++ b/pkg/apis/pipeline/v1/matrix_types.go @@ -236,7 +236,9 @@ func (m *Matrix) countGeneratedCombinationsFromParams() int { } count := 1 for _, param := range m.Params { - count *= len(param.Value.ArrayVal) + if len(param.Value.ArrayVal) > 0 { + count *= len(param.Value.ArrayVal) + } } return count } diff --git a/pkg/apis/pipeline/v1/matrix_types_test.go b/pkg/apis/pipeline/v1/matrix_types_test.go index 26b83f83c25..609d281d6dd 100644 --- a/pkg/apis/pipeline/v1/matrix_types_test.go +++ b/pkg/apis/pipeline/v1/matrix_types_test.go @@ -782,7 +782,7 @@ func TestPipelineTask_CountCombinations(t *testing.T) { }{{ name: "combinations count is zero", matrix: &v1.Matrix{ - Params: v1.Params{{}}}, + Params: v1.Params{}}, want: 0, }, { name: "combinations count is one from one parameter", @@ -915,11 +915,20 @@ func TestPipelineTask_CountCombinations(t *testing.T) { }}, }}, want: 7, + }, { + name: "matrix params with string value containing variable reference", + matrix: &v1.Matrix{ + Params: v1.Params{{ + Name: "GOARCH", Value: v1.ParamValue{ArrayVal: []string{"linux/amd64", "linux/ppc64le", "linux/s390x"}}, + }, { + Name: "version", Value: v1.ParamValue{StringVal: "$(tasks.platforms.results.str[*])"}}, + }}, + want: 3, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if d := cmp.Diff(tt.want, tt.matrix.CountCombinations()); d != "" { - t.Errorf("Matrix.CountCombinations() errors diff %s", diff.PrintWantGot(d)) + t.Errorf("%s Matrix.CountCombinations() errors diff %s", tt.name, diff.PrintWantGot(d)) } }) } diff --git a/pkg/reconciler/pipelinerun/pipelinerun_test.go b/pkg/reconciler/pipelinerun/pipelinerun_test.go index 780249b6a40..ddbda3da48e 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun_test.go +++ b/pkg/reconciler/pipelinerun/pipelinerun_test.go @@ -8942,8 +8942,14 @@ spec: script: | echo "$(params.platform)" - name: b-task - taskRef: - name: mytask + taskSpec: + params: + - name: platform + steps: + - name: echo + image: alpine + script: | + echo "$(params.platform)" matrix: params: - name: platform diff --git a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go index 9f93efe5456..47a8f0b972f 100644 --- a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go +++ b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go @@ -3801,10 +3801,10 @@ func TestResolvePipelineRunTask_WithMatrix(t *testing.T) { name: "task with matrix - whole array results", pt: pts[2], want: &ResolvedPipelineTask{ - TaskRunNames: nil, + TaskRunNames: []string{"pipelinerun-pipelinetask-with-whole-array-results"}, TaskRuns: nil, PipelineTask: &pts[2], - ResolvedTask: nil, + ResolvedTask: rtr, }, pst: pipelineRunState, }} { diff --git a/test/matrix_test.go b/test/matrix_test.go index 04475140b02..bfecd74377b 100644 --- a/test/matrix_test.go +++ b/test/matrix_test.go @@ -29,6 +29,7 @@ import ( v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" "github.com/tektoncd/pipeline/test/diff" "github.com/tektoncd/pipeline/test/parse" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" duckv1 "knative.dev/pkg/apis/duck/v1" @@ -68,11 +69,14 @@ spec: default: "" - name: package default: "" + results: + - name: str + type: string steps: - name: echo image: mirror.gcr.io/alpine script: | - echo "$(params.GOARCH) and $(params.version)" + echo -n "$(params.GOARCH) and $(params.version)" | tee $(results.str.path) `, namespace)) task1withresults := parse.MustParseV1Task(t, fmt.Sprintf(` @@ -107,6 +111,22 @@ spec: echo -n "[\"go1.17\",\"go1.18.1\"]" | tee $(results.versions.path) `, namespace)) + task3printer := parse.MustParseV1Task(t, fmt.Sprintf(` +metadata: + name: printer + namespace: %s +spec: + params: + - name: platform + value: "default-platform" + steps: + - name: produce-a-list-of-versions + image: mirror.gcr.io/bash + script: | + #!/usr/bin/env bash + echo "platform: $(params.platform)" +`, namespace)) + if _, err := c.V1TaskClient.Create(ctx, task, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task.Name, err) } @@ -116,6 +136,9 @@ spec: if _, err := c.V1TaskClient.Create(ctx, task2withresults, metav1.CreateOptions{}); err != nil { t.Fatalf("Failed to create Task `%s`: %s", task2withresults.Name, err) } + if _, err := c.V1TaskClient.Create(ctx, task3printer, metav1.CreateOptions{}); err != nil { + t.Fatalf("Failed to create Task `%s`: %s", task3printer.Name, err) + } pipeline := parse.MustParseV1Pipeline(t, fmt.Sprintf(` metadata: @@ -157,6 +180,13 @@ spec: params: - name: GOARCH value: I-do-not-exist + - name: printer-matrix + taskRef: + name: printer + matrix: + params: + - name: platform + value: $(tasks.matrix-include.results.str[*]) `, helpers.ObjectNameForTest(t), namespace)) pipelineRun := parse.MustParseV1PipelineRun(t, fmt.Sprintf(` @@ -208,6 +238,11 @@ spec: }}}, TaskRunStatusFields: v1.TaskRunStatusFields{ Artifacts: &v1.Artifacts{}, + Results: []v1.TaskRunResult{{ + Name: "str", + Type: v1.ResultsTypeString, + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/amd64 and go1.17"}, + }}, }, }, }, { @@ -240,6 +275,11 @@ spec: }}}, TaskRunStatusFields: v1.TaskRunStatusFields{ Artifacts: &v1.Artifacts{}, + Results: []v1.TaskRunResult{{ + Name: "str", + Type: v1.ResultsTypeString, + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/ppc64le and go1.17"}, + }}, }, }, }, { @@ -269,6 +309,11 @@ spec: }}}, TaskRunStatusFields: v1.TaskRunStatusFields{ Artifacts: &v1.Artifacts{}, + Results: []v1.TaskRunResult{{ + Name: "str", + Type: v1.ResultsTypeString, + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/amd64 and go1.18.1"}, + }}, }, }, }, { @@ -298,6 +343,11 @@ spec: }}}, TaskRunStatusFields: v1.TaskRunStatusFields{ Artifacts: &v1.Artifacts{}, + Results: []v1.TaskRunResult{{ + Name: "str", + Type: v1.ResultsTypeString, + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/ppc64le and go1.18.1"}, + }}, }, }, }, { @@ -319,6 +369,136 @@ spec: Reason: "Succeeded", Message: "All Steps have completed executing", }}}, + TaskRunStatusFields: v1.TaskRunStatusFields{ + Artifacts: &v1.Artifacts{}, + Results: []v1.TaskRunResult{{ + Name: "str", + Type: v1.ResultsTypeString, + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "I-do-not-exist and "}, + }}, + }, + }, + }, { + ObjectMeta: metav1.ObjectMeta{ + Name: "pr-printer-matrix-0", + }, + Spec: v1.TaskRunSpec{ + Params: v1.Params{{ + Name: "platform", + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/amd64 and go1.17"}, + }}, + ServiceAccountName: "default", + TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind}, + }, + Status: v1.TaskRunStatus{ + Status: duckv1.Status{ + Conditions: duckv1.Conditions{{ + Type: apis.ConditionSucceeded, + Status: corev1.ConditionTrue, + Reason: "Succeeded", + Message: "All Steps have completed executing", + }}, + }, + TaskRunStatusFields: v1.TaskRunStatusFields{ + Artifacts: &v1.Artifacts{}, + }, + }, + }, { + ObjectMeta: metav1.ObjectMeta{ + Name: "pr-printer-matrix-1", + }, + Spec: v1.TaskRunSpec{ + Params: v1.Params{{ + Name: "platform", + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/ppc64le and go1.17"}, + }}, + ServiceAccountName: "default", + TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind}, + }, + Status: v1.TaskRunStatus{ + Status: duckv1.Status{ + Conditions: duckv1.Conditions{{ + Type: apis.ConditionSucceeded, + Status: corev1.ConditionTrue, + Reason: "Succeeded", + Message: "All Steps have completed executing", + }}, + }, + TaskRunStatusFields: v1.TaskRunStatusFields{ + Artifacts: &v1.Artifacts{}, + }, + }, + }, { + ObjectMeta: metav1.ObjectMeta{ + Name: "pr-printer-matrix-2", + }, + Spec: v1.TaskRunSpec{ + Params: v1.Params{{ + Name: "platform", + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/amd64 and go1.18.1"}, + }}, + ServiceAccountName: "default", + TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind}, + }, + Status: v1.TaskRunStatus{ + Status: duckv1.Status{ + Conditions: duckv1.Conditions{{ + Type: apis.ConditionSucceeded, + Status: corev1.ConditionTrue, + Reason: "Succeeded", + Message: "All Steps have completed executing", + }}, + }, + TaskRunStatusFields: v1.TaskRunStatusFields{ + Artifacts: &v1.Artifacts{}, + }, + }, + }, { + ObjectMeta: metav1.ObjectMeta{ + Name: "pr-printer-matrix-3", + }, + Spec: v1.TaskRunSpec{ + Params: v1.Params{{ + Name: "platform", + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "linux/ppc64le and go1.18.1"}, + }}, + ServiceAccountName: "default", + TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind}, + }, + Status: v1.TaskRunStatus{ + Status: duckv1.Status{ + Conditions: duckv1.Conditions{{ + Type: apis.ConditionSucceeded, + Status: corev1.ConditionTrue, + Reason: "Succeeded", + Message: "All Steps have completed executing", + }}, + }, + TaskRunStatusFields: v1.TaskRunStatusFields{ + Artifacts: &v1.Artifacts{}, + }, + }, + }, { + ObjectMeta: metav1.ObjectMeta{ + Name: "pr-printer-matrix-4", + }, + Spec: v1.TaskRunSpec{ + Params: v1.Params{{ + Name: "platform", + Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "I-do-not-exist and "}, + }}, + ServiceAccountName: "default", + TaskRef: &v1.TaskRef{Name: "printer", Kind: v1.NamespacedTaskKind}, + }, + Status: v1.TaskRunStatus{ + Status: duckv1.Status{ + Conditions: duckv1.Conditions{{ + Type: apis.ConditionSucceeded, + Status: corev1.ConditionTrue, + Reason: "Succeeded", + Message: "All Steps have completed executing", + }}, + }, TaskRunStatusFields: v1.TaskRunStatusFields{ Artifacts: &v1.Artifacts{}, },