@@ -6,71 +6,91 @@ import (
6
6
"sort"
7
7
"time"
8
8
9
- "github.com/openservicemesh/osm/pkg/certificate"
10
9
"github.com/openservicemesh/osm/pkg/envoy"
11
10
)
12
11
13
12
const (
14
- specificProxyQueryKey = "proxy "
15
- proxyConfigQueryKey = "cfg"
13
+ uuidQueryKey = "uuid "
14
+ proxyConfigQueryKey = "cfg"
16
15
)
17
16
18
17
func (ds DebugConfig ) getProxies () http.Handler {
19
- // This function is needed to convert the list of connected proxies to
20
- // the type (map) required by the printProxies function.
21
- listConnected := func () map [certificate.CommonName ]time.Time {
22
- proxies := make (map [certificate.CommonName ]time.Time )
23
- for _ , proxy := range ds .proxyRegistry .ListConnectedProxies () {
24
- proxies [proxy .GetCertificateCommonName ()] = (* proxy ).GetConnectedAt ()
25
- }
26
- return proxies
27
- }
28
-
29
18
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
30
19
w .Header ().Set ("Content-Type" , "text/html" )
31
- if proxyConfigDump , ok := r .URL .Query ()[proxyConfigQueryKey ]; ok {
32
- ds .getConfigDump (certificate .CommonName (proxyConfigDump [0 ]), w )
33
- } else if specificProxy , ok := r .URL .Query ()[specificProxyQueryKey ]; ok {
34
- ds .getProxy (certificate .CommonName (specificProxy [0 ]), w )
35
- } else {
36
- printProxies (w , listConnected (), "Connected" )
20
+ proxyConfigDump := r .URL .Query ()[proxyConfigQueryKey ]
21
+ uuid := r .URL .Query ()[uuidQueryKey ]
22
+
23
+ switch {
24
+ case len (uuid ) == 0 :
25
+ ds .printProxies (w )
26
+ case len (proxyConfigDump ) > 0 :
27
+ ds .getConfigDump (uuid [0 ], w )
28
+ default :
29
+ ds .getProxy (uuid [0 ], w )
37
30
}
38
31
})
39
32
}
40
33
41
- func printProxies (w http.ResponseWriter , proxies map [certificate.CommonName ]time.Time , category string ) {
42
- var commonNames []string
43
- for cn := range proxies {
44
- commonNames = append (commonNames , cn .String ())
34
+ func (ds DebugConfig ) printProxies (w http.ResponseWriter ) {
35
+ // This function is needed to convert the list of connected proxies to
36
+ // the type (map) required by the printProxies function.
37
+ proxyMap := ds .proxyRegistry .ListConnectedProxies ()
38
+ proxies := make ([]* envoy.Proxy , 0 , len (proxyMap ))
39
+ for _ , proxy := range proxyMap {
40
+ proxies = append (proxies , proxy )
45
41
}
46
42
47
- sort .Strings (commonNames )
43
+ sort .Slice (proxies , func (i , j int ) bool {
44
+ return proxies [i ].Identity .String () < proxies [j ].Identity .String ()
45
+ })
48
46
49
- _ , _ = fmt .Fprintf (w , "<h1>%s Proxies (%d):</h1>" , category , len (proxies ))
47
+ _ , _ = fmt .Fprintf (w , "<h1>Connected Proxies (%d):</h1>" , len (proxies ))
50
48
_ , _ = fmt .Fprint (w , `<table>` )
51
- _ , _ = fmt .Fprint (w , "<tr><td>#</td><td>Envoy's certificate CN</td><td>Connected At</td><td>How long ago</td><td>tools</td></tr>" )
52
- for idx , cn := range commonNames {
53
- ts := proxies [certificate .CommonName (cn )]
54
- _ , _ = fmt .Fprintf (w , `<tr><td>%d:</td><td>%s</td><td>%+v</td><td>(%+v ago)</td><td><a href="/debug/proxy?%s=%s">certs</a></td><td><a href="/debug/proxy?%s=%s">cfg</a></td></tr>` ,
55
- idx , cn , ts , time .Since (ts ), specificProxyQueryKey , cn , proxyConfigQueryKey , cn )
49
+ _ , _ = 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>" )
50
+ for idx , proxy := range proxies {
51
+ ts := proxy .GetConnectedAt ()
52
+ proxyURL := fmt .Sprintf ("/debug/proxy?%s=%s" , uuidQueryKey , proxy .UUID )
53
+ configDumpURL := fmt .Sprintf ("%s&%s=%t" , proxyURL , proxyConfigQueryKey , true )
54
+ _ , _ = fmt .Fprintf (w , `<tr><td>%d:</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>` ,
55
+ idx + 1 , proxy .Identity , ts , time .Since (ts ), proxyURL , configDumpURL )
56
56
}
57
57
_ , _ = fmt .Fprint (w , `</table>` )
58
58
}
59
59
60
- func (ds DebugConfig ) getConfigDump (cn certificate.CommonName , w http.ResponseWriter ) {
61
- pod , err := envoy .GetPodFromCertificate (cn , ds .kubeController )
60
+ func (ds DebugConfig ) getConfigDump (uuid string , w http.ResponseWriter ) {
61
+ proxy , ok := ds .proxyRegistry .GetConnectedProxy (uuid )
62
+ if ! ok {
63
+ msg := fmt .Sprintf ("Proxy for UUID %s not found, may have been disconnected" , uuid )
64
+ log .Error ().Msg (msg )
65
+ http .Error (w , msg , http .StatusNotFound )
66
+ return
67
+ }
68
+ pod , err := envoy .GetPodFromCertificate (proxy .GetCertificateCommonName (), ds .kubeController )
62
69
if err != nil {
63
- log .Error ().Err (err ).Msgf ("Error getting Pod from certificate with CN=%s" , cn )
70
+ msg := fmt .Sprintf ("Error getting Pod from certificate with CN=%s" , proxy .GetCertificateCommonName ())
71
+ log .Error ().Err (err ).Msg (msg )
72
+ http .Error (w , msg , http .StatusNotFound )
73
+ return
64
74
}
65
75
w .Header ().Set ("Content-Type" , "application/json" )
66
76
envoyConfig := ds .getEnvoyConfig (pod , "config_dump" )
67
77
_ , _ = fmt .Fprintf (w , "%s" , envoyConfig )
68
78
}
69
79
70
- func (ds DebugConfig ) getProxy (cn certificate.CommonName , w http.ResponseWriter ) {
71
- pod , err := envoy .GetPodFromCertificate (cn , ds .kubeController )
80
+ func (ds DebugConfig ) getProxy (uuid string , w http.ResponseWriter ) {
81
+ proxy , ok := ds .proxyRegistry .GetConnectedProxy (uuid )
82
+ if ! ok {
83
+ msg := fmt .Sprintf ("Proxy for UUID %s not found, may have been disconnected" , uuid )
84
+ log .Error ().Msg (msg )
85
+ http .Error (w , msg , http .StatusNotFound )
86
+ return
87
+ }
88
+ pod , err := envoy .GetPodFromCertificate (proxy .GetCertificateCommonName (), ds .kubeController )
72
89
if err != nil {
73
- log .Error ().Err (err ).Msgf ("Error getting Pod from certificate with CN=%s" , cn )
90
+ msg := fmt .Sprintf ("Error getting Pod from certificate with CN=%s" , proxy .GetCertificateCommonName ())
91
+ log .Error ().Err (err ).Msg (msg )
92
+ http .Error (w , msg , http .StatusNotFound )
93
+ return
74
94
}
75
95
w .Header ().Set ("Content-Type" , "application/json" )
76
96
envoyConfig := ds .getEnvoyConfig (pod , "certs" )
0 commit comments