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

add support for customer SA namespace in snap meta lister tool #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Rakshith-R
Copy link
Contributor

What type of PR is this?
/kind bug

What this PR does / why we need it:
Add support for customer SA namespace in snap meta lister tool.
Currently SA namespace is picked up from snapshot namespace which is very limiting.

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/bug Categorizes issue or PR as related to a bug. labels Dec 2, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Rakshith-R
Once this PR has been reviewed and has the lgtm label, please assign prasadg193 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 2, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @Rakshith-R. Thanks for your PR.

I'm waiting for a kubernetes-csi 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-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 2, 2024
Copy link
Contributor

@carlbraganza carlbraganza left a comment

Choose a reason for hiding this comment

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

Good functional addition.

Some minor issues...

Comment on lines 300 to 302
func (iter *iterator) createSecurityToken(ctx context.Context,
serviceAccount,
serviceAccountNamespace,
audience string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Please don't break the arguments over multiple lines.
  2. Since you've introduced serviceAccountNamespace please rename serviceAccount to serviceAccountName. It would be acceptable to shorten these to saName and saNamespace.
  3. If possible, put the namespace argument before the name argument.

Comment on lines 220 to 223
securityToken, err := iter.h.createSecurityToken(ctx,
serviceAccount,
serviceAccountNamespace,
smsCR.Spec.Audience)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please stick with the style: I generally avoid multiple lines in a function call except if an argument data structure is assembled as part of the call. Exceptions are allowed of course, when the argument list gets complex, but this is not such a case.

tokenRequest := authv1.TokenRequest{
Spec: authv1.TokenRequestSpec{
Audiences: []string{audience},
ExpirationSeconds: &iter.TokenExpirySecs,
},
}

tokenResp, err := iter.KubeClient.CoreV1().ServiceAccounts(iter.Namespace).CreateToken(ctx, serviceAccount, &tokenRequest, apimetav1.CreateOptions{})
tokenResp, err := iter.KubeClient.CoreV1().ServiceAccounts(serviceAccountNamespace).
CreateToken(ctx, serviceAccount, &tokenRequest, apimetav1.CreateOptions{})
if err != nil {
return "", fmt.Errorf("ServiceAccounts.CreateToken(%s): %v", serviceAccount, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

You should change the error message to:

fmt.Errorf("ServiceAccounts.CreateToken(%s/%s): %v", serviceAccountNamespace, serviceAccount, err)

Comment on lines 107 to 108
// ServiceAccountNamespace is the namespace of the ServiceAccount.
ServiceAccountNamespace string
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. This is optional and should be stated so. I suggest combining the comment for the two as follows:
	// Specify the ServiceAccount object used to construct a security token
	// with the audience string from the SnapshotMetadataService CR.
	// If either of the following fields are unspecified, the default for the given client will be used.
	ServiceAccountNamespace string
	ServiceAccount string

It would be good if ServiceAccount was renamed to ServiceAccountName.

  1. The args.Validate function should be enhanced to ensure that either both fields are empty or both are set - impacts validation UTs too.

@@ -191,9 +194,10 @@ func newIterator(args Args) *iterator {
func (iter *iterator) run(ctx context.Context) error {
var err error

serviceAccount := iter.ServiceAccount // optional field
serviceAccount := iter.ServiceAccount // optional field
serviceAccountNamespace := iter.ServiceAccountNamespace // optional field
if serviceAccount == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

The check for an empty name (only) is fine as long as args.Validate ensures both or none semantics.

Copy link
Contributor

@carlbraganza carlbraganza left a comment

Choose a reason for hiding this comment

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

Looking good. Minor nits ...

Comment on lines +126 to +129
case a.SANamespace == "" && a.SAName != "":
return fmt.Errorf("%w: ServiceAccountName provided but ServiceAccountNamespace missing", ErrInvalidArgs)
case a.SANamespace != "" && a.SAName == "":
return fmt.Errorf("%w: ServiceAccountNamespace provided but ServiceAccountName missing", ErrInvalidArgs)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: The error message should refer to the fields - you're now calling them SAName and SANamespace and not ServiceAccountName and ServiceAccountNamespace

@@ -242,20 +248,19 @@ func (iter *iterator) run(ctx context.Context) error {
return err
}

func (iter *iterator) getDefaultServiceAccount(ctx context.Context) (string, error) {
func (iter *iterator) getDefaultServiceAccount(ctx context.Context) (string, string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Could you please make it clear in the function signature which return argument is the name and which is the namespace? e.g.

func (iter *iterator) getDefaultServiceAccount(ctx context.Context) (namespace string, name string, err error)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. release-note-none Denotes a PR that doesn't merit a release note. 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.

3 participants