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 tektoncd#69
  • Loading branch information
tannerb authored and knative-prow-robot committed Nov 1, 2018
1 parent c0a4731 commit e73d3af
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 11 deletions.
39 changes: 34 additions & 5 deletions docs/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [How do I make Resources?](#creating-resources)
* [How do I run a Pipeline?](#running-a-task)
* [How do I run a Task on its own?](#running-a-task)
* [How do I troubleshoot a PipelineRun?](#troubleshooting)

## Creating a Pipeline

Expand Down Expand Up @@ -127,12 +128,15 @@ See [the example TaskRun](../examples/invocations/run-kritis-test.yaml).

### Git Resource

Git resource represents a [git](https://git-scm.com/) repository, that containes the source code to be built by the pipeline. Adding the git resource as an input to a task will clone this repository and allow the task to perform the required actions on the contents of the repo.
Git resource represents a [git](https://git-scm.com/) repository, that containes
the source code to be built by the pipeline. Adding the git resource as an input
to a task will clone this repository and allow the task to perform the required
actions on the contents of the repo.

Use the following example to understand the syntax and strucutre of a Git Resource

1. Create a git resource using the `PipelineResource` CRD

```
apiVersion: pipeline.knative.dev/v1alpha1
kind: PipelineResource
Expand All @@ -150,7 +154,7 @@ Use the following example to understand the syntax and strucutre of a Git Resour
Params that can be added are the following:
1. URL: represents the location of the git repository
1. URL: represents the location of the git repository
1. Revision: Git [revision](https://git-scm.com/docs/gitrevisions#_specifying_revisions ) (branch, tag, commit SHA or ref) to clone. If no revision is specified, the resource will default to `latest` from `master`

2. Use the defined git resource in a `Task` definition:
Expand Down Expand Up @@ -178,7 +182,7 @@ Use the following example to understand the syntax and strucutre of a Git Resour
image: gcr.io/my-repo/my-imageg
args:
- --repo=${inputs.resources.wizzbang-git.url}
```
```

3. And finally set the version in the `TaskRun` definition:

Expand All @@ -202,4 +206,29 @@ Use the following example to understand the syntax and strucutre of a Git Resour
type: image
# Optional, indicate a serviceAccount for authentication.
serviceAccount: build-bot
```
```

## Troubleshooting

All objects created by the build-pipeline controller show the lineage of where
that object came from through labels, all the way down to the individual build.

There are a common set of labels that are set on objects. For `TaskRun` objects,
it will receive two labels:

* `pipeline.knative.dev/pipeline`, which will be set to the name of the owning pipeline
* `pipeline.knative.dev/pipelineRun`, which will be set to the name of the PipelineRun

When the underlying `Build` is created, it will receive each of the `pipeline`
and `pipelineRun` labels, as well as `pipeline.knative.dev/taskRun` which will
contain the `TaskRun` which caused the `Build` to be created.

In the end, this allows you to easily find the `Builds` and `TaskRuns` that are
associated with a given pipeline.

For example, to find all `Builds` created by a `Pipeline` named "build-image",
you could use the following command:

```shell
kubectl get builds --all-namespaces -l pipeline.knative.dev/pipeline=build-image
```
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 @@ -174,6 +174,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 @@ -340,13 +341,23 @@ func (c *Reconciler) createBuild(tr *v1alpha1.TaskRun, pvcName string) (*buildv1
return nil, fmt.Errorf("couldnt apply output resource templating: %s", 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] = s.Name
for k, v := range s.ObjectMeta.Labels {
labels[k] = v
}
Expand Down
16 changes: 13 additions & 3 deletions test/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 @@ -169,9 +170,18 @@ func TestPipelineRun(t *testing.T) {
r, err := c.TaskRunClient.Get(taskRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected TaskRun %s: %s", taskRunName, err)
} else {
if !r.Status.GetCondition(duckv1alpha1.ConditionSucceeded).IsTrue() {
t.Fatalf("Expected TaskRun %s to have succeeded but Status is %v", taskRunName, r.Status)
}
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{
hwPipelineRunName: pipeline.PipelineRunLabelKey,
hwPipelineName: 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)
}
}
}
Expand Down

0 comments on commit e73d3af

Please sign in to comment.