Skip to content

Commit f431590

Browse files
authored
Change batch attach validation to not allow DiskMode and SharingMode for RWX volumes (#3550)
1 parent 500fab4 commit f431590

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

pkg/common/cns-lib/volume/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3585,7 +3585,7 @@ func (m *defaultManager) BatchAttachVolumes(ctx context.Context,
35853585
var errMsg string
35863586
if len(volumesThatFailedToAttach) != 0 {
35873587
errMsg = strings.Join(volumesThatFailedToAttach, ",")
3588-
overallError = errors.New(errMsg)
3588+
overallError = errors.New("failed to attach volumes: " + errMsg)
35893589
return batchAttachResult, csifault.CSIBatchAttachFault, overallError
35903590
}
35913591
log.Infof("BatchAttach: all volumes attached successfully with opID %s", taskInfo.ActivationId)

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,14 @@ func constructBatchAttachRequest(ctx context.Context,
362362
UnitNumber: volume.PersistentVolumeClaim.UnitNumber,
363363
}
364364
// Validate each attach request before proceeding.
365-
err := validateBatchAttachRequest(ctx, currentBatchAttachRequest, instance.Namespace, pvcName)
365+
updateBatchAttachRequest, err := validateBatchAttachRequest(ctx,
366+
currentBatchAttachRequest, instance.Namespace, pvcName)
366367
if err != nil {
367368
log.Errorf("failed to validate attach request for PVC %s in namespace %s. Err: %s",
368369
pvcName, instance.Namespace, err)
369370
return pvcsInSpec, volumeIdsInSpec, batchAttachRequest, err
370371
}
371-
batchAttachRequest = append(batchAttachRequest, currentBatchAttachRequest)
372+
batchAttachRequest = append(batchAttachRequest, updateBatchAttachRequest)
372373
}
373374
return pvcsInSpec, volumeIdsInSpec, batchAttachRequest, nil
374375
}
@@ -378,7 +379,7 @@ func constructBatchAttachRequest(ctx context.Context,
378379
// RWX accessMode -> ControllerKey and UnitNumber are required, DiskMode must be IndependentPersistent.
379380
// RWO accessMode -> DiskMode must not be IndependentPersistent, SharingMode must not be SharingMultiWriter.
380381
func validateBatchAttachRequest(ctx context.Context,
381-
batchAttachRequest volumes.BatchAttachRequest, namespace string, pvcName string) error {
382+
batchAttachRequest volumes.BatchAttachRequest, namespace string, pvcName string) (volumes.BatchAttachRequest, error) {
382383
log := logger.GetLogger(ctx)
383384

384385
log.Infof("Verifying if PVC %s has correct input parameters for batch attach", pvcName)
@@ -387,46 +388,53 @@ func validateBatchAttachRequest(ctx context.Context,
387388
pvc, err := commonco.ContainerOrchestratorUtility.GetPvcObjectByName(ctx, pvcName, namespace)
388389
if err != nil {
389390
log.Errorf("failed to get PVC object for PVC %s. Err: %s", pvcName, err)
390-
return err
391+
return batchAttachRequest, err
391392
}
392393

393394
for _, accessMode := range pvc.Spec.AccessModes {
394395
// RWX accessMode -> ControllerKey and UnitNumber are required, DiskMode must be IndependentPersistent.
395396
if accessMode == v1.ReadWriteMany || accessMode == v1.ReadOnlyMany {
397+
if batchAttachRequest.DiskMode == "" {
398+
batchAttachRequest.DiskMode = string(v1alpha1.IndependentPersistent)
399+
}
396400
if batchAttachRequest.DiskMode != string(v1alpha1.IndependentPersistent) {
397-
return fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
401+
return batchAttachRequest, fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
398402
"DiskMode cannot be %s", pvcName, namespace, accessMode, batchAttachRequest.DiskMode)
399403
}
400404
if batchAttachRequest.ControllerKey == "" {
401-
return fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
405+
return batchAttachRequest, fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
402406
"ControllerKey cannot be empty", pvcName, namespace, accessMode)
403407
}
404408
if batchAttachRequest.UnitNumber == "" {
405-
return fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
409+
return batchAttachRequest, fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
406410
" UnitNumber cannot be empty", pvcName, namespace, accessMode)
407411
}
412+
if batchAttachRequest.SharingMode == "" {
413+
return batchAttachRequest, fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
414+
" SharingMode cannot be empty", pvcName, namespace, accessMode)
415+
}
408416
}
409417

