Skip to content

Commit

Permalink
Refactor command exec to use the same core function
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro committed Oct 18, 2023
1 parent 2ff0d0a commit 8780667
Showing 1 changed file with 36 additions and 57 deletions.
93 changes: 36 additions & 57 deletions pkg/clients/exec_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -112,62 +125,28 @@ 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)
}
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)
}

0 comments on commit 8780667

Please sign in to comment.