Skip to content

Commit

Permalink
Propagate TaskRunSpecs to created TaskRuns
Browse files Browse the repository at this point in the history
TaskRunSpec set in PipelineRun is never propagated to new TaskRuns in `createTaskRun()`, they are only propagated to Condition-pods in `makeConditionCheckContainer()`

This commit
- Adds propagation to new TaskRuns
- Also adds a unit test

Fixes #2682

/kind bug
  • Loading branch information
jlpettersson authored and tekton-robot committed Jun 2, 2020
1 parent 717fbc5 commit be9ac3b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
7 changes: 7 additions & 0 deletions internal/builder/v1beta1/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,13 @@ func PipelineRunServiceAccountNameTask(taskName, sa string) PipelineRunSpecOp {
}
}

// PipelineTaskRunSpec adds customs TaskRunSpecs
func PipelineTaskRunSpecs(taskRunSpecs []v1beta1.PipelineTaskRunSpec) PipelineRunSpecOp {
return func(prs *v1beta1.PipelineRunSpec) {
prs.TaskRunSpecs = taskRunSpecs
}
}

// PipelineRunParam add a param, with specified name and value, to the PipelineRunSpec.
func PipelineRunParam(name string, value string, additionalValues ...string) PipelineRunSpecOp {
arrayOrString := ArrayOrString(value, additionalValues...)
Expand Down
7 changes: 7 additions & 0 deletions internal/builder/v1beta1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,13 @@ func TaskResourceBindingPaths(paths ...string) TaskResourceBindingOp {
}
}

// TaskRunPodTemplate add a custom PodTemplate to the TaskRun
func TaskRunPodTemplate(podTemplate *v1beta1.PodTemplate) TaskRunSpecOp {
return func(spec *v1beta1.TaskRunSpec) {
spec.PodTemplate = podTemplate
}
}

// TaskRunWorkspaceEmptyDir adds a workspace binding to an empty dir volume source.
func TaskRunWorkspaceEmptyDir(name, subPath string) TaskRunSpecOp {
return func(spec *v1beta1.TaskRunSpec) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ func (c *Reconciler) createTaskRun(rprt *resources.ResolvedPipelineRunTask, pr *
return c.PipelineClientSet.TektonV1beta1().TaskRuns(pr.Namespace).UpdateStatus(tr)
}

serviceAccountName, podTemplate := pr.GetTaskRunSpecs(rprt.PipelineTask.Name)
tr = &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: rprt.TaskRunName,
Expand All @@ -755,9 +756,9 @@ func (c *Reconciler) createTaskRun(rprt *resources.ResolvedPipelineRunTask, pr *
},
Spec: v1beta1.TaskRunSpec{
Params: rprt.PipelineTask.Params,
ServiceAccountName: pr.GetServiceAccountName(rprt.PipelineTask.Name),
ServiceAccountName: serviceAccountName,
Timeout: getTaskRunTimeout(pr, rprt),
PodTemplate: pr.Spec.PodTemplate,
PodTemplate: podTemplate,
}}

if rprt.ResolvedTaskResources.TaskName != "" {
Expand Down Expand Up @@ -951,7 +952,7 @@ func (c *Reconciler) makeConditionCheckContainer(rprt *resources.ResolvedPipelin
if err != nil {
return nil, fmt.Errorf("failed to get TaskSpec from Condition: %w", err)
}
serviceAccountName, podtemplate := pr.GetTaskRunSpecs(rprt.PipelineTask.Name)
serviceAccountName, podTemplate := pr.GetTaskRunSpecs(rprt.PipelineTask.Name)
tr := &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: rcc.ConditionCheckName,
Expand All @@ -968,7 +969,7 @@ func (c *Reconciler) makeConditionCheckContainer(rprt *resources.ResolvedPipelin
Inputs: rcc.ToTaskResourceBindings(),
},
Timeout: getTaskRunTimeout(pr, rprt),
PodTemplate: podtemplate,
PodTemplate: podTemplate,
}}

