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 authored and tekton-robot committed Aug 27, 2024
1 parent 78d1f11 commit 504bdc8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 22 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 @@ -561,6 +561,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
16 changes: 12 additions & 4 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 Expand Up @@ -17675,7 +17683,7 @@ func Test_runNextSchedulableTask(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "task2",
ResourceVersion: "00002",
Labels: map[string]string{"tekton.dev/pipelineRun": "", "tekton.dev/pipelineTask": "task2"},
Labels: map[string]string{"tekton.dev/pipelineRun": "", "tekton.dev/pipelineTask": "task2", "tekton.dev/pipelineRunUID": ""},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "tekton.dev/v1",
Expand Down
46 changes: 31 additions & 15 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ func TestReconcile_ExplicitDefaultSA(t *testing.T) {
metadata:
name: test-taskrun-run-success
namespace: foo
uid: bar
spec:
taskRef:
apiVersion: v1
Expand All @@ -440,6 +441,7 @@ spec:
metadata:
name: test-taskrun-with-sa-run-success
namespace: foo
uid: bar
spec:
serviceAccountName: test-sa
taskRef:
Expand Down Expand Up @@ -469,15 +471,15 @@ spec:
}{{
name: "success",
taskRun: taskRunSuccess,
wantPod: expectedPod("test-taskrun-run-success-pod", "test-task", "test-taskrun-run-success", "foo", defaultSAName, false, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-run-success-pod", "test-task", "test-taskrun-run-success", "bar", "foo", defaultSAName, false, nil, []stepForExpectedPod{{
image: "foo",
name: "simple-step",
cmd: "/mycmd",
}}),
}, {
name: "serviceaccount",
taskRun: taskRunWithSaSuccess,
wantPod: expectedPod("test-taskrun-with-sa-run-success-pod", "test-with-sa", "test-taskrun-with-sa-run-success", "foo", "test-sa", false, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-with-sa-run-success-pod", "test-with-sa", "test-taskrun-with-sa-run-success", "bar", "foo", "test-sa", false, nil, []stepForExpectedPod{{
image: "foo",
name: "sa-step",
cmd: "/mycmd",
Expand Down Expand Up @@ -643,6 +645,7 @@ func TestReconcile(t *testing.T) {
metadata:
name: test-taskrun-run-success
namespace: foo
uid: bar
spec:
taskRef:
apiVersion: v1
Expand All @@ -652,6 +655,7 @@ spec:
metadata:
name: test-taskrun-with-sa-run-success
namespace: foo
uid: bar
spec:
serviceAccountName: test-sa
taskRef:
Expand All @@ -662,6 +666,7 @@ spec:
metadata:
name: test-taskrun-substitution
namespace: foo
uid: bar
spec:
params:
- name: myarg
Expand All @@ -678,6 +683,7 @@ spec:
metadata:
name: test-taskrun-with-taskspec
namespace: foo
uid: bar
spec:
params:
- name: myarg
Expand All @@ -699,6 +705,7 @@ spec:
metadata:
name: test-taskrun-with-cluster-task
namespace: foo
uid: bar
spec:
taskRef:
kind: ClusterTask
Expand All @@ -712,6 +719,7 @@ metadata:
tekton.dev/taskRun: WillNotBeUsed
name: test-taskrun-with-labels
namespace: foo
uid: bar
spec:
taskRef:
name: test-task
Expand All @@ -723,6 +731,7 @@ metadata:
TaskRunAnnotation: TaskRunValue
name: test-taskrun-with-annotations
namespace: foo
uid: bar
spec:
taskRef:
name: test-task
Expand All @@ -732,6 +741,7 @@ spec:
metadata:
name: test-taskrun-with-pod
namespace: foo
uid: bar
spec:
taskRef:
name: test-task
Expand All @@ -743,6 +753,7 @@ status:
metadata:
name: test-taskrun-with-credentials-variable
namespace: foo
uid: bar
spec:
taskSpec:
steps:
Expand Down Expand Up @@ -770,6 +781,7 @@ spec:
metadata:
name: test-taskrun-bundle
namespace: foo
uid: bar
spec:
taskRef:
bundle: %s
Expand Down Expand Up @@ -800,7 +812,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-run-success-pod", "test-task", "test-taskrun-run-success", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-run-success-pod", "test-task", "test-taskrun-run-success", "bar", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
image: "foo",
name: "simple-step",
cmd: "/mycmd",
Expand All @@ -812,7 +824,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-with-sa-run-success-pod", "test-with-sa", "test-taskrun-with-sa-run-success", "foo", "test-sa", false, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-with-sa-run-success-pod", "test-with-sa", "test-taskrun-with-sa-run-success", "bar", "foo", "test-sa", false, nil, []stepForExpectedPod{{
image: "foo",
name: "sa-step",
cmd: "/mycmd",
Expand All @@ -824,7 +836,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-substitution-pod", "test-task-with-substitution", "test-taskrun-substitution", "foo", config.DefaultServiceAccountValue, false, []corev1.Volume{{
wantPod: expectedPod("test-taskrun-substitution-pod", "test-task-with-substitution", "test-taskrun-substitution", "bar", "foo", config.DefaultServiceAccountValue, false, []corev1.Volume{{
Name: "volume-configmap",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
Expand Down Expand Up @@ -860,7 +872,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-with-taskspec-pod", "", "test-taskrun-with-taskspec", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{
wantPod: expectedPod("test-taskrun-with-taskspec-pod", "", "test-taskrun-with-taskspec", "bar", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{
{
name: "mycontainer",
image: "myimage",
Expand All @@ -874,7 +886,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-with-cluster-task-pod", "test-cluster-task", "test-taskrun-with-cluster-task", "foo", config.DefaultServiceAccountValue, true, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-with-cluster-task-pod", "test-cluster-task", "test-taskrun-with-cluster-task", "bar", "foo", config.DefaultServiceAccountValue, true, nil, []stepForExpectedPod{{
name: "simple-step",
image: "foo",
cmd: "/mycmd",
Expand All @@ -886,7 +898,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-with-pod-pod", "test-task", "test-taskrun-with-pod", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-with-pod-pod", "test-task", "test-taskrun-with-pod", "bar", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
name: "simple-step",
image: "foo",
cmd: "/mycmd",
Expand All @@ -898,7 +910,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-with-credentials-variable-pod", "", "test-taskrun-with-credentials-variable", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-with-credentials-variable-pod", "", "test-taskrun-with-credentials-variable", "bar", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
name: "mycontainer",
image: "myimage",
cmd: "/mycmd /tekton/creds",
Expand All @@ -910,7 +922,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-bundle-pod", "test-task", "test-taskrun-bundle", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-bundle-pod", "test-task", "test-taskrun-bundle", "bar", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
name: "simple-step",
image: "foo",
cmd: "/mycmd",
Expand Down Expand Up @@ -980,6 +992,7 @@ func TestAlphaReconcile(t *testing.T) {
metadata:
name: test-taskrun-with-output-config
namespace: foo
uid: bar
spec:
taskSpec:
steps:
Expand All @@ -995,6 +1008,7 @@ spec:
metadata:
name: test-taskrun-with-output-config-ws
namespace: foo
uid: bar
spec:
workspaces:
- name: data
Expand Down Expand Up @@ -1039,7 +1053,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: expectedPod("test-taskrun-with-output-config-pod", "", "test-taskrun-with-output-config", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
wantPod: expectedPod("test-taskrun-with-output-config-pod", "", "test-taskrun-with-output-config", "bar", "foo", config.DefaultServiceAccountValue, false, nil, []stepForExpectedPod{{
name: "mycontainer",
image: "myimage",
stdoutPath: "stdout.txt",
Expand All @@ -1052,7 +1066,7 @@ spec:
"Normal Started ",
"Normal Running Not all Steps",
},
wantPod: addVolumeMounts(expectedPod("test-taskrun-with-output-config-ws-pod", "", "test-taskrun-with-output-config-ws", "foo", config.DefaultServiceAccountValue, false,
wantPod: addVolumeMounts(expectedPod("test-taskrun-with-output-config-ws-pod", "", "test-taskrun-with-output-config-ws", "bar", "foo", config.DefaultServiceAccountValue, false,
[]corev1.Volume{{
Name: "ws-d872e",
VolumeSource: corev1.VolumeSource{
Expand Down Expand Up @@ -5942,7 +5956,7 @@ func podArgs(cmd string, stdoutPath string, stderrPath string, additionalArgs []
return args
}

func podObjectMeta(name, taskName, taskRunName, ns string, isClusterTask bool) metav1.ObjectMeta {
func podObjectMeta(name, taskName, taskRunName, taskRunUID, ns string, isClusterTask bool) metav1.ObjectMeta {
trueB := true
om := metav1.ObjectMeta{
Name: name,
Expand All @@ -5952,6 +5966,7 @@ func podObjectMeta(name, taskName, taskRunName, ns string, isClusterTask bool) m
},
Labels: map[string]string{
pipeline.TaskRunLabelKey: taskRunName,
pipeline.TaskRunUIDLabelKey: taskRunUID,
"app.kubernetes.io/managed-by": "tekton-pipelines",
},
OwnerReferences: []metav1.OwnerReference{{
Expand All @@ -5960,6 +5975,7 @@ func podObjectMeta(name, taskName, taskRunName, ns string, isClusterTask bool) m
Controller: &trueB,
BlockOwnerDeletion: &trueB,
APIVersion: currentAPIVersion,
UID: types.UID(taskRunUID),
}},
}

Expand All @@ -5986,13 +6002,13 @@ type stepForExpectedPod struct {
stderrPath string
}

func expectedPod(podName, taskName, taskRunName, ns, saName string, isClusterTask bool, extraVolumes []corev1.Volume, steps []stepForExpectedPod) *corev1.Pod {
func expectedPod(podName, taskName, taskRunName, taskRunUID, ns, saName string, isClusterTask bool, extraVolumes []corev1.Volume, steps []stepForExpectedPod) *corev1.Pod {
stepNames := make([]string, 0, len(steps))
for _, s := range steps {
stepNames = append(stepNames, "step-"+s.name)
}
p := &corev1.Pod{
ObjectMeta: podObjectMeta(podName, taskName, taskRunName, ns, isClusterTask),
ObjectMeta: podObjectMeta(podName, taskName, taskRunName, taskRunUID, ns, isClusterTask),
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
workspaceVolume,
Expand Down

0 comments on commit 504bdc8

Please sign in to comment.