forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
78 lines (66 loc) · 1.91 KB
/
graph.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
// TrafficMap is a map of ServiceNodes, each optionally holding Edge data. Metadata
// is a general purpose map for holding any desired service or edge information.
// Each Service node should have a unique name+version
package graph
import (
"fmt"
"strings"
)
const (
UnknownNamespace = "unknown"
UnknownService = "unknown"
UnknownVersion = "unknown"
)
type ServiceNode struct {
ID string // unique identifier for the service node
Name string // full service name
Version string // service version
ServiceName string // short service name
Namespace string // namespace name
Edges []*Edge // children services nodes
Metadata map[string]interface{} // app-specific data
}
type Edge struct {
Source *ServiceNode
Dest *ServiceNode
Metadata map[string]interface{} // app-specific data
}
type TrafficMap map[string]*ServiceNode
func NewServiceNode(name, version string) ServiceNode {
return NewServiceNodeWithId(Id(name, version), name, version)
}
func NewServiceNodeWithId(id, name, version string) ServiceNode {
split := strings.Split(name, ".")
serviceName := split[0]
namespace := UnknownNamespace
if len(split) > 1 {
namespace = split[1]
}
return ServiceNode{
ID: id,
Name: name,
Version: version,
ServiceName: serviceName,
Namespace: namespace,
Edges: []*Edge{},
Metadata: make(map[string]interface{}),
}
}
func (s *ServiceNode) AddEdge(dest *ServiceNode) *Edge {
e := NewEdge(s, dest)
s.Edges = append(s.Edges, &e)
return &e
}
func NewEdge(source, dest *ServiceNode) Edge {
return Edge{
Source: source,
Dest: dest,
Metadata: make(map[string]interface{}),
}
}
func NewTrafficMap() TrafficMap {
return make(map[string]*ServiceNode)
}
func Id(name, version string) string {
return fmt.Sprintf("%v_%v", name, version)
}