Skip to content

Commit

Permalink
Propagate annotations from Conditions to TaskRuns/Pods
Browse files Browse the repository at this point in the history
  • Loading branch information
impl authored and tekton-robot committed May 13, 2020
1 parent 14a69fc commit 498534d
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This document defines `Conditions` and their capabilities.
- [Check](#check)
- [Parameters](#parameters)
- [Resources](#resources)
- [Labels](#labels)
- [Labels and Annotations](#labels-and-annotations)
- [Examples](#examples)

## Syntax
Expand Down Expand Up @@ -80,9 +80,9 @@ Resources in Conditions work similar to the way they work in `Tasks` i.e. they c
[variable substitution](./resources.md#variable-substitution) and the `targetPath` field can be used
to [control where the resource is mounted](./resources.md#controlling-where-resources-are-mounted)

## Labels
## Labels and Annotations

[Labels](labels.md) defined as part of the `Condition` metadata will be automatically propagated to the `Pod`.
[Labels](labels.md) and annotations defined as part of the `Condition` metadata will be automatically propagated to the `Pod`.

## Examples

Expand Down
12 changes: 12 additions & 0 deletions internal/builder/v1alpha1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ func ConditionLabels(labels map[string]string) ConditionOp {
}
}

// ConditionAnnotations sets the annotations on the condition.
func ConditionAnnotations(annotations map[string]string) ConditionOp {
return func(Condition *v1alpha1.Condition) {
if Condition.ObjectMeta.Annotations == nil {
Condition.ObjectMeta.Annotations = map[string]string{}
}
for key, value := range annotations {
Condition.ObjectMeta.Annotations[key] = value
}
}
}

// ConditionSpec creates a ConditionSpec with default values.
// Any number of ConditionSpec modifiers can be passed to transform it.
func ConditionSpec(ops ...ConditionSpecOp) ConditionOp {
Expand Down
9 changes: 9 additions & 0 deletions internal/builder/v1alpha1/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func TestCondition(t *testing.T) {
"label-1": "label-value-1",
"label-2": "label-value-2",
}),
tb.ConditionAnnotations(
map[string]string{
"annotation-1": "annotation-value-1",
"annotation-2": "annotation-value-2",
}),
tb.ConditionSpec(tb.ConditionSpecCheck("", "ubuntu", tb.Command("exit 0")),
tb.ConditionDescription("Test Condition"),
tb.ConditionParamSpec("param-1", v1alpha1.ParamTypeString,
Expand All @@ -53,6 +58,10 @@ func TestCondition(t *testing.T) {
"label-1": "label-value-1",
"label-2": "label-value-2",
},
Annotations: map[string]string{
"annotation-1": "annotation-value-1",
"annotation-2": "annotation-value-2",
},
},
Spec: v1alpha1.ConditionSpec{
Check: v1alpha1.Step{
Expand Down
12 changes: 12 additions & 0 deletions internal/builder/v1alpha1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,18 @@ func TaskRunLabel(key, value string) TaskRunOp {
}
}

// TaskRunAnnotations adds the specified annotations to the TaskRun.
func TaskRunAnnotations(annotations map[string]string) TaskRunOp {
return func(tr *v1alpha1.TaskRun) {
if tr.ObjectMeta.Annotations == nil {
tr.ObjectMeta.Annotations = map[string]string{}
}
for key, value := range annotations {
tr.ObjectMeta.Annotations[key] = value
}
}
}

// TaskRunAnnotation adds an annotation with the specified key and value to the TaskRun.
func TaskRunAnnotation(key, value string) TaskRunOp {
return func(tr *v1alpha1.TaskRun) {
Expand Down
6 changes: 5 additions & 1 deletion internal/builder/v1alpha1/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func TestTaskRunWithTaskRef(t *testing.T) {
),
tb.TaskRunLabels(map[string]string{"label-2": "label-value-2", "label-3": "label-value-3"}),
tb.TaskRunLabel("label", "label-value"),
tb.TaskRunAnnotations(map[string]string{"annotation-1": "annotation-value-1", "annotation-2": "annotation-value-2"}),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task-output",
tb.TaskRefKind(v1alpha1.ClusterTaskKind),
Expand Down Expand Up @@ -236,7 +237,10 @@ func TestTaskRunWithTaskRef(t *testing.T) {
"label-2": "label-value-2",
"label-3": "label-value-3",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"annotation-1": "annotation-value-1",
"annotation-2": "annotation-value-2",
},
},
Spec: v1alpha1.TaskRunSpec{
Inputs: &v1alpha1.TaskRunInputs{
Expand Down
12 changes: 12 additions & 0 deletions internal/builder/v1beta1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,18 @@ func TaskRunLabel(key, value string) TaskRunOp {
}
}

// TaskRunAnnotations adds the specified annotations to the TaskRun.
func TaskRunAnnotations(annotations map[string]string) TaskRunOp {
return func(tr *v1beta1.TaskRun) {
if tr.ObjectMeta.Annotations == nil {
tr.ObjectMeta.Annotations = map[string]string{}
}
for key, value := range annotations {
tr.ObjectMeta.Annotations[key] = value
}
}
}

// TaskRunAnnotation adds an annotation with the specified key and value to the TaskRun.
func TaskRunAnnotation(key, value string) TaskRunOp {
return func(tr *v1beta1.TaskRun) {
Expand Down
6 changes: 5 additions & 1 deletion internal/builder/v1beta1/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func TestTaskRunWithTaskRef(t *testing.T) {
),
tb.TaskRunLabels(map[string]string{"label-2": "label-value-2", "label-3": "label-value-3"}),
tb.TaskRunLabel("label", "label-value"),
tb.TaskRunAnnotations(map[string]string{"annotation-1": "annotation-value-1", "annotation-2": "annotation-value-2"}),
tb.TaskRunSpec(
tb.TaskRunTaskRef("task-output",
tb.TaskRefKind(v1beta1.ClusterTaskKind),
Expand Down Expand Up @@ -231,7 +232,10 @@ func TestTaskRunWithTaskRef(t *testing.T) {
"label-2": "label-value-2",
"label-3": "label-value-3",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"annotation-1": "annotation-value-1",
"annotation-2": "annotation-value-2",
},
},
Spec: v1beta1.TaskRunSpec{
Params: []v1beta1.Param{{
Expand Down
9 changes: 8 additions & 1 deletion pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,13 @@ func (c *Reconciler) makeConditionCheckContainer(rprt *resources.ResolvedPipelin
labels[key] = value
}

// Propagate annotations from PipelineRun to TaskRun.
annotations := getTaskrunAnnotations(pr)

for key, value := range rcc.Condition.ObjectMeta.Annotations {
annotations[key] = value
}

taskSpec, err := rcc.ConditionToTaskSpec()
if err != nil {
return nil, fmt.Errorf("failed to get TaskSpec from Condition: %w", err)
Expand All @@ -910,7 +917,7 @@ func (c *Reconciler) makeConditionCheckContainer(rprt *resources.ResolvedPipelin
Namespace: pr.Namespace,
OwnerReferences: []metav1.OwnerReference{pr.GetOwnerReference()},
Labels: labels,
Annotations: getTaskrunAnnotations(pr), // Propagate annotations from PipelineRun to TaskRun.
Annotations: annotations,
},
Spec: v1beta1.TaskRunSpec{
TaskSpec: taskSpec,
Expand Down
9 changes: 7 additions & 2 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,10 @@ func TestReconcileWithConditionChecks(t *testing.T) {
"label-1": "value-1",
"label-2": "value-2",
}),
tbv1alpha1.ConditionAnnotations(
map[string]string{
"annotation-1": "value-1",
}),
tbv1alpha1.ConditionSpec(
tbv1alpha1.ConditionSpecCheck("", "foo", tb.Args("bar")),
)),
Expand Down Expand Up @@ -1593,7 +1597,7 @@ func TestReconcileWithConditionChecks(t *testing.T) {
}
expectedConditionChecks := make([]*v1beta1.TaskRun, len(conditions))
for index, condition := range conditions {
expectedConditionChecks[index] = makeExpectedTr(condition.Name, ccNames[condition.Name], condition.Labels)
expectedConditionChecks[index] = makeExpectedTr(condition.Name, ccNames[condition.Name], condition.Labels, condition.Annotations)
}

// Check that the expected TaskRun was created
Expand Down Expand Up @@ -1729,7 +1733,7 @@ func TestReconcileWithFailingConditionChecks(t *testing.T) {
}
}

func makeExpectedTr(condName, ccName string, labels map[string]string) *v1beta1.TaskRun {
func makeExpectedTr(condName, ccName string, labels, annotations map[string]string) *v1beta1.TaskRun {
return tb.TaskRun(ccName,
tb.TaskRunNamespace("foo"),
tb.TaskRunOwnerReference("PipelineRun", "test-pipeline-run",
Expand All @@ -1743,6 +1747,7 @@ func makeExpectedTr(condName, ccName string, labels map[string]string) *v1beta1.
tb.TaskRunLabel(pipeline.GroupName+pipeline.ConditionNameKey, condName),
tb.TaskRunLabels(labels),
tb.TaskRunAnnotation("PipelineRunAnnotation", "PipelineRunValue"),
tb.TaskRunAnnotations(annotations),
tb.TaskRunSpec(
tb.TaskRunTaskSpec(
tb.Step("foo", tb.StepName("condition-check-"+condName), tb.StepArgs("bar")),
Expand Down

0 comments on commit 498534d

Please sign in to comment.