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

Provide a way to specify default service accounts #1227

Merged
merged 1 commit into from
Oct 2, 2019

Conversation

sthaha
Copy link
Member

@sthaha sthaha commented Aug 21, 2019

This PR allows defaults service-account (configurable though the configmap)
to be set when none is specified in a PipelineRun.

Fixes: #1213
Signed-off-by: Sunil Thaha sthaha@redhat.com

Changes

Submitter Checklist

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

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

If pipeline controllers are deployed with a config-map that has `default-service-account` key set to a non-empty string, pipeline-runs that do not specify a ServiceAccount will be modified (mutated) to  the value of the `default-service-account`. 

@tekton-robot tekton-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 21, 2019
@googlebot googlebot added the cla: yes Trying to make the CLA bot happy with ppl from different companies work on one commit label Aug 21, 2019
@tekton-robot tekton-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Aug 21, 2019
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/config/default.go 77.8% 81.8% 4.0

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.

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 21, 2019
@sthaha
Copy link
Member Author

sthaha commented Aug 21, 2019

@vdemeester @bobcatfish does it make sense to implement the same for taskruns as well?

@sthaha sthaha changed the title WIP: Provide a way to specify default service accounts Provide a way to specify default service accounts Aug 21, 2019
@tekton-robot tekton-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 21, 2019
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/config/default.go 77.8% 81.8% 4.0

@vdemeester
Copy link
Member

@vdemeester @bobcatfish does it make sense to implement the same for taskruns as well?

I would think so yes

Copy link
Collaborator

@bobcatfish bobcatfish left a comment

Choose a reason for hiding this comment

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

Thanks for adding this @sthaha and thanks for your patience! 🙏 A few requests:

Before this change, if there was no serviceAccount or serviceAccounts specified in a PipelineRun or a TaskRun, I could be reasonably sure that the service account default was used, but that is no longer completely obvious.. And if the configuration of the controller was changed, I wouldn't be able to tell from looking at a previous Run which service account was used. I wonder if it would make sense to include this in the status field? I'm not 100% sure this is a good idea tho - maybe we can open a follow up issue and discuss? Or wait to see if ppl actually care (curious what you think @imjasonh @vdemeester )

