Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[branch/v9] Rollup backport of session fixes #11494

Merged
merged 5 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/kube/proxy/sess.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ func (s *session) launch() error {
}()

s.log.Debugf("Launching session: %v", s.id)
s.BroadcastMessage("Connecting to %v over K8S", s.podName)

q := s.req.URL.Query()
request := &remoteCommandRequest{
Expand All @@ -463,6 +462,9 @@ func (s *session) launch() error {
pingPeriod: s.forwarder.cfg.ConnPingPeriod,
}

s.podName = request.podName
s.BroadcastMessage("Connecting to %v over K8S", s.podName)

eventPodMeta := request.eventPodMeta(request.context, s.sess.creds)
s.io.OnWriteError = func(idString string, err error) {
s.mu.Lock()
Expand Down Expand Up @@ -541,7 +543,6 @@ func (s *session) launch() error {
}
}()

s.podName = request.podName
err = s.trackerUpdateState(types.SessionState_SessionStateRunning)
if err != nil {
s.log.Warn("Failed to set tracker state to running")
Expand Down
33 changes: 18 additions & 15 deletions lib/srv/termmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import (
log "github.com/sirupsen/logrus"
)

const maxHistory = 1000
// maxHistoryBytes is the maximum bytes that are retained as history and broadcasted to new clients.
const maxHistoryBytes = 1000

// maxPausedHistoryBytes is maximum bytes that are buffered when a session is paused.
const maxPausedHistoryBytes = 10000

// TermManager handles the streams of terminal-like sessions.
// It performs a number of tasks including:
Expand Down Expand Up @@ -70,18 +74,7 @@ func NewTermManager() *TermManager {

func (g *TermManager) writeToClients(p []byte) int {
g.lastWasBroadcast = false
truncateFront := func(slice []byte, max int) []byte {
if len(slice) > max {
return slice[len(slice)-max:]
}

return slice
}

g.history = append(g.history, truncateFront(p, maxHistory)...)
if len(g.history) > maxHistory {
g.history = g.history[:maxHistory]
}
g.history = truncateFront(append(g.history, p...), maxHistoryBytes)

atomic.AddUint64(&g.countWritten, uint64(len(p)))
for key, w := range g.writers {
Expand Down Expand Up @@ -113,7 +106,9 @@ func (g *TermManager) Write(p []byte) (int, error) {
if g.on {
g.writeToClients(p)
} else {
g.buffer = append(g.buffer, p...)
// Only keep the last maxPausedHistoryBytes of stdout/stderr while the session is paused.
// The alternative is flushing to disk but this should be a pretty rare occurrence and shouldn't be an issue in practice.
g.buffer = truncateFront(append(g.buffer, p...), maxPausedHistoryBytes)
}

return len(p), nil
Expand Down Expand Up @@ -282,7 +277,15 @@ func (g *TermManager) Close() {
func (g *TermManager) GetRecentHistory() []byte {
g.mu.Lock()
defer g.mu.Unlock()
data := make([]byte, len(g.history))
data := make([]byte, 0, len(g.history))
data = append(data, g.history...)
return data
}

func truncateFront(slice []byte, max int) []byte {
if len(slice) > max {
return slice[len(slice)-max:]
}

return slice
}
38 changes: 38 additions & 0 deletions lib/srv/termmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package srv

import (
"crypto/rand"
"io"
"testing"
"time"
Expand Down Expand Up @@ -48,3 +49,40 @@ func TestCTRLCCapture(t *testing.T) {
t.Fatal("terminateNotifier should've seen an event")
}
}

func TestHistoryKept(t *testing.T) {
m := NewTermManager()
m.On()

data := make([]byte, 10000)
n, err := rand.Read(data)
require.NoError(t, err)
require.Equal(t, len(data), n)

n, err = m.Write(data[:len(data)/2])
require.NoError(t, err)
require.Equal(t, len(data)/2, n)

n, err = m.Write(data[len(data)/2:])
require.NoError(t, err)
require.Equal(t, len(data)/2, n)

kept := data[len(data)-maxHistoryBytes:]
require.Equal(t, m.GetRecentHistory(), kept)
}

func TestBufferedKept(t *testing.T) {
m := NewTermManager()

data := make([]byte, 20000)
n, err := rand.Read(data)
require.NoError(t, err)
require.Equal(t, len(data), n)

n, err = m.Write(data)
require.NoError(t, err)
require.Equal(t, len(data), n)

kept := data[len(data)-maxPausedHistoryBytes:]
require.Equal(t, m.buffer, kept)
}
5 changes: 5 additions & 0 deletions tool/tsh/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -489,6 +490,10 @@ func (c *kubeSessionsCommand) run(cf *CLIConf) error {
}
}

sort.Slice(filteredSessions, func(i, j int) bool {
return filteredSessions[i].GetCreated().Before(filteredSessions[j].GetCreated())
})

printSessions(filteredSessions)
return nil
}
Expand Down