From 5d9aa0d5d1b7f116918b60a4b73857c3a027dad0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=84=8F=E7=90=A6=E8=A1=8C?=
<31980412+lixd@users.noreply.github.com>
Date: Mon, 4 Jul 2022 02:00:32 -0400
Subject: [PATCH] close remote terminal.(#5023) (#5024)
close remote terminal.(kubesphere#5023)
Co-authored-by: lixueduan
---
pkg/models/terminal/terminal.go | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/pkg/models/terminal/terminal.go b/pkg/models/terminal/terminal.go
index fffd712c7..fc730d58a 100644
--- a/pkg/models/terminal/terminal.go
+++ b/pkg/models/terminal/terminal.go
@@ -44,6 +44,8 @@ import (
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
+ // ctrl+d to close terminal.
+ endOfTransmission = "\u0004"
)
// PtyHandler is what remotecommand expects from a pty
@@ -76,7 +78,7 @@ type TerminalMessage struct {
Rows, Cols uint16
}
-// TerminalSize handles pty->process resize events
+// Next handles pty->process resize events
// Called in a loop from remotecommand as long as the process is running
func (t TerminalSession) Next() *remotecommand.TerminalSize {
size := <-t.sizeChan
@@ -93,7 +95,7 @@ func (t TerminalSession) Read(p []byte) (int, error) {
var msg TerminalMessage
err := t.conn.ReadJSON(&msg)
if err != nil {
- return 0, err
+ return copy(p, endOfTransmission), err
}
switch msg.Op {
@@ -103,7 +105,7 @@ func (t TerminalSession) Read(p []byte) (int, error) {
t.sizeChan <- remotecommand.TerminalSize{Width: msg.Cols, Height: msg.Rows}
return 0, nil
default:
- return 0, fmt.Errorf("unknown message type '%s'", msg.Op)
+ return copy(p, endOfTransmission), fmt.Errorf("unknown message type '%s'", msg.Op)
}
}
@@ -213,7 +215,7 @@ func (n *NodeTerminaler) getNSEnterPod() (*v1.Pod, error) {
pod, err := n.client.CoreV1().Pods(n.Namespace).Get(context.Background(), n.PodName, metav1.GetOptions{})
if err != nil || (pod.Status.Phase != v1.PodRunning && pod.Status.Phase != v1.PodPending) {
- //pod has timed out, but has not been cleaned up
+ // pod has timed out, but has not been cleaned up
if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
err := n.client.CoreV1().Pods(n.Namespace).Delete(context.Background(), n.PodName, metav1.DeleteOptions{})
if err != nil {