mirror of
https://github.com/1Panel-dev/KubePi.git
synced 2025-10-05 15:26:58 +08:00
58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package cluster
|
|
|
|
import (
|
|
"github.com/KubeOperator/kubepi/internal/service/v1/common"
|
|
"github.com/KubeOperator/kubepi/pkg/kubernetes"
|
|
"github.com/KubeOperator/kubepi/pkg/terminal"
|
|
"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")
|
|
|
|
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
|
|
}
|
|
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)
|
|
resp := TerminalResponse{ID: sessionID}
|
|
ctx.Values().Set("data", resp)
|
|
}
|
|
}
|