From 255623bd7c0277f8b8c61e66fdf873d5a43778a3 Mon Sep 17 00:00:00 2001 From: niusmallnan Date: Wed, 14 Apr 2021 13:34:14 +0800 Subject: [PATCH] K-EXPLORER: fix the k8s/local ws issue --- pkg/server/handler/apiserver.go | 4 ++-- pkg/server/handler/handlers.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/server/handler/apiserver.go b/pkg/server/handler/apiserver.go index aaf1a65..ca830da 100644 --- a/pkg/server/handler/apiserver.go +++ b/pkg/server/handler/apiserver.go @@ -48,9 +48,9 @@ func New(cfg *rest.Config, sf schema.Factory, authMiddleware auth.Middleware, ne APIRoot: w(a.apiHandler(apiRoot)), } if routerFunc == nil { - return a.server, router.Routes(handlers), nil + return a.server, rewriteLocalCluster(router.Routes(handlers)), nil } - return a.server, routerFunc(handlers), nil + return a.server, rewriteLocalCluster(routerFunc(handlers)), nil } type apiServer struct { diff --git a/pkg/server/handler/handlers.go b/pkg/server/handler/handlers.go index 0b4fa3e..cc46b02 100644 --- a/pkg/server/handler/handlers.go +++ b/pkg/server/handler/handlers.go @@ -1,6 +1,9 @@ package handler import ( + "net/http" + "strings" + "github.com/gorilla/mux" "github.com/rancher/apiserver/pkg/types" "github.com/rancher/steve/pkg/attributes" @@ -35,3 +38,15 @@ func k8sAPI(sf schema.Factory, apiOp *types.APIRequest) { func apiRoot(sf schema.Factory, apiOp *types.APIRequest) { apiOp.Type = "apiRoot" } + +func rewriteLocalCluster(next http.Handler) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + if strings.HasPrefix(req.URL.Path, "/k8s/clusters/local") { + req.URL.Path = strings.TrimPrefix(req.URL.Path, "/k8s/clusters/local") + if req.URL.Path == "" { + req.URL.Path = "/" + } + } + next.ServeHTTP(rw, req) + }) +}