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

Conversation

ljupchokotev
Copy link
Contributor

@ljupchokotev ljupchokotev commented Nov 24, 2020

Changes

Mapping a service account to a finally task in a PipelineRun's
serviceAccountNames field or taskRunSpecs would result in an error saying that the task doesn't
exist in the Pipeline.

This commit fixes the ValidateServiceaccountMapping and ValidateTasskRunSpecs functions by iterating over
the finally tasks in addition to the normal tasks of a Pipeline.

Also, added test cases for the bug.

/kind bug

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

  • Includes tests (if functionality changed/added)
  • Includes docs (if user facing)
  • Commit messages follow commit message best practices
  • Release notes block has been filled in or deleted (only if no user facing changes)

See the contribution guide for more details.

Double check this list of stuff that's easy to miss:

Reviewer Notes

If API changes are included, additive changes must be approved by at least two OWNERS and backwards incompatible changes must be approved by more than 50% of the OWNERS, and they must first be added in a backwards compatible way.

Release Notes

Fixes a bug where PipelineRun's serviceAccountNames and taskPodSpecs couldn't be applied on finally tasks and resulted in an error.

Mapping a service account to a finally task in a PipelineRun's
serviceAccountNames field would result in an error saying that the task doesn't
exist in the Pipeline.

This commit fixes the ValidateServiceaccountMapping function by iterating over
the finally tasks in addition to the normal tasks of a Pipeline.
@tekton-robot tekton-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. labels Nov 24, 2020
@tekton-robot tekton-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 24, 2020
@tekton-robot
Copy link
Collaborator

Hi @ljupchokotev. Thanks for your PR.

I'm waiting for a tektoncd member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@tekton-robot tekton-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Nov 24, 2020
@ljupchokotev
Copy link
Contributor Author

/assign afrittoli pritidesai

@pritidesai
Copy link
Member

/ok-to-test

@tekton-robot tekton-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Nov 24, 2020
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go 87.4% 88.0% 0.7

@pritidesai
Copy link
Member

pritidesai commented Nov 25, 2020

thanks @ljupchokotev for the fix.

/approve

Please note that the pipelineRun.Spec.ServiceAccountNames are deprecated and will be removed as early as May, 2021.

@vdemeester these changes are fixing finally task to use pipelineRun.Spec.ServiceAccountNames. It was not restricted at the time finally tasks were implemented but now since its deprecated, what is the procedure? Merge this now and drop the support while deprecating in May?

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 25, 2020
@pritidesai
Copy link
Member

or avoid fixing it? putting it on hold until we clarify

/hold

@tekton-robot tekton-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 25, 2020
@ljupchokotev
Copy link
Contributor Author

ljupchokotev commented Nov 25, 2020

@pritidesai just looked at the code and it seems that even by using taskRunSpecs you would have the same issue. See here, it doesn't iterate over the finally tasks which means you won't be able to set a service account for a finally task and fail with the same issue as what this PR tries to fix.

Should I fix that part and include it in this PR?

EDIT: I tested this using the following objects:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: echo
spec:
  steps:
    - name: echo
      image: busybox:latest
      script: |
        echo foo
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: echo
spec:
  tasks:
    - name: echo
      taskRef:
        name: echo
  finally:
    - name: echo-final
      taskRef:
        name: echo
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: echo-run
spec:
  pipelineRef:
    name: echo
  taskRunSpecs:
    - pipelineTaskName: echo-final
      taskServiceAccountName: tester

The PipelineRun failed with

PipelineRun tekton-pipelines/echo-run doesn't define taskRunSpecs correctly: PipelineRun's taskrunSpecs defined wrong taskName: "echo-final", does not exist in Pipeline

@vdemeester
Copy link
Member

@pritidesai just looked at the code and it seems that even by using taskRunSpecs you would have the same issue. See here, it doesn't iterate over the finally tasks which means you won't be able to set a service account for a finally task and fail with the same issue as what this PR tries to fix.

Should I fix that part and include it in this PR?

Definitely yes 😉

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.

Using TaskRunSpecs on finally tasks resulted in an error because the validation
function only checked the normal tasks.

This commit updates the validation function so that it iterates over the finally
tasks too fixing the issue. TaskRunSpecs can now be used to add a custom
TaskPodTemplate and TaskServiceAccountName to a finally task.
@tekton-robot tekton-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 25, 2020
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go 87.4% 91.9% 4.6

@ljupchokotev
Copy link
Contributor Author

@pritidesai just looked at the code and it seems that even by using taskRunSpecs you would have the same issue. See here, it doesn't iterate over the finally tasks which means you won't be able to set a service account for a finally task and fail with the same issue as what this PR tries to fix.
Should I fix that part and include it in this PR?

Definitely yes wink

Done, also edited the PR description to match the current state

@pritidesai
Copy link
Member

@vdemeester any more changes needed here?

@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: pritidesai, vdemeester

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [pritidesai,vdemeester]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@pritidesai
Copy link
Member

/hold cancel

@tekton-robot tekton-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 30, 2020
Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Dec 1, 2020
@tekton-robot tekton-robot merged commit 2931d52 into tektoncd:master Dec 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants