forked from 1Panel-dev/KubePi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
61 lines (57 loc) · 1.68 KB
/
terminal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cluster
import (
"github.com/godtool/kubeone/pkg/kubernetes"
"github.com/godtool/kubeone/pkg/terminal"
"github.com/godtool/kubeone/service/service/v1/common"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"k8s.io/client-go/tools/remotecommand"
)
type TerminalResponse struct {
ID string `json:"id"`
}
func (h *Handler) TerminalSessionHandler() iris.Handler {
return func(ctx *context.Context) {
namespace := ctx.URLParam("namespace")
podName := ctx.URLParam("podName")
containerName := ctx.URLParam("containerName")
shell := ctx.URLParam("shell")
sessionID, err := terminal.GenTerminalSessionId()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
clusterName := ctx.Params().GetString("name")
c, err := h.clusterService.Get(clusterName, common.DBOptions{})
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
k := kubernetes.NewKubernetes(c)
conf, err := k.Config()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
client, err := k.Client()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err)
return
}
if shell == "" {
shell = "sh"
}
terminal.TerminalSessions.Set(sessionID, terminal.TerminalSession{
Id: sessionID,
Bound: make(chan error),
SizeChan: make(chan remotecommand.TerminalSize),
})
go terminal.WaitForTerminal(client, conf, namespace, podName, containerName, sessionID, shell)
resp := TerminalResponse{ID: sessionID}
ctx.Values().Set("data", resp)
}
}