-
Notifications
You must be signed in to change notification settings - Fork 474
/
types.go
288 lines (252 loc) · 9.54 KB
/
types.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
// Graph package provides support for the graph handlers such as supported path
// variables and query params, as well as types for graph processing.
package graph
import (
"fmt"
"time"
)
const (
BlackHoleCluster string = "BlackHoleCluster"
GraphTypeApp string = "app"
GraphTypeService string = "service" // Treated as graphType Workload, with service injection, and then condensed
GraphTypeVersionedApp string = "versionedApp"
GraphTypeWorkload string = "workload"
NodeTypeAggregate string = "aggregate" // The special "aggregate" traffic node
NodeTypeApp string = "app"
NodeTypeBox string = "box" // The special "box" node. isBox will be set to "app" | "cluster" | "namespace"
NodeTypeService string = "service"
NodeTypeUnknown string = "unknown" // The special "unknown" traffic gen node
NodeTypeWorkload string = "workload"
PassthroughCluster string = "PassthroughCluster"
TF string = "2006-01-02 15:04:05" // TF is the TimeFormat for timestamps
Unknown string = "unknown" // Istio unknown label value
)
type Node struct {
ID string // unique identifier for the node
NodeType string // Node type
Cluster string // Cluster
Namespace string // Namespace
Workload string // Workload (deployment) name
App string // Workload app label value
Version string // Workload version label value
Service string // Service name
Edges []*Edge // child nodes
Metadata Metadata // app-specific data
}
type Edge struct {
Source *Node
Dest *Node
Metadata Metadata // app-specific data
}
type NamespaceInfo struct {
Name string
Duration time.Duration
IsIstio bool
}
type NamespaceInfoMap map[string]NamespaceInfo
func NewNamespaceInfoMap() NamespaceInfoMap {
return make(map[string]NamespaceInfo)
}
type ServiceName struct {
Cluster string `json:"cluster"`
Namespace string `json:"namespace"`
Name string `json:"name"`
}
// WEInfo provides static information about a workload entry
// associated with a workload node.
type WEInfo struct {
// Name of the workload entry object
// required:true
Name string `json:"name"`
}
// SEInfo provides static information about the service entry
type SEInfo struct {
Hosts []string `json:"hosts"` // configured list of hosts
Location string `json:"location"` // e.g. MESH_EXTERNAL, MESH_INTERNAL
Namespace string `json:"namespace"` // the definition namespace
}
func (s *ServiceName) Key() string {
return fmt.Sprintf("%s %s %s", s.Cluster, s.Namespace, s.Name)
}
// TrafficMap is a map of app Nodes, each optionally holding Edge data. Metadata
// is a general purpose map for holding any desired node or edge information.
// Each app node should have a unique namespace+workload. Note that it is feasible
// but likely unusual to have two nodes with the same name+version in the same
// namespace.
type TrafficMap map[string]*Node
// Edges returns all of the edges in the traffic map.
func (tm TrafficMap) Edges() []*Edge {
var edges []*Edge
for _, n := range tm {
edges = append(edges, n.Edges...)
}
return edges
}
// NewNode constructor
func NewNode(cluster, serviceNamespace, service, workloadNamespace, workload, app, version, graphType string) (*Node, error) {
id, nodeType, err := Id(cluster, serviceNamespace, service, workloadNamespace, workload, app, version, graphType)
if err != nil {
return nil, err
}
namespace := workloadNamespace
if !IsOK(namespace) {
namespace = serviceNamespace
}
return NewNodeExplicit(id, cluster, namespace, workload, app, version, service, nodeType, graphType), nil
}
// NewNodeExplicit constructor assigns the specified ID
func NewNodeExplicit(id, cluster, namespace, workload, app, version, service, nodeType, graphType string) *Node {
metadata := make(Metadata)
// trim unnecessary fields
switch nodeType {
case NodeTypeApp:
// note: we keep workload for a versioned app node because app+version labeling
// should be backed by a single workload and it can be useful to use the workload
// name as opposed to the label values.
if graphType != GraphTypeVersionedApp {
workload = ""
version = ""
}
service = ""
case NodeTypeService:
app = ""
workload = ""
version = ""
if service == PassthroughCluster || service == BlackHoleCluster {
metadata[IsEgressCluster] = true
}
case NodeTypeWorkload:
// maintain the app+version labeling if it is set, it can be useful
// for identifying destination rules, providing links, and grouping
if app == Unknown {
app = ""
}
if version == Unknown {
version = ""
}
service = ""
}
return &Node{
ID: id,
NodeType: nodeType,
Cluster: cluster,
Namespace: namespace,
Workload: workload,
App: app,
Version: version,
Service: service,
Edges: []*Edge{},
Metadata: metadata,
}
}
// AddEdge adds an edge to the specified dest node
func (s *Node) AddEdge(dest *Node) *Edge {
e := NewEdge(s, dest)
s.Edges = append(s.Edges, &e)
return &e
}
// NewEdge constructor
func NewEdge(source, dest *Node) Edge {
return Edge{
Source: source,
Dest: dest,
Metadata: NewMetadata(),
}
}
// NewTrafficMap constructor
func NewTrafficMap() TrafficMap {
return make(map[string]*Node)
}
// Id returns the unique node ID
func Id(cluster, serviceNamespace, service, workloadNamespace, workload, app, version, graphType string) (id, nodeType string, err error) {
// prefer the workload namespace
namespace := workloadNamespace
if !IsOK(namespace) {
namespace = serviceNamespace
}
// first, check for the special-case "unknown" source node
if Unknown == namespace && Unknown == workload && Unknown == app && service == "" {
return fmt.Sprintf("%s_unknown_source", cluster), NodeTypeUnknown, nil
}
// It is possible that a request is made for an unknown destination. For example, an Ingress
// request to an unknown path. In this case the namespace may or may not be unknown.
// Every other field is unknown. Allow one unknown service per namespace to help reflect these
// bad destinations in the graph, it may help diagnose a problem.
if Unknown == workload && Unknown == app && Unknown == service {
return fmt.Sprintf("svc_%s_%s_unknown", cluster, namespace), NodeTypeService, nil
}
workloadOk := IsOK(workload)
appOk := IsOK(app)
serviceOk := IsOK(service)
if !workloadOk && !appOk && !serviceOk {
return "", "", fmt.Errorf("Failed ID gen1: cluster=[%s] namespace=[%s] workload=[%s] app=[%s] version=[%s] service=[%s] graphType=[%s]", cluster, namespace, workload, app, version, service, graphType)
}
// handle workload graph nodes (service graphs are initially processed as workload graphs)
if graphType == GraphTypeWorkload || graphType == GraphTypeService {
// workload graph nodes are type workload or service
if !workloadOk {
if serviceOk {
return fmt.Sprintf("svc_%s_%s_%s", cluster, namespace, service), NodeTypeService, nil
}
// We have seen cases when app is set but workload is unknown and service may be unknown or not set (See #5696)
return fmt.Sprintf("svc_%s_%s_%s", cluster, namespace, Unknown), NodeTypeService, nil
}
return fmt.Sprintf("wl_%s_%s_%v", cluster, namespace, workload), NodeTypeWorkload, nil
}
// handle app and versionedApp graphs
versionOk := IsOKVersion(version)
if appOk {
// For a versionedApp graph use workload as the Id, if available. It allows us some protection
// against labeling anti-patterns. It won't be there in a few cases like:
// - root node of a node graph
// - app box node
// Otherwise use what we have and alter node type as necessary
// For a [versionless] App graph use the app label to aggregate versions/workloads into one node
if graphType == GraphTypeVersionedApp {
if workloadOk {
return fmt.Sprintf("vapp_%s_%s_%s", cluster, namespace, workload), NodeTypeApp, nil
}
if versionOk {
return fmt.Sprintf("vapp_%s_%s_%s_%s", cluster, namespace, app, version), NodeTypeApp, nil
}
}
return fmt.Sprintf("app_%s_%s_%s", cluster, namespace, app), NodeTypeApp, nil
}
// fall back to workload if applicable
if workloadOk {
return fmt.Sprintf("wl_%s_%s_%s", cluster, namespace, workload), NodeTypeWorkload, nil
}
// fall back to service as a last resort in the app graph
return fmt.Sprintf("svc_%s_%s_%s", cluster, namespace, service), NodeTypeService, nil
}
// NewAggregateNode constructor, set svcName and app to "" when not service-specific aggregate
func NewAggregateNode(cluster, namespace, aggregate, aggregateValue, svcName, app string) Node {
id := AggregateID(cluster, namespace, aggregate, aggregateValue, svcName)
return NewAggregateNodeExplicit(id, cluster, namespace, aggregate, aggregateValue, svcName, app)
}
// NewAggregateNodeExplicit constructor assigns the specified ID, , set svcName and app to ""
// when not service-specific aggregate
func NewAggregateNodeExplicit(id, cluster, namespace, aggregate, aggregateValue, svcName, app string) Node {
metadata := make(Metadata)
metadata[Aggregate] = aggregate
metadata[AggregateValue] = aggregateValue
return Node{
ID: id,
NodeType: NodeTypeAggregate,
Cluster: cluster,
Namespace: namespace,
Workload: "",
App: app,
Version: "",
Service: svcName,
Edges: []*Edge{},
Metadata: metadata,
}
}
// AggregateID returns the unique node ID
func AggregateID(cluster, namespace, aggregate, aggregateVal, svcName string) (id string) {
if svcName == "" {
return fmt.Sprintf("agg_%s_%s_%s_%s", cluster, namespace, aggregate, aggregateVal)
}
return fmt.Sprintf("agg_%s_%s_%s_%s_%s", cluster, namespace, aggregate, aggregateVal, svcName)
}