@@ -83,44 +83,13 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
83
83
84
84
if options .ShowAllResources {
85
85
// Adds resource sets and resource set bindings
86
- if resourceSetBindings , err := getResourceSetBindingsInCluster (ctx , c , namespace , name ); err == nil {
87
- // Should this be in it's own group?
88
- addons := VirtualObject (cluster .Namespace , "AddonGroup" , "Addons" )
89
- tree .Add (cluster , addons )
90
- for i , resourceSetBinding := range resourceSetBindings {
91
- tree .Add (addons , & resourceSetBindings [i ], ObjectMetaName ("ClusterResourceSetBinding" ))
92
- for _ , binding := range resourceSetBinding .Spec .Bindings {
93
- if resourceSet , err := getResourceSetInCluster (ctx , c , namespace , binding .ClusterResourceSetName ); err == nil {
94
- tree .Add (addons , resourceSet , ObjectMetaName ("ClusterResourceSet" ))
95
- }
96
- }
97
- }
98
- }
86
+ addAddonsToObjectTree (ctx , c , cluster , tree )
99
87
}
100
88
101
89
// Adds control plane
102
90
controlPlane , err := external .Get (ctx , c , cluster .Spec .ControlPlaneRef , cluster .Namespace )
103
91
if err == nil {
104
- tree .Add (cluster , controlPlane , ObjectMetaName ("ControlPlane" ), GroupingObject (true ))
105
-
106
- if options .ShowAllResources {
107
- // Add control plane infrastructure ref using spec fields guaranteed in contract
108
- infrastructureRef , found , err := unstructured .NestedMap (controlPlane .UnstructuredContent (), "spec" , "machineTemplate" , "infrastructureRef" )
109
- if err != nil {
110
- return nil , err
111
- }
112
- if found {
113
- infrastructureObjectRef := & corev1.ObjectReference {
114
- Kind : infrastructureRef ["kind" ].(string ),
115
- Namespace : infrastructureRef ["namespace" ].(string ),
116
- Name : infrastructureRef ["name" ].(string ),
117
- APIVersion : infrastructureRef ["apiVersion" ].(string ),
118
- }
119
- if machineTemplate , err := external .Get (ctx , c , infrastructureObjectRef , cluster .Namespace ); err == nil {
120
- tree .Add (controlPlane , machineTemplate , ObjectMetaName ("MachineInfrastructureTemplate" ))
121
- }
122
- }
123
- }
92
+ addControlPlane (ctx , c , cluster , controlPlane , tree , options )
124
93
}
125
94
126
95
// Adds control plane machines.
@@ -163,15 +132,76 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
163
132
workers := VirtualObject (cluster .Namespace , "WorkerGroup" , "Workers" )
164
133
tree .Add (cluster , workers )
165
134
135
+ err = addMachineDeploymentToObjectTree (ctx , c , cluster , workers , machinesList , tree , options , addMachineFunc )
136
+ if err != nil {
137
+ return nil , err
138
+ }
139
+
140
+ addMachinePoolsToObjectTree (ctx , c , cluster , workers , machinePoolList , tree , options )
141
+
142
+ // Handles orphan machines.
143
+ if len (machineMap ) < len (machinesList .Items ) {
144
+ other := VirtualObject (cluster .Namespace , "OtherGroup" , "Other" )
145
+ tree .Add (workers , other )
146
+
147
+ for i := range machinesList .Items {
148
+ m := & machinesList .Items [i ]
149
+ if _ , ok := machineMap [m .Name ]; ok {
150
+ continue
151
+ }
152
+ addMachineFunc (other , m )
153
+ }
154
+ }
155
+
156
+ return tree , nil
157
+ }
158
+
159
+ func addAddonsToObjectTree (ctx context.Context , c client.Client , cluster * clusterv1.Cluster , tree * ObjectTree ) {
160
+ if resourceSetBindings , err := getResourceSetBindingsInCluster (ctx , c , cluster .Namespace , cluster .Name ); err == nil {
161
+ // Should this be in it's own group?
162
+ addons := VirtualObject (cluster .Namespace , "AddonGroup" , "Addons" )
163
+ tree .Add (cluster , addons )
164
+ for i , resourceSetBinding := range resourceSetBindings {
165
+ tree .Add (addons , & resourceSetBindings [i ], ObjectMetaName ("ClusterResourceSetBinding" ))
166
+ for _ , binding := range resourceSetBinding .Spec .Bindings {
167
+ if resourceSet , err := getResourceSetInCluster (ctx , c , cluster .Namespace , binding .ClusterResourceSetName ); err == nil {
168
+ tree .Add (addons , resourceSet , ObjectMetaName ("ClusterResourceSet" ))
169
+ }
170
+ }
171
+ }
172
+ }
173
+ }
174
+
175
+ func addControlPlane (ctx context.Context , c client.Client , cluster * clusterv1.Cluster , controlPlane * unstructured.Unstructured , tree * ObjectTree , options DiscoverOptions ) {
176
+ tree .Add (cluster , controlPlane , ObjectMetaName ("ControlPlane" ), GroupingObject (true ))
177
+
178
+ if options .ShowAllResources {
179
+ // Add control plane infrastructure ref using spec fields guaranteed in contract
180
+ infrastructureRef , found , err := unstructured .NestedMap (controlPlane .UnstructuredContent (), "spec" , "machineTemplate" , "infrastructureRef" )
181
+ if err != nil && found {
182
+ infrastructureObjectRef := & corev1.ObjectReference {
183
+ Kind : infrastructureRef ["kind" ].(string ),
184
+ Namespace : infrastructureRef ["namespace" ].(string ),
185
+ Name : infrastructureRef ["name" ].(string ),
186
+ APIVersion : infrastructureRef ["apiVersion" ].(string ),
187
+ }
188
+ if machineTemplate , err := external .Get (ctx , c , infrastructureObjectRef , cluster .Namespace ); err == nil {
189
+ tree .Add (controlPlane , machineTemplate , ObjectMetaName ("MachineInfrastructureTemplate" ))
190
+ }
191
+ }
192
+ }
193
+ }
194
+
195
+ func addMachineDeploymentToObjectTree (ctx context.Context , c client.Client , cluster * clusterv1.Cluster , workers * unstructured.Unstructured , machinesList * clusterv1.MachineList , tree * ObjectTree , options DiscoverOptions , addMachineFunc func (parent client.Object , m * clusterv1.Machine )) error {
166
196
// Adds worker machines.
167
197
machinesDeploymentList , err := getMachineDeploymentsInCluster (ctx , c , cluster .Namespace , cluster .Name )
168
198
if err != nil {
169
- return nil , err
199
+ return err
170
200
}
171
201
172
202
machineSetList , err := getMachineSetsInCluster (ctx , c , cluster .Namespace , cluster .Name )
173
203
if err != nil {
174
- return nil , err
204
+ return err
175
205
}
176
206
177
207
for i := range machinesDeploymentList .Items {
@@ -209,6 +239,10 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
209
239
}
210
240
}
211
241
242
+ return nil
243
+ }
244
+
245
+ func addMachinePoolsToObjectTree (ctx context.Context , c client.Client , cluster * clusterv1.Cluster , workers * unstructured.Unstructured , machinePoolList * expv1.MachinePoolList , tree * ObjectTree , options DiscoverOptions ) {
212
246
for i := range machinePoolList .Items {
213
247
mp := & machinePoolList .Items [i ]
214
248
addOpts := make ([]AddObjectOption , 0 )
@@ -229,22 +263,6 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
229
263
230
264
// TODO: Add machinepoolmachines when it's implemented
231
265
}
232
-
233
- // Handles orphan machines.
234
- if len (machineMap ) < len (machinesList .Items ) {
235
- other := VirtualObject (cluster .Namespace , "OtherGroup" , "Other" )
236
- tree .Add (workers , other )
237
-
238
- for i := range machinesList .Items {
239
- m := & machinesList .Items [i ]
240
- if _ , ok := machineMap [m .Name ]; ok {
241
- continue
242
- }
243
- addMachineFunc (other , m )
244
- }
245
- }
246
-
247
- return tree , nil
248
266
}
249
267
250
268
func getResourceSetBindingsInCluster (ctx context.Context , c client.Client , namespace string , name string ) ([]addonsv1.ClusterResourceSetBinding , error ) {
0 commit comments