From 41e758a156b138f7295c18ce54288ccf557694b3 Mon Sep 17 00:00:00 2001 From: Jerop Date: Tue, 7 Jun 2022 15:12:43 -0400 Subject: [PATCH] TEP-0090: Indicate Resolved PipelineRunTask is Matrixed [TEP-0090: Matrix][tep-0090] proposed executing a `PipelineTask` in parallel `TaskRuns` and `Runs` with substitutions from combinations of `Parameters` in a `Matrix`. In this change, we add support for resolving a `PipelineTask` to indicate that it is matrixed. This is useful for processing the `ResolvedPipelineRunTask`. [tep-0090]: https://github.com/tektoncd/community/blob/main/teps/0090-matrix.md --- .../resources/pipelinerunresolution.go | 8 ++ .../resources/pipelinerunresolution_test.go | 80 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go index a24352e5c94..a18d9dc2cc3 100644 --- a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go +++ b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go @@ -79,6 +79,8 @@ type ResolvedPipelineRunTask struct { ResolvedTaskResources *resources.ResolvedTaskResources // ConditionChecks ~~TaskRuns but for evaling conditions ResolvedConditionChecks TaskConditionCheckState // Could also be a TaskRun or maybe just a Pod? + + Matrixed bool } // IsDone returns true only if the task is skipped, succeeded or failed @@ -105,6 +107,11 @@ func (t ResolvedPipelineRunTask) IsCustomTask() bool { return t.CustomTask } +// IsMatrixed return true if the PipelineTask has a Matrix. +func (t ResolvedPipelineRunTask) IsMatrixed() bool { + return t.Matrixed +} + // IsSuccessful returns true only if the run has completed successfully func (t ResolvedPipelineRunTask) IsSuccessful() bool { if t.IsCustomTask() { @@ -480,6 +487,7 @@ func ResolvePipelineRunTask( PipelineTask: &task, } rprt.CustomTask = isCustomTask(ctx, rprt) + rprt.Matrixed = len(rprt.PipelineTask.Matrix) != 0 if rprt.IsCustomTask() { rprt.RunName = getRunName(pipelineRun.Status.Runs, pipelineRun.Status.ChildReferences, task.Name, pipelineRun.Name) run, err := getRun(rprt.RunName) diff --git a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go index 0c38ba4af19..a5e5150df3b 100644 --- a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go +++ b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go @@ -3554,3 +3554,83 @@ func TestGetRunName(t *testing.T) { }) } } + +func TestIsMatrixed(t *testing.T) { + pr := v1beta1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pipelinerun", + }, + } + getTask := func(ctx context.Context, name string) (v1beta1.TaskObject, error) { return task, nil } + getTaskRun := func(name string) (*v1beta1.TaskRun, error) { return &trs[0], nil } + getRun := func(name string) (*v1alpha1.Run, error) { return &runs[0], nil } + getCondition := func(name string) (*v1alpha1.Condition, error) { return &condition, nil } + + for _, tc := range []struct { + name string + pt v1beta1.PipelineTask + want bool + }{{ + name: "custom task with matrix", + pt: v1beta1.PipelineTask{ + TaskRef: &v1beta1.TaskRef{ + APIVersion: "example.dev/v0", + Kind: "Sample", + }, + Matrix: []v1beta1.Param{{ + Name: "platform", + Value: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"linux", "mac", "windows"}}, + }}, + }, + want: true, + }, { + name: "custom task without matrix", + pt: v1beta1.PipelineTask{ + TaskRef: &v1beta1.TaskRef{ + APIVersion: "example.dev/v0", + Kind: "Sample", + }, + }, + want: false, + }, { + name: "task with matrix", + pt: v1beta1.PipelineTask{ + TaskRef: &v1beta1.TaskRef{ + Name: "my-task", + }, + Matrix: []v1beta1.Param{{ + Name: "platform", + Value: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"linux", "mac", "windows"}}, + }}, + }, + want: true, + }, { + name: "task without matrix", + pt: v1beta1.PipelineTask{ + TaskRef: &v1beta1.TaskRef{ + Name: "my-task", + }, + }, + want: false, + }} { + t.Run(tc.name, func(t *testing.T) { + ctx := context.Background() + cfg := config.NewStore(logtesting.TestLogger(t)) + cfg.OnConfigChanged(&corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName()}, + Data: map[string]string{ + "enable-api-fields": "alpha", + }, + }) + ctx = cfg.ToContext(ctx) + rprt, err := ResolvePipelineRunTask(ctx, pr, getTask, getTaskRun, getRun, getCondition, tc.pt, nil) + if err != nil { + t.Fatalf("Did not expect error when resolving PipelineRun: %v", err) + } + got := rprt.IsMatrixed() + if d := cmp.Diff(tc.want, got); d != "" { + t.Errorf("IsMatrixed: %s", diff.PrintWantGot(d)) + } + }) + } +}