Skip to content

Commit d68b804

Browse files
authored
[VCDA-4355] Update VcdProperties (vmware#251)
* update schema to make org and vdcs array in vcdproperties Signed-off-by: Aniruddha Shamasundar <[email protected]> * Update schema to remove vdc name property Signed-off-by: Aniruddha Shamasundar <[email protected]> * Add tkg version Signed-off-by: Aniruddha Shamasundar <[email protected]> * image change in infrastructure-components Signed-off-by: Aniruddha Shamasundar <[email protected]> * revert image name and update examples Signed-off-by: Aniruddha Shamasundar <[email protected]> * update example for tkgversion Signed-off-by: Aniruddha Shamasundar <[email protected]> * image version change for debug Signed-off-by: Aniruddha Shamasundar <[email protected]> * External ID update fix Signed-off-by: Aniruddha Shamasundar <[email protected]> * Address comments Signed-off-by: Aniruddha Shamasundar <[email protected]> * Remove tkgVersion updates Signed-off-by: Aniruddha Shamasundar <[email protected]> * Update examples Signed-off-by: Aniruddha Shamasundar <[email protected]> Signed-off-by: Aniruddha Shamasundar <[email protected]>
1 parent 6514a4a commit d68b804

File tree

5 files changed

+118
-30
lines changed

5 files changed

+118
-30
lines changed

Diff for: controllers/vcdcluster_controller.go

+63-19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
rdeType "github.com/vmware/cluster-api-provider-cloud-director/pkg/vcdtypes/rde_type_1_1_0"
2323
"github.com/vmware/cluster-api-provider-cloud-director/release"
2424
"github.com/vmware/go-vcloud-director/v2/govcd"
25+
"github.com/vmware/go-vcloud-director/v2/types/v56"
2526
apierrors "k8s.io/apimachinery/pkg/api/errors"
2627
"k8s.io/apimachinery/pkg/runtime"
2728
"k8s.io/klog"
@@ -229,10 +230,14 @@ func validateDerivedRDEProperties(vcdCluster *infrav1.VCDCluster, infraID string
229230

230231
// TODO: Remove uncommented code when decision to only keep capi.yaml as part of RDE spec is finalized
231232
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) {
235234

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+
}
236241
kcpList, err := getAllKubeadmControlPlaneForCluster(ctx, r.Client, *cluster)
237242
if err != nil {
238243
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 *
256261
ApiVersion: capisdk.CAPVCDClusterEntityApiVersion,
257262
Metadata: rdeType.Metadata{
258263
Name: vcdCluster.Name,
259-
Org: org,
260-
Vdc: vdc,
264+
Org: vcdOrg.Name,
265+
Vdc: vdc.Name,
261266
Site: vcdCluster.Spec.Site,
262267
},
263268
Spec: rdeType.CAPVCDSpec{},
@@ -283,9 +288,19 @@ func (r *VCDClusterReconciler) constructCapvcdRDE(ctx context.Context, cluster *
283288
},
284289
ParentUID: vcdCluster.Spec.ParentUID,
285290
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+
},
289304
OvdcNetwork: vcdCluster.Spec.OvdcNetwork,
290305
},
291306
CapiStatusYaml: "",
@@ -316,7 +331,11 @@ func (r *VCDClusterReconciler) constructCapvcdRDE(ctx context.Context, cluster *
316331
func (r *VCDClusterReconciler) constructAndCreateRDEFromCluster(ctx context.Context, workloadVCDClient *vcdsdk.Client, cluster *clusterv1.Cluster, vcdCluster *infrav1.VCDCluster) (string, error) {
317332
log := ctrl.LoggerFrom(ctx)
318333

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)
320339
if err != nil {
321340
return "", fmt.Errorf("error occurred while constructing RDE payload for the cluster [%s]: [%v]", vcdCluster.Name, err)
322341
}
@@ -341,19 +360,26 @@ func (r *VCDClusterReconciler) constructAndCreateRDEFromCluster(ctx context.Cont
341360
}
342361

343362
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 {
345364
log := ctrl.LoggerFrom(ctx)
346365

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+
}
347373
capvcdRdeManager := capisdk.NewCapvcdRdeManager(workloadVCDClient, vcdCluster.Status.InfraId)
348374
_, capvcdSpec, capvcdMetadata, capvcdStatus, err := capvcdRdeManager.GetCAPVCDEntity(ctx, vcdCluster.Status.InfraId)
349375
if err != nil {
350376
return fmt.Errorf("failed to get RDE with ID [%s] for cluster [%s]: [%v]", vcdCluster.Status.InfraId, vcdCluster.Name, err)
351377
}
352378
// TODO(VCDA-3107): Should we be updating org and vdc information here.
353379
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
357383
}
358384

