Skip to content

Commit

Permalink
TEP-0090: Indicate Resolved PipelineRunTask is Matrixed
Browse files Browse the repository at this point in the history
[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
  • Loading branch information
jerop committed Jun 7, 2022
1 parent 342ed52 commit 2904eb1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -461,6 +468,10 @@ func isCustomTask(ctx context.Context, rprt ResolvedPipelineRunTask) bool {
return cfg.FeatureFlags.EnableCustomTasks && !invalidSpec && (isTaskRefCustomTask || isTaskSpecCustomTask)
}

func isMatrixed(rprt ResolvedPipelineRunTask) bool {
return len(rprt.PipelineTask.Matrix) != 0
}

// ResolvePipelineRunTask retrieves a single Task's instance using the getTask to fetch
// the spec. If it is unable to retrieve an instance of a referenced Task, it will return
// an error, otherwise it returns a list of all of the Tasks retrieved. It will retrieve
Expand All @@ -480,6 +491,7 @@ func ResolvePipelineRunTask(
PipelineTask: &task,
}
rprt.CustomTask = isCustomTask(ctx, rprt)
rprt.Matrixed = isMatrixed(rprt)
if rprt.IsCustomTask() {
rprt.RunName = getRunName(pipelineRun.Status.Runs, pipelineRun.Status.ChildReferences, task.Name, pipelineRun.Name)
run, err := getRun(rprt.RunName)
Expand Down
80 changes: 80 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 nil, nil }
getRun := func(name string) (*v1alpha1.Run, error) { return nil, nil }
getCondition := func(name string) (*v1alpha1.Condition, error) { return nil, 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))
}
})
}
}

0 comments on commit 2904eb1

Please sign in to comment.