refactor kubectl websocket API
Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
@@ -19,38 +19,32 @@ package terminal
|
||||
|
||||
import (
|
||||
"github.com/emicklei/go-restful"
|
||||
"gopkg.in/igm/sockjs-go.v2/sockjs"
|
||||
"github.com/gorilla/websocket"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/models/terminal"
|
||||
"kubesphere.io/kubesphere/pkg/server/errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// TerminalResponse is sent by handleExecShell. The Id is a random session id that binds the original REST request and the SockJS connection.
|
||||
// Any clientapi in possession of this Id can hijack the terminal session.
|
||||
type TerminalResponse struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
// CreateAttachHandler is called from main for /api/sockjs
|
||||
func NewTerminalHandler(path string) http.Handler {
|
||||
return sockjs.NewHandler(path, sockjs.DefaultOptions, terminal.HandleTerminalSession)
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
// Allow connections from any Origin
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
// Handles execute shell API call
|
||||
func CreateTerminalSession(request *restful.Request, resp *restful.Response) {
|
||||
func HandleTerminalSession(request *restful.Request, resp *restful.Response) {
|
||||
|
||||
namespace := request.PathParameter("namespace")
|
||||
podName := request.PathParameter("pod")
|
||||
containerName := request.QueryParameter("container")
|
||||
shell := request.QueryParameter("shell")
|
||||
|
||||
sessionId, err := terminal.NewSession(shell, namespace, podName, containerName)
|
||||
|
||||
conn, err := upgrader.Upgrade(resp.ResponseWriter, request.Request, nil)
|
||||
if err != nil {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
klog.Warning(err)
|
||||
return
|
||||
}
|
||||
|
||||
TerminalResponse := &TerminalResponse{Id: sessionId}
|
||||
resp.WriteAsJson(TerminalResponse)
|
||||
terminal.HandleSession(shell, namespace, podName, containerName, conn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user