Skip to content

Commit c16a940

Browse files
authored
add validation for CNSRegisterVolume to disallow creating multiple PV with same volume ID (#3190)
1 parent 417b852 commit c16a940

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pkg/syncer/cnsoperator/controller/cnsregistervolume/cnsregistervolume_controller.go

+26
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@ func (r *ReconcileCnsRegisterVolume) Reconcile(ctx context.Context,
278278
return reconcile.Result{RequeueAfter: timeout}, nil
279279
}
280280

281+
if instance.Spec.DiskURLPath != "" {
282+
// if CNS Register Volume Instance is created with diskURLPath,
283+
// confirm using CNS Volume ID, if another PV is already present with same volume ID
284+
// If yes then fail.
285+
pvName, found := commonco.ContainerOrchestratorUtility.GetPVNameFromCSIVolumeID(volInfo.VolumeID.Id)
286+
if found {
287+
if pvName != staticPvNamePrefix+volInfo.VolumeID.Id {
288+
msg := fmt.Sprintf("PV: %q with the volume ID: %q for volume path: %q"+
289+
"is already present. Can not create multiple PV with same disk.", pvName, volInfo.VolumeID.Id,
290+
instance.Spec.DiskURLPath)
291+
log.Errorf(msg)
292+
setInstanceError(ctx, r, instance, msg)
293+
return reconcile.Result{RequeueAfter: timeout}, nil
294+
}
295+
}
296+
}
297+
281298
volumeID = volInfo.VolumeID.Id
282299
log.Infof("Created CNS volume with volumeID: %s", volumeID)
283300

@@ -685,6 +702,15 @@ func validateCnsRegisterVolumeSpec(ctx context.Context, instance *cnsregistervol
685702
instance.Spec.AccessMode != v1.ReadWriteOnce {
686703
msg = fmt.Sprintf("DiskURLPath cannot be used with accessMode: %q", instance.Spec.AccessMode)
687704
}
705+
if instance.Spec.VolumeID != "" {
706+
pvName, found := commonco.ContainerOrchestratorUtility.GetPVNameFromCSIVolumeID(instance.Spec.VolumeID)
707+
if found {
708+
if pvName != staticPvNamePrefix+instance.Spec.VolumeID {
709+
msg = fmt.Sprintf("PV: %q with the volume ID: %q "+
710+
"is already present. Can not create multiple PV with same volume Id.", pvName, instance.Spec.VolumeID)
711+
}
712+
}
713+
}
688714
if msg != "" {
689715
return errors.New(msg)
690716
}

0 commit comments

Comments
 (0)