Skip to content

Commit

Permalink
Add labels for tracking objects lineage
Browse files Browse the repository at this point in the history
Adds labels to TaskRuns created by PipelineRuns as well as Builds that
are created. In TaskRuns and Builds will have labels set telling which
Pipeline, PipelineRun and TaskRun are the owners of it.

This is useful when doing filtering in searches.

Fixes #69
  • Loading branch information
Tanner Bruce committed Oct 29, 2018
1 parent 8325e78 commit e15669f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
8 changes: 7 additions & 1 deletion pkg/apis/pipeline/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

5 changes: 5 additions & 0 deletions pkg/reconciler/v1alpha1/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 13 additions & 2 deletions pkg/reconciler/v1alpha1/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 14 additions & 5 deletions test/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
}

Expand Down

0 comments on commit e15669f

Please sign in to comment.