Skip to content

Commit

Permalink
fixup! Update debug container to redhat-partner-solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro committed Jan 8, 2024
1 parent c591dcc commit b0d1476
Showing 1 changed file with 42 additions and 35 deletions.
77 changes: 42 additions & 35 deletions pkg/clients/exec_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
"time"

"github.com/Netflix/go-expect"
"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubectl/pkg/scheme"

"github.com/redhat-partner-solutions/vse-sync-collection-tools/pkg/utils"
)

const (
Expand Down Expand Up @@ -158,8 +159,6 @@ func (c *ContainerExecContext) execCommand(command []string, buffInPtr *bytes.Bu
}

// 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 *ContainerExecContext) ExecCommand(command []string) (stdout, stderr string, err error) {
return c.execCommand(command, nil)
}
Expand Down Expand Up @@ -379,17 +378,21 @@ func NewContainerCreationExecContext(

var anythingThenPromptRE = regexp.MustCompile(`(.+)(sh-\d.\d#\s*)`)

const shellCommand = "/usr/bin/sh"
const (
shellCommand = "/usr/bin/sh"
expectTimeout = 30 * time.Second
commandChannelSize = 10
)

type result struct {
err error
stdout string
stderr string
err error
}

type command struct {
cmd string
result chan *result
cmd string
}
type Shell struct {
expecter *expect.Console
Expand All @@ -398,14 +401,14 @@ type Shell struct {

type ReusedConnectionContext struct {
*ContainerExecContext
shell Shell
commandChannel chan *command
commandQuit chan os.Signal
wg *utils.WaitGroupCount
shell *Shell
}

func (c *ReusedConnectionContext) openShell(tty *os.File) error {
log.Debugf(
log.Errorf(
"execute command on ns=%s, pod=%s container=%s, cmd: %s",
c.GetNamespace(),
c.GetPodName(),
Expand All @@ -426,13 +429,11 @@ func (c *ReusedConnectionContext) openShell(tty *os.File) error {
TTY: true,
}, scheme.ParameterCodec)

// quit := make(chan os.Signal)
exec, err := NewSPDYExecutor(c.clientset.RestConfig, "POST", req.URL())
if err != nil {
log.Debug(err)
err = fmt.Errorf("error setting up remote command: %w", err)
return err

}

c.wg.Add(1)
Expand All @@ -450,38 +451,44 @@ func (c *ReusedConnectionContext) openShell(tty *os.File) error {
}()

c.wg.Add(1)
go func() {
defer c.wg.Done()
for {
select {
case <-c.commandQuit:
c.shell.expecter.SendLine("quit")
for c.wg.GetCount() == 1 {
time.Sleep(time.Microsecond)
}
return
case cmd := <-c.commandChannel:
c.shell.expecter.Send(cmd.cmd)
stdout, err := c.shell.expecter.Expect(expect.Regexp(anythingThenPromptRE))
stderr := c.shell.errBuff.String()
c.shell.errBuff.Reset()
cmd.result <- &result{stdout: stdout, stderr: stderr, err: err}
}
}
}()
go c.commandSender()

return nil
}

func (c *ReusedConnectionContext) commandSender() {
defer c.wg.Done()
for {
select {
case <-c.commandQuit:
_, err := c.shell.expecter.SendLine("quit")
if err != nil {
log.Errorf("Error when quiting shell: %s", err.Error())
}
for c.wg.GetCount() == 1 {
time.Sleep(time.Microsecond)
}
return
case cmd := <-c.commandChannel:
_, err := c.shell.expecter.Send(cmd.cmd)
if err != nil {
cmd.result <- &result{stdout: "", stderr: "", err: fmt.Errorf("failed to send command: %w", err)}
}
stdout, err := c.shell.expecter.Expect(expect.Regexp(anythingThenPromptRE))
stderr := c.shell.errBuff.String()
c.shell.errBuff.Reset()
cmd.result <- &result{stdout: stdout, stderr: stderr, err: err}
}
}
}

func (c *ReusedConnectionContext) execCommand(cmd string) (stdout, stderr string, err error) {
resChan := make(chan *result, 1)
c.commandChannel <- &command{cmd: cmd, result: resChan}
resp := <-resChan
return resp.stdout, resp.stderr, resp.err

}

//nolint:lll,funlen // allow slightly long function definition and allow a slightly long function
func (c ReusedConnectionContext) ExecCommand(cmd []string) (stdout, stderr string, err error) {
return c.execCommand(strings.Join(cmd, " "))
}
Expand Down Expand Up @@ -510,17 +517,17 @@ func NewReusedConnectionContext(
return ReusedConnectionContext{}, err
}

expecter, err := expect.NewConsole(expect.WithDefaultTimeout(1 * time.Minute))
expecter, err := expect.NewConsole(expect.WithDefaultTimeout(expectTimeout))
if err != nil {
return ReusedConnectionContext{}, err
return ReusedConnectionContext{}, fmt.Errorf("failed to create expect console: %w", err)
}

ctx := ReusedConnectionContext{
ContainerExecContext: containerCtx,
shell: Shell{
shell: &Shell{
expecter: expecter,
},
commandChannel: make(chan *command, 10),
commandChannel: make(chan *command, commandChannelSize),
commandQuit: make(chan os.Signal, 1),
}
err = ctx.openShell(expecter.Tty())
Expand Down

0 comments on commit b0d1476

Please sign in to comment.