forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cytoscape.go
383 lines (324 loc) · 11.1 KB
/
cytoscape.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Cytoscape package provides conversion from our graph to the CystoscapeJS
// configuration json model.
//
// The following links are useful for understanding CytoscapeJS and it's configuration:
//
// Main page: http://js.cytoscape.org/
// JSON config: http://js.cytoscape.org/#notation/elements-json
// Demos: http://js.cytoscape.org/#demos
//
// Algorithm: Process the graph structure adding nodes and edges, decorating each
// with information provided. An optional second pass generates compound
// nodes for version grouping.
//
package cytoscape
import (
"crypto/md5"
"fmt"
"sort"
"github.com/kiali/kiali/graph"
"github.com/kiali/kiali/graph/options"
)
type NodeData struct {
// Cytoscape Fields
Id string `json:"id"` // unique internal node ID (n0, n1...)
Parent string `json:"parent,omitempty"` // Compound Node parent ID
// App Fields (not required by Cytoscape)
NodeType string `json:"nodeType"`
Namespace string `json:"namespace"`
Workload string `json:"workload,omitempty"`
App string `json:"app,omitempty"`
Version string `json:"version,omitempty"`
Service string `json:"service,omitempty"` // requested service for NodeTypeService
DestServices map[string]bool `json:"destServices,omitempty"` // requested services for [dest] node
Rate string `json:"rate,omitempty"` // edge aggregate
Rate3xx string `json:"rate3XX,omitempty"` // edge aggregate
Rate4xx string `json:"rate4XX,omitempty"` // edge aggregate
Rate5xx string `json:"rate5XX,omitempty"` // edge aggregate
RateOut string `json:"rateOut,omitempty"` // edge aggregate
RateTcpSent string `json:"rateTcpSent,omitempty"` // edge aggregate
RateTcpSentOut string `json:"rateTcpSentOut,omitempty"` // edge aggregate
HasCB bool `json:"hasCB,omitempty"` // true (has circuit breaker) | false
HasMissingSC bool `json:"hasMissingSC,omitempty"` // true (has missing sidecar) | false
HasVS bool `json:"hasVS,omitempty"` // true (has route rule) | false
IsDead bool `json:"isDead,omitempty"` // true (has no pods) | false
IsGroup string `json:"isGroup,omitempty"` // set to the grouping type, current values: [ 'version' ]
IsMisconfigured string `json:"isMisconfigured,omitempty"` // set to misconfiguration list, current values: [ 'labels' ]
IsOutside bool `json:"isOutside,omitempty"` // true | false
IsRoot bool `json:"isRoot,omitempty"` // true | false
IsUnused bool `json:"isUnused,omitempty"` // true | false
}
type EdgeData struct {
// Cytoscape Fields
Id string `json:"id"` // unique internal edge ID (e0, e1...)
Source string `json:"source"` // parent node ID
Target string `json:"target"` // child node ID
// App Fields (not required by Cytoscape)
Rate string `json:"rate,omitempty"`
Rate3xx string `json:"rate3XX,omitempty"`
Rate4xx string `json:"rate4XX,omitempty"`
Rate5xx string `json:"rate5XX,omitempty"`
PercentErr string `json:"percentErr,omitempty"`
PercentRate string `json:"percentRate,omitempty"` // percent of total parent requests
ResponseTime string `json:"responseTime,omitempty"`
IsMTLS bool `json:"isMTLS,omitempty"` // true (mutual TLS connection) | false
IsUnused bool `json:"isUnused,omitempty"` // true | false
TcpSentRate string `json:"tcpSentRate,omitempty"`
}
type NodeWrapper struct {
Data *NodeData `json:"data"`
}
type EdgeWrapper struct {
Data *EdgeData `json:"data"`
}
type Elements struct {
Nodes []*NodeWrapper `json:"nodes"`
Edges []*EdgeWrapper `json:"edges"`
}
type Config struct {
Timestamp int64 `json:"timestamp"`
GraphType string `json:"graphType"`
Elements Elements `json:"elements"`
}
func nodeHash(id string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(id)))
}
func edgeHash(from string, to string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%s.%s", from, to))))
}
func NewConfig(trafficMap graph.TrafficMap, o options.VendorOptions) (result Config) {
nodes := []*NodeWrapper{}
edges := []*EdgeWrapper{}
buildConfig(trafficMap, &nodes, &edges, o)
// Add compound nodes that group together different versions of the same node
if o.GraphType == graph.GraphTypeVersionedApp && o.GroupBy == options.GroupByVersion {
groupByVersion(&nodes)
}
// sort nodes and edges for better json presentation (and predictable testing)
// kiali-1258 compound/isGroup/parent nodes must come before the child references
sort.Slice(nodes, func(i, j int) bool {
switch {
case nodes[i].Data.Namespace != nodes[j].Data.Namespace:
return nodes[i].Data.Namespace < nodes[j].Data.Namespace
case nodes[i].Data.IsGroup != nodes[j].Data.IsGroup:
return nodes[i].Data.IsGroup > nodes[j].Data.IsGroup
case nodes[i].Data.App != nodes[j].Data.App:
return nodes[i].Data.App < nodes[j].Data.App
case nodes[i].Data.Version != nodes[j].Data.Version:
return nodes[i].Data.Version < nodes[j].Data.Version
default:
return nodes[i].Data.Workload < nodes[j].Data.Workload
}
})
sort.Slice(edges, func(i, j int) bool {
switch {
case edges[i].Data.Source < edges[j].Data.Source:
return true
case edges[i].Data.Source > edges[j].Data.Source:
return false
default:
return edges[i].Data.Target < edges[j].Data.Target
}
})
elements := Elements{nodes, edges}
result = Config{
Timestamp: o.Timestamp,
GraphType: o.GraphType,
Elements: elements,
}
return result
}
func buildConfig(trafficMap graph.TrafficMap, nodes *[]*NodeWrapper, edges *[]*EdgeWrapper, o options.VendorOptions) {
for id, n := range trafficMap {
nodeId := nodeHash(id)
nd := &NodeData{
Id: nodeId,
NodeType: n.NodeType,
Namespace: n.Namespace,
Workload: n.Workload,
App: n.App,
Version: n.Version,
Service: n.Service,
}
addNodeTelemetry(n, nd)
// node may have deployment but no pods running)
if val, ok := n.Metadata["isDead"]; ok {
nd.IsDead = val.(bool)
}
// node may be a root
if val, ok := n.Metadata["isRoot"]; ok {
nd.IsRoot = val.(bool)
}
// node may be unused
if val, ok := n.Metadata["isUnused"]; ok {
nd.IsUnused = val.(bool)
}
// node may have a circuit breaker
if val, ok := n.Metadata["hasCB"]; ok {
nd.HasCB = val.(bool)
}
// node may have a virtual service
if val, ok := n.Metadata["hasVS"]; ok {
nd.HasVS = val.(bool)
}
// set sidecars checks, if available
if val, ok := n.Metadata["hasMissingSC"]; ok {
nd.HasMissingSC = val.(bool)
}
// check if node is misconfigured
if val, ok := n.Metadata["isMisconfigured"]; ok {
nd.IsMisconfigured = val.(string)
}
// check if node is on another namespace
if val, ok := n.Metadata["isOutside"]; ok {
nd.IsOutside = val.(bool)
}
// node may have destination service info
if val, ok := n.Metadata["destServices"]; ok {
nd.DestServices = val.(map[string]bool)
}
nw := NodeWrapper{
Data: nd,
}
*nodes = append(*nodes, &nw)
for _, e := range n.Edges {
sourceIdHash := nodeHash(n.ID)
destIdHash := nodeHash(e.Dest.ID)
edgeId := edgeHash(sourceIdHash, destIdHash)
ed := EdgeData{
Id: edgeId,
Source: sourceIdHash,
Target: destIdHash,
}
addEdgeTelemetry(&ed, e, o)
ew := EdgeWrapper{
Data: &ed,
}
*edges = append(*edges, &ew)
}
}
}
func addNodeTelemetry(s *graph.Node, nd *NodeData) {
rate := getRate(s.Metadata, "rate")
if rate > 0.0 {
nd.Rate = fmt.Sprintf("%.3f", rate)
rate3xx := getRate(s.Metadata, "rate3xx")
rate4xx := getRate(s.Metadata, "rate4xx")
rate5xx := getRate(s.Metadata, "rate5xx")
if rate3xx > 0.0 {
nd.Rate3xx = fmt.Sprintf("%.3f", rate3xx)
}
if rate4xx > 0.0 {
nd.Rate4xx = fmt.Sprintf("%.3f", rate4xx)
}
if rate5xx > 0.0 {
nd.Rate5xx = fmt.Sprintf("%.3f", rate5xx)
}
}
rateOut := getRate(s.Metadata, "rateOut")
if rateOut > 0.0 {
nd.RateOut = fmt.Sprintf("%.3f", rateOut)
}
tcpSent := getRate(s.Metadata, "tcpSentRate")
tcpSentOut := getRate(s.Metadata, "tcpSentRateOut")
if tcpSent > 0.0 {
nd.RateTcpSent = fmt.Sprintf("%.3f", tcpSent)
}
if tcpSentOut > 0.0 {
nd.RateTcpSentOut = fmt.Sprintf("%.3f", tcpSentOut)
}
}
func getRate(md map[string]interface{}, k string) float64 {
if rate, ok := md[k]; ok {
return rate.(float64)
}
return 0.0
}
func addEdgeTelemetry(ed *EdgeData, e *graph.Edge, o options.VendorOptions) {
rate := getRate(e.Metadata, "rate")
if rate > 0.0 {
rate3xx := getRate(e.Metadata, "rate3xx")
rate4xx := getRate(e.Metadata, "rate4xx")
rate5xx := getRate(e.Metadata, "rate5xx")
rateErr := rate4xx + rate5xx
percentErr := rateErr / rate * 100.0
ed.Rate = fmt.Sprintf("%.3f", rate)
if rate3xx > 0.0 {
ed.Rate3xx = fmt.Sprintf("%.3f", rate3xx)
}
if rate4xx > 0.0 {
ed.Rate4xx = fmt.Sprintf("%.3f", rate4xx)
}
if rate5xx > 0.0 {
ed.Rate5xx = fmt.Sprintf("%.3f", rate5xx)
}
if percentErr > 0.0 {
ed.PercentErr = fmt.Sprintf("%.3f", percentErr)
}
if val, ok := e.Metadata["responseTime"]; ok {
responseTime := val.(float64)
ed.ResponseTime = fmt.Sprintf("%.3f", responseTime)
}
percentRate := rate / getRate(e.Source.Metadata, "rateOut") * 100.0
if percentRate < 100.0 {
ed.PercentRate = fmt.Sprintf("%.3f", percentRate)
}
} else {
if val, ok := e.Source.Metadata["isUnused"]; ok {
ed.IsUnused = val.(bool)
}
}
if val, ok := e.Metadata["isMTLS"]; ok {
ed.IsMTLS = val.(bool)
}
tcpSentRate := getRate(e.Metadata, "tcpSentRate")
if tcpSentRate > 0.0 {
ed.TcpSentRate = fmt.Sprintf("%.3f", tcpSentRate)
}
}
// groupByVersion adds compound nodes to group multiple versions of the same app
func groupByVersion(nodes *[]*NodeWrapper) {
grouped := make(map[string][]*NodeData)
for _, nw := range *nodes {
if nw.Data.NodeType == graph.NodeTypeApp {
k := fmt.Sprintf("box_%s_%s", nw.Data.Namespace, nw.Data.App)
grouped[k] = append(grouped[k], nw.Data)
}
}
for k, members := range grouped {
if len(members) > 1 {
// create the compound grouping all versions of the app
nodeId := nodeHash(k)
nd := NodeData{
Id: nodeId,
NodeType: graph.NodeTypeApp,
Namespace: members[0].Namespace,
App: members[0].App,
Version: "",
IsGroup: options.GroupByVersion,
}
nw := NodeWrapper{
Data: &nd,
}
// assign each app version node to the compound parent
hasVirtualService := false
nd.HasMissingSC = false
nd.IsOutside = false
for _, n := range members {
n.Parent = nodeId
nd.HasMissingSC = nd.HasMissingSC || n.HasMissingSC
nd.IsOutside = nd.IsOutside || n.IsOutside
// If there is a virtual service defined in version node, move it to compound parent
if n.HasVS {
n.HasVS = false
hasVirtualService = true
}
}
if hasVirtualService {
nd.HasVS = true
}
// add the compound node to the list of nodes
*nodes = append(*nodes, &nw)
}
}
}