Skip to content

Commit aae08b0

Browse files
committed
Only attach the volumes which need to be attached
1 parent 3a9e5c3 commit aae08b0

File tree

4 files changed

+206
-89
lines changed

4 files changed

+206
-89
lines changed

pkg/common/unittestcommon/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sync"
2323

2424
cnstypes "github.com/vmware/govmomi/cns/types"
25+
2526
"github.com/vmware/govmomi/object"
2627
"github.com/vmware/govmomi/vim25/types"
2728
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/migration"
@@ -212,6 +213,7 @@ func (m *MockVolumeManager) MonitorCreateVolumeTask(ctx context.Context,
212213
func (m *MockVolumeManager) BatchAttachVolumes(ctx context.Context,
213214
vm *vsphere.VirtualMachine,
214215
batchAttachRequest []cnsvolume.BatchAttachRequest) ([]cnsvolume.BatchAttachResult, string, error) {
216+
215217
for _, request := range batchAttachRequest {
216218
if request.VolumeID == "fail-attach" {
217219
return []cnsvolume.BatchAttachResult{}, "", fmt.Errorf("failed to attach volume")

pkg/syncer/cnsoperator/controller/cnsnodevmbatchattachment/cnsnodevmbatchattachment_controller.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ func (r *Reconciler) Reconcile(ctx context.Context,
264264
}
265265

266266
volumesToDetach := make(map[string]string)
267+
volumesToAttach := make(map[string]string)
267268
if vm == nil {
268269
// If VM is nil, it means it is deleted from the vCenter.
269270
if instance.DeletionTimestamp == nil {
@@ -278,7 +279,7 @@ func (r *Reconciler) Reconcile(ctx context.Context,
278279
request.NamespacedName)
279280
} else {
280281
// If VM was found on vCenter, find the volumes to be detached from it.
281-
volumesToDetach, err = getVolumesToDetach(batchAttachCtx, instance, vm, r.client, k8sClient)
282+
volumesToAttach, volumesToDetach, err = getVolumesToAttachAndDetach(batchAttachCtx, instance, vm, r.client, k8sClient)
282283
if err != nil {
283284
log.Errorf("failed to find volumes to detach for instance %s. Err: %s",
284285
request.NamespacedName.String(), err)
@@ -335,7 +336,8 @@ func (r *Reconciler) Reconcile(ctx context.Context,
335336
}
336337

337338
// Call reconcile when deletion timestamp is not set on the instance.
338-
err := r.reconcileInstanceWithoutDeletionTimestamp(batchAttachCtx, k8sClient, instance, volumesToDetach, vm)
339+
err := r.reconcileInstanceWithoutDeletionTimestamp(batchAttachCtx, k8sClient, instance, volumesToDetach,
340+
volumesToAttach, vm)
339341
if err != nil {
340342
log.Errorf("failed to reconile instance %s. Err: %s", request.NamespacedName.String(), err)
341343
return r.completeReconciliationWithError(batchAttachCtx, instance, request.NamespacedName, timeout, err)
@@ -385,6 +387,7 @@ func (r *Reconciler) reconcileInstanceWithoutDeletionTimestamp(ctx context.Conte
385387
k8sClient kubernetes.Interface,
386388
instance *v1alpha1.CnsNodeVMBatchAttachment,
387389
volumesToDetach map[string]string,
390+
volumesToAttach map[string]string,
388391
vm *cnsvsphere.VirtualMachine) error {
389392
log := logger.GetLogger(ctx)
390393

@@ -400,7 +403,7 @@ func (r *Reconciler) reconcileInstanceWithoutDeletionTimestamp(ctx context.Conte
400403
}
401404

402405
// Call batch attach for volumes.
403-
attachErr := r.processBatchAttach(ctx, k8sClient, vm, instance)
406+
attachErr := r.processBatchAttach(ctx, k8sClient, vm, instance, volumesToAttach)
404407
if attachErr != nil {
405408
log.Errorf("failed to attach all volumes. Err: %+v", attachErr)
406409
}
@@ -497,16 +500,18 @@ func removeFinalizerAndStatusEntry(ctx context.Context, client client.Client, k8
497500
// and then calls CNS batch attach for them.
498501
func (r *Reconciler) processBatchAttach(ctx context.Context, k8sClient kubernetes.Interface,
499502
vm *cnsvsphere.VirtualMachine,
500-
instance *v1alpha1.CnsNodeVMBatchAttachment) error {
503+
instance *v1alpha1.CnsNodeVMBatchAttachment,
504+
volumesToAttach map[string]string) error {
501505
log := logger.GetLogger(ctx)
502506

503-
if len(instance.Spec.Volumes) == 0 {
507+
if len(instance.Spec.Volumes) == 0 || len(volumesToAttach) == 0 {
504508
log.Infof("No volumes to attach to VM %q", instance.Spec.InstanceUUID)
505509
return nil
506510
}
507511

508512
// Construct batch attach request
509-
pvcsInSpec, volumeIdsInSpec, batchAttachRequest, err := constructBatchAttachRequest(ctx, instance)
513+
pvcsInAttachList, volumeIdsInAttachList, batchAttachRequest, err := constructBatchAttachRequest(ctx,
514+
volumesToAttach, instance)
510515
if err != nil {
511516
log.Errorf("failed to construct batch attach request. Err: %s", err)
512517
return err
@@ -522,12 +527,12 @@ func (r *Reconciler) processBatchAttach(ctx context.Context, k8sClient kubernete
522527

523528
// Update instance based on the result of BatchAttach
524529
for _, result := range batchAttachResult {
525-
pvcName, ok := volumeIdsInSpec[result.VolumeID]
530+
pvcName, ok := volumeIdsInAttachList[result.VolumeID]
526531
if !ok {
527532
log.Errorf("failed to get pvcName for volumeID %s", result.VolumeID)
528533
return fmt.Errorf("failed to get pvcName for volumeID %s", result.VolumeID)
529534
}
530-
volumeName, ok := pvcsInSpec[pvcName]
535+
volumeName, ok := pvcsInAttachList[pvcName]
531536
if !ok {
532537
log.Errorf("failed to get volumeName for pvc %s", pvcName)
533538
return fmt.Errorf("failed to get volumeName for pvc %s", pvcName)

0 commit comments

Comments
 (0)