Skip to content

Commit

Permalink
Fix issue with exec and TTY over teleport
Browse files Browse the repository at this point in the history
Make sure to not set stderr when using TTY for exec over teleport.
  • Loading branch information
HeavyWombat committed May 23, 2024
1 parent 80ef801 commit ddb4ed9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/gonvenience/wait v1.0.3
github.com/gonvenience/wrap v1.2.0
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-isatty v0.0.18
github.com/onsi/ginkgo/v2 v2.17.3
github.com/onsi/gomega v1.33.1
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -58,7 +59,6 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
Expand Down
23 changes: 16 additions & 7 deletions pkg/havener/kubexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ package havener
import (
"fmt"
"io"
"os"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gonvenience/term"
"github.com/gonvenience/text"
terminal "golang.org/x/term"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/utils/pointer"

"github.com/gonvenience/text"
"github.com/mattn/go-isatty"
"golang.org/x/term"
)

type NodeExecHelperPodConfig struct {
Expand Down Expand Up @@ -81,7 +82,7 @@ func (h *Hvnr) PodExec(pod *corev1.Pod, container string, execConfig ExecConfig)
}

var tsq *terminalSizeQueue
if execConfig.TTY && term.IsTerminal() {
if execConfig.TTY && isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {

Check warning on line 85 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L85

Added line #L85 was not covered by tests
tsq = setupTerminalResizeWatcher()
defer tsq.stop()
}
Expand All @@ -90,9 +91,11 @@ func (h *Hvnr) PodExec(pod *corev1.Pod, container string, execConfig ExecConfig)
// The raw mode is the one where characters are not printed twice in the terminal. See
// https://en.wikipedia.org/wiki/POSIX_terminal_interface#History for a bit more details.
if execConfig.TTY {
if stateToBeRestored, err := terminal.MakeRaw(0); err == nil {
defer func() { _ = terminal.Restore(0, stateToBeRestored) }()
oldState, err := term.MakeRaw(0)
if err != nil {
return fmt.Errorf("failed to use raw terminal: %w", err)

Check warning on line 96 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L93-L96

Added lines #L93 - L96 were not covered by tests
}
defer func() { _ = term.Restore(0, oldState) }()

Check warning on line 98 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L98

Added line #L98 was not covered by tests
}

var streamOption = remotecommand.StreamOptions{
Expand Down Expand Up @@ -126,6 +129,12 @@ func (h *Hvnr) NodeExec(node corev1.Node, hlpPodConfig NodeExecHelperPodConfig,
return err
}

// Unset the stderr in case TTY is set
// https://github.com/kubernetes/kubectl/blob/5b7c8b24b4361a97ab19de1d1e301a6c1bbaed1a/pkg/cmd/exec/exec.go#L370-L372
if execConfig.TTY {
execConfig.Stderr = nil

Check warning on line 135 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L134-L135

Added lines #L134 - L135 were not covered by tests
}

// Execute command on pod and redirect output to users provided stdout and stderr
logf(Verbose, "Executing command on node: `%v`", strings.Join(execConfig.Command, " "))

Check warning on line 139 in pkg/havener/kubexec.go

View check run for this annotation

Codecov / codecov/patch

pkg/havener/kubexec.go#L139

Added line #L139 was not covered by tests

Expand Down

0 comments on commit ddb4ed9

Please sign in to comment.