refactor code structure (#1738)

This commit is contained in:
zryfish
2020-01-04 12:44:54 +08:00
committed by GitHub
parent eceadec69c
commit c40d1542a2
50 changed files with 695 additions and 456 deletions

View File

@@ -29,5 +29,5 @@ func init() {
}
func Install(c *restful.Container) {
urlruntime.Must(v1alpha2.AddToContainer(c))
urlruntime.Must(v1alpha2.AddToContainer(c, nil, nil))
}

View 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)
}

View File

@@ -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)