-
Notifications
You must be signed in to change notification settings - Fork 459
/
debug.go
186 lines (152 loc) · 5.13 KB
/
debug.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package graph
import (
"fmt"
"net/http"
"net/url"
"sort"
"strings"
gonumgraph "gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/simple"
)
// DebugHandlerPath is the HTTP handler path for this debug handler.
const DebugHandlerPath = "/debug/resource-dependency-graph"
type handler struct {
graph *simple.DirectedGraph
}
// NewDebugHandler creates a new HTTP handler for debugging the resource dependency graph.
func NewDebugHandler(graph *graph) http.HandlerFunc {
return (&handler{graph.graph}).Handle
}
func (h *handler) Handle(w http.ResponseWriter, r *http.Request) {
var (
out string
kind = getQueryParameter(r.URL.Query(), "kind")
namespace = getQueryParameter(r.URL.Query(), "namespace")
name = getQueryParameter(r.URL.Query(), "name")
nodesIterator = h.graph.Nodes()
nodes []*vertex
)
// On large landscapes, if there are many vertices then the entire graph cannot be rendered fast enough. Hence, we
// apply some default filtering for the 'seed' kind.
if kind == "" && namespace == "" && name == "" && nodesIterator.Len() > 2000 {
kind = "Seed"
}
// Filter for all relevant nodes and sort them.
for nodesIterator.Next() {
v := nodesIterator.Node().(*vertex)
if (kind != "" && vertexTypes[v.vertexType] != kind) ||
(namespace != "" && v.namespace != namespace) ||
(name != "" && v.name != name) {
continue
}
nodes = append(nodes, v)
}
sort.Sort(vertexSorter(nodes))
// Render filtering form.
out += `
<form action="` + DebugHandlerPath + `" method="GET">
<select name="kind">`
out += fmt.Sprintf(`<option value=""%s><all></option>`, selected("", kind))
for _, vt := range vertexTypes {
out += fmt.Sprintf(`<option value="%s"%s>%s</option>`, vt, selected(vt, kind), vt)
}
out += `
</select>
<input type="test" name="namespace" value="` + namespace + `" />
<input type="test" name="name" value="` + name + `" />
<input type="submit" value="go" />
</form>` + separate(true)
// Iterate over each nodes, determine and sort their incoming and outgoing neighbors, and render the output.
for i, v := range nodes {
out += indent(0, "# %s", link(v))
for _, n := range []struct {
prefix string
iterator gonumgraph.Nodes
}{
{"<- (incoming)", h.graph.To(v.ID())},
{"-> (outgoing)", h.graph.From(v.ID())},
} {
var neighbors []*vertex
for n.iterator.Next() {
neighbors = append(neighbors, n.iterator.Node().(*vertex))
}
sort.Sort(vertexSorter(neighbors))
if len(neighbors) > 0 {
out += indent(1, "%s (%d)", n.prefix, len(neighbors))
for _, u := range neighbors {
out += indent(2, link(u))
}
}
}
out += emptyNewline() + separate(i < len(nodes)-1)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, `<font size="2" face="Courier New">`+out+`</font>`)
}
func getQueryParameter(query url.Values, param string) string {
var out string
values, ok := query[param]
if ok && len(values[0]) >= 1 {
out = values[0]
}
return out
}
type vertexSorter []*vertex
func (v vertexSorter) Len() int { return len(v) }
func (v vertexSorter) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v vertexSorter) Less(i, j int) bool {
if vertexTypes[v[i].vertexType] < vertexTypes[v[j].vertexType] {
return true
} else if vertexTypes[v[i].vertexType] > vertexTypes[v[j].vertexType] {
return false
}
if v[i].namespace < v[j].namespace {
return true
} else if v[i].namespace > v[j].namespace {
return false
}
return v[i].name < v[j].name
}
func emptyNewline() string {
return "|<br />"
}
func separate(withNewBeginning bool) string {
result := "-------------------------------------------------------------------------------<br />"
if withNewBeginning {
result += emptyNewline()
}
return result
}
func indent(level int, format string, a ...interface{}) string {
return fmt.Sprintf("| "+strings.Repeat(" ", level)+format+"<br />", a...)
}
func link(v *vertex) string {
path := fmt.Sprintf("%s?kind=%s", DebugHandlerPath, vertexTypes[v.vertexType])
out := fmt.Sprintf(`<a href="%s">%s</a>:`, path, vertexTypes[v.vertexType])
if v.namespace != "" {
path += fmt.Sprintf("&namespace=%s", v.namespace)
out += fmt.Sprintf(`<a href="%s">%s</a>/`, path, v.namespace)
}
path += fmt.Sprintf("&name=%s", v.name)
out += fmt.Sprintf(`<a href="%s">%s</a>`, path, v.name)
return out
}
func selected(name, selected string) string {
if selected == name {
return " selected"
}
return ""
}