@@ -19,9 +19,11 @@ package tree
19
19
import (
20
20
"context"
21
21
22
+ "github.com/pkg/errors"
22
23
corev1 "k8s.io/api/core/v1"
23
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
+ "k8s.io/apimachinery/pkg/runtime/schema"
25
27
"sigs.k8s.io/controller-runtime/pkg/client"
26
28
27
29
addonsv1 "sigs.k8s.io/cluster-api/api/addons/v1beta2"
@@ -110,7 +112,9 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
110
112
111
113
addAnnotation (controlPlane , ObjectContractAnnotation , "ControlPlane" )
112
114
addAnnotation (controlPlane , ObjectContractVersionAnnotation , contractVersion )
113
- addControlPlane (cluster , controlPlane , tree , options )
115
+ if err := addControlPlane (ctx , c , cluster , controlPlane , tree , options ); err != nil {
116
+ return nil , err
117
+ }
114
118
}
115
119
116
120
// Adds control plane machines.
@@ -120,7 +124,7 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
120
124
}
121
125
machineMap := map [string ]bool {}
122
126
addMachineFunc := func (parent client.Object , m * clusterv1.Machine ) {
123
- _ , visible := tree .Add (parent , m )
127
+ _ , visible := tree .Add (parent , m , GroupVersionKind ( clusterv1 . GroupVersion . WithKind ( "Machine" )) )
124
128
machineMap [m .Name ] = true
125
129
126
130
if visible {
@@ -204,30 +208,51 @@ func addClusterResourceSetsToObjectTree(ctx context.Context, c client.Client, cl
204
208
}
205
209
}
206
210
207
- func addControlPlane (cluster * clusterv1.Cluster , controlPlane * unstructured.Unstructured , tree * ObjectTree , options DiscoverOptions ) {
211
+ func addControlPlane (ctx context. Context , c client. Client , cluster * clusterv1.Cluster , controlPlane * unstructured.Unstructured , tree * ObjectTree , options DiscoverOptions ) error {
208
212
tree .Add (cluster , controlPlane , ObjectMetaName ("ControlPlane" ), GroupingObject (true ))
209
213
210
214
if options .ShowTemplates {
211
215
// Add control plane infrastructure ref using spec fields guaranteed in contract
212
- infrastructureRef , found , err := unstructured .NestedMap (controlPlane .UnstructuredContent (), "spec" , "machineTemplate" , "infrastructureRef" )
213
- if err == nil && found {
214
- infrastructureObjectRef := & corev1.ObjectReference {
215
- Kind : infrastructureRef ["kind" ].(string ),
216
- Namespace : infrastructureRef ["namespace" ].(string ),
217
- Name : infrastructureRef ["name" ].(string ),
218
- APIVersion : infrastructureRef ["apiVersion" ].(string ),
219
- }
216
+ contractVersion , err := contract .GetContractVersionForVersion (ctx , c , controlPlane .GroupVersionKind ().GroupKind (), controlPlane .GroupVersionKind ().Version )
217
+ if err != nil {
218
+ return errors .Wrapf (err , "failed to get contract version for the ControlPlane object" )
219
+ }
220
220
221
- machineTemplateRefObject := ObjectReferenceObject (infrastructureObjectRef )
222
- var templateParent client.Object
223
- if options .AddTemplateVirtualNode {
224
- templateParent = addTemplateVirtualNode (tree , controlPlane , cluster .Namespace )
225
- } else {
226
- templateParent = controlPlane
221
+ var infrastructureObjectRef * corev1.ObjectReference
222
+ if contractVersion == "v1beta1" {
223
+ currentRef , err := contract .ControlPlane ().MachineTemplate ().InfrastructureV1Beta1Ref ().Get (controlPlane )
224
+ if err != nil {
225
+ return nil //nolint:nilerr // intentionally ignoring the error here because infraRef in CP is optional
227
226
}
228
- tree .Add (templateParent , machineTemplateRefObject , ObjectMetaName ("MachineInfrastructureTemplate" ))
227
+ infrastructureObjectRef = currentRef
228
+ } else {
229
+ currentRef , err := contract .ControlPlane ().MachineTemplate ().InfrastructureRef ().Get (controlPlane )
230
+ if err != nil {
231
+ return nil //nolint:nilerr // intentionally ignoring the error here because infraRef in CP is optional
232
+ }
233
+ apiVersion , err := contract .GetAPIVersion (ctx , c , currentRef .GroupKind ())
234
+ if err != nil {
235
+ return err
236
+ }
237
+ infrastructureObjectRef = & corev1.ObjectReference {
238
+ APIVersion : apiVersion ,
239
+ Kind : currentRef .Kind ,
240
+ Namespace : controlPlane .GetNamespace (),
241
+ Name : currentRef .Name ,
242
+ }
243
+ }
244
+
245
+ machineTemplateRefObject := ObjectReferenceObject (infrastructureObjectRef )
246
+ var templateParent client.Object
247
+ if options .AddTemplateVirtualNode {
248
+ templateParent = addTemplateVirtualNode (tree , controlPlane , cluster .Namespace )
249
+ } else {
250
+ templateParent = controlPlane
229
251
}
252
+ tree .Add (templateParent , machineTemplateRefObject , ObjectMetaName ("MachineInfrastructureTemplate" ))
230
253
}
254
+
255
+ return nil
231
256
}
232
257
233
258
func addMachineDeploymentToObjectTree (ctx context.Context , c client.Client , cluster * clusterv1.Cluster , workers * NodeObject , machinesList * clusterv1.MachineList , tree * ObjectTree , options DiscoverOptions , addMachineFunc func (parent client.Object , m * clusterv1.Machine )) error {
@@ -248,6 +273,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
248
273
if ! options .ShowMachineSets {
249
274
addOpts = append (addOpts , GroupingObject (true ))
250
275
}
276
+ addOpts = append (addOpts , GroupVersionKind (clusterv1 .GroupVersion .WithKind ("MachineDeployment" )))
251
277
tree .Add (workers , md , addOpts ... )
252
278
253
279
if options .ShowTemplates {
@@ -286,17 +312,17 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
286
312
tree .Add (templateParent , machineTemplateRefObject , ObjectMetaName ("MachineInfrastructureTemplate" ))
287
313
}
288
314
289
- machineSets := selectMachinesSetsControlledBy (machineSetList , md )
315
+ machineSets := selectMachinesSetsControlledBy (machineSetList , md , clusterv1 . GroupVersion . WithKind ( "MachineDeployment" ). GroupKind () )
290
316
for i := range machineSets {
291
317
ms := machineSets [i ]
292
318
293
319
var parent client.Object = md
294
320
if options .ShowMachineSets {
295
- tree .Add (md , ms , GroupingObject (true ))
321
+ tree .Add (md , ms , GroupingObject (true ), GroupVersionKind ( clusterv1 . GroupVersion . WithKind ( "MachineSet" )) )
296
322
parent = ms
297
323
}
298
324
299
- machines := selectMachinesControlledBy (machinesList , ms )
325
+ machines := selectMachinesControlledBy (machinesList , ms , clusterv1 . GroupVersion . WithKind ( "MachineSet" ). GroupKind () )
300
326
for _ , w := range machines {
301
327
addMachineFunc (parent , w )
302
328
}
@@ -309,7 +335,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
309
335
func addMachinePoolsToObjectTree (ctx context.Context , c client.Client , workers * NodeObject , machinePoolList * clusterv1.MachinePoolList , machinesList * clusterv1.MachineList , tree * ObjectTree , addMachineFunc func (parent client.Object , m * clusterv1.Machine )) {
310
336
for i := range machinePoolList .Items {
311
337
mp := & machinePoolList .Items [i ]
312
- _ , visible := tree .Add (workers , mp , GroupingObject (true ))
338
+ _ , visible := tree .Add (workers , mp , GroupingObject (true ), GroupVersionKind ( clusterv1 . GroupVersion . WithKind ( "MachinePool" )) )
313
339
314
340
if visible {
315
341
if machinePoolBootstrap , err := external .GetObjectFromContractVersionedRef (ctx , c , mp .Spec .Template .Spec .Bootstrap .ConfigRef , mp .Namespace ); err == nil {
@@ -321,7 +347,7 @@ func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, workers *
321
347
}
322
348
}
323
349
324
- machines := selectMachinesControlledBy (machinesList , mp )
350
+ machines := selectMachinesControlledBy (machinesList , mp , clusterv1 . GroupVersion . WithKind ( "MachinePool" ). GroupKind () )
325
351
for _ , m := range machines {
326
352
addMachineFunc (mp , m )
327
353
}
@@ -417,22 +443,22 @@ func selectControlPlaneMachines(machineList *clusterv1.MachineList) []*clusterv1
417
443
return machines
418
444
}
419
445
420
- func selectMachinesSetsControlledBy (machineSetList * clusterv1.MachineSetList , controller client.Object ) []* clusterv1.MachineSet {
446
+ func selectMachinesSetsControlledBy (machineSetList * clusterv1.MachineSetList , controller client.Object , controllerGK schema. GroupKind ) []* clusterv1.MachineSet {
421
447
machineSets := []* clusterv1.MachineSet {}
422
448
for i := range machineSetList .Items {
423
449
m := & machineSetList .Items [i ]
424
- if util .IsControlledBy (m , controller ) {
450
+ if util .IsControlledBy (m , controller , controllerGK ) {
425
451
machineSets = append (machineSets , m )
426
452
}
427
453
}
428
454
return machineSets
429
455
}
430
456
431
- func selectMachinesControlledBy (machineList * clusterv1.MachineList , controller client.Object ) []* clusterv1.Machine {
457
+ func selectMachinesControlledBy (machineList * clusterv1.MachineList , controller client.Object , controllerGK schema. GroupKind ) []* clusterv1.Machine {
432
458
machines := []* clusterv1.Machine {}
433
459
for i := range machineList .Items {
434
460
m := & machineList .Items [i ]
435
- if util .IsControlledBy (m , controller ) {
461
+ if util .IsControlledBy (m , controller , controllerGK ) {
436
462
machines = append (machines , m )
437
463
}
438
464
}
0 commit comments