-
Notifications
You must be signed in to change notification settings - Fork 327
/
dep.go
498 lines (416 loc) · 11.9 KB
/
dep.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package envoyx
type (
// DepGraph provides a collection of optionally connected subgraphs
//
// Each subgraph is dedicated for a specific node scope.
// A subgraph with scoped nodes may be connected to the subgraph with no
// defined scope.
DepGraph struct {
graphs []*depSubgraph
}
// depSubgraph provides a bi-directional dependency graph
depSubgraph struct {
scope Scope
nodes []*depNode
nodeMap map[*Node]*depNode
parentSubgraphs map[*depSubgraph]bool
childSubgraphs map[*depSubgraph]bool
}
// depNode provides a wrapper around the resource for eas of use within the dep. graph
depNode struct {
Node *Node
parents map[*depNode]bool
children map[*depNode]bool
// Index to keep track of missing references at build time
missingReferences map[string]Ref
}
)
// BuildDepGraph constructs a dependency graph from the provided nodes
//
// We firstly group the nodes by scope, then build a subgraph for each scope,
// and lastly merge the subgraphs into a single graph.
func BuildDepGraph(nn ...*Node) (out *DepGraph) {
scopes := scopeNodes(nn...)
aux := make([]*depSubgraph, 0, len(scopes))
for _, ss := range scopes {
aux = append(aux, buildDepSubgraph(ss))
}
return buildDepGraph(aux...)
}
// scopeNodes groups the nodes by the scope
//
// The function returns a slice of NodeSet where each NodeSet only contains
// nodes of the same scope (or no scope if none defined).
func scopeNodes(nn ...*Node) (out []NodeSet) {
type (
// Defining a little wrapper around the NodeSet so we can use pointers
scopeWrap struct {
nn NodeSet
}
)
var (
// resource type -> identifier -> wrap
scopes = make(map[string]map[string]*scopeWrap)
// Holds the nodes with no defined scope
empty NodeSet
)
// Bucket nodes into scopes
//
// Use maps to make the process as efficient as possible.
for _, n := range nn {
// Empty scopes are a special case
if n.Scope.IsEmpty() {
empty = append(empty, n)
continue
}
// New resource type
if _, ok := scopes[n.Scope.ResourceType]; !ok {
scopes[n.Scope.ResourceType] = make(map[string]*scopeWrap, 4)
}
// Check if the identifiers of the current node scope exist in the index.
// If they do, update the wrap struct by adding this node, then register
// the same wrap struct to all other identifiers -- this allows for some
// recovery in case some resource uses a subset of one but not the other.
hasIdent := false
firstIdent := ""
for _, i := range n.Scope.Identifiers.Slice {
hasIdent = hasIdent || scopes[n.Scope.ResourceType][i] != nil
if hasIdent {
firstIdent = i
break
}
}
if !hasIdent {
// Not registered yet, create a new wrap for all of the identifiers
w := &scopeWrap{nn: append(make(NodeSet, 0, 10), n)}
for _, i := range n.Scope.Identifiers.Slice {
scopes[n.Scope.ResourceType][i] = w
}
} else {
// Already registered; update it and add missing identifiers
w := scopes[n.Scope.ResourceType][firstIdent]
w.nn = append(w.nn, n)
for _, i := range n.Scope.Identifiers.Slice {
scopes[n.Scope.ResourceType][i] = w
}
}
}
// Unpack the map into a slice of NodeSet the return expects
//
// @note since all identifiers are registered, the wraps would appear
// duplicated so we need to filter them a bit
out = make([]NodeSet, 0, 10)
if len(empty) > 0 {
out = append(out, empty)
}
seen := make(map[*scopeWrap]bool)
for _, s := range scopes {
for _, ss := range s {
if seen[ss] {
continue
}
seen[ss] = true
out = append(out, ss.nn)
}
}
return out
}
// buildDepSubgraph constructs a dep. subgraph from the provided nodes
//
// The function returns a bidirectional graph of the nodes where the parent
// represents the dependency of the current resource (a namespace would be
// a parent of a module).
//
// The build process indexes some data for optimal operations down the line.
func buildDepSubgraph(nn NodeSet) (out *depSubgraph) {
out = &depSubgraph{
scope: nn[0].Scope,
nodes: make([]*depNode, len(nn)),
nodeMap: make(map[*Node]*depNode, len(nn)),
parentSubgraphs: make(map[*depSubgraph]bool),
childSubgraphs: make(map[*depSubgraph]bool),
}
byIdentifier := make(map[string]map[string]*depNode, 8)
byNode := make(map[*Node]*depNode, len(nn))
// 1. index all of the nodes in a map so we can trivially connect them later
for i, _n := range nn {
n := _n
// Function blindly trusts it will be called with the correct data.
// @todo consider implementing this check but make sure all of the decoders
// correctly set the scopes.
// if !n.Scope.Equals(nn[0].Scope) {
// panic("invalid state: subgraphs can only be constructed with nodes from the same scope")
// }
aux := &depNode{
Node: n,
// Keep track of missing references so we can figure them out optimally
missingReferences: make(map[string]Ref),
parents: make(map[*depNode]bool),
children: make(map[*depNode]bool),
}
for field, ref := range n.References {
aux.missingReferences[field] = ref
}
byNode[n] = aux
out.nodes[i] = aux
out.nodeMap[n] = aux
if _, ok := byIdentifier[n.ResourceType]; !ok {
byIdentifier[n.ResourceType] = make(map[string]*depNode, 8)
}
for _, i := range n.Identifiers.Slice {
byIdentifier[n.ResourceType][i] = byNode[n]
}
}
// 2. link up the node with it's dependencies
for _, _n := range out.nodes {
n := _n
for field, ref := range n.Node.References {
resource := ref.ResourceType
found := false
for _, i := range ref.Identifiers.Slice {
ch, ok := byIdentifier[resource][i]
found = found || ok
if ok {
delete(n.missingReferences, field)
n.parents[ch] = true
ch.children[n] = true
break
}
}
}
}
return
}
// buildDepGraph constructs a dependency graph from the given set of subgraphs
//
// The subgraphs can be connected in case a scoped node would reference a
// unscoped node (unscoped nodes can not reference scoped nodes, nor can nodes
// from different scopes -- unneeded and removes some complexity).
func buildDepGraph(gg ...*depSubgraph) (out *DepGraph) {
out = &DepGraph{
graphs: make([]*depSubgraph, 0, len(gg)),
}
// Get the unscoped graph
//
// For now, we can only xref to unscoped graphs.
// This might need to be generalized.
unscopedG := unscopedSubgraph(gg...)
// Iterate all graphs and try to resolve unresolved deps
for _, g := range gg {
out.graphs = append(out.graphs, g)
if unscopedG == nil {
continue
}
// Get nodes with missing references
missingNodes := g.nodesWithMissingRefs()
if len(missingNodes) == 0 {
continue
}
// Try to resolve missing refs using the unscoped graph
for _, _mn := range missingNodes {
mn := _mn
for field, ref := range mn.missingReferences {
n := depNodeForRef(ref, unscopedG.nodes...)
if n == nil {
continue
}
mn.parents[n] = true
n.children[mn] = true
delete(mn.missingReferences, field)
if len(mn.missingReferences) == 0 {
mn.missingReferences = nil
}
}
}
// xref the subgraphs
g.parentSubgraphs[unscopedG] = true
unscopedG.childSubgraphs[g] = true
}
return
}
// Graph traversal functions
// Roots returns all nodes which are considered as root resources based on the current state
//
// For the most part, these are all resources with no parent resources.
// If all resources define parents, then some home brew logic is ran
func (g DepGraph) Roots() (out NodeSet) {
for _, sg := range g.graphs {
out = append(out, sg.Roots()...)
}
return
}
// ParentForRef returns a parent node of n which matches ref (nil if none)
func (g DepGraph) ParentForRef(n *Node, ref Ref) (out *Node) {
for _, sg := range g.graphs {
out = sg.ParentForRef(n, ref)
if out != nil {
return
}
}
return
}
// ParentForRT returns a set of parent nodes matching the resource type
func (g DepGraph) ParentForRT(n *Node, rt string) (out NodeSet) {
for _, sg := range g.graphs {
out = sg.ParentForRT(n, rt)
if out != nil {
return
}
}
return
}
// ChildrenForResourceType returns child nodes of n which match the resource type
func (g DepGraph) ChildrenForResourceType(n *Node, rt string) (out NodeSet) {
for _, sg := range g.graphs {
out = sg.ChildrenForResourceType(n, rt)
if out != nil {
return
}
}
return
}
func (g DepGraph) NodeForRef(ref Ref) (out *Node) {
aux := make([]*Node, 0, 10)
for _, sg := range g.graphs {
for _, n := range sg.nodes {
aux = append(aux, n.Node)
}
}
return NodeForRef(ref, aux...)
}
// Children returns all child nodes of n
func (g DepGraph) Children(n *Node) (out NodeSet) {
for _, sg := range g.graphs {
out = sg.Children(n)
if out != nil {
return
}
}
return
}
// MissingRegs returns a slice of all refs that are requested but not found in the graph
func (g DepGraph) MissingRefs() (out []map[string]Ref) {
for _, sg := range g.graphs {
for _, n := range sg.nodes {
if len(n.missingReferences) == 0 {
continue
}
out = append(out, n.missingReferences)
}
}
return
}
func (g DepGraph) allNodes() (out []*depNode) {
for _, sg := range g.graphs {
out = append(out, sg.nodes...)
}
return
}
// Roots returns all nodes which are considered as root resources based on the current state
//
// For the most part, these are all resources with no parent resources.
// If all resources define parents, then some home brew logic is ran
//
// @todo when we add more resources, we might need to expand this; for now
// it should work just fine.
func (g depSubgraph) Roots() (out NodeSet) {
for _, n := range g.nodes {
if needyResources[n.Node.ResourceType] {
continue
}
out = append(out, n.Node)
}
if len(out) != 0 {
return
}
for _, n := range g.nodes {
if superNeedyResources[n.Node.ResourceType] {
continue
}
out = append(out, n.Node)
}
return
}
// ParentForRef returns a parent node of n which matches ref (nil if none)
func (g depSubgraph) ParentForRef(n *Node, ref Ref) *Node {
return NodeForRef(ref, g.parent(n)...)
}
// ParentForRT returns a set of parent nodes matching the resource type
func (g depSubgraph) ParentForRT(n *Node, rt string) NodeSet {
return NodesForResourceType(rt, g.parent(n)...)
}
// ChildrenForResourceType returns child nodes of n which match the resource type
func (g depSubgraph) ChildrenForResourceType(n *Node, rt string) (out NodeSet) {
out = g.Children(n)
out = NodesForResourceType(rt, out...)
return
}
// Children returns all child nodes of n
func (g depSubgraph) Children(n *Node) (out NodeSet) {
aux, ok := g.nodeMap[n]
if !ok {
return
}
for n := range aux.children {
out = append(out, n.Node)
}
return
}
func (g depSubgraph) parent(n *Node) (out NodeSet) {
aux, ok := g.nodeMap[n]
if !ok {
return
}
for n := range aux.parents {
out = append(out, n.Node)
}
return
}
// Utility functions
func (g *depSubgraph) nodesWithMissingRefs() (out []*depNode) {
out = make([]*depNode, 0, 3)
for _, n := range g.nodes {
if len(n.missingReferences) > 0 {
out = append(out, n)
}
}
return
}
func unscopedSubgraph(gg ...*depSubgraph) *depSubgraph {
for _, g := range gg {
if g.scope.IsEmpty() {
return g
}
}
return nil
}
// depNodeForRef returns a node which matches the ref (nil if none)
func depNodeForRef(ref Ref, nn ...*depNode) (out *depNode) {
for _, n := range nn {
if n.Node.ResourceType != ref.ResourceType {
continue
}
if n.Node.Identifiers.HasIntersection(ref.Identifiers) {
return n
}
}
return
}
// depNodesByResourceType returns a map where key is resource type, value slice of nodes
func depNodesByResourceType(nn ...*depNode) (out map[string][]*depNode) {
out = make(map[string][]*depNode, 4)
for _, n := range nn {
out[n.Node.ResourceType] = append(out[n.Node.ResourceType], n)
}
return
}
// unpackDepNodes extracts envoy Nodes from the dep. nodes
//
// The function does no validation nor filtering for nil values.
func unpackDepNodes(nn ...*depNode) (out NodeSet) {
out = make(NodeSet, 0, len(nn))
for _, n := range nn {
out = append(out, n.Node)
}
return out
}