@@ -22,25 +22,26 @@ import (
22
22
"encoding/hex"
23
23
"encoding/json"
24
24
"fmt"
25
- "github.com/gin-gonic/gin"
26
- "github.com/karmada-io/dashboard/cmd/api/app/router"
27
- "github.com/karmada-io/dashboard/pkg/client"
28
25
"io"
29
26
"net/http"
30
27
"sync"
31
28
"time"
32
29
30
+ "github.com/gin-gonic/gin"
33
31
"gopkg.in/igm/sockjs-go.v2/sockjs"
34
32
v1 "k8s.io/api/core/v1"
35
33
"k8s.io/client-go/kubernetes"
36
34
"k8s.io/client-go/kubernetes/scheme"
37
35
"k8s.io/client-go/rest"
38
36
"k8s.io/client-go/tools/remotecommand"
39
37
"k8s.io/klog/v2"
40
- //"k8s.io/dashboard/api/pkg/args"
38
+
39
+ "github.com/karmada-io/dashboard/cmd/api/app/router"
40
+ "github.com/karmada-io/dashboard/pkg/client"
41
41
)
42
42
43
- const END_OF_TRANSMISSION = "\u0004 "
43
+ // ENDOFTRANSMISSION signals the end of data transmission in the terminal session.
44
+ const ENDOFTRANSMISSION = "\u0004 "
44
45
45
46
// PtyHandler is what remotecommand expects from a pty
46
47
type PtyHandler interface {
@@ -87,12 +88,12 @@ func (t TerminalSession) Read(p []byte) (int, error) {
87
88
m , err := t .sockJSSession .Recv ()
88
89
if err != nil {
89
90
// Send terminated signal to process to avoid resource leak
90
- return copy (p , END_OF_TRANSMISSION ), err
91
+ return copy (p , ENDOFTRANSMISSION ), err
91
92
}
92
93
93
94
var msg TerminalMessage
94
95
if err := json .Unmarshal ([]byte (m ), & msg ); err != nil {
95
- return copy (p , END_OF_TRANSMISSION ), err
96
+ return copy (p , ENDOFTRANSMISSION ), err
96
97
}
97
98
98
99
switch msg .Op {
@@ -102,7 +103,7 @@ func (t TerminalSession) Read(p []byte) (int, error) {
102
103
t .sizeChan <- remotecommand.TerminalSize {Width : msg .Cols , Height : msg .Rows }
103
104
return 0 , nil
104
105
default :
105
- return copy (p , END_OF_TRANSMISSION ), fmt .Errorf ("unknown message type '%s'" , msg .Op )
106
+ return copy (p , ENDOFTRANSMISSION ), fmt .Errorf ("unknown message type '%s'" , msg .Op )
106
107
}
107
108
}
108
109
@@ -146,33 +147,33 @@ type SessionMap struct {
146
147
Lock sync.RWMutex
147
148
}
148
149
149
- // Get return a given terminalSession by sessionId
150
- func (sm * SessionMap ) Get (sessionId string ) TerminalSession {
150
+ // Get returns a given TerminalSession by sessionID.
151
+ func (sm * SessionMap ) Get (sessionID string ) TerminalSession {
151
152
sm .Lock .RLock ()
152
153
defer sm .Lock .RUnlock ()
153
- return sm .Sessions [sessionId ]
154
+ return sm .Sessions [sessionID ]
154
155
}
155
156
156
157
// Set store a TerminalSession to SessionMap
157
- func (sm * SessionMap ) Set (sessionId string , session TerminalSession ) {
158
+ func (sm * SessionMap ) Set (sessionID string , session TerminalSession ) {
158
159
sm .Lock .Lock ()
159
160
defer sm .Lock .Unlock ()
160
- sm .Sessions [sessionId ] = session
161
+ sm .Sessions [sessionID ] = session
161
162
}
162
163
163
164
// Close shuts down the SockJS connection and sends the status code and reason to the client
164
165
// Can happen if the process exits or if there is an error starting up the process
165
166
// For now the status code is unused and reason is shown to the user (unless "")
166
- func (sm * SessionMap ) Close (sessionId string , status uint32 , reason string ) {
167
+ func (sm * SessionMap ) Close (sessionID string , status uint32 , reason string ) {
167
168
sm .Lock .Lock ()
168
169
defer sm .Lock .Unlock ()
169
- ses := sm .Sessions [sessionId ]
170
+ ses := sm .Sessions [sessionID ]
170
171
err := ses .sockJSSession .Close (status , reason )
171
172
if err != nil {
172
173
klog .Error (err )
173
174
}
174
175
close (ses .sizeChan )
175
- delete (sm .Sessions , sessionId )
176
+ delete (sm .Sessions , sessionID )
176
177
}
177
178
178
179
var terminalSessions = SessionMap {Sessions : make (map [string ]TerminalSession )}
@@ -215,7 +216,6 @@ func handleTerminalSession(session sockjs.Session) {
215
216
return
216
217
}
217
218
218
-
219
219
// Update the terminal session with the new SockJS session
220
220
terminalSession .sockJSSession = session
221
221
@@ -272,11 +272,11 @@ func startProcess(k8sClient kubernetes.Interface, cfg *rest.Config, terminalInfo
272
272
return nil
273
273
}
274
274
275
- // genTerminalSessionId generates a random session ID string. The format is not really interesting.
275
+ // genTerminalSessionID generates a random session ID string. The format is not really interesting.
276
276
// This ID is used to identify the session when the client opens the SockJS connection.
277
277
// Not the same as the SockJS session id! We can't use that as that is generated
278
278
// on the client side and we don't have it yet at this point.
279
- func genTerminalSessionId () (string , error ) {
279
+ func genTerminalSessionID () (string , error ) {
280
280
bytes := make ([]byte , 16 )
281
281
if _ , err := rand .Read (bytes ); err != nil {
282
282
return "" , err
@@ -298,6 +298,8 @@ func isValidShell(validShells []string, shell string) bool {
298
298
299
299
// WaitForTerminal is called from apihandler.handleAttach as a goroutine
300
300
// Waits for the SockJS connection to be opened by the client the session to be bound in handleTerminalSession
301
+ //
302
+ //revive:disable:var-naming
301
303
func WaitForTerminal (k8sClient kubernetes.Interface , cfg * rest.Config , terminalInfo TerminalInfo , sessionId string ) {
302
304
shell := terminalInfo .Shell
303
305
@@ -345,12 +347,12 @@ func GetToken(c *gin.Context) {
345
347
c .JSON (http .StatusOK , gin.H {"token" : rawToken })
346
348
}
347
349
350
+ // Init initializes the terminal setup.
348
351
func Init () {
349
352
r := router .V1 ()
350
353
351
354
r .POST ("/terminal" , TriggerTerminal )
352
355
353
-
354
356
r .Any ("/terminal/sockjs/*w" , gin .WrapH (CreateAttachHandler ("/api/v1/terminal/sockjs" )))
355
357
r .GET ("/terminal/pod/:namespace/:pod/shell/:container" , handleExecShell )
356
358
}
0 commit comments