From 89f2934d787972df7ef64f465f63c4256a0414d5 Mon Sep 17 00:00:00 2001 From: k8s-infra-cherrypick-robot <90416843+k8s-infra-cherrypick-robot@users.noreply.github.com> Date: Sat, 29 Mar 2025 01:20:44 -0700 Subject: [PATCH] Plugin: Improve error handling. (#13112) Co-authored-by: morguldir --- cmd/plugin/kubectl/kubectl.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/plugin/kubectl/kubectl.go b/cmd/plugin/kubectl/kubectl.go index cb33243fc..98970aaf7 100644 --- a/cmd/plugin/kubectl/kubectl.go +++ b/cmd/plugin/kubectl/kubectl.go @@ -32,7 +32,7 @@ import ( // PodExecString takes a pod and a command, uses kubectl exec to run the command in the pod // and returns stdout as a string func PodExecString(flags *genericclioptions.ConfigFlags, pod *apiv1.Pod, container string, args []string) (string, error) { - args = append([]string{"exec", "-n", pod.Namespace, "-c", container, pod.Name}, args...) + args = append([]string{"exec", "-n", pod.Namespace, "-c", container, pod.Name, "--"}, args...) return ExecToString(flags, args) } @@ -73,6 +73,8 @@ func execToWriter(args []string, writer io.Writer) error { //nolint:gosec // Ignore G204 error cmd := exec.Command(args[0], args[1:]...) + var stderr bytes.Buffer + cmd.Stderr = &stderr op, err := cmd.StdoutPipe() if err != nil { return err @@ -83,6 +85,9 @@ func execToWriter(args []string, writer io.Writer) error { }() err = cmd.Run() if err != nil { + if _, ok := err.(*exec.ExitError); ok { + println(stderr.String()) + } return err }