diff --git a/pkg/apis/pipeline/register.go b/pkg/apis/pipeline/register.go index ff713753d06..9971a9b79d6 100644 --- a/pkg/apis/pipeline/register.go +++ b/pkg/apis/pipeline/register.go @@ -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" diff --git a/pkg/pod/pod.go b/pkg/pod/pod.go index fc0db922d8a..4da3fc75338 100644 --- a/pkg/pod/pod.go +++ b/pkg/pod/pod.go @@ -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 } diff --git a/pkg/pod/pod_test.go b/pkg/pod/pod_test.go index 8eb65ae5e60..f283c76f10b 100644 --- a/pkg/pod/pod_test.go +++ b/pkg/pod/pod_test.go @@ -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" @@ -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", diff --git a/pkg/reconciler/pipelinerun/pipelinerun.go b/pkg/reconciler/pipelinerun/pipelinerun.go index b25b1ad75b0..484b29a6e28 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun.go +++ b/pkg/reconciler/pipelinerun/pipelinerun.go @@ -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 } diff --git a/pkg/reconciler/pipelinerun/pipelinerun_test.go b/pkg/reconciler/pipelinerun/pipelinerun_test.go index 6c6dfc37b64..b12fcce2744 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun_test.go +++ b/pkg/reconciler/pipelinerun/pipelinerun_test.go @@ -244,6 +244,7 @@ func TestReconcile(t *testing.T) { metadata: name: test-pipeline-run-success namespace: foo + uid: bar spec: params: - name: bar @@ -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)) @@ -427,6 +430,7 @@ func TestReconcile_V1Beta1CustomTask(t *testing.T) { simpleCustomTaskPRYAML := `metadata: name: test-pipelinerun namespace: namespace + uid: bar spec: pipelineSpec: tasks: @@ -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: @@ -454,6 +459,7 @@ spec: controller: true kind: PipelineRun name: test-pipelinerun + uid: bar spec: params: - name: param1 @@ -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{}, } @@ -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", diff --git a/pkg/reconciler/taskrun/taskrun_test.go b/pkg/reconciler/taskrun/taskrun_test.go index 9c970ecdf3a..044a0184ad9 100644 --- a/pkg/reconciler/taskrun/taskrun_test.go +++ b/pkg/reconciler/taskrun/taskrun_test.go @@ -430,6 +430,7 @@ func TestReconcile_ExplicitDefaultSA(t *testing.T) { metadata: name: test-taskrun-run-success namespace: foo + uid: bar spec: taskRef: apiVersion: v1 @@ -439,6 +440,7 @@ spec: metadata: name: test-taskrun-with-sa-run-success namespace: foo + uid: bar spec: serviceAccountName: test-sa taskRef: @@ -468,7 +470,7 @@ 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", @@ -476,7 +478,7 @@ spec: }, { 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", @@ -642,6 +644,7 @@ func TestReconcile(t *testing.T) { metadata: name: test-taskrun-run-success namespace: foo + uid: bar spec: taskRef: apiVersion: v1 @@ -651,6 +654,7 @@ spec: metadata: name: test-taskrun-with-sa-run-success namespace: foo + uid: bar spec: serviceAccountName: test-sa taskRef: @@ -661,6 +665,7 @@ spec: metadata: name: test-taskrun-substitution namespace: foo + uid: bar spec: params: - name: myarg @@ -677,6 +682,7 @@ spec: metadata: name: test-taskrun-with-taskspec namespace: foo + uid: bar spec: params: - name: myarg @@ -698,6 +704,7 @@ spec: metadata: name: test-taskrun-with-cluster-task namespace: foo + uid: bar spec: taskRef: kind: ClusterTask @@ -711,6 +718,7 @@ metadata: tekton.dev/taskRun: WillNotBeUsed name: test-taskrun-with-labels namespace: foo + uid: bar spec: taskRef: name: test-task @@ -722,6 +730,7 @@ metadata: TaskRunAnnotation: TaskRunValue name: test-taskrun-with-annotations namespace: foo + uid: bar spec: taskRef: name: test-task @@ -731,6 +740,7 @@ spec: metadata: name: test-taskrun-with-pod namespace: foo + uid: bar spec: taskRef: name: test-task @@ -742,6 +752,7 @@ status: metadata: name: test-taskrun-with-credentials-variable namespace: foo + uid: bar spec: taskSpec: steps: @@ -769,6 +780,7 @@ spec: metadata: name: test-taskrun-bundle namespace: foo + uid: bar spec: taskRef: bundle: %s @@ -799,7 +811,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", @@ -811,7 +823,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", @@ -823,7 +835,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{ @@ -859,7 +871,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", @@ -873,7 +885,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", @@ -885,7 +897,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", @@ -897,7 +909,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", @@ -909,7 +921,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", @@ -979,6 +991,7 @@ func TestAlphaReconcile(t *testing.T) { metadata: name: test-taskrun-with-output-config namespace: foo + uid: bar spec: taskSpec: steps: @@ -994,6 +1007,7 @@ spec: metadata: name: test-taskrun-with-output-config-ws namespace: foo + uid: bar spec: workspaces: - name: data @@ -1038,7 +1052,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", @@ -1051,7 +1065,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{ @@ -5941,7 +5955,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, @@ -5951,6 +5965,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{{ @@ -5959,6 +5974,7 @@ func podObjectMeta(name, taskName, taskRunName, ns string, isClusterTask bool) m Controller: &trueB, BlockOwnerDeletion: &trueB, APIVersion: currentAPIVersion, + UID: types.UID(taskRunUID), }}, } @@ -5985,13 +6001,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,