refactor code structure (#1738)
This commit is contained in:
43
pkg/kapis/terminal/v1alpha2/handler.go
Normal file
43
pkg/kapis/terminal/v1alpha2/handler.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package v1alpha2
|
||||
|
||||
import (
|
||||
"github.com/emicklei/go-restful"
|
||||
"github.com/gorilla/websocket"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/models/terminal"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
// Allow connections from any Origin
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
type terminalHandler struct {
|
||||
terminaler terminal.Interface
|
||||
}
|
||||
|
||||
func newTerminalHandler(client kubernetes.Interface, config *rest.Config) *terminalHandler {
|
||||
return &terminalHandler{
|
||||
terminaler: terminal.NewTerminaler(client, config),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *terminalHandler) handleTerminalSession(request *restful.Request, response *restful.Response) {
|
||||
namespace := request.PathParameter("namespace")
|
||||
podName := request.PathParameter("pod")
|
||||
containerName := request.QueryParameter("container")
|
||||
shell := request.QueryParameter("shell")
|
||||
|
||||
conn, err := upgrader.Upgrade(response.ResponseWriter, request.Request, nil)
|
||||
if err != nil {
|
||||
klog.Warning(err)
|
||||
return
|
||||
}
|
||||
|
||||
t.terminaler.HandleSession(shell, namespace, podName, containerName, conn)
|
||||
}
|
||||
@@ -21,30 +21,29 @@ import (
|
||||
"github.com/emicklei/go-restful"
|
||||
"github.com/emicklei/go-restful-openapi"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/terminal"
|
||||
"kubesphere.io/kubesphere/pkg/models"
|
||||
)
|
||||
|
||||
const GroupName = "terminal.kubesphere.io"
|
||||
const (
|
||||
GroupName = "terminal.kubesphere.io"
|
||||
tag = "Terminal"
|
||||
)
|
||||
|
||||
var GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1alpha2"}
|
||||
|
||||
var (
|
||||
WebServiceBuilder = runtime.NewContainerBuilder(addWebService)
|
||||
AddToContainer = WebServiceBuilder.AddToContainer
|
||||
)
|
||||
|
||||
func addWebService(c *restful.Container) error {
|
||||
func AddToContainer(c *restful.Container, client kubernetes.Interface, config *rest.Config) error {
|
||||
|
||||
webservice := runtime.NewWebService(GroupVersion)
|
||||
|
||||
tags := []string{"Terminal"}
|
||||
handler := newTerminalHandler(client, config)
|
||||
|
||||
webservice.Route(webservice.GET("/namespaces/{namespace}/pods/{pod}").
|
||||
To(terminal.HandleTerminalSession).
|
||||
To(handler.handleTerminalSession).
|
||||
Doc("create terminal session").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{tag}).
|
||||
Writes(models.PodInfo{}))
|
||||
|
||||
c.Add(webservice)
|
||||
|
||||
Reference in New Issue
Block a user