Repository navigation
Expand file tree
/
Copy pathhandlers.go
More file actions
99 lines (82 loc) · 2.75 KB
/
Copy pathhandlers.go
File metadata and controls
99 lines (82 loc) · 2.75 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package srv
import (
"bytes"
"fmt"
"net/http"
"regexp"
"github.com/julienschmidt/httprouter"
"github.com/linkerd/linkerd2/pkg/k8s"
profiles "github.com/linkerd/linkerd2/pkg/profiles"
vizPb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
var proxyPathRegexp = regexp.MustCompile("/api/v1/namespaces/.*/proxy/")
type (
renderTemplate func(http.ResponseWriter, string, string, interface{}) error
handler struct {
render renderTemplate
apiClient vizPb.ApiClient
k8sAPI *k8s.KubernetesAPI
uuid string
version string
controllerNamespace string
clusterDomain string
grafana string
grafanaExternalURL string
grafanaPrefix string
jaeger string
grafanaProxy *reverseProxy
jaegerProxy *reverseProxy
hc healthChecker
statCache *cache.Cache
}
)
func (h *handler) handleIndex(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
// when running the dashboard via `linkerd dashboard`, serve the index bundle at the right path
pathPfx := proxyPathRegexp.FindString(req.URL.Path)
if pathPfx == "" {
pathPfx = "/"
}
params := appParams{
UUID: h.uuid,
ReleaseVersion: h.version,
ControllerNamespace: h.controllerNamespace,
PathPrefix: pathPfx,
Grafana: h.grafana,
GrafanaExternalURL: h.grafanaExternalURL,
GrafanaPrefix: h.grafanaPrefix,
Jaeger: h.jaeger,
}
err := h.render(w, "app.tmpl.html", "base", params)
if err != nil {
log.Error(err)
}
}
func (h *handler) handleProfileDownload(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
service := req.FormValue("service")
namespace := req.FormValue("namespace")
if service == "" || namespace == "" {
err := fmt.Errorf("Service and namespace must be provided to create a new profile")
log.Error(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
profileYaml := &bytes.Buffer{}
err := profiles.RenderProfileTemplate(namespace, service, h.clusterDomain, profileYaml, "yaml")
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dispositionHeaderVal := fmt.Sprintf("attachment; filename=%s-profile.yml", service)
w.Header().Set("Content-Type", "text/yaml")
w.Header().Set("Content-Disposition", dispositionHeaderVal)
w.Write(profileYaml.Bytes())
}
func (h *handler) handleGrafana(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
h.grafanaProxy.ServeHTTP(w, req)
}
func (h *handler) handleJaeger(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
h.jaegerProxy.ServeHTTP(w, req)
}