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

Fix PipelineRun serviceAccountNames for finally tasks #3560

Merged
merged 2 commits into from
Dec 1, 2020
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
8 changes: 8 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ func ValidateTaskRunSpecs(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) erro
pipelineTasks[task.Name] = task.Name
}

for _, task := range p.Finally {
pipelineTasks[task.Name] = task.Name
}

for _, taskrunSpec := range pr.Spec.TaskRunSpecs {
if _, ok := pipelineTasks[taskrunSpec.PipelineTaskName]; !ok {
return fmt.Errorf("PipelineRun's taskrunSpecs defined wrong taskName: %q, does not exist in Pipeline", taskrunSpec.PipelineTaskName)
Expand All @@ -282,6 +286,10 @@ func ValidateServiceaccountMapping(p *v1beta1.PipelineSpec, pr *v1beta1.Pipeline
pipelineTasks[task.Name] = task.Name
}

for _, task := range p.Finally {
pipelineTasks[task.Name] = task.Name
}

for _, name := range pr.Spec.ServiceAccountNames {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, this is a miss when we deprecated ServiceAccountNames 🤔 it shouldn't be looking here (or at least it should be sharing the same code as what the reconciler does)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still trying to understand this but it looks a bit weird so far 😕

Reconciler runs validation on pipelineRun.Spec.ServiceAccountNames and pipelineRun.Spec.taskRunSpecs, making sure the tasks specified in these sections exist under tasks and finally:

if err := resources.ValidateServiceaccountMapping(pipelineSpec, pr); err != nil {

and

if err := resources.ValidateTaskRunSpecs(pipelineSpec, pr); err != nil {

But, resolvePipelineState resolves task with pr.Spec.ServiceAccountName:

pipelineRunState, err := c.resolvePipelineState(ctx, tasks, pipelineMeta, pr, providedResources)

fn, _, err := tresources.GetTaskFunc(ctx, c.KubeClientSet, c.PipelineClientSet, task.TaskRef, pr.Namespace, pr.Spec.ServiceAccountName)

Only applies to bundle at this point, i.e. bundles does not honor pr.Spec.ServiceAccountNames or pr.Spec.TaskRunSpecs 🤔

ServiceAccountName: saName,

Looking further 🔍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, createTaskRun resolves service account name in expected order:

serviceAccountName, podTemplate := pr.GetTaskRunSpecs(rprt.PipelineTask.Name)

First, read serviceAccountName specified in pr.Spec.ServiceAccountNames:

func (pr *PipelineRun) GetServiceAccountName(pipelineTaskName string) string {

which get replaced with pr.Spec.TaskRunSpecs if specified:

if task.TaskServiceAccountName != "" {
serviceAccountName = task.TaskServiceAccountName

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it shouldn't be looking here pr.Spec.ServiceAccountNames (or at least it should be sharing the same code as what the reconciler does)

It's looking here to validate which is fine from the validation perspective, reconciler reads serviceAccountName, serviceAccountNames, and taskRunSpecs, and sets the service account name in expected order.

if _, ok := pipelineTasks[name.TaskName]; !ok {
return fmt.Errorf("PipelineRun's ServiceAccountNames defined wrong taskName: %q, does not exist in Pipeline", name.TaskName)
Expand Down
130 changes: 121 additions & 9 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1796,16 +1796,128 @@ func TestValidateWorkspaceBindingsWithInvalidWorkspaces(t *testing.T) {
}
}

func TestValidateTaskRunSpecs(t *testing.T) {
for _, tc := range []struct {
name string
p *v1beta1.Pipeline
run *v1beta1.PipelineRun
wantErr bool
}{{
name: "valid task mapping",
p: tb.Pipeline("pipelines", tb.PipelineSpec(
tb.PipelineTask("mytask1", "task",
tb.PipelineTaskInputResource("input1", "git-resource")),
)),
run: tb.PipelineRun("pipelinerun", tb.PipelineRunSpec("pipeline",
tb.PipelineTaskRunSpecs(
[]v1beta1.PipelineTaskRunSpec{{
PipelineTaskName: "mytask1",
TaskServiceAccountName: "default",
}},
),
)),
wantErr: false,
}, {
name: "valid finally task mapping",
p: tb.Pipeline("pipelines", tb.PipelineSpec(
tb.PipelineTask("mytask1", "task",
tb.PipelineTaskInputResource("input1", "git-resource")),
tb.FinalPipelineTask("myfinaltask1", "finaltask"),
)),
run: tb.PipelineRun("pipelinerun", tb.PipelineRunSpec("pipeline",
tb.PipelineTaskRunSpecs(
[]v1beta1.PipelineTaskRunSpec{{
PipelineTaskName: "myfinaltask1",
TaskServiceAccountName: "default",
}},
),
)),
wantErr: false,
}, {
name: "invalid task mapping",
p: tb.Pipeline("pipelines", tb.PipelineSpec(
tb.PipelineTask("mytask1", "task",
tb.PipelineTaskInputResource("input1", "git-resource")),
tb.FinalPipelineTask("myfinaltask1", "finaltask"),
)),
run: tb.PipelineRun("pipelinerun", tb.PipelineRunSpec("pipeline",
tb.PipelineTaskRunSpecs(
[]v1beta1.PipelineTaskRunSpec{{
PipelineTaskName: "wrongtask",
TaskServiceAccountName: "default",
}},
),
)),
wantErr: true,
}} {
t.Run(tc.name, func(t *testing.T) {
spec := tc.p.Spec
err := ValidateTaskRunSpecs(&spec, tc.run)
if tc.wantErr {
if err == nil {
t.Fatalf("Did not get error when it was expected for test: %s", tc.name)
}
} else {
if err != nil {
t.Fatalf("Unexpected error when no error expected: %v", err)
}
}
})
}
}

func TestValidateServiceaccountMapping(t *testing.T) {
p := tb.Pipeline("pipelines", tb.PipelineSpec(
tb.PipelineTask("mytask1", "task",
tb.PipelineTaskInputResource("input1", "git-resource")),
))
pr := tb.PipelineRun("pipelinerun", tb.PipelineRunSpec("pipeline",
tb.PipelineRunServiceAccountNameTask("mytaskwrong", "default"),
))
if err := ValidateServiceaccountMapping(&p.Spec, pr); err == nil {
t.Fatalf("Expected error indicating `mytaskwrong` was not defined as `task` in Pipeline but got no error")
for _, tc := range []struct {
name string
p *v1beta1.Pipeline
run *v1beta1.PipelineRun
wantErr bool
}{{
name: "valid task mapping",
p: tb.Pipeline("pipelines", tb.PipelineSpec(
tb.PipelineTask("mytask1", "task",
tb.PipelineTaskInputResource("input1", "git-resource")),
)),
run: tb.PipelineRun("pipelinerun", tb.PipelineRunSpec("pipeline",
tb.PipelineRunServiceAccountNameTask("mytask1", "default"),
)),
wantErr: false,
}, {
name: "valid finally task mapping",
p: tb.Pipeline("pipelines", tb.PipelineSpec(
tb.PipelineTask("mytask1", "task",
tb.PipelineTaskInputResource("input1", "git-resource")),
tb.FinalPipelineTask("myfinaltask1", "finaltask"),
)),
run: tb.PipelineRun("pipelinerun", tb.PipelineRunSpec("pipeline",
tb.PipelineRunServiceAccountNameTask("myfinaltask1", "default"),
)),
wantErr: false,
}, {
name: "invalid task mapping",
p: tb.Pipeline("pipelines", tb.PipelineSpec(
tb.PipelineTask("mytask1", "task",
tb.PipelineTaskInputResource("input1", "git-resource")),
tb.FinalPipelineTask("myfinaltask1", "finaltask"),
)),
run: tb.PipelineRun("pipelinerun", tb.PipelineRunSpec("pipeline",
tb.PipelineRunServiceAccountNameTask("wrongtask", "default"),
)),
wantErr: true,
}} {
t.Run(tc.name, func(t *testing.T) {
spec := tc.p.Spec
err := ValidateServiceaccountMapping(&spec, tc.run)
if tc.wantErr {
if err == nil {
t.Fatalf("Did not get error when it was expected for test: %s", tc.name)
}
} else {
if err != nil {
t.Fatalf("Unexpected error when no error expected: %v", err)
}
}
})
}
}

Expand Down