@@ -19,6 +19,7 @@ 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"
@@ -111,7 +112,9 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
111
112
112
113
addAnnotation (controlPlane , ObjectContractAnnotation , "ControlPlane" )
113
114
addAnnotation (controlPlane , ObjectContractVersionAnnotation , contractVersion )
114
- addControlPlane (cluster , controlPlane , tree , options )
115
+ if err := addControlPlane (ctx , c , cluster , controlPlane , tree , options ); err != nil {
116
+ return nil , err
117
+ }
115
118
}
116
119
117
120
// Adds control plane machines.
@@ -121,7 +124,7 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt
121
124
}
122
125
machineMap := map [string ]bool {}
123
126
addMachineFunc := func (parent client.Object , m * clusterv1.Machine ) {
124
- _ , visible := tree .Add (parent , m )
127
+ _ , visible := tree .Add (parent , m , GroupVersionKind ( clusterv1 . GroupVersion . WithKind ( "Machine" )) )
125
128
machineMap [m .Name ] = true
126
129
127
130
if visible {
@@ -205,30 +208,51 @@ func addClusterResourceSetsToObjectTree(ctx context.Context, c client.Client, cl
205
208
}
206
209
}
207
210
208
- 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 {
209
212
tree .Add (cluster , controlPlane , ObjectMetaName ("ControlPlane" ), GroupingObject (true ))
210
213
211
214
if options .ShowTemplates {
212
215
// Add control plane infrastructure ref using spec fields guaranteed in contract
213
- infrastructureRef , found , err := unstructured .NestedMap (controlPlane .UnstructuredContent (), "spec" , "machineTemplate" , "infrastructureRef" )
214
- if err == nil && found {
215
- infrastructureObjectRef := & corev1.ObjectReference {
216
- Kind : infrastructureRef ["kind" ].(string ),
217
- Namespace : infrastructureRef ["namespace" ].(string ),
218
- Name : infrastructureRef ["name" ].(string ),
219
- APIVersion : infrastructureRef ["apiVersion" ].(string ),
220
- }
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
+ }
221
220
222
- machineTemplateRefObject := ObjectReferenceObject (infrastructureObjectRef )
223
- var templateParent client.Object
224
- if options .AddTemplateVirtualNode {
225
- templateParent = addTemplateVirtualNode (tree , controlPlane , cluster .Namespace )
226
- } else {
227
- 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
228
226
}
229
- 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
230
251
}
252
+ tree .Add (templateParent , machineTemplateRefObject , ObjectMetaName ("MachineInfrastructureTemplate" ))
231
253
}
254
+
255
+ return nil
232
256
}
233
257
234
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 {
@@ -249,6 +273,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
249
273
if ! options .ShowMachineSets {
250
274
addOpts = append (addOpts , GroupingObject (true ))
251
275
}
276
+ addOpts = append (addOpts , GroupVersionKind (clusterv1 .GroupVersion .WithKind ("MachineDeployment" )))
252
277
tree .Add (workers , md , addOpts ... )
253
278
254
279
if options .ShowTemplates {
@@ -293,7 +318,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
293
318
294
319
var parent client.Object = md
295
320
if options .ShowMachineSets {
296
- tree .Add (md , ms , GroupingObject (true ))
321
+ tree .Add (md , ms , GroupingObject (true ), GroupVersionKind ( clusterv1 . GroupVersion . WithKind ( "MachineSet" )) )
297
322
parent = ms
298
323
}
299
324
@@ -310,7 +335,7 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus
310
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 )) {
311
336
for i := range machinePoolList .Items {
312
337
mp := & machinePoolList .Items [i ]
313
- _ , visible := tree .Add (workers , mp , GroupingObject (true ))
338
+ _ , visible := tree .Add (workers , mp , GroupingObject (true ), GroupVersionKind ( clusterv1 . GroupVersion . WithKind ( "MachinePool" )) )
314
339
315
340
if visible {
316
341
if machinePoolBootstrap , err := external .GetObjectFromContractVersionedRef (ctx , c , mp .Spec .Template .Spec .Bootstrap .ConfigRef , mp .Namespace ); err == nil {
0 commit comments