@@ -22,6 +22,7 @@ import (
22
22
rdeType "github.com/vmware/cluster-api-provider-cloud-director/pkg/vcdtypes/rde_type_1_1_0"
23
23
"github.com/vmware/cluster-api-provider-cloud-director/release"
24
24
"github.com/vmware/go-vcloud-director/v2/govcd"
25
+ "github.com/vmware/go-vcloud-director/v2/types/v56"
25
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
26
27
"k8s.io/apimachinery/pkg/runtime"
27
28
"k8s.io/klog"
@@ -229,10 +230,14 @@ func validateDerivedRDEProperties(vcdCluster *infrav1.VCDCluster, infraID string
229
230
230
231
// TODO: Remove uncommented code when decision to only keep capi.yaml as part of RDE spec is finalized
231
232
func (r * VCDClusterReconciler ) constructCapvcdRDE (ctx context.Context , cluster * clusterv1.Cluster ,
232
- vcdCluster * infrav1.VCDCluster ) (* swagger.DefinedEntity , error ) {
233
- org := vcdCluster .Spec .Org
234
- vdc := vcdCluster .Spec .Ovdc
233
+ vcdCluster * infrav1.VCDCluster , vdc * types.Vdc , vcdOrg * types.Org ) (* swagger.DefinedEntity , error ) {
235
234
235
+ if vdc == nil {
236
+ return nil , fmt .Errorf ("VDC cannot be nil" )
237
+ }
238
+ if vcdOrg == nil {
239
+ return nil , fmt .Errorf ("org cannot be nil" )
240
+ }
236
241
kcpList , err := getAllKubeadmControlPlaneForCluster (ctx , r .Client , * cluster )
237
242
if err != nil {
238
243
return nil , fmt .Errorf ("error getting KubeadmControlPlane objects for cluster [%s]: [%v]" , vcdCluster .Name , err )
@@ -256,8 +261,8 @@ func (r *VCDClusterReconciler) constructCapvcdRDE(ctx context.Context, cluster *
256
261
ApiVersion : capisdk .CAPVCDClusterEntityApiVersion ,
257
262
Metadata : rdeType.Metadata {
258
263
Name : vcdCluster .Name ,
259
- Org : org ,
260
- Vdc : vdc ,
264
+ Org : vcdOrg . Name ,
265
+ Vdc : vdc . Name ,
261
266
Site : vcdCluster .Spec .Site ,
262
267
},
263
268
Spec : rdeType.CAPVCDSpec {},
@@ -283,9 +288,19 @@ func (r *VCDClusterReconciler) constructCapvcdRDE(ctx context.Context, cluster *
283
288
},
284
289
ParentUID : vcdCluster .Spec .ParentUID ,
285
290
VcdProperties : rdeType.VCDProperties {
286
- Site : vcdCluster .Spec .Site ,
287
- Org : org ,
288
- Vdc : vdc ,
291
+ Site : vcdCluster .Spec .Site ,
292
+ Org : []rdeType.Org {
293
+ rdeType.Org {
294
+ Name : vcdOrg .Name ,
295
+ ID : vcdOrg .ID ,
296
+ },
297
+ },
298
+ Ovdc : []rdeType.Ovdc {
299
+ rdeType.Ovdc {
300
+ Name : vdc .Name ,
301
+ ID : vdc .ID ,
302
+ },
303
+ },
289
304
OvdcNetwork : vcdCluster .Spec .OvdcNetwork ,
290
305
},
291
306
CapiStatusYaml : "" ,
@@ -316,7 +331,11 @@ func (r *VCDClusterReconciler) constructCapvcdRDE(ctx context.Context, cluster *
316
331
func (r * VCDClusterReconciler ) constructAndCreateRDEFromCluster (ctx context.Context , workloadVCDClient * vcdsdk.Client , cluster * clusterv1.Cluster , vcdCluster * infrav1.VCDCluster ) (string , error ) {
317
332
log := ctrl .LoggerFrom (ctx )
318
333
319
- rde , err := r .constructCapvcdRDE (ctx , cluster , vcdCluster )
334
+ org , err := workloadVCDClient .VCDClient .GetOrgByName (vcdCluster .Spec .Org )
335
+ if err != nil {
336
+ return "" , fmt .Errorf ("failed to get org by name [%s]" , vcdCluster .Spec .Org )
337
+ }
338
+ rde , err := r .constructCapvcdRDE (ctx , cluster , vcdCluster , workloadVCDClient .VDC .Vdc , org .Org )
320
339
if err != nil {
321
340
return "" , fmt .Errorf ("error occurred while constructing RDE payload for the cluster [%s]: [%v]" , vcdCluster .Name , err )
322
341
}
@@ -341,19 +360,26 @@ func (r *VCDClusterReconciler) constructAndCreateRDEFromCluster(ctx context.Cont
341
360
}
342
361
343
362
func (r * VCDClusterReconciler ) reconcileRDE (ctx context.Context , cluster * clusterv1.Cluster ,
344
- vcdCluster * infrav1.VCDCluster , workloadVCDClient * vcdsdk.Client ) error {
363
+ vcdCluster * infrav1.VCDCluster , workloadVCDClient * vcdsdk.Client , vappID string , updateExternalID bool ) error {
345
364
log := ctrl .LoggerFrom (ctx )
346
365
366
+ org , err := workloadVCDClient .VCDClient .GetOrgByName (vcdCluster .Spec .Org )
367
+ if err != nil {
368
+ return fmt .Errorf ("failed to get org by name [%s]" , vcdCluster .Spec .Org )
369
+ }
370
+ if org == nil || org .Org == nil {
371
+ return fmt .Errorf ("found nil org when getting org by name [%s]" , vcdCluster .Spec .Org )
372
+ }
347
373
capvcdRdeManager := capisdk .NewCapvcdRdeManager (workloadVCDClient , vcdCluster .Status .InfraId )
348
374
_ , capvcdSpec , capvcdMetadata , capvcdStatus , err := capvcdRdeManager .GetCAPVCDEntity (ctx , vcdCluster .Status .InfraId )
349
375
if err != nil {
350
376
return fmt .Errorf ("failed to get RDE with ID [%s] for cluster [%s]: [%v]" , vcdCluster .Status .InfraId , vcdCluster .Name , err )
351
377
}
352
378
// TODO(VCDA-3107): Should we be updating org and vdc information here.
353
379
metadataPatch := make (map [string ]interface {})
354
- org := vcdCluster . Spec . Org
355
- if org != capvcdMetadata .Org {
356
- metadataPatch ["Org" ] = org
380
+
381
+ if org . Org . Name != capvcdMetadata .Org {
382
+ metadataPatch ["Org" ] = org . Org . Name
357
383
}
358
384
359
385
vdc := vcdCluster .Spec .Ovdc
@@ -525,9 +551,19 @@ func (r *VCDClusterReconciler) reconcileRDE(ctx context.Context, cluster *cluste
525
551
}
526
552
527
553
vcdResources := rdeType.VCDProperties {
528
- Site : vcdCluster .Spec .Site ,
529
- Org : vcdCluster .Spec .Org ,
530
- Vdc : vcdCluster .Spec .Ovdc ,
554
+ Site : vcdCluster .Spec .Site ,
555
+ Org : []rdeType.Org {
556
+ rdeType.Org {
557
+ Name : org .Org .Name ,
558
+ ID : org .Org .ID ,
559
+ },
560
+ },
561
+ Ovdc : []rdeType.Ovdc {
562
+ rdeType.Ovdc {
563
+ Name : workloadVCDClient .VDC .Vdc .Name ,
564
+ ID : workloadVCDClient .VDC .Vdc .Name ,
565
+ },
566
+ },
531
567
OvdcNetwork : vcdCluster .Spec .OvdcNetwork ,
532
568
}
533
569
if ! reflect .DeepEqual (vcdResources , capvcdStatus .VcdProperties ) {
@@ -547,7 +583,7 @@ func (r *VCDClusterReconciler) reconcileRDE(ctx context.Context, cluster *cluste
547
583
}
548
584
}
549
585
550
- updatedRDE , err := capvcdRdeManager .PatchRDE (ctx , specPatch , metadataPatch , capvcdStatusPatch , vcdCluster .Status .InfraId )
586
+ updatedRDE , err := capvcdRdeManager .PatchRDE (ctx , specPatch , metadataPatch , capvcdStatusPatch , vcdCluster .Status .InfraId , vappID , updateExternalID )
551
587
if err != nil {
552
588
return fmt .Errorf ("failed to update defined entity with ID [%s] for cluster [%s]: [%v]" , vcdCluster .Status .InfraId , vcdCluster .Name , err )
553
589
}
@@ -696,7 +732,7 @@ func (r *VCDClusterReconciler) reconcileNormal(ctx context.Context, cluster *clu
696
732
return ctrl.Result {}, errors .Wrapf (err , "failed to upgrade RDE [%s]" , infraID )
697
733
}
698
734
// calling reconcileRDE here to avoid delay in updating the RDE contents
699
- if err = r .reconcileRDE (ctx , cluster , vcdCluster , workloadVCDClient ); err != nil {
735
+ if err = r .reconcileRDE (ctx , cluster , vcdCluster , workloadVCDClient , "" , false ); err != nil {
700
736
// TODO: can we recover the RDE to a proper state if RDE fails to reconcile?
701
737
log .Error (err , "failed to reconcile RDE after upgrading RDE" , "rdeID" , infraID )
702
738
return ctrl.Result {}, errors .Wrapf (err , "failed to reconcile RDE after upgrading RDE [%s]" , infraID )
@@ -844,7 +880,7 @@ func (r *VCDClusterReconciler) reconcileNormal(ctx context.Context, cluster *clu
844
880
if ! strings .HasPrefix (vcdCluster .Status .InfraId , NoRdePrefix ) {
845
881
_ , resp , _ , err := workloadVCDClient .APIClient .DefinedEntityApi .GetDefinedEntity (ctx , vcdCluster .Status .InfraId )
846
882
if err == nil && resp != nil && resp .StatusCode == http .StatusOK {
847
- if err = r .reconcileRDE (ctx , cluster , vcdCluster , workloadVCDClient ); err != nil {
883
+ if err = r .reconcileRDE (ctx , cluster , vcdCluster , workloadVCDClient , "" , false ); err != nil {
848
884
log .Error (err , "Error occurred during RDE reconciliation" ,
849
885
"InfraId" , vcdCluster .Status .InfraId )
850
886
}
@@ -891,6 +927,14 @@ func (r *VCDClusterReconciler) reconcileNormal(ctx context.Context, cluster *clu
891
927
if clusterVApp == nil || clusterVApp .VApp == nil {
892
928
return ctrl.Result {}, errors .Wrapf (err , "found nil value for VApp [%s]" , vcdCluster .Name )
893
929
}
930
+ if ! strings .HasPrefix (vcdCluster .Status .InfraId , NoRdePrefix ) {
931
+ if err := r .reconcileRDE (ctx , cluster , vcdCluster , workloadVCDClient , clusterVApp .VApp .ID , true ); err != nil {
932
+ log .Error (err , "failed to add VApp ID to RDE" , "rdeID" , infraID , "vappID" , clusterVApp .VApp .ID )
933
+ return ctrl.Result {}, errors .Wrapf (err , "failed to update RDE [%s] with VApp ID [%s]: [%v]" , vcdCluster .Status .InfraId , clusterVApp .VApp .ID , err )
934
+ }
935
+ log .Info ("successfully updated external ID of RDE with VApp ID" , "infraID" , infraID , "vAppID" , clusterVApp .VApp .ID )
936
+ }
937
+
894
938
if metadataMap != nil && len (metadataMap ) > 0 && ! vcdCluster .Status .VAppMetadataUpdated {
895
939
if err := vdcManager .AddMetadataToVApp (vcdCluster .Name , metadataMap ); err != nil {
896
940
return ctrl.Result {}, fmt .Errorf ("unable to add metadata [%s] to vApp [%s]: [%v]" , metadataMap , vcdCluster .Name , err )
0 commit comments