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

Return non-zero exit code in case of errors #690

Merged
merged 2 commits into from
Sep 24, 2024

Conversation

petrutlucian94
Copy link
Contributor

At the moment, k8s and k8sd return 0 even if the command fails, which is a problem especially when used inside scripts.

We'll ensure that a non-zero exit code is returned if the commands fail.

@petrutlucian94 petrutlucian94 requested a review from a team as a code owner September 23, 2024 06:46
@bschimke95
Copy link
Contributor

LGTM thanks
Please run go fmt to make the Go CI happy, then we are good to merge.

Copy link
Contributor

@bschimke95 bschimke95 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@HomayoonAlimohammadi HomayoonAlimohammadi left a comment

Choose a reason for hiding this comment

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

I think we're using Run instead of RunE which indicates that we're relying on the os.Exit(1) to occur in the individual cmds. I might be wrong, but I think the ExecuteContext here only returns an error if the RunE, PreRunE, PostRunE or similar "error-returning" methods return an error which we don't have any of them (AFAIK).
This change is a valid change but if we're addressing the fact that k8s commands does not exit with code 1 when they should, this won't solve that issue.
In other words, this change won't cause any behavior changes because of the current implementation.
If there's any k8s command that exits with code 0 while also facing with an error I think we should change the code for that specific cmd and call the os.Exit(1) there (e.g.:

).

@petrutlucian94
Copy link
Contributor Author

petrutlucian94 commented Sep 23, 2024

@HomayoonAlimohammadi thanks for bringing this up. I did a simple check and it seemed to work fine but I'll take a closer look.

w/ fix
cloudbase@ubuntuk8s2ha:~/workspace/k8s-snap/src/k8s$ bin/static/k8sd wrong-cmd
Error: unknown command "wrong-cmd" for "k8sd"
Run 'k8sd --help' for usage.
cloudbase@ubuntuk8s2ha:~/workspace/k8s-snap/src/k8s$ echo $?
1

# w/o fix
ubuntu@hatwo:~$ /snap/k8s/1211/bin/k8sd wrong-cmd
Error: unknown command "wrong-cmd" for "k8sd"
Run 'k8sd --help' for usage.
ubuntu@hatwo:~$ echo $?
0
cloudbase@ubuntuk8s2ha:~/workspace/k8s-snap/src/k8s$ bin/static/k8sd cluster-recover
Error: k8s-dqlite state dir not specified
Usage:
  k8sd cluster-recover [flags]

...
  -v, --verbose                                   Show all information messages (default true)

cloudbase@ubuntuk8s2ha:~/workspace/k8s-snap/src/k8s$ echo $?
1

LE: I see that the commands that use Run instead of RunE do call os.Exit, so that's fine. This change will accomodate commands that use RunE as well as the case in which the argument parsing fails.

@bschimke95
Copy link
Contributor

As discussed in the standup: You both are correct.
We currently use the Run command for every k8s command except the k8sd cluster recover command. I overlooked that in #618.

So,
a) @petrutlucian94 please create a PR to refactor the k8sd cluster recover command to use the environment, similar to what commands do.
b) I'd still merge this PR to be a safe-net in case we miss this again for other commands.

Copy link
Contributor

@bschimke95 bschimke95 left a comment

Choose a reason for hiding this comment

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

LGTM. Please add a note to the error check.
e.g. // Although k8s commands typically use Run instead of RunE and handle errors directly within the command execution, this acts as a safeguard in case any are overlooked.

@petrutlucian94
Copy link
Contributor Author

a) @petrutlucian94 please create a PR to refactor the k8sd cluster recover command to use the environment, similar to what commands do.

I've added another commit here, I hope that's ok.

b) I'd still merge this PR to be a safe-net in case we miss this again for other commands.

The Run/RunE functions won't get called at all if there are argument parsing errors, so that's another reason why we need this. I've added an inline comment.

Copy link
Contributor

@bschimke95 bschimke95 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the change!

Copy link
Contributor

@HomayoonAlimohammadi HomayoonAlimohammadi left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks a lot @petrutlucian94! Just some very minor comments.

src/k8s/cmd/main.go Outdated Show resolved Hide resolved
src/k8s/cmd/k8sd/k8sd_cluster_recover.go Outdated Show resolved Hide resolved
At the moment, k8s and k8sd return 0 even if the command fails,
which is a problem especially when used inside scripts.

We'll ensure that a non-zero exit code is returned if the commands
fail.
The cluster recovery command currently uses "RunE" and returns an
error in case of failures.

To stay consistent with other commands, we'll use "Run" and call
env.Exit as part of the command callback instead of returning the
errors.
@bschimke95 bschimke95 merged commit a5d0ae4 into canonical:main Sep 24, 2024
16 of 18 checks passed
evilnick pushed a commit to evilnick/k8s-snap that referenced this pull request Nov 14, 2024
* Return non-zero exit code in case of errors

At the moment, k8s and k8sd return 0 even if the command fails,
which is a problem especially when used inside scripts.

We'll ensure that a non-zero exit code is returned if the commands
fail.

* Update the cluster recovery command to use cobra "Run"

The cluster recovery command currently uses "RunE" and returns an
error in case of failures.

To stay consistent with other commands, we'll use "Run" and call
env.Exit as part of the command callback instead of returning the
errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants