Skip to content

Commit

Permalink
fix: the label is set to the wrong pipeline name
Browse files Browse the repository at this point in the history
when PipelineRun uses ClusterResolver to parse the Pipeline, the name of the Pipeline set in the label is wrong.

Signed-off-by: 0xff-dev <[email protected]>
  • Loading branch information
0xff-dev committed Jun 27, 2024
1 parent 009c171 commit 91b7499
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
67 changes: 67 additions & 0 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 91b7499

Please sign in to comment.