/
prometheus.go
40 lines (37 loc) · 990 Bytes
/
prometheus.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
package proxy
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"github.com/KubeOperator/KubeOperator/pkg/constant"
"github.com/kataras/iris/v12/context"
)
func PrometheusProxy(ctx context.Context) {
clusterName := ctx.Params().Get("cluster_name")
proxyPath := ctx.Params().Get("p")
if clusterName == "" {
_, _ = ctx.JSON(http.StatusBadRequest)
return
}
endpoint, err := clusterService.GetRouterEndpoint(clusterName)
if err != nil {
_, _ = ctx.JSON(http.StatusInternalServerError)
return
}
host := fmt.Sprintf(constant.DefaultPrometheusIngress)
u, err := url.Parse(fmt.Sprintf("http://%s", endpoint.Address))
if err != nil {
_, _ = ctx.JSON(http.StatusInternalServerError)
return
}
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
req := ctx.Request()
req.Host = host
req.URL.Path = proxyPath
proxy.ServeHTTP(ctx.ResponseWriter(), req)
}