Skip to content

Commit

Permalink
Add UID label to PipelineRun and TaskRun
Browse files Browse the repository at this point in the history
TaskRun pods have tekton.dev/taskRunUID and tekton.dev/pipelineRunUID labels
  • Loading branch information
khrm committed Aug 13, 2024
1 parent dd36119 commit 69013a7
Show file tree
Hide file tree
Showing 6 changed files with 722 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pkg/apis/pipeline/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ const (
// TaskRunLabelKey is used as the label identifier for a TaskRun
TaskRunLabelKey = GroupName + "/taskRun"

// TaskRunLabelKey is used as the label identifier for a TaskRun
TaskRunUIDLabelKey = GroupName + "/taskRunUID"

// PipelineLabelKey is used as the label identifier for a Pipeline
PipelineLabelKey = GroupName + "/pipeline"

// PipelineRunLabelKey is used as the label identifier for a PipelineRun
PipelineRunLabelKey = GroupName + "/pipelineRun"

// PipelineRunLabelKey is used as the label identifier for a PipelineRun
PipelineRunUIDLabelKey = GroupName + "/pipelineRunUID"

// PipelineTaskLabelKey is used as the label identifier for a PipelineTask
PipelineTaskLabelKey = GroupName + "/pipelineTask"

Expand Down
1 change: 1 addition & 0 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ func makeLabels(s *v1.TaskRun) map[string]string {
// NB: Set this *after* passing through TaskRun Labels. If the TaskRun
// specifies this label, it should be overridden by this value.
labels[pipeline.TaskRunLabelKey] = s.Name
labels[pipeline.TaskRunUIDLabelKey] = string(s.UID)
return labels
}

Expand Down
10 changes: 7 additions & 3 deletions pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/version"
fakediscovery "k8s.io/client-go/discovery/fake"
fakek8s "k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -3250,14 +3251,17 @@ func verifyTaskLevelComputeResources(expectedComputeResources []ExpectedComputeR

func TestMakeLabels(t *testing.T) {
taskRunName := "task-run-name"
taskRunUID := types.UID("taskrunuid")
want := map[string]string{
pipeline.TaskRunLabelKey: taskRunName,
"foo": "bar",
"hello": "world",
pipeline.TaskRunLabelKey: taskRunName,
"foo": "bar",
"hello": "world",
pipeline.TaskRunUIDLabelKey: string(taskRunUID),
}
got := makeLabels(&v1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: taskRunName,
UID: taskRunUID,
Labels: map[string]string{
"foo": "bar",
"hello": "world",
Expand Down
1 change: 1 addition & 0 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,7 @@ func getTaskrunLabels(pr *v1.PipelineRun, pipelineTaskName string, includePipeli
}
}
labels[pipeline.PipelineRunLabelKey] = pr.Name
labels[pipeline.PipelineRunUIDLabelKey] = string(pr.UID)
if pipelineTaskName != "" {
labels[pipeline.PipelineTaskLabelKey] = pipelineTaskName
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func TestReconcile(t *testing.T) {
metadata:
name: test-pipeline-run-success
namespace: foo
uid: bar
spec:
params:
- name: bar
Expand Down Expand Up @@ -397,6 +398,8 @@ spec:
name: unit-test-task
kind: Task
`)
expectedTaskRun.Labels["tekton.dev/pipelineRunUID"] = "bar"
expectedTaskRun.OwnerReferences[0].UID = "bar"
// ignore IgnoreUnexported ignore both after and before steps fields
if d := cmp.Diff(expectedTaskRun, actual, ignoreTypeMeta, ignoreResourceVersion); d != "" {
t.Errorf("expected to see TaskRun %v created. Diff %s", expectedTaskRun, diff.PrintWantGot(d))
Expand Down Expand Up @@ -427,6 +430,7 @@ func TestReconcile_V1Beta1CustomTask(t *testing.T) {
simpleCustomTaskPRYAML := `metadata:
name: test-pipelinerun
namespace: namespace
uid: bar
spec:
pipelineSpec:
tasks:
Expand All @@ -446,6 +450,7 @@ spec:
tekton.dev/pipeline: test-pipelinerun
tekton.dev/pipelineRun: test-pipelinerun
tekton.dev/pipelineTask: custom-task
tekton.dev/pipelineRunUID: bar
name: test-pipelinerun-custom-task
namespace: namespace
ownerReferences:
Expand All @@ -454,6 +459,7 @@ spec:
controller: true
kind: PipelineRun
name: test-pipelinerun
uid: bar
spec:
params:
- name: param1
Expand Down Expand Up @@ -9334,11 +9340,13 @@ func taskRunObjectMeta(trName, ns, prName, pipelineName, pipelineTaskName string
APIVersion: "tekton.dev/v1",
Controller: &trueb,
BlockOwnerDeletion: &trueb,
UID: "",
}},
Labels: map[string]string{
pipeline.PipelineLabelKey: pipelineName,
pipeline.PipelineRunLabelKey: prName,
pipeline.PipelineTaskLabelKey: pipelineTaskName,
pipeline.PipelineLabelKey: pipelineName,
pipeline.PipelineRunLabelKey: prName,
pipeline.PipelineTaskLabelKey: pipelineTaskName,
pipeline.PipelineRunUIDLabelKey: "",
},
Annotations: map[string]string{},
}
Expand Down
Loading

0 comments on commit 69013a7

Please sign in to comment.