-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[clusterctl] Fix ensure namespace function in clusterclient #1209
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
[clusterctl] Fix ensure namespace function in clusterclient #1209
Conversation
|
Welcome @eromanova! |
|
Hi @eromanova. Thanks for your PR. I'm waiting for a kubernetes-sigs or kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the 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. |
|
/assign @justinsb |
ncdc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eromanova thank you for your PR! I added a couple of comments.
| return errors.Wrap(err, "error creating core clientset") | ||
| } | ||
|
|
||
| namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Get instead of List to check for a single namespace. You can use apierrors.IsNotFound(err) to distinguish between a 404 not found and other errors.
| }, | ||
| } | ||
| _, err = clientset.CoreV1().Namespaces().Create(&namespace) | ||
| if err != nil && !apierrors.IsAlreadyExists(err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave in the check for existence. There is a slim chance that the Get call could return a 404 not found, then something else creates the namespace, and then this line tries to create it.
detiber
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eromanova Thank you for the contribution, a couple of comments related to the new check.
| return errors.Wrap(err, "error creating core clientset") | ||
| } | ||
|
|
||
| namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason to use List here instead of Get?
This is also missing a if err != nil check. It might be good to test the error against apierrors.IsForbidden to see if the user is also missing permissions to List or Get Namespaces, in which case we can likely emit a warning to the user.
1bfbd78 to
d6cc8dc
Compare
d6cc8dc to
6d874f4
Compare
|
@detiber @ncdc Thank you for your comments, guys. The reason I used List instead of Get is possible missing Get permissions for user. But such case when user does not have permissions to List, but can Get namespaces should also be handled. I've made changes to this PR according to your comments, please take a look one more time. What do you think about this approach? |
|
|
||
| _, err = clientset.CoreV1().Namespaces().Get(namespaceName, metav1.GetOptions{}) | ||
| if err != nil { | ||
| if apierrors.IsAlreadyExists(err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You won't get this error from a Get call
|
@eromanova I'm curious to know when/where you ran into a situation where you couldn't list or get namespaces? Is that common? |
6d874f4 to
2356b57
Compare
|
@ncdc I'm not so sure that it is a common case. But I had to deal with that in my environment (strong restrictions for user, only list namespaces permissions). I guess, the absence of one of these permissions is not the reason to fail ensureNamespace. |
| _, err = clientset.CoreV1().Namespaces().Create(&namespace) | ||
| if err != nil && !apierrors.IsAlreadyExists(err) { | ||
| return err | ||
| _, err = clientset.CoreV1().Namespaces().Get(namespaceName, metav1.GetOptions{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about restructuring it like this?
// Happy path - return quickly
if err == nil {
return nil
}
if apierrors.IsForbidden(err) {
// get was forbidden. see if list works
namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if apierrors.IsForbidden(err) {
// either return err, which means we halt work
// or return nil, and either things will be ok (user has permissions to create CAPI resources but they can't check for namespace existence), or things won't (they can't create CAPI resources, or the namespace doesn't exist)
}
if err != nil {
return err
}
// check the list
}
if !apierrors.IsNotFound(err) {
// err is not Forbidden or NotFound - return it
return err
}
// try to create
// ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ncdc I'm sorry for long delay. Thank you so much for the proposal. Could you please take a look again.
|
@eromanova Did you have a chance to look at @ncdc's feedback? |
Is needed to avoid possible tracebacks due to auth permission Change-Id: Id9e197be1fee6bb300cb2681a81b7f65bdd27d74
2356b57 to
8f67f5d
Compare
|
@vincepri I'm sorry it's been so long. I updated this PR, could you please take a look. Thanks in advance. |
|
/ok-to-test |
vincepri
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: eromanova, vincepri 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 |
|
/test pull-cluster-api-test |
Is needed to avoid possible tracebacks due to auth permission (if user does not have permission to create or get namespaces)
Fixes #1208