Commit 8f79857b authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #54094 from juanvallejo/jvallejo/exit-plugin-cmd-w-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
parents 47b4f0ed 96d3f455
......@@ -19,6 +19,9 @@ package cmd
import (
"fmt"
"io"
"os"
"os/exec"
"syscall"
"github.com/golang/glog"
"github.com/spf13/cobra"
......@@ -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)
}
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment