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

select default_secret for service account if there is more than one #281

Merged
merged 7 commits into from
Jan 31, 2019
Merged

select default_secret for service account if there is more than one #281

merged 7 commits into from
Jan 31, 2019

Conversation

pablo-ruth
Copy link
Contributor

Proposal to fix #94

If there is more than one secret created by default with a service account (ex: openshift), default to the first and override by a secret with "token" in the name if there is any.

@ghost ghost added the size/XS label Jan 21, 2019
@pablo-ruth pablo-ruth changed the title [WIP] select default_secret for service account if there is more than one select default_secret for service account if there is more than one Jan 21, 2019
@ghost ghost added the documentation label Jan 21, 2019
Copy link
Contributor

@pdecat pdecat left a comment

Choose a reason for hiding this comment

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

Maybe the matching string token could be replaced by something more specific like -token- and/or be configurable: https://github.com/terraform-providers/terraform-provider-kubernetes/pull/281/files#diff-375ac94c5bd4e304ddeb13226700ec77R111

Other than this, LGTM.

website/docs/r/service_account.html.markdown Outdated Show resolved Hide resolved
@pablo-ruth
Copy link
Contributor Author

I changed matching string too, it's cleaner.

Copy link
Member

@alexsomesan alexsomesan left a comment

Choose a reason for hiding this comment

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

Hi, thanks for taking a stab at this issue.

We do need a more robust way of determining this default secret. Please see my comment down below.

if len(diff) > 1 {
return fmt.Errorf("Expected 1 generated default secret, %d found: %s", len(diff), diff)
for _, secret := range diff {
if strings.Contains(secret.Name, "-token-") {
Copy link
Member

Choose a reason for hiding this comment

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

This approach is problematic because there is no API contract to guarantee that token secrets must have the -token- string will be part of the name for token secrets.

There is however a Type attribute on Secret objects (see here). You can filter the list of Secrets on this Type attribute for all objects with a value of kubernetes.io/service-account-token and that should give you a list of just the token type secrets. You'll most likely just get one during create, but I guess you can just pick the first one if you get more (very non-deterministic unfortunately).

Copy link
Contributor

Choose a reason for hiding this comment

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

Filtering on the secret type is indeed much cleaner!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the filtering on secret type. I think we can keep the previous logic and return an error if we have more than one service account token. The filtering applies even if there is only one secret, I'm not sure if it's what you want... And we possibly can move this part in the "resource.Retry" block if we want to wait for a service account token to appear ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@pablo-ruth I think adding the resource.Retry around it is a good idea.

However, I'm a bit concerned about doing .Get(..) calls for individual objects in a loop (the for...range around it) AND wrapping that in retries. It has the theoretical potential to cause a lot of requests in worst case conditions (and stress the API server unnecessarily).
The alternative would be to use a .Secrets(namespace).List(..) type of call OUTSIDE the loop and then filter it's results for the relevant secret name. We can that wrap that in a retry and not have it quadratically grow out of control.

What do you think? If you'd rather not add the retry, than I think we can merge like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok done, I moved it to the retry func and used List with filtering instead of Get calls.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

@ghost ghost added size/S and removed size/XS labels Jan 31, 2019
Copy link
Member

@alexsomesan alexsomesan left a comment

Choose a reason for hiding this comment

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

Acceptance tests all green.
LGTM

@alexsomesan alexsomesan merged commit cef8ec0 into hashicorp:master Jan 31, 2019
@pablo-ruth pablo-ruth deleted the sa_default_secret branch January 31, 2019 21:47
@ghost ghost locked and limited conversation to collaborators Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to create service_account on OpenShift 3.7 due to 2 default secrets
3 participants