diff --git a/pkg/apis/pipeline/register.go b/pkg/apis/pipeline/register.go index 2f7dc2f2f64..c8010eeb0b1 100644 --- a/pkg/apis/pipeline/register.go +++ b/pkg/apis/pipeline/register.go @@ -17,4 +17,10 @@ limitations under the License. package pipeline // GroupName is the Kubernetes resource group name for Pipeline types. -const GroupName = "pipeline.knative.dev" +const ( + GroupName = "pipeline.knative.dev" + TaskRunLabelKey = "/taskRun" + PipelineLabelKey = "/pipeline" + PipelineRunLabelKey = "/pipelineRun" +) + diff --git a/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun.go b/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun.go index 7ac4c14b99a..e4bbcceff81 100644 --- a/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun.go +++ b/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun.go @@ -22,6 +22,7 @@ import ( "reflect" "time" + "github.com/knative/build-pipeline/pkg/apis/pipeline" "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" "github.com/knative/build-pipeline/pkg/reconciler" "github.com/knative/build-pipeline/pkg/reconciler/v1alpha1/pipelinerun/resources" @@ -225,6 +226,10 @@ func (c *Reconciler) createTaskRun(t *v1alpha1.Task, trName string, pr *v1alpha1 OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(pr, groupVersionKind), }, + Labels: map[string]string{ + pipeline.GroupName + pipeline.PipelineLabelKey: pr.Spec.PipelineRef.Name, + pipeline.GroupName + pipeline.PipelineRunLabelKey: pr.Name, + }, }, Spec: v1alpha1.TaskRunSpec{ TaskRef: v1alpha1.TaskRef{ diff --git a/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go b/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go index c0ad4cfec5f..2f225d14864 100644 --- a/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go +++ b/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go @@ -158,6 +158,10 @@ func TestReconcile(t *testing.T) { Controller: &trueB, BlockOwnerDeletion: &trueB, }}, + Labels: map[string]string{ + "pipeline.knative.dev/pipeline": "test-pipeline", + "pipeline.knative.dev/pipelineRun": "test-pipeline-run-success", + }, }, Spec: v1alpha1.TaskRunSpec{ ServiceAccount: "test-sa", diff --git a/pkg/reconciler/v1alpha1/taskrun/taskrun.go b/pkg/reconciler/v1alpha1/taskrun/taskrun.go index a56479f6894..537b455a7fe 100644 --- a/pkg/reconciler/v1alpha1/taskrun/taskrun.go +++ b/pkg/reconciler/v1alpha1/taskrun/taskrun.go @@ -21,6 +21,8 @@ import ( "fmt" "reflect" + "github.com/knative/build-pipeline/pkg/apis/pipeline" + "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" "github.com/knative/build-pipeline/pkg/reconciler" "github.com/knative/build-pipeline/pkg/reconciler/v1alpha1/taskrun/resources" @@ -55,7 +57,6 @@ const ( taskRunAgentName = "taskrun-controller" // taskRunControllerName defines name for TaskRun Controller taskRunControllerName = "TaskRun" - taskRunNameLabelKey = "taskrun.knative.dev/taskName" pvcSizeBytes = 5 * 1024 * 1024 * 1024 // 5 GBs ) @@ -337,13 +338,23 @@ func (c *Reconciler) createBuild(tr *v1alpha1.TaskRun, pvcName string) (*buildv1 return nil, err } + build.Labels[pipeline.GroupName+pipeline.TaskRunLabelKey] = tr.Name + // Only propagate the pipeline and pipelinerun keys if they are there. If + // the TaskRun was created via PipelineRun these should be there. + if val, ok := tr.Labels[pipeline.PipelineLabelKey]; ok { + build.Labels[pipeline.GroupName+pipeline.PipelineLabelKey] = val + } + if val, ok := tr.Labels[pipeline.PipelineRunLabelKey]; ok { + build.Labels[pipeline.GroupName+pipeline.PipelineRunLabelKey] = val + } + return c.BuildClientSet.BuildV1alpha1().Builds(tr.Namespace).Create(build) } // makeLabels constructs the labels we will apply to TaskRun resources. func makeLabels(s *v1alpha1.TaskRun) map[string]string { labels := make(map[string]string, len(s.ObjectMeta.Labels)+1) - labels[taskRunNameLabelKey] = s.Name + labels[pipeline.GroupName+pipeline.TaskRunLabelKey] = pipeline.TaskRunLabelKey for k, v := range s.ObjectMeta.Labels { labels[k] = v } diff --git a/test/pipelinerun_test.go b/test/pipelinerun_test.go index c422b17d241..736e109d06d 100644 --- a/test/pipelinerun_test.go +++ b/test/pipelinerun_test.go @@ -24,6 +24,7 @@ import ( "testing" "time" + "github.com/knative/build-pipeline/pkg/apis/pipeline" buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" knativetest "github.com/knative/pkg/test" @@ -78,11 +79,19 @@ func TestPipelineRun(t *testing.T) { r, err := c.TaskRunClient.Get(runName, metav1.GetOptions{}) if err != nil { t.Errorf("Couldn't get expected TaskRun %s: %s", runName, err) - } else { - c := r.Status.GetCondition(duckv1alpha1.ConditionSucceeded) - if c.Status != corev1.ConditionTrue { - t.Errorf("Expected TaskRun %s to have succeeded but Status is %s", runName, c.Status) - } + continue + } + + c := r.Status.GetCondition(duckv1alpha1.ConditionSucceeded) + if c.Status != corev1.ConditionTrue { + t.Errorf("Expected TaskRun %s to have succeeded but Status is %s", runName, c.Status) + } + + if r.Labels[pipeline.GroupName+pipeline.PipelineRunLabelKey] != hwPipelineRunName { + t.Errorf("Expected Taskrun to have PipelineRun label set") + } + if r.Labels[pipeline.GroupName+pipeline.PipelineLabelKey] != hwPipelineName { + t.Errorf("Expected Taskrun to have Pipeline label set") } }