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

Create secrets for serviceaccounts #125

Merged
merged 4 commits into from
Jun 30, 2022
Merged

Conversation

vthapar
Copy link
Contributor

@vthapar vthapar commented Jun 23, 2022

K8s 1.24+ doesn't autocreate secret for serviceaccount. This
breaks submariner deployments which expects secret to be created
and associated to the corresponding serviceaccount.

The fix ensures the Secret of type service-account-token exists,
is annotated with ServiceAccount.Name, and is added to Secrets list in
the ServiceAccount.

Fixes #102

Signed-off-by: Vishal Thapar [email protected]

@submariner-bot
Copy link
Contributor

🤖 Created branch: z_pr125/vthapar/secret-tokens
🚀 Full E2E won't run until the "ready-to-test" label is applied. I will add it automatically once the PR has 2 approvals, or you can add it manually.

@vthapar vthapar changed the title [WiP] Do not merge yet Create secrets for serviceaccounts Jun 24, 2022
@vthapar vthapar requested a review from tpantelis June 24, 2022 13:30
@vthapar vthapar marked this pull request as ready for review June 24, 2022 13:32
@vthapar vthapar dismissed tpantelis’s stale review June 24, 2022 13:33

Reworked the code, addressed comments

@vthapar
Copy link
Contributor Author

vthapar commented Jun 24, 2022

@skitt @tpantelis This is ready for review. I have only tweaked the UTs just enough to pass so that we can get this in and unblock submariner-io/shipyard#825 I can add UTs in a subsequent PR, unless you think UTs need to come in this patch itself.

@vthapar vthapar requested review from skitt and tpantelis June 25, 2022 15:49
@nyechiel
Copy link
Member

@vthapar is this ready for review? It looks like two CI jobs are failing

@vthapar
Copy link
Contributor Author

vthapar commented Jun 27, 2022

@vthapar is this ready for review? It looks like two CI jobs are failing

Upgrade job is the only one that failed more than once, e2e has failed just once. Re-ran the jobs to see if e2e fails again. Looking into upgrade job issue.

@vthapar vthapar force-pushed the secret-tokens branch 3 times, most recently from 543b847 to 678c374 Compare June 28, 2022 06:03
@submariner-bot submariner-bot added the ready-to-test When a PR is ready for full E2E testing label Jun 29, 2022
@submariner-bot
Copy link
Contributor

🤖 I had an issue pushing the updated branch: already up-to-date

@nyechiel
Copy link
Member

@skitt @sridhargaddam @tpantelis this one needs review

return nil, err
}

_, err = serviceaccount.EnsureSecretFromSA(kubeClient, sa.Name, inNamespace)
Copy link
Member

Choose a reason for hiding this comment

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

My previous comment still applies, unless I’ve missed something:

I would prefer the secret handling to be hidden in the SA handling — callers shouldn’t have to know that to set up a service account correctly, they have to set up a secret.

In other words, there should only be a call to serviceaccount.Ensure() in this function, and serviceaccount.Ensure() should handle the secret (perhaps by calling EnsureSecretFromSA(), of course, but that should be transparent to the caller).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My previous comment still applies, unless I’ve missed something:

I would prefer the secret handling to be hidden in the SA handling — callers shouldn’t have to know that to set up a service account correctly, they have to set up a secret.

In other words, there should only be a call to serviceaccount.Ensure() in this function, and serviceaccount.Ensure() should handle the secret (perhaps by calling EnsureSecretFromSA(), of course, but that should be transparent to the caller).

Got it. I missed the context of comment for Ensure and only addressed for EnsureFromYaml.

@nyechiel
Copy link
Member

@vthapar can you please address Stephen's comment?

@vthapar
Copy link
Contributor Author

vthapar commented Jun 29, 2022

@vthapar can you please address Stephen's comment?

Done.

@nyechiel nyechiel requested a review from skitt June 29, 2022 16:51
Comment on lines 68 to 70
if err == nil {
return true, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should distinguish NotFound error

Suggested change
if err == nil {
return true, nil
}
if apierrors.IsNotFound(err)
return nil
}
if err != nil {
return err
}

@vthapar vthapar force-pushed the secret-tokens branch 3 times, most recently from f152db6 to 3b61f57 Compare June 29, 2022 18:22
@submariner-bot
Copy link
Contributor

🤖 I had an issue pushing the updated branch: already up-to-date

@vthapar vthapar requested a review from tpantelis June 29, 2022 18:26
vthapar added 2 commits June 30, 2022 10:27
K8s 1.24+ doesn't autocreate secret for serviceaccount. This
breaks submariner deployments which expects secret to be created
and associated to the corresponding serviceaccount.

The fix ensures the Secret of type service-account-token exists,
is annotated with ServiceAccount.Name, and is added to Secrets list in
the ServiceAccount.

Fixes submariner-io#102

Signed-off-by: Vishal Thapar <[email protected]>
Signed-off-by: Vishal Thapar <[email protected]>
Comment on lines 211 to 213
if apierrors.IsNotFound(err) {

}
Copy link
Member

Choose a reason for hiding this comment

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

There’s something missing here, I presume.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There’s something missing here, I presume.

Missed cleanup after refactor.

if err != nil {
newSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-token-%s", sa.Name, generateRandomString(5)),
Copy link
Member

Choose a reason for hiding this comment

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

Can’t we use GenerateName here?

Copy link
Member

Choose a reason for hiding this comment

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

We can fix this up later, this shouldn’t block the PR.

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 used GenerateName in intial draft, but then needed to do another Get to find the generated name. Went with this coz similar to what OCP's controller is doing.

Refactor serviceaccount.Ensure so that creation of
secret is transparent to caller.

Signed-off-by: Vishal Thapar <[email protected]>
@skitt skitt added the backport This change requires a backport to eligible release branches label Jun 30, 2022
@skitt skitt enabled auto-merge (rebase) June 30, 2022 09:43
@skitt skitt disabled auto-merge June 30, 2022 09:43
@skitt skitt enabled auto-merge (squash) June 30, 2022 09:45
@skitt skitt merged commit 09decb5 into submariner-io:devel Jun 30, 2022
@submariner-bot
Copy link
Contributor

🤖 Closed branches: [z_pr125/vthapar/secret-tokens]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport This change requires a backport to eligible release branches backport-handled ready-to-test When a PR is ready for full E2E testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

In K8s 1.22/1.24 and later, SAs don’t automatically get a secret with a token
8 participants