(The coverage reporting seems a bit odd, opened #1248)

@@ -30,17 +30,20 @@ const (
DefaultTimeoutMinutes = 60
NoTimeoutDuration = 0 * time.Minute
defaultTimeoutMinutesKey = "default-timeout-minutes"
defaultServiceAccountKey = "default-service-account"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a bit confused why in config/config-defaults.yaml the default service account is default, but if that file isn't use, the default will be default-service-account - would it make sense to use default in both cases?

Copy link
Member

Choose a reason for hiding this comment

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

@bobcatfish default-service-account is the "config" key to get the value for the default-service-account

Copy link
Member Author

Choose a reason for hiding this comment

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

@bobcatfish like @vdemeester mentioned, defaultServiceAccountKey (default-service-account) refers to the key part of the configmap e.g.

data:
  default-service-account: tekton

which if unspecified would spawn the pods with the default ServiceAccount.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah whoops, thanks for the explanation!

@sthaha
Copy link
Member Author

sthaha commented Aug 28, 2019

👍 will do

Nice! didn't think of this test 🤗

👍

Before this change, if there was no serviceAccount or serviceAccounts specified in a PipelineRun or a TaskRun, I could be reasonably sure that the service account default was used, but that is no longer completely obvious.. And if the configuration of the controller was changed, I wouldn't be able to tell from looking at a previous Run which service account was used. I wonder if it would make sense to include this in the status field? I'm not 100% sure this is a good idea tho - maybe we can open a follow up issue and discuss? Or wait to see if ppl actually care (curious what you think @imjasonh @vdemeester )

I agree that this can be taken up as a follow up issue. On a related note, would metadata.annotations.run be a good place to store this?

@bobcatfish
Copy link
Collaborator

On a related note, would metadata.annotations.run be a good place to store this?

I'm not sure! I feel like the status field is where we have been storing most information like this up until this point. I know there is a lot of precedent for using the metadata for this kind of thing. In this case I don't have a strong opinion either way!

@vdemeester vdemeester added this to the Pipelines 0.7 🐱 milestone Sep 16, 2019
@sthaha
Copy link
Member Author

sthaha commented Sep 18, 2019

@bobcatfish

Before this change, if there was no serviceAccount or serviceAccounts specified in a PipelineRun or a TaskRun, I could be reasonably sure that the service account default was used, but that is no longer completely obvious.. And if the configuration of the controller was changed, I wouldn't be able to tell from looking at a previous Run which service account was used. I wonder if it would make sense to include this in the status field? I'm not 100% sure this is a good idea tho - maybe we can open a follow up issue and discuss? Or wait to see if ppl actually care (curious what you think @imjasonh @vdemeester )

IIUC, the serviceAccount set by the mutating webhook can still be seen when you inspect the pipelinerun or the taskrun

@sthaha
Copy link
Member Author

sthaha commented Sep 18, 2019

@bobcatfish

Can we add a test that ensures that the serviceAccount and serviceAccounts fields will override the configured default? (https://github.com/tektoncd/pipeline/blob/master/docs/pipelineruns.md#service-accounts) Looking at the way you implemented it, I bet it will work, but just to be sure :) (could be a reconciler test, or an end to end test, or even an example)

This requires the test to

  • setup: create an SA account say foobar
  • setup: run webhook with the config-defaults.default-service-account set to foobar
  • test: run a pipelinerun with the serviceAccount explicitly set to something-else

I was looking for a precedence (may be in the case of default-timeout-minutes) but can't find any. Do you have any pointers?

@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 Sep 18, 2019
@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/config/default.go 77.8% 81.8% 4.0

@bobcatfish
Copy link
Collaborator

Hey @sthaha ! probably the easiest way would be to add a reconciler level test.

For example this test case is making sure the right service account is used:

name: "serviceaccount",
taskRun: taskRunWithSaSuccess,
wantPod: tb.Pod("test-taskrun-with-sa-run-success-pod-123456", "foo",
tb.PodAnnotation("tekton.dev/ready", ""),
tb.PodLabel(taskNameLabelKey, "test-with-sa"),
tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-sa-run-success"),
tb.PodLabel(resources.ManagedByLabelKey, resources.ManagedByLabelValue),
tb.PodOwnerReference("TaskRun", "test-taskrun-with-sa-run-success",
tb.OwnerReferenceAPIVersion(currentApiVersion)),
tb.PodSpec(
tb.PodServiceAccountName("test-sa"),
tb.PodVolumes(toolsVolume, downward, workspaceVolume, homeVolume),
tb.PodRestartPolicy(corev1.RestartPolicyNever),
getCredentialsInitContainer("9l9zj"),
getPlaceToolsInitContainer(),
tb.PodContainer("step-sa-step", "foo",
tb.Command(entrypointLocation),
tb.Args("-wait_file", "/builder/downward/ready", "-post_file", "/builder/tools/0", "-wait_file_content", "-entrypoint", "/mycmd", "--"),
tb.WorkingDir(workspaceDir),
tb.EnvVar("HOME", "/builder/home"),
tb.VolumeMount("tools", "/builder/tools"),
tb.VolumeMount("downward", "/builder/downward"),
tb.VolumeMount("workspace", workspaceDir),
tb.VolumeMount("home", "/builder/home"),
tb.Resources(tb.Requests(
tb.CPU("0"),
tb.Memory("0"),
tb.EphemeralStorage("0"),
)),
),
),
),

saName := tc.taskRun.Spec.ServiceAccount
if saName == "" {
saName = "default"
}
if _, err := clients.Kube.CoreV1().ServiceAccounts(tc.taskRun.Namespace).Create(&corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: saName,
Namespace: tc.taskRun.Namespace,
},
}); err != nil {
t.Fatal(err)
}

It looks like the reconciler that this test instantiates passes a config map watcher into the reconciler, so i think if you use those fake kube clients + configure a config map, the test reconciler would use it:

func getTaskRunController(t *testing.T, d test.Data) (test.TestAssets, func()) {
ctx, _ := rtesting.SetupFakeContext(t)
ctx, cancel := context.WithCancel(ctx)
cloudEventClientBehaviour := cloudevent.FakeClientBehaviour{
SendSuccessfully: true,
}
ctx = cloudevent.WithClient(ctx, &cloudEventClientBehaviour)
entrypointCache, _ = entrypoint.NewCache()
c, _ := test.SeedTestData(t, ctx, d)
configMapWatcher := configmap.NewInformedWatcher(c.Kube, system.GetNamespace())
return test.TestAssets{
Controller: NewController(
ctx,
configMapWatcher,
),
Clients: c,
}, cancel
}

So what I would do is probably make a completely new test function, copying a lot of the body of the Run function from the test I linked above. (Sorry writing reconciler tests is kind of tedious!!)

@vdemeester vdemeester self-assigned this Sep 23, 2019
This PR allows defaults service-account (configurable though the configmap)
to be set  when none is specified in a PipelineRun.

Fixes: tektoncd#1213
Signed-off-by: Sunil Thaha <sthaha@redhat.com>
}

defaultSAName := "pipelines"
defaultCfg := &config.Config{
Copy link
Member Author

Choose a reason for hiding this comment

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

@bobcatfish @vdemeester Does this suffice? Has 2 tests

  1. verifies that default service account specified in defaults is used
  2. verifies that if SA is set ( test-sa ) then that will be used

Copy link
Member

Choose a reason for hiding this comment

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

yes 😻 We may want to refactor some of this, but I think that's the job of a follow-up

@tekton-robot
Copy link
Collaborator

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

File Old Coverage New Coverage Delta
pkg/apis/config/default.go 77.8% 81.8% 4.0

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
/hold
^^ to let @bobcatfish review 👼

}

defaultSAName := "pipelines"
defaultCfg := &config.Config{
Copy link
Member

Choose a reason for hiding this comment

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

yes 😻 We may want to refactor some of this, but I think that's the job of a follow-up

@tekton-robot tekton-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm Indicates that a PR is ready to be merged. labels Sep 27, 2019
@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sthaha, 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:

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

@vdemeester
Copy link
Member

/hold cancel

This pull request was closed.
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. cla: yes Trying to make the CLA bot happy with ppl from different companies work on one commit lgtm Indicates that a PR is ready to be merged. 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.

Provide a way to specify default service accounts
6 participants