-
Notifications
You must be signed in to change notification settings - Fork 39.9k
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
Makes kubectl wait exit with status 1 and print an error message, if there is no resources matching selectors #66692
Makes kubectl wait exit with status 1 and print an error message, if there is no resources matching selectors #66692
Conversation
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.
Added a couple of small comments.
pkg/kubectl/cmd/wait/wait.go
Outdated
@@ -61,6 +62,9 @@ var ( | |||
kubectl wait --for=delete pod/busybox1 --timeout=60s`) | |||
) | |||
|
|||
// ErrNoMatchingResources is returned when there is no resources matching a query. | |||
var ErrNoMatchingResources = errors.New("failed to find matching resources") |
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.
var -> const
If this is used only in this package you can lowercase
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.
If this is used only in this package you can lowercase
Good idea, thanks. I've made it to be unexported.
var -> const
I think, the go compiler will complain about errors.New("something")
not being a constant. I've stolen the approach from other packages where errors are defined as vars using errors.New()
. Few examples:
var ErrPodCompleted = fmt.Errorf("pod ran to completion") |
kubernetes/pkg/cloudprovider/providers/openstack/openstack.go
Lines 62 to 68 in d2721ec
var ErrNotFound = errors.New("failed to find object") | |
// ErrMultipleResults is used when we unexpectedly get back multiple results | |
var ErrMultipleResults = errors.New("multiple results where only one expected") | |
// ErrNoAddressFound is used when we cannot find an ip address for the host | |
var ErrNoAddressFound = errors.New("no address found for host") |
pkg/kubectl/cmd/wait/wait.go
Outdated
@@ -221,14 +227,24 @@ func (o *WaitOptions) RunWait() error { | |||
} | |||
return err | |||
}) | |||
|
|||
if err != nil { |
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 can remove this check and just "return err" at the end of the function.
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 can also remove the empty lines. :)
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 can remove this check and just "return err" at the end of the function.
Hm. I think, if I remove this condition it will break the existing behaviour. For example
kubectl wait deployment something-that-does-not-exist --for condition=available --timeout=5s
is currently printing an error from the API:
Error from server (NotFound): deployments.extensions "something-that-does-not-exist" not found
with the change you suggest to make it will printing "error: failed to find matching resources" in this case.
Another example (invalid command args):
kubectl wait deployment --for condition=available --timeout=5s
currently provides a helpful message: "error: resource(s) were provided, but no name, label selector, or --all flag specified", but if I remove the check it will also print "error: failed to find matching resources".
So, I think, we need to return errors from the visitors first.
Thanks for your PR @m1kola |
/sig cli |
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.
@neolit123 thanks for the review.
I've made changes and left some comments. ./hack/verify-bazel.sh
didn't generate any changes for this PR.
Could you, please, have another look? Thanks :)
pkg/kubectl/cmd/wait/wait.go
Outdated
@@ -221,14 +227,24 @@ func (o *WaitOptions) RunWait() error { | |||
} | |||
return err | |||
}) | |||
|
|||
if err != nil { |
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 can remove this check and just "return err" at the end of the function.
Hm. I think, if I remove this condition it will break the existing behaviour. For example
kubectl wait deployment something-that-does-not-exist --for condition=available --timeout=5s
is currently printing an error from the API:
Error from server (NotFound): deployments.extensions "something-that-does-not-exist" not found
with the change you suggest to make it will printing "error: failed to find matching resources" in this case.
Another example (invalid command args):
kubectl wait deployment --for condition=available --timeout=5s
currently provides a helpful message: "error: resource(s) were provided, but no name, label selector, or --all flag specified", but if I remove the check it will also print "error: failed to find matching resources".
So, I think, we need to return errors from the visitors first.
pkg/kubectl/cmd/wait/wait.go
Outdated
@@ -61,6 +62,9 @@ var ( | |||
kubectl wait --for=delete pod/busybox1 --timeout=60s`) | |||
) | |||
|
|||
// ErrNoMatchingResources is returned when there is no resources matching a query. | |||
var ErrNoMatchingResources = errors.New("failed to find matching resources") |
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.
If this is used only in this package you can lowercase
Good idea, thanks. I've made it to be unexported.
var -> const
I think, the go compiler will complain about errors.New("something")
not being a constant. I've stolen the approach from other packages where errors are defined as vars using errors.New()
. Few examples:
var ErrPodCompleted = fmt.Errorf("pod ran to completion") |
kubernetes/pkg/cloudprovider/providers/openstack/openstack.go
Lines 62 to 68 in d2721ec
var ErrNotFound = errors.New("failed to find object") | |
// ErrMultipleResults is used when we unexpectedly get back multiple results | |
var ErrMultipleResults = errors.New("multiple results where only one expected") | |
// ErrNoAddressFound is used when we cannot find an ip address for the host | |
var ErrNoAddressFound = errors.New("no address found for host") |
Thanks for the update |
@m1kola this looks good to me. |
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.
One small nit and you're good to go. Please squash your changes into a single commit as well.
pkg/kubectl/cmd/wait/wait.go
Outdated
@@ -61,6 +62,9 @@ var ( | |||
kubectl wait --for=delete pod/busybox1 --timeout=60s`) | |||
) | |||
|
|||
// errNoMatchingResources is returned when there is no resources matching a query. | |||
var errNoMatchingResources = errors.New("failed to find matching resources") |
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.
no matching resources found
This makes `kubectl wait` print useful message when there is no resources matching a query. Also it will now exit with the exit status 1.
/lgtm |
Thanks for the code review to everyone. @soltysh I've updated the PR (error message changed and commits squashed). Tests are green ;) |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: juanvallejo, m1kola, mengqiy 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 all [submit-queue is verifying that this PR is safe to merge] |
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions here. |
What this PR does / why we need it:
It makes the
kubectl wait
command print an error message and exit with exit code 1, if there is no resource matching users's query. This can happen when user specifies selectors. Example:Which issue(s) this PR fixes:
Fixes #66456
Special notes for your reviewer:
This is my first contribution into the project (except one line change in docs) and don't have much experience with Go. I learned a lot while working on this (about resource finders and the
Visitor
interface and it's implementations), but it is very likely that I'm doing something wrong :)I'm keen to continue contributing into the project (into the cli part for now), so I will really appreciate detailed feedback, if you have a chance to provide it (point me into a right direction and/or explain why it's not a good idea to do something in a certain way).
Thanks!
Release note: