forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
69 lines (55 loc) · 2.05 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
package graph
import (
"fmt"
"github.com/gonum/graph"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
)
const (
UnknownNodeKind = "UnknownNode"
)
const (
UnknownEdgeKind = "UnknownEdge"
// ReferencedByEdgeKind is the kind to use if you're building reverse links that don't have a specific edge in the other direction
// other uses are discouraged. You should create a kind for your edge
ReferencedByEdgeKind = "ReferencedBy"
// ContainsEdgeKind is the kind to use if one node's contents logically contain another node's contents. A given node can only have
// a single inbound Contais edge. The code does not prevent contains cycles, but that's insane, don't do that.
ContainsEdgeKind = "Contains"
)
func GetUniqueRuntimeObjectNodeName(nodeKind string, obj runtime.Object) UniqueName {
meta, err := meta.Accessor(obj)
if err != nil {
panic(err)
}
return UniqueName(fmt.Sprintf("%s|%s/%s", nodeKind, meta.GetNamespace(), meta.GetName()))
}
// GetTopLevelContainerNode traverses the reverse ContainsEdgeKind edges until it finds a node
// that does not have an inbound ContainsEdgeKind edge. This could be the node itself
func GetTopLevelContainerNode(g Graph, containedNode graph.Node) graph.Node {
// my kingdom for a LinkedHashSet
visited := map[int]bool{}
prevContainingNode := containedNode
for {
visited[prevContainingNode.ID()] = true
currContainingNode := GetContainingNode(g, prevContainingNode)
if currContainingNode == nil {
return prevContainingNode
}
if _, alreadyVisited := visited[currContainingNode.ID()]; alreadyVisited {
panic(fmt.Sprintf("contains cycle in %v", visited))
}
prevContainingNode = currContainingNode
}
}
// GetContainingNode returns the direct predecessor that is linked to the node by a ContainsEdgeKind. It returns
// nil if no container is found.
func GetContainingNode(g Graph, containedNode graph.Node) graph.Node {
for _, node := range g.To(containedNode) {
edge := g.Edge(node, containedNode)
if g.EdgeKinds(edge).Has(ContainsEdgeKind) {
return node
}
}
return nil
}