cctr, err := c.PipelineClientSet.TektonV1beta1().TaskRuns(pr.Namespace).Create(tr)
Expand Down
78 changes: 78 additions & 0 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,84 @@ func TestGetTaskRunTimeout(t *testing.T) {
}
}

// TestReconcileAndPropagateCustomPipelineTaskRunSpec tests that custom PipelineTaskRunSpec declared
// in PipelineRun is propagated to created TaskRuns
func TestReconcileAndPropagateCustomPipelineTaskRunSpec(t *testing.T) {
names.TestingSeed()
prName := "test-pipeline-run"
ps := []*v1beta1.Pipeline{tb.Pipeline("test-pipeline", tb.PipelineNamespace("foo"), tb.PipelineSpec(
tb.PipelineTask("hello-world-1", "hello-world"),
))}
prs := []*v1beta1.PipelineRun{tb.PipelineRun(prName, tb.PipelineRunNamespace("foo"),
tb.PipelineRunAnnotation("PipelineRunAnnotation", "PipelineRunValue"),
tb.PipelineRunSpec("test-pipeline",
tb.PipelineRunServiceAccountName("test-sa"),
tb.PipelineTaskRunSpecs([]v1beta1.PipelineTaskRunSpec{{
PipelineTaskName: "hello-world-1",
TaskServiceAccountName: "custom-sa",
TaskPodTemplate: &v1beta1.PodTemplate{
NodeSelector: map[string]string{
"workloadtype": "tekton",
},
},
}}),
),
)}
ts := []*v1beta1.Task{tb.Task("hello-world", tb.TaskNamespace("foo"))}

d := test.Data{
PipelineRuns: prs,
Pipelines: ps,
Tasks: ts,
}

testAssets, cancel := getPipelineRunController(t, d)
defer cancel()
c := testAssets.Controller
clients := testAssets.Clients

err := c.Reconciler.Reconcile(context.Background(), "foo/"+prName)
if err != nil {
t.Errorf("Did not expect to see error when reconciling completed PipelineRun but saw %s", err)
}

// Check that the PipelineRun was reconciled correctly
_, err = clients.Pipeline.TektonV1beta1().PipelineRuns("foo").Get(prName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Somehow had error getting completed reconciled run out of fake client: %s", err)
}

// Check that the expected TaskRun was created
actual := clients.Pipeline.Actions()[1].(ktesting.CreateAction).GetObject().(*v1beta1.TaskRun)
if actual == nil {
t.Errorf("Expected a TaskRun to be created, but it wasn't.")
}
expectedTaskRun := tb.TaskRun("test-pipeline-run-hello-world-1-9l9zj",
tb.TaskRunNamespace("foo"),
tb.TaskRunOwnerReference("PipelineRun", "test-pipeline-run",
tb.OwnerReferenceAPIVersion("tekton.dev/v1beta1"),
tb.Controller, tb.BlockOwnerDeletion,
),
tb.TaskRunLabel(pipeline.GroupName+pipeline.PipelineLabelKey, "test-pipeline"),
tb.TaskRunLabel(pipeline.GroupName+pipeline.PipelineTaskLabelKey, "hello-world-1"),
tb.TaskRunLabel(pipeline.GroupName+pipeline.PipelineRunLabelKey, "test-pipeline-run"),
tb.TaskRunAnnotation("PipelineRunAnnotation", "PipelineRunValue"),
tb.TaskRunSpec(
tb.TaskRunTaskRef("hello-world"),
tb.TaskRunServiceAccountName("custom-sa"),
tb.TaskRunPodTemplate(&v1beta1.PodTemplate{
NodeSelector: map[string]string{
"workloadtype": "tekton",
},
}),
),
)

if d := cmp.Diff(actual, expectedTaskRun); d != "" {
t.Errorf("expected to see propagated custom ServiceAccountName and PodTemplate in TaskRun %v created. Diff %s", expectedTaskRun, diff.PrintWantGot(d))
}
}

func TestReconcileWithConditionChecks(t *testing.T) {
names.TestingSeed()
prName := "test-pipeline-run"
Expand Down

0 comments on commit be9ac3b

Please sign in to comment.