From 87806676613c06ce54749d1c6075a4b3725a7854 Mon Sep 17 00:00:00 2001 From: nocturnalastro Date: Wed, 18 Oct 2023 10:38:02 +0100 Subject: [PATCH] Refactor command exec to use the same core function --- pkg/clients/exec_command.go | 93 ++++++++++++++----------------------- 1 file changed, 36 insertions(+), 57 deletions(-) diff --git a/pkg/clients/exec_command.go b/pkg/clients/exec_command.go index 88b052c1..3cb023f2 100644 --- a/pkg/clients/exec_command.go +++ b/pkg/clients/exec_command.go @@ -65,15 +65,16 @@ func (c *ContainerContext) GetContainerName() string { return c.containerName } -// ExecCommand runs command in a container and returns output buffers -// -//nolint:lll,funlen // allow slightly long function definition and allow a slightly long function -func (c *ContainerContext) ExecCommandContainer(command []string) (stdout, stderr string, err error) { +//nolint:lll,funlen // allow slightly long function definition and function length +func (c *ContainerContext) execCommand(command []string, buffInPtr *bytes.Buffer) (stdout, stderr string, err error) { commandStr := command var buffOut bytes.Buffer var buffErr bytes.Buffer + + useBuffIn := buffInPtr != nil + log.Debugf( - "execute command on ns=%s, pod=%s container=%s, cmd: %s\n", + "execute command on ns=%s, pod=%s container=%s, cmd: %s", c.GetNamespace(), c.GetPodName(), c.GetContainerName(), @@ -86,8 +87,8 @@ func (c *ContainerContext) ExecCommandContainer(command []string) (stdout, stder SubResource("exec"). VersionedParams(&corev1.PodExecOptions{ Container: c.GetContainerName(), - Command: commandStr, - Stdin: false, + Command: command, + Stdin: useBuffIn, Stdout: true, Stderr: true, TTY: false, @@ -99,10 +100,22 @@ func (c *ContainerContext) ExecCommandContainer(command []string) (stdout, stder return stdout, stderr, fmt.Errorf("error setting up remote command: %w", err) } - err = exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{ - Stdout: &buffOut, - Stderr: &buffErr, - }) + var streamOptions remotecommand.StreamOptions + + if useBuffIn { + streamOptions = remotecommand.StreamOptions{ + Stdin: buffInPtr, + Stdout: &buffOut, + Stderr: &buffErr, + } + } else { + streamOptions = remotecommand.StreamOptions{ + Stdout: &buffOut, + Stderr: &buffErr, + } + } + + err = exec.StreamWithContext(context.TODO(), streamOptions) stdout, stderr = buffOut.String(), buffErr.String() if err != nil { if errors.IsNotFound(err) { @@ -112,9 +125,13 @@ func (c *ContainerContext) ExecCommandContainer(command []string) (stdout, stder log.Debug("Failed to refresh container context", refreshErr) } } + log.Debug(err) log.Debug(req.URL()) log.Debug("command: ", command) + if useBuffIn { + log.Debug("stdin: ", buffInPtr.String()) + } log.Debug("stderr: ", stderr) log.Debug("stdout: ", stdout) return stdout, stderr, fmt.Errorf("error running remote command: %w", err) @@ -122,52 +139,14 @@ func (c *ContainerContext) ExecCommandContainer(command []string) (stdout, stder return stdout, stderr, nil } +// ExecCommand runs command in a container and returns output buffers +// +//nolint:lll,funlen // allow slightly long function definition and allow a slightly long function +func (c *ContainerContext) ExecCommandContainer(command []string) (stdout, stderr string, err error) { + return c.execCommand(command, nil) +} + //nolint:lll // allow slightly long function definition func (c *ContainerContext) ExecCommandContainerStdIn(command []string, buffIn bytes.Buffer) (stdout, stderr string, err error) { - commandStr := command - var buffOut bytes.Buffer - var buffErr bytes.Buffer - log.Debugf( - "execute command on ns=%s, pod=%s container=%s, cmd: %s", - c.GetNamespace(), - c.GetPodName(), - c.GetContainerName(), - strings.Join(commandStr, " "), - ) - req := c.clientset.K8sRestClient.Post(). - Namespace(c.GetNamespace()). - Resource("pods"). - Name(c.GetPodName()). - SubResource("exec"). - VersionedParams(&corev1.PodExecOptions{ - Container: c.GetContainerName(), - Command: command, - Stdin: true, - Stdout: true, - Stderr: true, - TTY: false, - }, scheme.ParameterCodec) - - exec, err := NewSPDYExecutor(c.clientset.RestConfig, "POST", req.URL()) - if err != nil { - log.Debug(err) - return stdout, stderr, fmt.Errorf("error setting up remote command: %w", err) - } - - err = exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{ - Stdin: &buffIn, - Stdout: &buffOut, - Stderr: &buffErr, - }) - stdin, stdout, stderr := buffIn.String(), buffOut.String(), buffErr.String() - if err != nil { - log.Debug(err) - log.Debug(req.URL()) - log.Debug("command: ", command) - log.Debug("stdin: ", stdin) - log.Debug("stderr: ", stderr) - log.Debug("stdout: ", stdout) - return stdout, stderr, fmt.Errorf("error running remote command: %w", err) - } - return stdout, stderr, nil + return c.execCommand(command, &buffIn) }