Skip to content

Commit

Permalink
Merge pull request #159 from uselagoon/terminal-resize
Browse files Browse the repository at this point in the history
fix: handle terminal resizing correctly
  • Loading branch information
smlx authored Dec 23, 2022
2 parents d16917d + 0ff7062 commit 89b5ad7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
11 changes: 7 additions & 4 deletions internal/k8s/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"time"

"github.com/gliderlabs/ssh"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -191,16 +192,18 @@ func (c *Client) getExecutor(ctx context.Context, namespace, deployment,
// shell, running in a pod inside the deployment.
func (c *Client) Exec(ctx context.Context, namespace, deployment,
container string, command []string, stdio io.ReadWriter, stderr io.Writer,
tty bool) error {
tty bool, winch <-chan ssh.Window) error {
exec, err := c.getExecutor(ctx, namespace, deployment, container, command,
stderr, tty)
if err != nil {
return fmt.Errorf("couldn't get executor: %v", err)
}
// execute the command
return exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: stdio,
Stdout: stdio,
Stderr: stderr,
Stdin: stdio,
Stdout: stdio,
Stderr: stderr,
Tty: tty,
TerminalSizeQueue: newTermSizeQueue(ctx, winch),
})
}
40 changes: 40 additions & 0 deletions internal/k8s/termsizequeue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package k8s

import (
"context"

"github.com/gliderlabs/ssh"
"k8s.io/client-go/tools/remotecommand"
)

type termSizeQueue struct {
send chan *remotecommand.TerminalSize
}

// newTermSizeQueue returns a termSizeQueue which implements the
// remotecommand.TerminalSizeQueue interface. It starts a goroutine which exits
// when the given context is done.
func newTermSizeQueue(ctx context.Context, winch <-chan ssh.Window) *termSizeQueue {
tsq := termSizeQueue{
send: make(chan *remotecommand.TerminalSize, 1),
}
go func() {
for {
select {
case <-ctx.Done():
close(tsq.send)
return
case window := <-winch:
tsq.send <- &remotecommand.TerminalSize{
Width: uint16(window.Width),
Height: uint16(window.Height),
}
}
}
}()
return &tsq
}

func (t *termSizeQueue) Next() *remotecommand.TerminalSize {
return <-t.send
}
6 changes: 3 additions & 3 deletions internal/sshserver/sessionhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func sessionHandler(log *zap.Logger, c *k8s.Client, sftp bool) ssh.Handler {
}
return
}
// check if a pty was requested
_, _, pty := s.Pty()
// check if a pty was requested, and get the window size channel
_, winch, pty := s.Pty()
// extract info passed through the context by the authhandler
ctx := s.Context()
eid, ok := ctx.Value(environmentIDKey).(int)
Expand Down Expand Up @@ -137,7 +137,7 @@ func sessionHandler(log *zap.Logger, c *k8s.Client, sftp bool) ssh.Handler {
zap.Strings("command", cmd),
)
err = c.Exec(s.Context(), s.User(), deployment, container, cmd, s,
s.Stderr(), pty)
s.Stderr(), pty, winch)
if err != nil {
log.Warn("couldn't execute command",
zap.String("sessionID", sid),
Expand Down

0 comments on commit 89b5ad7

Please sign in to comment.