Skip to content

Commit

Permalink
Check labels in e3e PipelineRun tests and document label propagation
Browse files Browse the repository at this point in the history
The e2e tests in test/pipelinerun.go now assert that custom labels set
on the PipelineRun are propagated through to the TaskRun and Pod, and
that the static labels that are added to all TaskRuns and Pods are
propagated correctly as well.

In addition, documentation has been added to explain that labels are
propagated and to mention the specific labels that are added
automatically to generated TaskRuns and Pods.

Follow-up to tektoncd#488.
  • Loading branch information
dwnusbaum committed Feb 13, 2019
1 parent b52f7a9 commit 4a292d4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
20 changes: 20 additions & 0 deletions docs/pipelineruns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions docs/taskruns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 38 additions & 13 deletions test/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4a292d4

Please sign in to comment.