Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate TaskRunSpecs to created TaskRuns #2683

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -723,6 +723,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 @@ -733,9 +734,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 @@ -921,7 +922,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 @@ -938,7 +939,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 @@ -1530,6 +1530,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