forked from openservicemesh/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
105 lines (95 loc) · 3.42 KB
/
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
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
100
101
102
103
104
105
package debugger
import (
"fmt"
"net/http"
"sort"
"strconv"
"time"
"github.com/openservicemesh/osm/pkg/models"
)
const (
streamIDQueryKey = "stream-id"
proxyConfigQueryKey = "cfg"
)
func (ds DebugConfig) getProxies() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
proxyConfigDump := r.URL.Query()[proxyConfigQueryKey]
streamIDQ := r.URL.Query()[streamIDQueryKey]
if len(streamIDQ) == 0 {
ds.printProxies(w)
return
}
streamID, err := strconv.ParseInt(streamIDQ[0], 10, 64)
if err != nil {
msg := fmt.Sprintf("couldn't parse streamID %s", streamIDQ[0])
log.Error().Msg(msg)
http.Error(w, msg, http.StatusBadRequest)
return
}
if len(proxyConfigDump) > 0 {
ds.getConfigDump(streamID, w)
} else {
ds.getProxy(streamID, w)
}
})
}
func (ds DebugConfig) printProxies(w http.ResponseWriter) {
// This function is needed to convert the list of connected proxies to
// the type (map) required by the printProxies function.
proxyMap := ds.proxyRegistry.ListConnectedProxies()
proxies := make([]*models.Proxy, 0, len(proxyMap))
for _, proxy := range proxyMap {
proxies = append(proxies, proxy)
}
sort.Slice(proxies, func(i, j int) bool {
return proxies[i].Identity.String() < proxies[j].Identity.String()
})
_, _ = fmt.Fprintf(w, "<h1>Connected Proxies (%d):</h1>", len(proxies))
_, _ = fmt.Fprint(w, `<table>`)
_, _ = fmt.Fprint(w, "<tr><td>#</td><td>Envoy's Service Identity</td><td>Envoy's UUID</td><td>Connected At</td><td>How long ago</td><td>tools</td></tr>")
for idx, proxy := range proxies {
ts := proxy.GetConnectedAt()
proxyURL := fmt.Sprintf("/debug/proxy?%s=%d", streamIDQueryKey, proxy.GetConnectionID())
configDumpURL := fmt.Sprintf("%s&%s=%t", proxyURL, proxyConfigQueryKey, true)
_, _ = fmt.Fprintf(w, `<tr><td>%d:</td><td>%s</td><td>%s</td><td>%+v</td><td>(%+v ago)</td><td><a href="%s">certs</a></td><td><a href="%s">cfg</a></td></tr>`,
idx+1, proxy.Identity, proxy.UUID, ts, time.Since(ts), proxyURL, configDumpURL)
}
_, _ = fmt.Fprint(w, `</table>`)
}
func (ds DebugConfig) getConfigDump(streamID int64, w http.ResponseWriter) {
proxy := ds.proxyRegistry.GetConnectedProxy(streamID)
if proxy == nil {
msg := fmt.Sprintf("Proxy for Stream ID %d not found, may have been disconnected", streamID)
log.Error().Msg(msg)
http.Error(w, msg, http.StatusNotFound)
return
}
envoyConfig, err := ds.computeClient.GetProxyConfig(proxy, "config_dump", ds.kubeConfig)
if err != nil {
msg := fmt.Sprintf("Error getting envoy config from proxy %s", proxy)
log.Error().Err(err).Msg(msg)
http.Error(w, msg, http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = fmt.Fprintf(w, "%s", envoyConfig)
}
func (ds DebugConfig) getProxy(streamID int64, w http.ResponseWriter) {
proxy := ds.proxyRegistry.GetConnectedProxy(streamID)
if proxy == nil {
msg := fmt.Sprintf("Proxy for Stream ID %d not found, may have been disconnected", streamID)
log.Error().Msg(msg)
http.Error(w, msg, http.StatusNotFound)
return
}
envoyConfig, err := ds.computeClient.GetProxyConfig(proxy, "certs", ds.kubeConfig)
if err != nil {
msg := fmt.Sprintf("Error getting envoy config from proxy %s", proxy)
log.Error().Err(err).Msg(msg)
http.Error(w, msg, http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = fmt.Fprintf(w, "%s", envoyConfig)
}