Skip to content

Commit d76bcfa

Browse files
committed
Fix lint errors and sock-js import
Signed-off-by: MdSayemkhan <[email protected]>
1 parent b3750ae commit d76bcfa

File tree

6 files changed

+37
-34
lines changed

6 files changed

+37
-34
lines changed

cmd/api/app/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ import (
4848
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/service" // Importing route packages forces route registration
4949
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/statefulset" // Importing route packages forces route registration
5050
_ "github.com/karmada-io/dashboard/cmd/api/app/routes/unstructured" // Importing route packages forces route registration
51+
"github.com/karmada-io/dashboard/cmd/api/terminalsetup"
5152
"github.com/karmada-io/dashboard/pkg/client"
5253
"github.com/karmada-io/dashboard/pkg/config"
5354
"github.com/karmada-io/dashboard/pkg/environment"
54-
"github.com/karmada-io/dashboard/cmd/api/terminalsetup"
5555
)
5656

5757
// NewAPICommand creates a *cobra.Command object with default parameters
@@ -146,5 +146,5 @@ func serve(opts *options.Options) {
146146

147147
// Custom function to initialize the application
148148
func webterminal() {
149-
terminalsetup.Init()// Call the init function
149+
terminalsetup.Init() // Call the init function
150150
}

cmd/api/app/routes/auth/me.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package auth
1919
import (
2020
"bytes"
2121
"encoding/json"
22-
2322
"net/http"
23+
2424
"github.com/gin-gonic/gin"
2525
"github.com/golang-jwt/jwt/v5"
2626

@@ -33,6 +33,7 @@ const (
3333
tokenServiceAccountKey = "serviceaccount"
3434
)
3535

36+
// GetCurrentUser returns the current user from the context .
3637
func GetCurrentUser(c *gin.Context) (*v1.User, int, error) {
3738
return me(c.Request)
3839
}

cmd/api/terminalsetup/handler.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@ import (
2222
"encoding/hex"
2323
"encoding/json"
2424
"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"
2825
"io"
2926
"net/http"
3027
"sync"
3128
"time"
3229

30+
"github.com/gin-gonic/gin"
3331
"gopkg.in/igm/sockjs-go.v2/sockjs"
3432
v1 "k8s.io/api/core/v1"
3533
"k8s.io/client-go/kubernetes"
3634
"k8s.io/client-go/kubernetes/scheme"
3735
"k8s.io/client-go/rest"
3836
"k8s.io/client-go/tools/remotecommand"
3937
"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"
4141
)
4242

43-
const END_OF_TRANSMISSION = "\u0004"
43+
// ENDOFTRANSMISSION signals the end of data transmission in the terminal session.
44+
const ENDOFTRANSMISSION = "\u0004"
4445

4546
// PtyHandler is what remotecommand expects from a pty
4647
type PtyHandler interface {
@@ -87,12 +88,12 @@ func (t TerminalSession) Read(p []byte) (int, error) {
8788
m, err := t.sockJSSession.Recv()
8889
if err != nil {
8990
// Send terminated signal to process to avoid resource leak
90-
return copy(p, END_OF_TRANSMISSION), err
91+
return copy(p, ENDOFTRANSMISSION), err
9192
}
9293

9394
var msg TerminalMessage
9495
if err := json.Unmarshal([]byte(m), &msg); err != nil {
95-
return copy(p, END_OF_TRANSMISSION), err
96+
return copy(p, ENDOFTRANSMISSION), err
9697
}
9798

9899
switch msg.Op {
@@ -102,7 +103,7 @@ func (t TerminalSession) Read(p []byte) (int, error) {
102103
t.sizeChan <- remotecommand.TerminalSize{Width: msg.Cols, Height: msg.Rows}
103104
return 0, nil
104105
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)
106107
}
107108
}
108109

@@ -146,33 +147,33 @@ type SessionMap struct {
146147
Lock sync.RWMutex
147148
}
148149

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 {
151152
sm.Lock.RLock()
152153
defer sm.Lock.RUnlock()
153-
return sm.Sessions[sessionId]
154+
return sm.Sessions[sessionID]
154155
}
155156

156157
// Set store a TerminalSession to SessionMap
157-
func (sm *SessionMap) Set(sessionId string, session TerminalSession) {
158+
func (sm *SessionMap) Set(sessionID string, session TerminalSession) {
158159
sm.Lock.Lock()
159160
defer sm.Lock.Unlock()
160-
sm.Sessions[sessionId] = session
161+
sm.Sessions[sessionID] = session
161162
}
162163

163164
// Close shuts down the SockJS connection and sends the status code and reason to the client
164165
// Can happen if the process exits or if there is an error starting up the process
165166
// 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) {
167168
sm.Lock.Lock()
168169
defer sm.Lock.Unlock()
169-
ses := sm.Sessions[sessionId]
170+
ses := sm.Sessions[sessionID]
170171
err := ses.sockJSSession.Close(status, reason)
171172
if err != nil {
172173
klog.Error(err)
173174
}
174175
close(ses.sizeChan)
175-
delete(sm.Sessions, sessionId)
176+
delete(sm.Sessions, sessionID)
176177
}
177178

178179
var terminalSessions = SessionMap{Sessions: make(map[string]TerminalSession)}
@@ -215,7 +216,6 @@ func handleTerminalSession(session sockjs.Session) {
215216
return
216217
}
217218

218-
219219
// Update the terminal session with the new SockJS session
220220
terminalSession.sockJSSession = session
221221

@@ -272,11 +272,11 @@ func startProcess(k8sClient kubernetes.Interface, cfg *rest.Config, terminalInfo
272272
return nil
273273
}
274274

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.
276276
// This ID is used to identify the session when the client opens the SockJS connection.
277277
// Not the same as the SockJS session id! We can't use that as that is generated
278278
// on the client side and we don't have it yet at this point.
279-
func genTerminalSessionId() (string, error) {
279+
func genTerminalSessionID() (string, error) {
280280
bytes := make([]byte, 16)
281281
if _, err := rand.Read(bytes); err != nil {
282282
return "", err
@@ -298,6 +298,8 @@ func isValidShell(validShells []string, shell string) bool {
298298

299299
// WaitForTerminal is called from apihandler.handleAttach as a goroutine
300300
// Waits for the SockJS connection to be opened by the client the session to be bound in handleTerminalSession
301+
//
302+
//revive:disable:var-naming
301303
func WaitForTerminal(k8sClient kubernetes.Interface, cfg *rest.Config, terminalInfo TerminalInfo, sessionId string) {
302304
shell := terminalInfo.Shell
303305

@@ -345,12 +347,12 @@ func GetToken(c *gin.Context) {
345347
c.JSON(http.StatusOK, gin.H{"token": rawToken})
346348
}
347349

350+
// Init initializes the terminal setup.
348351
func Init() {
349352
r := router.V1()
350353

351354
r.POST("/terminal", TriggerTerminal)
352355

353-
354356
r.Any("/terminal/sockjs/*w", gin.WrapH(CreateAttachHandler("/api/v1/terminal/sockjs")))
355357
r.GET("/terminal/pod/:namespace/:pod/shell/:container", handleExecShell)
356358
}

cmd/api/terminalsetup/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
package terminalsetup
1818

19+
// TerminalInfo represents the information required for terminal operations.
1920
type TerminalInfo struct {
2021
Shell string
2122
Namespace string

cmd/api/terminalsetup/terminal.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@ import (
2020
"bytes"
2121
"context"
2222
"fmt"
23-
"github.com/karmada-io/dashboard/cmd/api/app/routes/auth"
24-
v1 "github.com/karmada-io/dashboard/cmd/api/app/types/api/v1"
2523
"strings"
2624
"time"
2725

2826
"github.com/gin-gonic/gin"
29-
"github.com/karmada-io/dashboard/cmd/api/app/types/common"
30-
"github.com/karmada-io/dashboard/pkg/client"
31-
3227
corev1 "k8s.io/api/core/v1"
3328
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3429
"k8s.io/apimachinery/pkg/types"
@@ -40,7 +35,12 @@ import (
4035
"k8s.io/client-go/tools/clientcmd"
4136
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
4237
"k8s.io/client-go/tools/remotecommand"
43-
"k8s.io/utils/ptr"
38+
"k8s.io/utils/ptr"
39+
40+
"github.com/karmada-io/dashboard/cmd/api/app/routes/auth"
41+
v1 "github.com/karmada-io/dashboard/cmd/api/app/types/api/v1"
42+
"github.com/karmada-io/dashboard/cmd/api/app/types/common"
43+
"github.com/karmada-io/dashboard/pkg/client"
4444
)
4545

4646
// waitForPodReady polls until the Pod is Running and Ready, or times out.
@@ -219,6 +219,7 @@ func GenerateKubeConfig(token string) ([]byte, error) {
219219
return clientcmd.Write(cfg)
220220
}
221221

222+
// ExecIntoPodWithInput Inject the kubeconfig into the pod
222223
func ExecIntoPodWithInput(
223224
ctx context.Context,
224225
restCfg *rest.Config,
@@ -333,8 +334,6 @@ func TriggerTerminal(c *gin.Context) {
333334
return
334335
}
335336

336-
337-
338337
c.JSON(200, gin.H{
339338
"data": gin.H{
340339
"podName": pod.Name,
@@ -345,12 +344,13 @@ func TriggerTerminal(c *gin.Context) {
345344
})
346345
}
347346

347+
// TerminalResponse represents the response in WaitForTerminal.
348348
type TerminalResponse struct {
349349
ID string `json:"id"`
350350
}
351351

352352
func handleExecShell(c *gin.Context) {
353-
sessionID, err := genTerminalSessionId()
353+
sessionID, err := genTerminalSessionID()
354354
if err != nil {
355355
common.Fail(c, err)
356356
return
@@ -367,7 +367,6 @@ func handleExecShell(c *gin.Context) {
367367
sizeChan: make(chan remotecommand.TerminalSize),
368368
})
369369

370-
371370
//restfulRequest := restful.NewRequest(c.Request)
372371
//go WaitForTerminal(client.InClusterClient(), cfg, restfulRequest, sessionID)
373372
info := TerminalInfo{

ui/packages/terminal/src/container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import SockJS from 'sockjs-client/dist/sockjs';
17+
import SockJS from 'sockjs-client';
1818
import BaseTerminal from './base.ts';
1919
import { BaseTerminalOptions } from './typing';
2020
import { getDebugger } from './utils.ts';

0 commit comments

Comments
 (0)