Skip to content

Commit

Permalink
feat: Simple proxy for kube-explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
orangedeng committed Oct 8, 2024
1 parent d4546e6 commit 99f81b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ARG TARGETARCH
ARG TARGETOS

ENV TARGETPLATFORM=${TARGETPLATFORM:-"linux/amd64"} ARCH=${TARGETARCH:-"amd64"} OS=${TARGETOS:-"linux"}
ENV KUBE_EXPLORER_VERSION=v0.4.0
ENV KUBE_EXPLORER_VERSION=v0.5.0
ENV HELM_DASHBOARD_VERSION=1.3.3

RUN zypper -n install curl ca-certificates tar gzip
Expand Down
24 changes: 17 additions & 7 deletions pkg/server/proxy/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package proxy
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"

"github.com/cnrancher/autok3s/pkg/common"
"github.com/gorilla/mux"
"github.com/rancher/apiserver/pkg/urlbuilder"
)

type ExplorerHandler struct {
Expand Down Expand Up @@ -47,13 +49,21 @@ func (ep *ExplorerHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)
_, _ = rw.Write([]byte(err.Error()))
return
}
prefix := fmt.Sprintf("/proxy/explorer/%s", clusterID)

u.Path = strings.TrimPrefix(req.URL.Path, fmt.Sprintf("/proxy/explorer/%s", clusterID))
u.RawQuery = req.URL.RawQuery
req.URL.Host = req.Host

ph := RemoteHandler{
Location: u,
proxy := &httputil.ReverseProxy{}
proxy.Director = func(req *http.Request) {
scheme := urlbuilder.GetScheme(req)
host := urlbuilder.GetHost(req, scheme)
req.Header.Set(urlbuilder.ForwardedProtoHeader, scheme)
req.Header.Set(urlbuilder.ForwardedHostHeader, host)
req.Header.Set(urlbuilder.PrefixHeader, prefix)
req.URL.Scheme = u.Scheme
req.URL.Host = u.Host
req.URL.Path = strings.TrimPrefix(req.URL.Path, prefix)
if req.URL.Path == "" {
req.URL.Path = "/"
}
}
ph.ServeHTTP(rw, req)
proxy.ServeHTTP(rw, req)
}

0 comments on commit 99f81b4

Please sign in to comment.