Skip to content

Commit

Permalink
TEP-0090: Matrix - Get Names of Runs
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 a function to get names of `TaskRuns` from
Child References. If none are available, it generates new names for
`Runs` based on the `PipelineRun` and `PipelineTask` names.

[tep-0090]: https://github.com/tektoncd/community/blob/main/teps/0090-matrix.md
  • Loading branch information
jerop committed Jun 24, 2022
1 parent b12907f commit 3dfa37d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,25 @@ func getRunName(runsStatus map[string]*v1beta1.PipelineRunRunStatus, childRefs [
return kmeta.ChildName(prName, fmt.Sprintf("-%s", ptName))
}

// getNamesOfRuns should return a unique names for `Runs` if they have not already been defined,
// and the existing ones otherwise.
func getNamesOfRuns(childRefs []v1beta1.ChildStatusReference, ptName, prName string, combinationCount int) []string {
if runNames := getRunNamesFromChildRefs(childRefs, ptName); runNames != nil {
return runNames
}
return getNewTaskRunNames(ptName, prName, combinationCount)
}

func getRunNamesFromChildRefs(childRefs []v1beta1.ChildStatusReference, ptName string) []string {
var runNames []string
for _, cr := range childRefs {
if cr.Kind == pipeline.RunControllerName && cr.PipelineTaskName == ptName {
runNames = append(runNames, cr.Name)
}
}
return runNames
}

// resolvePipelineTaskResources matches PipelineResources referenced by pt inputs and outputs with the
// providedResources and returns an instance of ResolvedTaskResources.
func resolvePipelineTaskResources(pt v1beta1.PipelineTask, ts *v1beta1.TaskSpec, taskName string, kind v1beta1.TaskKind, providedResources map[string]*resourcev1alpha1.PipelineResource) (*resources.ResolvedTaskResources, error) {
Expand Down
63 changes: 63 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2906,6 +2906,69 @@ func TestGetNamesOfTaskRuns(t *testing.T) {
}
}

func TestGetNamesOfRuns(t *testing.T) {
prName := "mypipelinerun"
childRefs := []v1beta1.ChildStatusReference{{
TypeMeta: runtime.TypeMeta{Kind: "Run"},
Name: "mypipelinerun-mytask-0",
PipelineTaskName: "mytask",
}, {
TypeMeta: runtime.TypeMeta{Kind: "Run"},
Name: "mypipelinerun-mytask-1",
PipelineTaskName: "mytask",
}}

for _, tc := range []struct {
name string
ptName string
prName string
wantRunNames []string
}{{
name: "existing runs",
ptName: "mytask",
wantRunNames: []string{"mypipelinerun-mytask-0", "mypipelinerun-mytask-1"},
}, {
name: "new runs",
ptName: "mynewtask",
wantRunNames: []string{"mypipelinerun-mynewtask-0", "mypipelinerun-mynewtask-1"},
}, {
name: "new pipelinetask with long names",
ptName: "longtask-0123456789-0123456789-0123456789-0123456789-0123456789",
wantRunNames: []string{
"mypipelinerun09c563f6b29a3a2c16b98e6dc95979c5-longtask-01234567",
"mypipelinerunab643c1924b632f050e5a07fe482fc25-longtask-01234567",
},
}, {
name: "new runs, pipelinerun with long name",
ptName: "task3",
prName: "pipeline-run-0123456789-0123456789-0123456789-0123456789",
wantRunNames: []string{
"pipeline-run-01234567891276ed292277c9bebded38d907a517fe-task3-0",
"pipeline-run-01234567891276ed292277c9bebded38d907a517fe-task3-1",
},
}, {
name: "new runs, pipelinetask and pipelinerun with long name",
ptName: "task2-0123456789-0123456789-0123456789-0123456789-0123456789",
prName: "pipeline-run-0123456789-0123456789-0123456789-0123456789",
wantRunNames: []string{
"pipeline-run-0123456789-01234563c0313c59d28c85a2c2b3fd3b17a9514",
"pipeline-run-0123456789-01234569d54677e88e96776942290e00b578ca5",
},
}} {
t.Run(tc.name, func(t *testing.T) {
testPrName := prName
if tc.prName != "" {
testPrName = tc.prName
}
namesOfRunsFromChildRefs := getNamesOfRuns(childRefs, tc.ptName, testPrName, 2)
sort.Strings(namesOfRunsFromChildRefs)
if d := cmp.Diff(tc.wantRunNames, namesOfRunsFromChildRefs); d != "" {
t.Errorf("GetTaskRunName: %s", diff.PrintWantGot(d))
}
})
}
}

func TestGetRunName(t *testing.T) {
prName := "pipeline-run"
runsStatus := map[string]*v1beta1.PipelineRunRunStatus{
Expand Down

0 comments on commit 3dfa37d

Please sign in to comment.