Skip to content

Commit 948c0f8

Browse files
committed
UpdateCnsRegisterApi for multiwriter
1 parent fca6b7c commit 948c0f8

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

pkg/apis/cnsoperator/cnsregistervolume/v1alpha1/cnsregistervolume_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ type CnsRegisterVolumeSpec struct {
5454
// This is for a 34a9c05d-5f03-e254-e692-02004479cb91/vm2_1.vmdk
5555
// file under datacenter "Datacenter-1" and datastore "vsanDatastore".
5656
DiskURLPath string `json:"diskURLPath,omitempty"`
57+
58+
// VolumeMode can either be Block (for raw block volume) or
59+
// Filesystem. Default values will be assumed if it is missing:
60+
// With RWO accessmode, default is Filesystem.
61+
// With RWX accessmode, default is Block.
62+
VolumeMode v1.PersistentVolumeMode `json:"volumeMode,omitempty"`
63+
64+
// FsType represents the filesystem type.
65+
// Default values will be assumed if it is missing:
66+
// With RWO, it will be ext4.
67+
// With RWX with volumeMode as Filsystem, it will be ntf4.
68+
FsType string `json:"fsType,omitempty"`
5769
}
5870

5971
// CnsRegisterVolumeStatus defines the observed state of CnsRegisterVolume

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ func (r *ReconcileCnsRegisterVolume) Reconcile(ctx context.Context,
247247
// Currently file volume registration is not supported.
248248
ok := isBlockVolumeRegisterRequest(ctx, instance)
249249
if !ok {
250-
msg := fmt.Sprintf("AccessMode: %s is not supported", instance.Spec.AccessMode)
250+
msg := fmt.Sprintf("AccessMode: %s is not supported with volume %s is not supported",
251+
instance.Spec.AccessMode, instance.Spec.VolumeMode)
251252
log.Error(msg)
252253
setInstanceError(ctx, r, instance, msg)
253254
return reconcile.Result{RequeueAfter: timeout}, nil
@@ -482,7 +483,7 @@ func (r *ReconcileCnsRegisterVolume) Reconcile(ctx context.Context,
482483
Name: instance.Spec.PvcName,
483484
}
484485
pvSpec := getPersistentVolumeSpec(pvName, volumeID, capacityInMb,
485-
accessMode, storageClassName, claimRef)
486+
accessMode, instance.Spec.VolumeMode, storageClassName, claimRef, instance.Spec.FsType)
486487
pvSpec.Spec.NodeAffinity = pvNodeAffinity
487488
log.Debugf("PV spec is: %+v", pvSpec)
488489
pv, err = k8sclient.CoreV1().PersistentVolumes().Create(ctx, pvSpec, metav1.CreateOptions{})
@@ -510,7 +511,7 @@ func (r *ReconcileCnsRegisterVolume) Reconcile(ctx context.Context,
510511
// Create PVC mapping to above created PV.
511512
log.Infof("Creating PVC: %s", instance.Spec.PvcName)
512513
pvcSpec, err := getPersistentVolumeClaimSpec(ctx, instance.Spec.PvcName, instance.Namespace, capacityInMb,
513-
storageClassName, accessMode, pvName, datastoreAccessibleTopology)
514+
storageClassName, accessMode, instance.Spec.VolumeMode, pvName, datastoreAccessibleTopology)
514515
if err != nil {
515516
msg := fmt.Sprintf("Failed to create spec for PVC: %q. Error: %v", instance.Spec.PvcName, err)
516517
log.Errorf(msg)
@@ -701,6 +702,16 @@ func validateCnsRegisterVolumeSpec(ctx context.Context, instance *cnsregistervol
701702
} else if instance.Spec.DiskURLPath != "" && instance.Spec.AccessMode != "" &&
702703
instance.Spec.AccessMode != v1.ReadWriteOnce {
703704
msg = fmt.Sprintf("DiskURLPath cannot be used with accessMode: %q", instance.Spec.AccessMode)
705+
} else if instance.Spec.AccessMode == v1.ReadWriteMany && instance.Spec.VolumeMode != "" && instance.Spec.VolumeMode != v1.PersistentVolumeBlock {
706+
return errors.New(fmt.Sprintf("AccessMode %q is not allowed with VolumeMode %q", instance.Spec.AccessMode, instance.Spec.VolumeMode))
707+
}
708+
if instance.Spec.VolumeID != "" && instance.Spec.AccessMode == "" {
709+
return errors.New(fmt.Sprintf("AccessMode cannot be empty when VolumeID is provided"))
710+
}
711+
if instance.Spec.AccessMode == v1.ReadWriteMany {
712+
if instance.Spec.VolumeMode != "" || instance.Spec.VolumeMode != v1.PersistentVolumeFilesystem {
713+
return errors.New(fmt.Sprintf("AccessMode %q is not allowed with VolumeMode %q", instance.Spec.AccessMode, instance.Spec.VolumeMode))
714+
}
704715
}
705716
if instance.Spec.VolumeID != "" {
706717
pvName, found := commonco.ContainerOrchestratorUtility.GetPVNameFromCSIVolumeID(instance.Spec.VolumeID)
@@ -721,7 +732,7 @@ func validateCnsRegisterVolumeSpec(ctx context.Context, instance *cnsregistervol
721732
// via CnsRegisterVolume instance.
722733
func isBlockVolumeRegisterRequest(ctx context.Context, instance *cnsregistervolumev1alpha1.CnsRegisterVolume) bool {
723734
if instance.Spec.AccessMode != "" {
724-
if instance.Spec.AccessMode == v1.ReadWriteOnce {
735+
if instance.Spec.AccessMode == v1.ReadWriteOnce || (instance.Spec.AccessMode == v1.ReadWriteMany && instance.Spec.VolumeMode == v1.PersistentVolumeBlock) {
725736
return true
726737
}
727738
} else {

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func constructCreateSpecForInstance(r *ReconcileCnsRegisterVolume,
138138
BackingDiskUrlPath: instance.Spec.DiskURLPath,
139139
}
140140
}
141-
if instance.Spec.AccessMode == v1.ReadWriteOnce || instance.Spec.AccessMode == "" {
141+
if instance.Spec.AccessMode == v1.ReadWriteOnce || instance.Spec.AccessMode == "" || (instance.Spec.AccessMode == v1.ReadWriteMany && instance.Spec.VolumeMode == v1.PersistentVolumeBlock) {
142142
createSpec.VolumeType = common.BlockVolumeType
143143
} else {
144144
createSpec.VolumeType = common.FileVolumeType
@@ -241,8 +241,20 @@ func getK8sStorageClassNameWithImmediateBindingModeForPolicy(ctx context.Context
241241

242242
// getPersistentVolumeSpec to create PV volume spec for the given input params.
243243
func getPersistentVolumeSpec(volumeName string, volumeID string, capacity int64,
244-
accessMode v1.PersistentVolumeAccessMode, scName string, claimRef *v1.ObjectReference) *v1.PersistentVolume {
244+
accessMode v1.PersistentVolumeAccessMode, volumeMode v1.PersistentVolumeMode, scName string, claimRef *v1.ObjectReference, fsType string) *v1.PersistentVolume {
245245
capacityInMb := strconv.FormatInt(capacity, 10) + "Mi"
246+
if fsType == "" {
247+
if accessMode == v1.ReadWriteOnce {
248+
fsType = "ext4"
249+
}
250+
}
251+
if volumeMode == "" {
252+
if accessMode == v1.ReadWriteOnce {
253+
volumeMode = v1.PersistentVolumeFilesystem
254+
} else {
255+
volumeMode = v1.PersistentVolumeBlock
256+
}
257+
}
246258
pv := &v1.PersistentVolume{
247259
TypeMeta: metav1.TypeMeta{},
248260
ObjectMeta: metav1.ObjectMeta{
@@ -258,7 +270,7 @@ func getPersistentVolumeSpec(volumeName string, volumeID string, capacity int64,
258270
Driver: cnsoperatortypes.VSphereCSIDriverName,
259271
VolumeHandle: volumeID,
260272
ReadOnly: false,
261-
FSType: "ext4",
273+
FSType: fsType,
262274
},
263275
},
264276
AccessModes: []v1.PersistentVolumeAccessMode{
@@ -269,6 +281,9 @@ func getPersistentVolumeSpec(volumeName string, volumeID string, capacity int64,
269281
},
270282
Status: v1.PersistentVolumeStatus{},
271283
}
284+
285+
*pv.Spec.VolumeMode = volumeMode
286+
272287
annotations := make(map[string]string)
273288
annotations["pv.kubernetes.io/provisioned-by"] = cnsoperatortypes.VSphereCSIDriverName
274289
pv.Annotations = annotations
@@ -278,7 +293,7 @@ func getPersistentVolumeSpec(volumeName string, volumeID string, capacity int64,
278293
// getPersistentVolumeClaimSpec return the PersistentVolumeClaim spec with
279294
// specified storage class.
280295
func getPersistentVolumeClaimSpec(ctx context.Context, name string, namespace string, capacity int64,
281-
storageClassName string, accessMode v1.PersistentVolumeAccessMode, pvName string,
296+
storageClassName string, accessMode v1.PersistentVolumeAccessMode, volumeMode v1.PersistentVolumeMode, pvName string,
282297
datastoreAccessibleTopology []map[string]string) (*v1.PersistentVolumeClaim, error) {
283298

284299
log := logger.GetLogger(ctx)
@@ -318,6 +333,16 @@ func getPersistentVolumeClaimSpec(ctx context.Context, name string, namespace st
318333
VolumeName: pvName,
319334
},
320335
}
336+
337+
if volumeMode == "" {
338+
if accessMode == v1.ReadWriteOnce {
339+
volumeMode = v1.PersistentVolumeFilesystem
340+
} else {
341+
volumeMode = v1.PersistentVolumeBlock
342+
}
343+
}
344+
*claim.Spec.VolumeMode = volumeMode
345+
321346
return claim, nil
322347
}
323348

0 commit comments

Comments
 (0)