From 3dfa37d6acc92b140dd930801a11a0d63cc34c32 Mon Sep 17 00:00:00 2001 From: Jerop Date: Fri, 24 Jun 2022 10:49:11 -0400 Subject: [PATCH] TEP-0090: Matrix - Get Names of `Runs` [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 --- .../resources/pipelinerunresolution.go | 19 ++++++ .../resources/pipelinerunresolution_test.go | 63 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go index 1a6c852707d..aa71618ca89 100644 --- a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go +++ b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go @@ -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) { diff --git a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go index 7b565a3ee5a..898f139fe25 100644 --- a/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go +++ b/pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go @@ -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{