410-
// RWO accessMode -> DiskMode must NOT be IndependentPersistent, SharingMode must not be SharingMultiWriter.
418+
// RWO accessMode -> DiskMode, SharingMode and ControllerNumber and Unit Number must be empty.
411419
if accessMode == v1.ReadWriteOnce {
412-
if batchAttachRequest.DiskMode != string(v1alpha1.Persistent) && batchAttachRequest.DiskMode != "" {
413-
return fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
420+
if batchAttachRequest.DiskMode != "" {
421+
return batchAttachRequest, fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
414422
"DiskMode cannot be %s", pvcName, namespace, accessMode, batchAttachRequest.DiskMode)
415423
}
416-
if batchAttachRequest.SharingMode != string(v1alpha1.SharingNone) && batchAttachRequest.SharingMode != "" {
417-
return fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
424+
if batchAttachRequest.SharingMode != "" {
425+
return batchAttachRequest, fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
418426
"SharingMode cannot be %s", pvcName, namespace, accessMode, batchAttachRequest.SharingMode)
419427
}
420-
if batchAttachRequest.ControllerKey != "" && batchAttachRequest.UnitNumber != "" {
421-
return fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
428+
if batchAttachRequest.ControllerKey != "" || batchAttachRequest.UnitNumber != "" {
429+
return batchAttachRequest, fmt.Errorf("incorrect input for PVC %s in namespace %s with accessMode %s. "+
422430
"ControllerNumber and UnitNumber must not be provided with RWO accessMode", pvcName, namespace, accessMode)
423431
}
424432
}
425433
}
426434

427435
log.Infof("Validated request for PVC %s in namespace %s", pvcName, namespace)
428436

429-
return nil
437+
return batchAttachRequest, nil
430438
}
431439

432440
// getVmObject find the VM object on vCenter.

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func TestValidateBatchAttachRequestWithRwoPvc(t *testing.T) {
376376
}
377377

378378
commonco.ContainerOrchestratorUtility = &unittestcommon.FakeK8SOrchestrator{}
379-
err := validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-1")
379+
_, err := validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-1")
380380
expectedEr := fmt.Errorf("incorrect input for PVC pvc-1 in namespace test-ns with accessMode ReadWriteOnce. " +
381381
"DiskMode cannot be IndependentPersistent")
382382
assert.EqualError(t, expectedEr, err.Error())
@@ -386,10 +386,19 @@ func TestValidateBatchAttachRequestWithRwoPvc(t *testing.T) {
386386
}
387387

388388
commonco.ContainerOrchestratorUtility = &unittestcommon.FakeK8SOrchestrator{}
389-
err = validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-1")
389+
_, err = validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-1")
390390
expectedEr = fmt.Errorf("incorrect input for PVC pvc-1 in namespace test-ns with accessMode ReadWriteOnce. " +
391391
"SharingMode cannot be sharingMultiWriter")
392392
assert.EqualError(t, expectedEr, err.Error())
393+
394+
batchAttachRequest = volumes.BatchAttachRequest{
395+
SharingMode: "",
396+
}
397+
398+
commonco.ContainerOrchestratorUtility = &unittestcommon.FakeK8SOrchestrator{}
399+
_, err = validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-1")
400+
assert.NoError(t, err)
401+
393402
})
394403
}
395404

@@ -404,7 +413,7 @@ func TestValidateBatchAttachRequestWithRwxPvc(t *testing.T) {
404413
}
405414

406415
commonco.ContainerOrchestratorUtility = &unittestcommon.FakeK8SOrchestrator{}
407-
err := validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-rwx")
416+
_, err := validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-rwx")
408417
expectedErr := fmt.Errorf("incorrect input for PVC pvc-rwx in namespace test-ns with accessMode ReadWriteMany. " +
409418
"DiskMode cannot be persistent")
410419
assert.EqualError(t, expectedErr, err.Error())
@@ -416,10 +425,22 @@ func TestValidateBatchAttachRequestWithRwxPvc(t *testing.T) {
416425
}
417426

418427
commonco.ContainerOrchestratorUtility = &unittestcommon.FakeK8SOrchestrator{}
419-
err = validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-rwx")
428+
_, err = validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-rwx")
420429
expectedErr = fmt.Errorf("incorrect input for PVC pvc-rwx in namespace test-ns with accessMode ReadWriteMany. " +
421430
"ControllerKey cannot be empty")
422431
assert.EqualError(t, expectedErr, err.Error())
432+
433+
batchAttachRequest = volumes.BatchAttachRequest{
434+
ControllerKey: "1001",
435+
UnitNumber: "12",
436+
DiskMode: "",
437+
SharingMode: "None",
438+
}
439+
440+
commonco.ContainerOrchestratorUtility = &unittestcommon.FakeK8SOrchestrator{}
441+
attacheReq, err := validateBatchAttachRequest(context.TODO(), batchAttachRequest, testNamespace, "pvc-rwx")
442+
assert.NoError(t, err)
443+
assert.Equal(t, "independent_persistent", attacheReq.DiskMode)
423444
})
424445
}
425446

0 commit comments

Comments
 (0)