Skip to content

Commit

Permalink
TEP-0090: Resolve PipelineTask with Custom Task
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`.

This change resolves a `PipelineTask` with a matrixed `Custom Task`.

[tep-0090]: https://github.com/tektoncd/community/blob/main/teps/0090-matrix.md
  • Loading branch information
jerop authored and tekton-robot committed Jun 28, 2022
1 parent 29f68de commit 8611c55
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type ResolvedPipelineTask struct {
CustomTask bool
RunName string
Run *v1alpha1.Run
RunNames []string
Runs []*v1alpha1.Run
PipelineTask *v1beta1.PipelineTask
ResolvedTaskResources *resources.ResolvedTaskResources
Expand Down Expand Up @@ -562,6 +563,15 @@ func ResolvePipelineTask(
}
rpt.CustomTask = isCustomTask(ctx, rpt)
switch {
case rpt.IsCustomTask() && rpt.IsMatrixed():
rpt.RunNames = getNamesOfRuns(pipelineRun.Status.ChildReferences, pipelineTask.Name, pipelineRun.Name, pipelineTask.GetMatrixCombinationsCount())
for _, runName := range rpt.RunNames {
run, err := getRun(runName)
if err != nil && !kerrors.IsNotFound(err) {
return nil, fmt.Errorf("error retrieving Run %s: %w", runName, err)
}
rpt.Runs = append(rpt.Runs, run)
}
case rpt.IsCustomTask():
rpt.RunName = getRunName(pipelineRun.Status.Runs, pipelineRun.Status.ChildReferences, pipelineTask.Name, pipelineRun.Name)
run, err := getRun(rpt.RunName)
Expand Down
100 changes: 100 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3336,6 +3336,106 @@ func TestResolvePipelineRunTask_WithMatrix(t *testing.T) {
}
}

func TestResolvePipelineRunTask_WithMatrixedCustomTask(t *testing.T) {
pipelineRunName := "pipelinerun"
pipelineTaskName := "pipelinetask"

pr := v1beta1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Name: pipelineRunName,
},
}

var runs []*v1alpha1.Run
var runNames []string
runsMap := map[string]*v1alpha1.Run{}
for i := 0; i < 9; i++ {
runName := fmt.Sprintf("%s-%s-%d", pipelineRunName, pipelineTaskName, i)
run := &v1alpha1.Run{
ObjectMeta: metav1.ObjectMeta{
Name: runName,
},
}
runs = append(runs, run)
runNames = append(runNames, runName)
runsMap[runName] = run
}

pts := []v1beta1.PipelineTask{{
Name: "pipelinetask",
TaskRef: &v1beta1.TaskRef{
APIVersion: "example.dev/v0",
Kind: "Example",
Name: "my-task",
},
Matrix: []v1beta1.Param{{
Name: "platform",
Value: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"linux", "mac", "windows"}},
}},
}, {
Name: "pipelinetask",
TaskRef: &v1beta1.TaskRef{
APIVersion: "example.dev/v0",
Kind: "Example",
Name: "my-task",
},
Matrix: []v1beta1.Param{{
Name: "platform",
Value: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"linux", "mac", "windows"}},
}, {
Name: "browsers",
Value: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"chrome", "safari", "firefox"}},
}},
}}

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 runsMap[name], nil }

for _, tc := range []struct {
name string
pt v1beta1.PipelineTask
want *ResolvedPipelineTask
}{{
name: "custom task with matrix - single parameter",
pt: pts[0],
want: &ResolvedPipelineTask{
CustomTask: true,
RunNames: runNames[:3],
Runs: runs[:3],
PipelineTask: &pts[0],
},
}, {
name: "custom task with matrix - multiple parameters",
pt: pts[1],
want: &ResolvedPipelineTask{
CustomTask: true,
RunNames: runNames,
Runs: runs,
PipelineTask: &pts[1],
},
}} {
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)
rpt, err := ResolvePipelineTask(ctx, pr, getTask, getTaskRun, getRun, tc.pt, nil)
if err != nil {
t.Fatalf("Did not expect error when resolving PipelineRun: %v", err)
}
if d := cmp.Diff(tc.want, rpt); d != "" {
t.Errorf("Did not get expected ResolvedPipelineTask with Matrix and Custom Task: %s", diff.PrintWantGot(d))
}
})
}
}

func TestIsSuccessful(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down

0 comments on commit 8611c55

Please sign in to comment.