Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UID label to PipelineRun and TaskRun #8166

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ -430,6 +430,7 @@ func TestReconcile_ExplicitDefaultSA(t *testing.T) {
metadata:
name: test-taskrun-run-success
namespace: foo
uid: bar
spec:
taskRef:
apiVersion: v1
Expand All @@ -439,6 +440,7 @@ spec:
metadata:
name: test-taskrun-with-sa-run-success
namespace: foo
uid: bar
spec:
serviceAccountName: test-sa
taskRef:
Expand Down Expand Up @@ -468,15 +470,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 @@ -642,6 +644,7 @@ func TestReconcile(t *testing.T) {
metadata:
name: test-taskrun-run-success
namespace: foo
uid: bar
spec:
taskRef:
apiVersion: v1
Expand All @@ -651,6 +654,7 @@ spec:
metadata:
name: test-taskrun-with-sa-run-success
namespace: foo
uid: bar
spec:
serviceAccountName: test-sa
taskRef:
Expand All @@ -661,6 +665,7 @@ spec:
metadata:
name: test-taskrun-substitution
namespace: foo
uid: bar
spec:
params:
- name: myarg
Expand All @@ -677,6 +682,7 @@ spec:
metadata:
name: test-taskrun-with-taskspec
namespace: foo
uid: bar
spec:
params:
- name: myarg
Expand All @@ -698,6 +704,7 @@ spec:
metadata:
name: test-taskrun-with-cluster-task
namespace: foo
uid: bar
spec:
taskRef:
kind: ClusterTask
Expand All @@ -711,6 +718,7 @@ metadata:
tekton.dev/taskRun: WillNotBeUsed
name: test-taskrun-with-labels
namespace: foo
uid: bar
spec:
taskRef:
name: test-task
Expand All @@ -722,6 +730,7 @@ metadata:
TaskRunAnnotation: TaskRunValue
name: test-taskrun-with-annotations
namespace: foo
uid: bar
spec:
taskRef:
name: test-task
Expand All @@ -731,6 +740,7 @@ spec:
metadata:
name: test-taskrun-with-pod
namespace: foo
uid: bar
spec:
taskRef:
name: test-task
Expand All @@ -742,6 +752,7 @@ status:
metadata:
name: test-taskrun-with-credentials-variable
namespace: foo
uid: bar
spec:
taskSpec:
steps:
Expand Down Expand Up @@ -769,6 +780,7 @@ spec:
metadata:
name: test-taskrun-bundle
namespace: foo
uid: bar
spec:
taskRef:
bundle: %s
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -979,6 +991,7 @@ func TestAlphaReconcile(t *testing.T) {
metadata:
name: test-taskrun-with-output-config
namespace: foo
uid: bar
spec:
taskSpec:
steps:
Expand All @@ -994,6 +1007,7 @@ spec:
metadata:
name: test-taskrun-with-output-config-ws
namespace: foo
uid: bar
spec:
workspaces:
- name: data
Expand Down Expand Up @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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{{
Expand All @@ -5959,6 +5974,7 @@ func podObjectMeta(name, taskName, taskRunName, ns string, isClusterTask bool) m
Controller: &trueB,
BlockOwnerDeletion: &trueB,
APIVersion: currentAPIVersion,
UID: types.UID(taskRunUID),
}},
}

Expand All @@ -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,
Expand Down