Plugin: Improve error handling. (#13112)

Co-authored-by: morguldir <morguldir@protonmail.com>
This commit is contained in:
k8s-infra-cherrypick-robot 2025-03-29 01:20:44 -07:00 committed by GitHub
parent ddfe1d84df
commit 89f2934d78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
}