Update public_routes.go, fetchRunner.go, and 3 more files...

This commit is contained in:
RamiBerm
2021-07-11 13:07:25 +03:00
parent b3dcff2cd5
commit b1df4b69ae
5 changed files with 44 additions and 115 deletions

View File

@@ -1,14 +1,17 @@
package kubernetes
import (
"context"
"fmt"
"k8s.io/kubectl/pkg/proxy"
"net"
"net/http"
"net/url"
"time"
)
func StartProxy(ctx context.Context, kubernetesProvider *Provider, mizuPort uint16, mizuNamespace string, mizuServiceName string) error {
//o := cmdProxy.NewProxyOptions(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
const k8sProxyApiPrefix = "/"
func StartProxy(kubernetesProvider *Provider, mizuPort uint16, mizuNamespace string, mizuServiceName string) error {
filter := &proxy.FilterServer{
AcceptPaths: proxy.MakeRegexpArrayOrDie(proxy.DefaultPathAcceptRE),
RejectPaths: proxy.MakeRegexpArrayOrDie(proxy.DefaultPathRejectRE),
@@ -16,26 +19,37 @@ func StartProxy(ctx context.Context, kubernetesProvider *Provider, mizuPort uint
RejectMethods: proxy.MakeRegexpArrayOrDie(proxy.DefaultMethodRejectRE),
}
server, err := proxy.NewServer("", "/", "/static/", filter, &kubernetesProvider.clientConfig, time.Second * 1)
mizuProxiedUrl := GetMizuCollectorProxiesHostAndPath(mizuPort, mizuNamespace, mizuServiceName)
proxyHandler, err := proxy.NewProxyHandler(k8sProxyApiPrefix, filter, &kubernetesProvider.clientConfig, time.Second * 2)
if err != nil {
return err
}
mux := http.NewServeMux()
mux.Handle(k8sProxyApiPrefix, proxyHandler)
//work around to make static resources available to the dashboard (all .svgs will not load without this)
mux.Handle("/static/", getRerouteHttpHandler(proxyHandler, mizuProxiedUrl))
l, err := server.Listen("127.0.0.1", int(mizuPort))
//l, err := server.Listen("127.0.0.1", int(mizuPort))
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "127.0.0.1", int(mizuPort)))
if err != nil {
return err
}
go func() {
for {
select {
case <-ctx.Done():
fmt.Printf("Closing connection due to context done")
err := l.Close()
if err != nil {
fmt.Printf("Error stopping proxy network handler %v", err)
}
return
}
}
}()
fmt.Printf("Mizu is available at http://localhost:%d/api/v1/namespaces/%s/services/%s:80/proxy\n", mizuPort, mizuNamespace, mizuServiceName)
return server.ServeOnListener(l)
server := http.Server{
Handler: mux,
}
return server.Serve(l)
}
func GetMizuCollectorProxiesHostAndPath(mizuPort uint16, mizuNamespace string, mizuServiceName string) string {
return fmt.Sprintf("localhost:%d/api/v1/namespaces/%s/services/%s:80/proxy", mizuPort, mizuNamespace, mizuServiceName)
}
// rewrites requests so they end up reaching the mizu-collector k8s service via the k8s proxy handler
func getRerouteHttpHandler(proxyHandler http.Handler, mizuProxyUrl string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
newUrl, _ := url.Parse(fmt.Sprintf("http://%s%s", mizuProxyUrl, r.URL.Path))
r.URL = newUrl
proxyHandler.ServeHTTP(w, r)
})
}