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

cmd/kube-scheduler: return error instead of os.Exit when something goes wrong #104503

Conversation

sanposhiho
Copy link
Member

@sanposhiho sanposhiho commented Aug 22, 2021

Hello team.

What type of PR is this?

/kind bug

What this PR does / why we need it:

The defer function will not be called if os.Exit.
https://play.golang.org/p/f7IjXeb62dd

deferred functions are not run.
https://golang.org/pkg/os/#Exit

Which issue(s) this PR fixes:

part of #102231

Special notes for your reviewer:

Does this PR introduce a user-facing change?

kube-scheduler now doesn't print any usage message when unknown flag is specified

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@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. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Aug 22, 2021
@k8s-ci-robot
Copy link
Contributor

@sanposhiho: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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.

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Aug 22, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @sanposhiho. Thanks for your PR.

I'm waiting for a kubernetes 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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Aug 22, 2021
@@ -79,15 +79,14 @@ suitable Node. Multiple different schedulers may be used within a cluster;
kube-scheduler is the reference implementation.
See [scheduling](https://kubernetes.io/docs/concepts/scheduling-eviction/)
for more information about scheduling and the kube-scheduler component.`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to not change the error messages. Just return the errors to preserve the current behavior, whatever it is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed thanks

Copy link
Member

Choose a reason for hiding this comment

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

You may also want to set SilenceErrors and SilenceUsage to avoid Cobra printing the error and the usage information on the error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, fixed.
As you said, if we don't set SilenceErrors/SilenceUsage, it seems that cobra changes error msg and adds usage message on error msg. thanks

https://github.com/spf13/cobra/blob/v1.2.1/command.go#L975-L994

@ash2k
Copy link
Member

ash2k commented Aug 22, 2021

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 22, 2021
@sanposhiho
Copy link
Member Author

/retest

1 similar comment
@sanposhiho
Copy link
Member Author

/retest

Copy link
Member

@chendave chendave left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 26, 2021
@ahg-g
Copy link
Member

ahg-g commented Aug 26, 2021

Thanks! did you manually try to test the changes? can you show us the difference between the old and new behavior?

@sanposhiho
Copy link
Member Author

sanposhiho commented Aug 26, 2021

Hello @ahg-g.

I've tried it briefly

1. When cobra.Command.RunE return an error.

(For this case, I made RunE always return an error(something goes wrong).)

 $ ./_output/bin/kube-scheduler              
something goes wrong

This is the same output as old. (but, internally, logs.FlushLogs() will be executed only on this PR code)

2. When user specify wrong flag

$ ./_output/bin/kube-scheduler --add-dir-hea
unknown flag: --add-dir-hea

This output is different from old (see below). This is because we set SilenceErrors and SilenceUsage true.

# on release-1.22
$ ./_output/bin/kube-scheduler --add-dir-hea
Error: unknown flag: --add-dir-hea
Usage:
  kube-scheduler [flags]

Misc flags:

      --config string                                                                                                                                                                                            
                The path to the configuration file. The following flags can overwrite fields in this file:
                  --policy-config-file
                  --policy-configmap
                  --policy-configmap-namespace

.....(super long usage message)

But if we don't set SilenceErrors and SilenceUsage on this PR, usage will be print when cobra.Command.RunE return an error. (And this is different output from old)

# not set SilenceErrors and SilenceUsage 
$ ./_output/bin/kube-scheduler     
Error: something goes wrong
Usage:
  kube-scheduler [flags]

Misc flags:

      --config string                                                                                                                                                                                            
                The path to the configuration file. The following flags can overwrite fields in this file:
                  --policy-config-file
                  --policy-configmap
                  --policy-configmap-namespace

.....(super long usage message)

@ahg-g
Copy link
Member

ahg-g commented Aug 26, 2021

Thanks!

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahg-g, sanposhiho

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 26, 2021
Copy link
Member

@ahg-g ahg-g left a comment

Choose a reason for hiding this comment

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

/hold

},
SilenceErrors: true,
Copy link
Member

Choose a reason for hiding this comment

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

what does this mean exactly?

Copy link
Member Author

Choose a reason for hiding this comment

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

When SilenceErrors = false (or not set), an error will be printed with the prefix Error:. In the above example, it prints Error: something goes wrong.
We print error on this line, so I think we don't need this.

https://github.com/spf13/cobra/blob/v1.2.1/command.go#L983-L987

Copy link
Member Author

Choose a reason for hiding this comment

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

So, if we set false (or don't set SilenceErrors), error is printed twice like this.

$ ./_output/bin/kube-scheduler                               
Error: something goes wrong
something goes wrong

Copy link
Member

Choose a reason for hiding this comment

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

thanks for the explanation

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm going to replace this with a more generic solution that'll work for most of the Kubernetes commands in #105076.

See #105076 (comment)

It would be good to get some additional eyes on that. Perhaps someone can even try out the PR?

I ran tests manually with kube-scheduler as example and I think it still works as you intended here.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 26, 2021
@ahg-g
Copy link
Member

ahg-g commented Aug 26, 2021

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 26, 2021
@sanposhiho
Copy link
Member Author

sanposhiho commented Aug 26, 2021

This output is different from old (see below). This is because we set SilenceErrors and SilenceUsage true.

Should we add this change to the release notes? (Like kube-scheduler now don't print any usage message when unknown flag is specified)

@ahg-g
Copy link
Member

ahg-g commented Aug 26, 2021

sure, wont hurt to do that.

s/don't/doesn't

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Aug 26, 2021
@k8s-ci-robot k8s-ci-robot merged commit d0f6983 into kubernetes:master Aug 26, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Aug 26, 2021
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. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/scheduling Categorizes an issue or PR as relevant to SIG Scheduling. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants