-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
nodes.go
213 lines (181 loc) · 7.04 KB
/
nodes.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
package nodes
import (
"github.com/gonum/graph"
kapi "k8s.io/kubernetes/pkg/api"
kapps "k8s.io/kubernetes/pkg/apis/apps"
"k8s.io/kubernetes/pkg/apis/autoscaling"
osgraph "github.com/openshift/origin/pkg/api/graph"
)
func EnsurePodNode(g osgraph.MutableUniqueGraph, pod *kapi.Pod) *PodNode {
podNodeName := PodNodeName(pod)
podNode := osgraph.EnsureUnique(g,
podNodeName,
func(node osgraph.Node) graph.Node {
return &PodNode{node, pod}
},
).(*PodNode)
podSpecNode := EnsurePodSpecNode(g, &pod.Spec, pod.Namespace, podNodeName)
g.AddEdge(podNode, podSpecNode, osgraph.ContainsEdgeKind)
return podNode
}
func EnsurePodSpecNode(g osgraph.MutableUniqueGraph, podSpec *kapi.PodSpec, namespace string, ownerName osgraph.UniqueName) *PodSpecNode {
return osgraph.EnsureUnique(g,
PodSpecNodeName(podSpec, ownerName),
func(node osgraph.Node) graph.Node {
return &PodSpecNode{node, podSpec, namespace, ownerName}
},
).(*PodSpecNode)
}
// EnsureServiceNode adds the provided service to the graph if it does not already exist.
func EnsureServiceNode(g osgraph.MutableUniqueGraph, svc *kapi.Service) *ServiceNode {
return osgraph.EnsureUnique(g,
ServiceNodeName(svc),
func(node osgraph.Node) graph.Node {
return &ServiceNode{node, svc, true}
},
).(*ServiceNode)
}
// FindOrCreateSyntheticServiceNode returns the existing service node or creates a synthetic node in its place
func FindOrCreateSyntheticServiceNode(g osgraph.MutableUniqueGraph, svc *kapi.Service) *ServiceNode {
return osgraph.EnsureUnique(g,
ServiceNodeName(svc),
func(node osgraph.Node) graph.Node {
return &ServiceNode{node, svc, false}
},
).(*ServiceNode)
}
func EnsureServiceAccountNode(g osgraph.MutableUniqueGraph, o *kapi.ServiceAccount) *ServiceAccountNode {
return osgraph.EnsureUnique(g,
ServiceAccountNodeName(o),
func(node osgraph.Node) graph.Node {
return &ServiceAccountNode{node, o, true}
},
).(*ServiceAccountNode)
}
func FindOrCreateSyntheticServiceAccountNode(g osgraph.MutableUniqueGraph, o *kapi.ServiceAccount) *ServiceAccountNode {
return osgraph.EnsureUnique(g,
ServiceAccountNodeName(o),
func(node osgraph.Node) graph.Node {
return &ServiceAccountNode{node, o, false}
},
).(*ServiceAccountNode)
}
func EnsureSecretNode(g osgraph.MutableUniqueGraph, o *kapi.Secret) *SecretNode {
return osgraph.EnsureUnique(g,
SecretNodeName(o),
func(node osgraph.Node) graph.Node {
return &SecretNode{
Node: node,
Secret: o,
IsFound: true,
}
},
).(*SecretNode)
}
func FindOrCreateSyntheticSecretNode(g osgraph.MutableUniqueGraph, o *kapi.Secret) *SecretNode {
return osgraph.EnsureUnique(g,
SecretNodeName(o),
func(node osgraph.Node) graph.Node {
return &SecretNode{
Node: node,
Secret: o,
IsFound: false,
}
},
).(*SecretNode)
}
// EnsureReplicationControllerNode adds a graph node for the ReplicationController if it does not already exist.
func EnsureReplicationControllerNode(g osgraph.MutableUniqueGraph, rc *kapi.ReplicationController) *ReplicationControllerNode {
rcNodeName := ReplicationControllerNodeName(rc)
rcNode := osgraph.EnsureUnique(g,
rcNodeName,
func(node osgraph.Node) graph.Node {
return &ReplicationControllerNode{node, rc, true}
},
).(*ReplicationControllerNode)
rcSpecNode := EnsureReplicationControllerSpecNode(g, &rc.Spec, rc.Namespace, rcNodeName)
g.AddEdge(rcNode, rcSpecNode, osgraph.ContainsEdgeKind)
return rcNode
}
func FindOrCreateSyntheticReplicationControllerNode(g osgraph.MutableUniqueGraph, rc *kapi.ReplicationController) *ReplicationControllerNode {
return osgraph.EnsureUnique(g,
ReplicationControllerNodeName(rc),
func(node osgraph.Node) graph.Node {
return &ReplicationControllerNode{node, rc, false}
},
).(*ReplicationControllerNode)
}
func EnsureReplicationControllerSpecNode(g osgraph.MutableUniqueGraph, rcSpec *kapi.ReplicationControllerSpec, namespace string, ownerName osgraph.UniqueName) *ReplicationControllerSpecNode {
rcSpecName := ReplicationControllerSpecNodeName(rcSpec, ownerName)
rcSpecNode := osgraph.EnsureUnique(g,
rcSpecName,
func(node osgraph.Node) graph.Node {
return &ReplicationControllerSpecNode{node, rcSpec, namespace, ownerName}
},
).(*ReplicationControllerSpecNode)
if rcSpec.Template != nil {
ptSpecNode := EnsurePodTemplateSpecNode(g, rcSpec.Template, namespace, rcSpecName)
g.AddEdge(rcSpecNode, ptSpecNode, osgraph.ContainsEdgeKind)
}
return rcSpecNode
}
func EnsurePodTemplateSpecNode(g osgraph.MutableUniqueGraph, ptSpec *kapi.PodTemplateSpec, namespace string, ownerName osgraph.UniqueName) *PodTemplateSpecNode {
ptSpecName := PodTemplateSpecNodeName(ptSpec, ownerName)
ptSpecNode := osgraph.EnsureUnique(g,
ptSpecName,
func(node osgraph.Node) graph.Node {
return &PodTemplateSpecNode{node, ptSpec, namespace, ownerName}
},
).(*PodTemplateSpecNode)
podSpecNode := EnsurePodSpecNode(g, &ptSpec.Spec, namespace, ptSpecName)
g.AddEdge(ptSpecNode, podSpecNode, osgraph.ContainsEdgeKind)
return ptSpecNode
}
func EnsurePersistentVolumeClaimNode(g osgraph.MutableUniqueGraph, pvc *kapi.PersistentVolumeClaim) *PersistentVolumeClaimNode {
return osgraph.EnsureUnique(g,
PersistentVolumeClaimNodeName(pvc),
func(node osgraph.Node) graph.Node {
return &PersistentVolumeClaimNode{Node: node, PersistentVolumeClaim: pvc, IsFound: true}
},
).(*PersistentVolumeClaimNode)
}
func FindOrCreateSyntheticPVCNode(g osgraph.MutableUniqueGraph, pvc *kapi.PersistentVolumeClaim) *PersistentVolumeClaimNode {
return osgraph.EnsureUnique(g,
PersistentVolumeClaimNodeName(pvc),
func(node osgraph.Node) graph.Node {
return &PersistentVolumeClaimNode{Node: node, PersistentVolumeClaim: pvc, IsFound: false}
},
).(*PersistentVolumeClaimNode)
}
func EnsureHorizontalPodAutoscalerNode(g osgraph.MutableUniqueGraph, hpa *autoscaling.HorizontalPodAutoscaler) *HorizontalPodAutoscalerNode {
return osgraph.EnsureUnique(g,
HorizontalPodAutoscalerNodeName(hpa),
func(node osgraph.Node) graph.Node {
return &HorizontalPodAutoscalerNode{Node: node, HorizontalPodAutoscaler: hpa}
},
).(*HorizontalPodAutoscalerNode)
}
func EnsureStatefulSetNode(g osgraph.MutableUniqueGraph, statefulSet *kapps.StatefulSet) *StatefulSetNode {
nodeName := StatefulSetNodeName(statefulSet)
node := osgraph.EnsureUnique(g,
nodeName,
func(node osgraph.Node) graph.Node {
return &StatefulSetNode{node, statefulSet}
},
).(*StatefulSetNode)
specNode := EnsureStatefulSetSpecNode(g, &statefulSet.Spec, statefulSet.Namespace, nodeName)
g.AddEdge(node, specNode, osgraph.ContainsEdgeKind)
return node
}
func EnsureStatefulSetSpecNode(g osgraph.MutableUniqueGraph, spec *kapps.StatefulSetSpec, namespace string, ownerName osgraph.UniqueName) *StatefulSetSpecNode {
specName := StatefulSetSpecNodeName(spec, ownerName)
specNode := osgraph.EnsureUnique(g,
specName,
func(node osgraph.Node) graph.Node {
return &StatefulSetSpecNode{node, spec, namespace, ownerName}
},
).(*StatefulSetSpecNode)
ptSpecNode := EnsurePodTemplateSpecNode(g, &spec.Template, namespace, specName)
g.AddEdge(specNode, ptSpecNode, osgraph.ContainsEdgeKind)
return specNode
}