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

Makes kubectl wait exit with status 1 and print an error message, if there is no resources matching selectors #66692

Merged
merged 1 commit into from
Aug 6, 2018
Merged

Conversation

m1kola
Copy link
Member

@m1kola m1kola commented Jul 26, 2018

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:

kubectl wait deployment -l app=something-that-does-not-exist --for condition=available --timeout=5s

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:

kubectl: the wait command now prints an error message and exits with the code 1, if there is no resources matching selectors

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. 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 Jul 26, 2018
@k8s-ci-robot k8s-ci-robot requested review from eparis and mengqiy July 26, 2018 22:51
Copy link
Member

@neolit123 neolit123 left a 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.

@@ -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")
Copy link
Member

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

Copy link
Member Author

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")

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")

@@ -221,14 +227,24 @@ func (o *WaitOptions) RunWait() error {
}
return err
})

if err != nil {
Copy link
Member

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.

Copy link
Member

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. :)

Copy link
Member Author

@m1kola m1kola Jul 27, 2018

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.

@neolit123
Copy link
Member

Thanks for your PR @m1kola
Please make sure you run "./hack/verify-bazel.sh" and include the changes in the PR.

@neolit123
Copy link
Member

/sig cli

@k8s-ci-robot k8s-ci-robot added the sig/cli Categorizes an issue or PR as relevant to SIG CLI. label Jul 27, 2018
Copy link
Member Author

@m1kola m1kola left a 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 :)

@@ -221,14 +227,24 @@ func (o *WaitOptions) RunWait() error {
}
return err
})

if err != nil {
Copy link
Member Author

@m1kola m1kola Jul 27, 2018

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.

@@ -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")
Copy link
Member Author

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")

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")

@neolit123
Copy link
Member

Thanks for the update
/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 28, 2018
@neolit123
Copy link
Member

@m1kola this looks good to me.
waiting on approval from @kubernetes/sig-cli-pr-reviews

Copy link
Contributor

@soltysh soltysh left a 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.

@@ -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")
Copy link
Contributor

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.
@juanvallejo
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 31, 2018
@m1kola
Copy link
Member Author

m1kola commented Jul 31, 2018

Thanks for the code review to everyone.

@soltysh I've updated the PR (error message changed and commits squashed). Tests are green ;)

@mengqiy
Copy link
Member

mengqiy commented Aug 6, 2018

/lgtm

@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 6, 2018
@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit 5544187 into kubernetes:master Aug 6, 2018
@m1kola m1kola deleted the 66456_waitcmd__error_for_selectors branch August 6, 2018 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. 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.

kubectl wait with selectors does not output anything if resource is not found
7 participants