Skip to content

Commit

Permalink
Merge pull request #54094 from juanvallejo/jvallejo/exit-plugin-cmd-w…
Browse files Browse the repository at this point in the history
…-correct-exit-code

Automatic merge from submit-queue (batch tested with PRs 54107, 54184, 54377, 54094, 54111). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

exit with correct exit code on plugin failure

**Release note**:
```release-note
NONE
```

Correctly exits after a plugin command execution failure with the correct exit code.
Related downstream bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1501756

**Before**
```
$ kubectl plugin will-fail-plugin
ls: cannot access 'does-not-exist': No such file or directory
error: exit status 2

$ echo $?
1
```

**After**
```
$ kubectl plugin will-fail-plugin
ls: cannot access 'does-not-exist': No such file or directory
error: exit status 2

$ echo $?
2
```

cc @fabianofranz
  • Loading branch information
Kubernetes Submit Queue committed Oct 24, 2017
2 parents 47b4f0e + 96d3f45 commit 8f79857
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/kubectl/cmd/plugin.go
Expand Up @@ -19,6 +19,9 @@ package cmd
import (
"fmt"
"io"
"os"
"os/exec"
"syscall"

"github.com/golang/glog"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -109,6 +112,15 @@ func NewCmdForPlugin(f cmdutil.Factory, plugin *plugins.Plugin, runner plugins.P
}

if err := runner.Run(plugin, runningContext); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// check for (and exit with) the correct exit code
// from a failed plugin command execution
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
fmt.Fprintf(errout, "error: %v\n", err)
os.Exit(status.ExitStatus())
}
}

cmdutil.CheckErr(err)
}
},
Expand Down

0 comments on commit 8f79857

Please sign in to comment.