diff --git a/cmd/exec.go b/cmd/exec.go index 4ef037e..87bb590 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -14,6 +14,7 @@ import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" + "k8s.io/klog/v2" ) type Options struct { @@ -53,6 +54,7 @@ type cliSession struct { clientConf *rest.Config k8sClient *kubernetes.Clientset namespace string + RawMode bool } func NewCliSession(o *Options) (*cliSession, error) { @@ -154,7 +156,12 @@ func (c *cliSession) prepExec() (*http.Request, error) { } if c.opts.TTY { - query.Add("tty", "true") + stdIn, _, _ := term.StdStreams() + _, c.RawMode = term.GetFdInfo(stdIn) + if !c.RawMode { + klog.V(2).Infof("Unable to use a TTY - input is not a terminal or the right kind of file") + } + query.Add("tty", fmt.Sprintf("%t", c.RawMode)) } if c.opts.Stdin { @@ -184,26 +191,25 @@ func (c *cliSession) doExec(req *http.Request) error { Subprotocols: protocols, } - initState := &TerminalState{} - if c.opts.TTY { + initState := &TerminalState{ + IsRaw: c.RawMode, + } + if c.RawMode { stdIn, stdOut, _ := term.StdStreams() - stdInFd, isTerm := term.GetFdInfo(stdIn) + stdInFd, _ := term.GetFdInfo(stdIn) stdOutFd, _ := term.GetFdInfo(stdOut) - if isTerm { - initState.StdInFd = stdInFd - initState.StdOutFd = stdOutFd - initState.StateBlob, err = term.SetRawTerminal(stdInFd) - if err != nil { - return err - } - defer term.RestoreTerminal(stdInFd, initState.StateBlob) + initState.StdInFd = stdInFd + initState.StdOutFd = stdOutFd + initState.StateBlob, err = term.SetRawTerminal(stdInFd) + if err != nil { + return err } + defer term.RestoreTerminal(stdInFd, initState.StateBlob) } rt := &WebsocketRoundTripper{ Dialer: dialer, TermState: initState, - opts: c.opts, } rter, err := rest.HTTPWrappersForConfig(c.clientConf, rt) diff --git a/cmd/terminal.go b/cmd/terminal.go index 5e4f37a..c769e1a 100644 --- a/cmd/terminal.go +++ b/cmd/terminal.go @@ -12,6 +12,7 @@ type TerminalState struct { StdOutFd uintptr StateBlob *term.State Initialised bool + IsRaw bool } type TerminalSize struct { diff --git a/cmd/websocket.go b/cmd/websocket.go index d27c853..7a144bb 100644 --- a/cmd/websocket.go +++ b/cmd/websocket.go @@ -20,7 +20,6 @@ import ( type WebsocketRoundTripper struct { Dialer *websocket.Dialer TermState *TerminalState - opts Options SendBuffer bytes.Buffer } @@ -157,7 +156,7 @@ func (d *WebsocketRoundTripper) concurrentRecv(wg *sync.WaitGroup, ws *websocket func (d *WebsocketRoundTripper) concurrentResize(wg *sync.WaitGroup, ws *websocket.Conn, errChan chan error) { defer wg.Done() - if d.opts.TTY { + if d.TermState.IsRaw { resizeNotify := registerResizeSignal() d.TermState.Initialised = false