Skip to content

Commit d7653fa

Browse files
authored
Update vm-operator api version and parallelize sync volume api calls (#3604)
1 parent 0c95ee2 commit d7653fa

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/pkg/sftp v1.13.6
2424
github.com/prometheus/client_golang v1.22.0
2525
github.com/stretchr/testify v1.10.0
26-
github.com/vmware-tanzu/vm-operator/api v1.8.7-0.20250820184450-53a697d52f9c
26+
github.com/vmware-tanzu/vm-operator/api v1.9.1-0.20250923172217-bf5a74e51c65
2727
github.com/vmware-tanzu/vm-operator/external/byok v0.0.0-20250509154507-b93e51fc90fa
2828
github.com/vmware/govmomi v0.52.0-alpha.0.0.20250807230438-0eee109f1f2c
2929
go.uber.org/zap v1.27.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ github.com/thecodeteam/gofsutil v0.1.2 h1:FL87mBzZeeuDMZm8hpYLFcYylQdq6bbm8UQ1oc
287287
github.com/thecodeteam/gofsutil v0.1.2/go.mod h1:7bDOpr2aMnmdm9RTdxBEeqdOr+8RpnQhsB/VUEI3DgM=
288288
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE=
289289
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75/go.mod h1:KO6IkyS8Y3j8OdNO85qEYBsRPuteD+YciPomcXdrMnk=
290-
github.com/vmware-tanzu/vm-operator/api v1.8.7-0.20250820184450-53a697d52f9c h1:+lECoxyxQcMsxU2CJ53YvaLEwqfGduS/3yQaLLRDYDo=
291-
github.com/vmware-tanzu/vm-operator/api v1.8.7-0.20250820184450-53a697d52f9c/go.mod h1:hkc/QZCSHcosWWMPS6VWWR12WenZcNE3BaTJ/8A8sNE=
290+
github.com/vmware-tanzu/vm-operator/api v1.9.1-0.20250923172217-bf5a74e51c65 h1:oby5iJHxU5KgtajXxT8B8VxUUPH20Zh0KTYlI+18AYs=
291+
github.com/vmware-tanzu/vm-operator/api v1.9.1-0.20250923172217-bf5a74e51c65/go.mod h1:nWTPpxfe4gHuuYuFcrs86+NMxfkqPk3a3IlvI8TCWak=
292292
github.com/vmware-tanzu/vm-operator/external/byok v0.0.0-20250509154507-b93e51fc90fa h1:4MKu14YJ7J54O6QKmT4ds5EUpysWLLtQRMff73cVkmU=
293293
github.com/vmware-tanzu/vm-operator/external/byok v0.0.0-20250509154507-b93e51fc90fa/go.mod h1:8tiuyYslzjLIUmOlXZuGKQdQP2ZgWGCVhVeyptmZYnk=
294294
github.com/vmware/govmomi v0.52.0-alpha.0.0.20250807230438-0eee109f1f2c h1:1nMVFr1CBMSNLLjsfx3QPfZ5k0R1/O29QX/A2X0w3RQ=

pkg/syncer/cnsoperator/controller/virtualmachinesnapshot/virtualmachinesnapshot_controller.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
vmoperatortypes "github.com/vmware-tanzu/vm-operator/api/v1alpha5"
3030
cnstypes "github.com/vmware/govmomi/cns/types"
3131
"go.uber.org/zap"
32+
syncgroup "golang.org/x/sync/errgroup"
3233
corev1 "k8s.io/api/core/v1"
3334
apierrors "k8s.io/apimachinery/pkg/api/errors"
3435
"k8s.io/apimachinery/pkg/runtime"
@@ -292,7 +293,7 @@ func (r *ReconcileVirtualMachineSnapshot) reconcileNormal(ctx context.Context, l
292293
// if found fetch vmsnapshot and pvcs and pvs
293294
vmKey := apitypes.NamespacedName{
294295
Namespace: vmsnapshot.Namespace,
295-
Name: vmsnapshot.Spec.VMRef.Name,
296+
Name: vmsnapshot.Spec.VMName,
296297
}
297298
log.Infof("reconcileNormal: get virtulal machine %s/%s", vmKey.Namespace, vmKey.Name)
298299
virtualMachine, _, err := utils.GetVirtualMachineAllApiVersions(ctx, vmKey,
@@ -426,6 +427,7 @@ func (r *ReconcileVirtualMachineSnapshot) syncVolumesAndUpdateCNSVolumeInfo(ctx
426427
var err error
427428
cnsVolumeIds := []cnstypes.CnsVolumeId{}
428429
syncMode := []string{string(cnstypes.CnsSyncVolumeModeSPACE_USAGE)}
430+
syncgrp, sgctx := syncgroup.WithContext(ctx)
429431
for _, vmVolume := range vm.Spec.Volumes {
430432
if vmVolume.VirtualMachineVolumeSource.PersistentVolumeClaim == nil {
431433
continue
@@ -461,15 +463,10 @@ func (r *ReconcileVirtualMachineSnapshot) syncVolumesAndUpdateCNSVolumeInfo(ctx
461463
SyncMode: syncMode,
462464
},
463465
}
464-
// Trigger CNS VolumeSync API for identified volume-lds and Fetch Latest Aggregated snapshot size
465-
log.Infof("syncVolumesAndUpdateCNSVolumeInfo: Trigger CNS VolumeSync API for volume %s",
466-
cnsVolId)
467-
syncVolumeFaultType, err := r.volumeManager.SyncVolume(ctx, syncVolumeSpecs)
468-
if err != nil {
469-
log.Errorf("syncVolumesAndUpdateCNSVolumeInfo: error while sync volume %s "+
470-
"cnsfault %s. error: %v", cnsVolId, syncVolumeFaultType, err)
471-
return err
472-
}
466+
// parallelize sync volume api calls
467+
syncgrp.Go(func() error {
468+
return r.invokeSyncVolume(sgctx, log, syncVolumeSpecs)
469+
})
473470
}
474471
} else {
475472
err = fmt.Errorf("could not find the PV associated with PVC %s/%s",
@@ -478,10 +475,19 @@ func (r *ReconcileVirtualMachineSnapshot) syncVolumesAndUpdateCNSVolumeInfo(ctx
478475
return err
479476
}
480477
}
478+
// if syncvolume api is not invoked for any volume, no need to update CNSVolumeInfos
481479
if len(cnsVolumeIds) == 0 {
482-
log.Infof("syncVolumesAndUpdateCNSVolumeInfo: no volumes found to sync, skipping volume sync")
480+
log.Info("syncVolumesAndUpdateCNSVolumeInfo: no volumes found to sync, skipped volume sync")
483481
return nil
484482
}
483+
log.Info("syncVolumesAndUpdateCNSVolumeInfo: wait for syncvolume operation to be completed")
484+
if err := syncgrp.Wait(); err != nil {
485+
log.Errorf("syncVolumesAndUpdateCNSVolumeInfo: error while sync volume "+
486+
"error: %v", err)
487+
return err
488+
}
489+
log.Info("syncVolumesAndUpdateCNSVolumeInfo: volumes are synced successfully. " +
490+
"will update related CNSVolumeInfos")
485491
// fetch updated cns volumes
486492
queryFilter := cnstypes.CnsQueryFilter{
487493
VolumeIds: cnsVolumeIds,
@@ -500,7 +506,7 @@ func (r *ReconcileVirtualMachineSnapshot) syncVolumesAndUpdateCNSVolumeInfo(ctx
500506

501507
// Update CNSVolumeInfo with latest aggregated Size and Update SPU used value.
502508
patch, err := common.GetCNSVolumeInfoPatch(ctx, val.AggregatedSnapshotCapacityInMb,
503-
cnsvolume.VolumeId.Id) // TODO: UDPATE to value returned
509+
cnsvolume.VolumeId.Id)
504510
if err != nil {
505511
log.Errorf("syncVolumesAndUpdateCNSVolumeInfo: failed to get cnsvolumeinfo patch for "+
506512
"volume %s, error: %v", cnsvolume.VolumeId.Id, err)
@@ -529,3 +535,24 @@ func (r *ReconcileVirtualMachineSnapshot) syncVolumesAndUpdateCNSVolumeInfo(ctx
529535
}
530536
return nil
531537
}
538+
539+
func (r *ReconcileVirtualMachineSnapshot) invokeSyncVolume(ctx context.Context, log *zap.SugaredLogger,
540+
syncVolumeSpecs []cnstypes.CnsSyncVolumeSpec) error {
541+
select {
542+
case <-ctx.Done():
543+
log.Infof("invokeSyncVolume: Sync Volume Operation cancelled for volume %s",
544+
syncVolumeSpecs[0])
545+
return ctx.Err()
546+
default:
547+
// Trigger CNS VolumeSync API for identified volume-lds and Fetch Latest Aggregated snapshot size
548+
log.Infof("invokeSyncVolume: Trigger CNS SyncVolume API for volume %s",
549+
syncVolumeSpecs[0])
550+
syncVolumeFaultType, err := r.volumeManager.SyncVolume(ctx, syncVolumeSpecs)
551+
if err != nil {
552+
log.Errorf("invokeSyncVolume: error while sync volume %s "+
553+
"cnsfault %s. error: %v", syncVolumeSpecs[0], syncVolumeFaultType, err)
554+
return err
555+
}
556+
return nil
557+
}
558+
}

0 commit comments

Comments
 (0)