/
k8s_proxy.go
60 lines (51 loc) · 1.48 KB
/
k8s_proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package proxy
import (
"net/http"
"net/url"
"strings"
"github.com/cnrancher/autok3s/pkg/utils"
"github.com/sirupsen/logrus"
k8sproxy "k8s.io/apimachinery/pkg/util/proxy"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
type K8sProxyHandler struct{}
func NewK8sProxy() http.Handler {
return &K8sProxyHandler{}
}
type errorResponder struct{}
func (r *errorResponder) Error(w http.ResponseWriter, _ *http.Request, err error) {
logrus.Errorf("Error while proxying request: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
func (kh *K8sProxyHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
config := req.Header.Get("config")
cfg, err := clientcmd.RESTConfigFromKubeConfig([]byte(utils.StringSupportBase64(config)))
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(err.Error()))
return
}
host := cfg.Host
if !strings.HasSuffix(host, "/") {
host = host + "/"
}
u, err := url.Parse(host)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(err.Error()))
return
}
u.Path = strings.TrimPrefix(req.URL.Path, "/k8s/proxy")
u.RawQuery = req.URL.RawQuery
req.URL.Host = req.Host
responder := &errorResponder{}
transport, err := rest.TransportFor(cfg)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(err.Error()))
return
}
handler := k8sproxy.NewUpgradeAwareHandler(u, transport, true, false, responder)
handler.ServeHTTP(rw, req)
}