diff --git a/pkg/reconciler/pipelinerun/pipelinerun.go b/pkg/reconciler/pipelinerun/pipelinerun.go index 2caea94bd0e..3b668f7534b 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun.go +++ b/pkg/reconciler/pipelinerun/pipelinerun.go @@ -1322,6 +1322,20 @@ func propagatePipelineNameLabelToPipelineRun(pr *v1.PipelineRun) error { pr.ObjectMeta.Labels[pipeline.PipelineLabelKey] = pr.Name case pr.Spec.PipelineRef != nil && pr.Spec.PipelineRef.Resolver != "": pr.ObjectMeta.Labels[pipeline.PipelineLabelKey] = pr.Name + + // https://tekton.dev/docs/pipelines/cluster-resolver/#pipeline-resolution + var kind, name string + for _, param := range pr.Spec.PipelineRef.Params { + if param.Name == "kind" { + kind = param.Value.StringVal + } + if param.Name == "name" { + name = param.Value.StringVal + } + } + if kind == "pipeline" { + pr.ObjectMeta.Labels[pipeline.PipelineLabelKey] = name + } default: return fmt.Errorf("pipelineRun %s not providing PipelineRef or PipelineSpec", pr.Name) } diff --git a/pkg/reconciler/pipelinerun/pipelinerun_test.go b/pkg/reconciler/pipelinerun/pipelinerun_test.go index 447fee02d63..bf36e073385 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun_test.go +++ b/pkg/reconciler/pipelinerun/pipelinerun_test.go @@ -8244,6 +8244,73 @@ spec: } } +func TestReconciler_ReconcileKind_PipelineRunLabels(t *testing.T) { + names.TestingSeed() + + pipelineName := "p-pipelinetask" + pipelineRunName := "pr-pipelinetask" + + ps := []*v1.Pipeline{parse.MustParseV1Pipeline(t, ` +metadata: + name: p-pipelinetask + namespace: foo +spec: + tasks: + - name: task1 + taskRef: + name: mytask +`)} + + prs := []*v1.PipelineRun{parse.MustParseV1PipelineRun(t, ` +metadata: + name: pr-pipelinetask + namespace: foo +spec: + pipelineRef: + params: + - name: kind + value: pipeline + - name: name + value: p-pipelinetask + - name: namespace + value: foo + resolver: cluster +`)} + + ts := []*v1.Task{{ObjectMeta: baseObjectMeta("mytask", "foo")}} + + trs := []*v1.TaskRun{mustParseTaskRunWithObjectMeta(t, + taskRunObjectMeta(pipelineRunName+"-task1-xxyy", "foo", pipelineRunName, pipelineName, "task1", false), + ` +spec: + serviceAccountName: test-sa + taskRef: + name: mytask +status: + conditions: + - reason: "done" + status: "True" + type: Succeeded +`)} + + d := test.Data{ + PipelineRuns: prs, + Pipelines: ps, + Tasks: ts, + TaskRuns: trs, + } + prt := newPipelineRunTest(t, d) + defer prt.Cancel() + + actualPipelineRun, _ := prt.reconcileRun("foo", pipelineRunName, []string{}, false) + if actualPipelineRun.Labels == nil { + t.Fatalf("Pelinerun should have labels") + } + if v, ok := actualPipelineRun.Labels[pipeline.PipelineLabelKey]; !ok || v != pipelineName { + t.Fatalf("The expected name of the pipeline is %s, but the actual name is %s", pipelineName, v) + } +} + // newPipelineRunTest returns PipelineRunTest with a new PipelineRun controller created with specified state through data // This PipelineRunTest can be reused for multiple PipelineRuns by calling reconcileRun for each pipelineRun func newPipelineRunTest(t *testing.T, data test.Data) *PipelineRunTest {