359385
vdc := vcdCluster.Spec.Ovdc
@@ -525,9 +551,19 @@ func (r *VCDClusterReconciler) reconcileRDE(ctx context.Context, cluster *cluste
525551
}
526552

527553
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+
},
531567
OvdcNetwork: vcdCluster.Spec.OvdcNetwork,
532568
}
533569
if !reflect.DeepEqual(vcdResources, capvcdStatus.VcdProperties) {
@@ -547,7 +583,7 @@ func (r *VCDClusterReconciler) reconcileRDE(ctx context.Context, cluster *cluste
547583
}
548584
}
549585

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)
551587
if err != nil {
552588
return fmt.Errorf("failed to update defined entity with ID [%s] for cluster [%s]: [%v]", vcdCluster.Status.InfraId, vcdCluster.Name, err)
553589
}
@@ -696,7 +732,7 @@ func (r *VCDClusterReconciler) reconcileNormal(ctx context.Context, cluster *clu
696732
return ctrl.Result{}, errors.Wrapf(err, "failed to upgrade RDE [%s]", infraID)
697733
}
698734
// 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 {
700736
// TODO: can we recover the RDE to a proper state if RDE fails to reconcile?
701737
log.Error(err, "failed to reconcile RDE after upgrading RDE", "rdeID", infraID)
702738
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
844880
if !strings.HasPrefix(vcdCluster.Status.InfraId, NoRdePrefix) {
845881
_, resp, _, err := workloadVCDClient.APIClient.DefinedEntityApi.GetDefinedEntity(ctx, vcdCluster.Status.InfraId)
846882
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 {
848884
log.Error(err, "Error occurred during RDE reconciliation",
849885
"InfraId", vcdCluster.Status.InfraId)
850886
}
@@ -891,6 +927,14 @@ func (r *VCDClusterReconciler) reconcileNormal(ctx context.Context, cluster *clu
891927
if clusterVApp == nil || clusterVApp.VApp == nil {
892928
return ctrl.Result{}, errors.Wrapf(err, "found nil value for VApp [%s]", vcdCluster.Name)
893929
}
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+
894938
if metadataMap != nil && len(metadataMap) > 0 && !vcdCluster.Status.VAppMetadataUpdated {
895939
if err := vdcManager.AddMetadataToVApp(vcdCluster.Name, metadataMap); err != nil {
896940
return ctrl.Result{}, fmt.Errorf("unable to add metadata [%s] to vApp [%s]: [%v]", metadataMap, vcdCluster.Name, err)

Diff for: examples/rde1_1_0.json

+12-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,18 @@
102102
"createdByVersion": "1.1.0",
103103
"capvcdVersion": "ABCD",
104104
"vcdProperties": {
105-
"orgName": "ABCDEFGHIJKLMNOPQRSTUVWXYZAB",
106-
"virtualDataCenterName": "ABCDEFGHIJKLMNOPQRSTUVWX",
105+
"vcdOrg": [
106+
{
107+
"name": "ABCDEFGHIJKLMNOPQRSTUVWXYZAB",
108+
"id": "274c6240-3304-11ed-a261-0242ac120002"
109+
}
110+
],
111+
"orgVdc": [
112+
{
113+
"name": "ABCDEFGHIJKLMNOPQRSTUVWXYZAB",
114+
"id": "274c6240-3304-11ed-a261-0242ac120002"
115+
}
116+
],
107117
"ovdcNetworkName": "ABCDEFGHIJKLMNOPQ",
108118
"site": "ABCDEFGHIJKLMNOPQRSTUVWX"
109119
},

Diff for: pkg/capisdk/defined_entity.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (capvcdRdeManager *CapvcdRdeManager) SetIsManagementClusterInRDE(ctx contex
136136
}
137137
capvcdStatusPatch := make(map[string]interface{})
138138
capvcdStatusPatch["UsedAsManagementCluster"] = true
139-
_, err := capvcdRdeManager.PatchRDE(ctx, nil, nil, capvcdStatusPatch, managementClusterRDEId)
139+
_, err := capvcdRdeManager.PatchRDE(ctx, nil, nil, capvcdStatusPatch, managementClusterRDEId, "", false)
140140
if err != nil {
141141
return fmt.Errorf("failed to set isManagementCluster flag for management cluster with RDE ID [%s]: [%v]",
142142
managementClusterRDEId, err)
@@ -150,7 +150,8 @@ func (capvcdRdeManager *CapvcdRdeManager) SetIsManagementClusterInRDE(ctx contex
150150
// specPatch["CapiYaml"] = updated-yaml
151151
// metadataPatch["Name"] = updated-name
152152
// capvcdStatusPatch["Version"] = updated-version
153-
func (capvcdRdeManager *CapvcdRdeManager) PatchRDE(ctx context.Context, specPatch, metadataPatch, capvcdStatusPatch map[string]interface{}, rdeID string) (rde *swagger.DefinedEntity, err error) {
153+
func (capvcdRdeManager *CapvcdRdeManager) PatchRDE(ctx context.Context, specPatch, metadataPatch,
154+
capvcdStatusPatch map[string]interface{}, rdeID string, externalID string, updateExternalID bool) (rde *swagger.DefinedEntity, err error) {
154155
defer func() {
155156
// recover from panic if panic occurs because of
156157
// 1. calling Set() on a zero value
@@ -211,6 +212,10 @@ func (capvcdRdeManager *CapvcdRdeManager) PatchRDE(ctx context.Context, specPatc
211212
return nil, fmt.Errorf("failed to patch capvcd status in the CAPVCD entity: [%v]", err)
212213
}
213214
}
215+
if updateExternalID {
216+
klog.V(4).Infof("setting externalID as [%s] in RDE [%s]", externalID, rdeID)
217+
rde.ExternalId = externalID
218+
}
214219

215220
// update the defined entity
216221
rde, resp, err = client.APIClient.DefinedEntityApi.UpdateDefinedEntity(ctx, rde, etag, rdeID, nil)

Diff for: pkg/vcdtypes/rde_type_1_1_0/types_1_1_0.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,21 @@ type Services struct {
4646
CidrBlocks []string `json:"cidrBlocks,omitempty"`
4747
}
4848

49+
type Ovdc struct {
50+
Name string `json:"name,omitempty"`
51+
ID string `json:"id,omitempty"`
52+
}
53+
54+
type Org struct {
55+
Name string `json:"name,omitempty"`
56+
ID string `json:"id,omitempty"`
57+
}
58+
4959
type VCDProperties struct {
5060
Site string `json:"site,omitempty"`
51-
Org string `json:"orgName,omitempty"`
52-
Vdc string `json:"virtualDataCenterName,omitempty"`
5361
OvdcNetwork string `json:"ovdcNetworkName,omitempty"`
62+
Ovdc []Ovdc `json:"orgVdc,omitempty"`
63+
Org []Org `json:"vcdOrg,omitempty"`
5464
}
5565

5666
type ApiEndpoints struct {

Diff for: schema/schema_1_1_0.json

+24-5
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,36 @@
175175
"vcdProperties": {
176176
"type": "object",
177177
"properties": {
178-
"orgName": {
179-
"type": "string"
180-
},
181-
"virtualDataCenterName": {
182-
"type": "string"
178+
"vcdOrg": {
179+
"type": "object",
180+
"properties": {
181+
"name": {
182+
"type": "string"
183+
},
184+
"id": {
185+
"type": "string"
186+
}
187+
}
183188
},
184189
"ovdcNetworkName": {
185190
"type": "string"
186191
},
187192
"site": {
188193
"type": "string"
194+
},
195+
"orgVdc": {
196+
"type": "array",
197+
"items": {
198+
"type": "object",
199+
"properties": {
200+
"name": {
201+
"type": "string"
202+
},
203+
"id": {
204+
"type": "string"
205+
}
206+
}
207+
}
189208
}
190209
}
191210
},

0 commit comments

Comments
 (0)