diff --git a/docs/pipelineruns.md b/docs/pipelineruns.md index 5916ca9bcb1..9d27b89a68b 100644 --- a/docs/pipelineruns.md +++ b/docs/pipelineruns.md @@ -97,6 +97,26 @@ of the `TaskRun` resource object. For examples and more information about specifying service accounts, see the [`ServiceAccount`](./auth.md) reference topic. +## Labels + +Any labels specified in the metadata field of a `PipelineRun` will be +propagated to the `TaskRuns` created automatically for each `Task` in the +`Pipeline` and then to the `Pods` created for those `TaskRuns`. In addition, +the following labels will be added automatically: + +* `pipeline.knative.dev/pipeline` will contain the name of the `Pipeline` +* `pipeline.knative.dev/pipelineRun` will contain the name of the `PipelineRun` + +These labels make it easier to find the resources that are associated with a +given pipeline. + +For example, to find all `Pods` created by a `Pipeline` named test-pipeline, +you could use the following command: + +```shell +kubectl get pods --all-namespaces -l pipeline.knative.dev/pipeline=test-pipeline +``` + ## Cancelling a PipelineRun In order to cancel a running pipeline (`PipelineRun`), you need to update its diff --git a/docs/taskruns.md b/docs/taskruns.md index 8e6d2293aad..be0164b51a3 100644 --- a/docs/taskruns.md +++ b/docs/taskruns.md @@ -152,6 +152,30 @@ of the `TaskRun` resource object. For examples and more information about specifying service accounts, see the [`ServiceAccount`](./auth.md) reference topic. +## Labels + +Any labels specified in the metadata field of a `TaskRun` will be +propagated to the `Pod` created to execute the `Task`. In addition, +the following label will be added automatically: + +* `pipeline.knative.dev/taskRun` will contain the name of the `TaskRun` + +If the `TaskRun` was created automatically by a `PipelineRun`, then the +following two labels will also be added to the `TaskRun` and `Pod`: + +* `pipeline.knative.dev/pipeline` will contain the name of the `Pipeline` +* `pipeline.knative.dev/pipelineRun` will contain the name of the `PipelineRun` + +These labels make it easier to find the resources that are associated with a +given `TaskRun`. + +For example, to find all `Pods` created by a `TaskRun` named test-taskrun, +you could use the following command: + +```shell +kubectl get pods --all-namespaces -l pipeline.knative.dev/taskRun=test-taskrun +``` + ## Cancelling a TaskRun In order to cancel a running task (`TaskRun`), you need to update its spec to diff --git a/test/pipelinerun_test.go b/test/pipelinerun_test.go index 3740075dca1..d47318d5abe 100644 --- a/test/pipelinerun_test.go +++ b/test/pipelinerun_test.go @@ -138,7 +138,8 @@ func TestPipelineRun(t *testing.T) { td.testSetup(t, c, namespace, i) prName := fmt.Sprintf("%s%d", pipelineRunName, i) - if _, err := c.PipelineRunClient.Create(td.pipelineRunFunc(i, namespace)); err != nil { + pr, err := c.PipelineRunClient.Create(td.pipelineRunFunc(i, namespace)) + if err != nil { t.Fatalf("Failed to create PipelineRun `%s`: %s", prName, err) } @@ -160,14 +161,36 @@ func TestPipelineRun(t *testing.T) { if !r.Status.GetCondition(duckv1alpha1.ConditionSucceeded).IsTrue() { t.Fatalf("Expected TaskRun %s to have succeeded but Status is %v", taskRunName, r.Status) } - for name, key := range map[string]string{ - pipelineRunName: pipeline.PipelineRunLabelKey, - pipelineName: pipeline.PipelineLabelKey, - } { - expectedName := getName(name, i) - lbl := pipeline.GroupName + key - if val := r.Labels[lbl]; val != expectedName { - t.Errorf("Expected label %s=%s but got %s", lbl, expectedName, val) + + // Check label propagation to Tasks + labels := make(map[string]string, len(pr.ObjectMeta.Labels)+3) + for key, val := range pr.ObjectMeta.Labels { + labels[key] = val + } + labels[pipeline.GroupName+pipeline.PipelineLabelKey] = getName(pipelineName, i) + labels[pipeline.GroupName+pipeline.PipelineRunLabelKey] = getName(pipelineRunName, i) + for key, pipelineRunVal := range labels { + if taskRunVal := r.ObjectMeta.Labels[key]; taskRunVal != pipelineRunVal { + t.Errorf("Expected label %s=%s but got %s", key, pipelineRunVal, taskRunVal) + } + } + + // Check label propagation to Pods + taskRunLabelKey := pipeline.GroupName + pipeline.TaskRunLabelKey + labels[taskRunLabelKey] = taskRunName + pods, err := c.KubeClient.Kube.CoreV1().Pods(namespace).List(metav1.ListOptions{ + LabelSelector: taskRunLabelKey + " = " + taskRunName, + }) + if err != nil { + t.Fatalf("Couldn't get expected Pod for %s: %s", taskRunName, err) + } + if numPods := len(pods.Items); numPods != 1 { + t.Fatalf("Expected 1 Pod for %s, but got %d Pods", taskRunName, numPods) + } + pod := pods.Items[0] + for key, taskRunVal := range labels { + if podVal := pod.ObjectMeta.Labels[key]; podVal != taskRunVal { + t.Errorf("Expected label %s=%s but got %s", key, taskRunVal, podVal) } } } @@ -322,10 +345,12 @@ func getPipelineRunSecret(suffix int, namespace string) *corev1.Secret { } func getHelloWorldPipelineRun(suffix int, namespace string) *v1alpha1.PipelineRun { - return tb.PipelineRun(getName(pipelineRunName, suffix), namespace, tb.PipelineRunSpec( - getName(pipelineName, suffix), - tb.PipelineRunServiceAccount(fmt.Sprintf("%s%d", saName, suffix)), - )) + return tb.PipelineRun(getName(pipelineRunName, suffix), namespace, + tb.PipelineRunLabel("hello-world-key", "hello-world-value"), + tb.PipelineRunSpec(getName(pipelineName, suffix), + tb.PipelineRunServiceAccount(fmt.Sprintf("%s%d", saName, suffix)), + ), + ) } func getName(namespace string, suffix int) string {