From e73d3afc0f775b4e8485f7a8a8a81e7c528ff5f7 Mon Sep 17 00:00:00 2001 From: Tanner Bruce Date: Mon, 29 Oct 2018 01:24:52 -0500 Subject: [PATCH] Add labels for tracking objects lineage 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 --- docs/using.md | 39 ++++++++++++++++--- pkg/apis/pipeline/register.go | 8 +++- .../v1alpha1/pipelinerun/pipelinerun.go | 5 +++ .../v1alpha1/pipelinerun/pipelinerun_test.go | 4 ++ pkg/reconciler/v1alpha1/taskrun/taskrun.go | 15 ++++++- test/pipelinerun_test.go | 16 ++++++-- 6 files changed, 76 insertions(+), 11 deletions(-) diff --git a/docs/using.md b/docs/using.md index d5767386379..4923f9f4476 100644 --- a/docs/using.md +++ b/docs/using.md @@ -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 @@ -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 @@ -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: @@ -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: @@ -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 - ``` \ No newline at end of file + ``` + +## 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 +``` \ No newline at end of file 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 7354ecdc13e..26d85387736 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 1a3a353a7cb..97ee9094023 100644 --- a/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go +++ b/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go @@ -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", diff --git a/pkg/reconciler/v1alpha1/taskrun/taskrun.go b/pkg/reconciler/v1alpha1/taskrun/taskrun.go index 51fe861f2d2..f41cc3d5a0d 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 ) @@ -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 } diff --git a/test/pipelinerun_test.go b/test/pipelinerun_test.go index 2a8f53555da..b87450a2323 100644 --- a/test/pipelinerun_test.go +++ b/test/pipelinerun_test.go @@ -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" @@ -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) } } }