Skip to content

Commit 6340850

Browse files
authored
For shared block volume, return true in IsFileVolume method (#3451)
1 parent 57fd5e8 commit 6340850

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

pkg/csi/service/common/commonco/k8sorchestrator/k8sorchestrator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,15 +974,15 @@ func pvcAdded(obj interface{}) {}
974974
// This ensures that all existing PVs in the cluster are added to the map, even
975975
// across container restarts.
976976
func pvAdded(obj interface{}) {
977-
_, log := logger.GetNewContextWithLogger()
977+
ctx, log := logger.GetNewContextWithLogger()
978978
pv, ok := obj.(*v1.PersistentVolume)
979979
if pv == nil || !ok {
980980
log.Warnf("pvAdded: unrecognized object %+v", obj)
981981
return
982982
}
983983

984984
if pv.Spec.CSI != nil && pv.Spec.CSI.Driver == csitypes.Name {
985-
if !isFileVolume(pv) && pv.Spec.ClaimRef != nil && pv.Status.Phase == v1.VolumeBound {
985+
if !isFileVolume(ctx, pv) && pv.Spec.ClaimRef != nil && pv.Status.Phase == v1.VolumeBound {
986986
// We should not be caching file volumes to the map.
987987
// Add volume handle to PVC mapping.
988988
objKey := pv.Spec.CSI.VolumeHandle
@@ -1010,7 +1010,7 @@ func pvAdded(obj interface{}) {
10101010

10111011
// pvUpdated updates the volumeIDToPvcMap and pvcToVolumeIDMap when a PV goes to Bound phase.
10121012
func pvUpdated(oldObj, newObj interface{}) {
1013-
_, log := logger.GetNewContextWithLogger()
1013+
ctx, log := logger.GetNewContextWithLogger()
10141014
// Get old and new PV objects.
10151015
oldPv, ok := oldObj.(*v1.PersistentVolume)
10161016
if oldPv == nil || !ok {
@@ -1028,7 +1028,7 @@ func pvUpdated(oldObj, newObj interface{}) {
10281028
if oldPv.Status.Phase != v1.VolumeBound && newPv.Status.Phase == v1.VolumeBound {
10291029
if newPv.Spec.CSI != nil && newPv.Spec.CSI.Driver == csitypes.Name &&
10301030
newPv.Spec.ClaimRef != nil {
1031-
if !isFileVolume(newPv) {
1031+
if !isFileVolume(ctx, newPv) {
10321032

10331033
log.Debugf("pvUpdated: PV %s went to Bound phase", newPv.Name)
10341034
// Add volume handle to PVC mapping.

pkg/csi/service/common/commonco/k8sorchestrator/k8sorchestrator_helper.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,17 @@ func (c *K8sOrchestrator) updatePVCAnnotations(ctx context.Context,
106106

107107
// isFileVolume checks if the Persistent Volume has ReadWriteMany or
108108
// ReadOnlyMany support.
109-
func isFileVolume(pv *v1.PersistentVolume) bool {
109+
func isFileVolume(ctx context.Context, pv *v1.PersistentVolume) bool {
110110
if len(pv.Spec.AccessModes) == 0 {
111111
return false
112112
}
113+
114+
if k8sOrchestratorInstance.IsFSSEnabled(ctx, common.SharedDiskFss) &&
115+
*pv.Spec.VolumeMode == v1.PersistentVolumeBlock {
116+
// If volumeMode is block, then volume is Block volume.
117+
return false
118+
}
119+
113120
for _, accessMode := range pv.Spec.AccessModes {
114121
if accessMode == v1.ReadWriteMany || accessMode == v1.ReadOnlyMany {
115122
return true

pkg/csi/service/common/commonco/k8sorchestrator/k8sorchestrator_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525

2626
cnstypes "github.com/vmware/govmomi/cns/types"
27+
v1 "k8s.io/api/core/v1"
2728

2829
cnsconfig "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/config"
2930
)
@@ -260,3 +261,61 @@ func TestGetNodesForVolumes(t *testing.T) {
260261
t.Errorf("Expected node names %v but got %v", expectedNodeNames, nodeNames)
261262
}
262263
}
264+
265+
func TestIsFileVolume(t *testing.T) {
266+
ctx := context.Background()
267+
268+
tests := []struct {
269+
name string
270+
pv *v1.PersistentVolume
271+
want bool
272+
}{
273+
{
274+
name: "No AccessModes",
275+
pv: &v1.PersistentVolume{
276+
Spec: v1.PersistentVolumeSpec{
277+
AccessModes: []v1.PersistentVolumeAccessMode{},
278+
},
279+
},
280+
want: false,
281+
},
282+
{
283+
name: "AccessMode ReadWriteMany",
284+
pv: &v1.PersistentVolume{
285+
Spec: v1.PersistentVolumeSpec{
286+
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteMany},
287+
},
288+
},
289+
want: true,
290+
},
291+
{
292+
name: "AccessMode ReadOnlyMany",
293+
pv: &v1.PersistentVolume{
294+
Spec: v1.PersistentVolumeSpec{
295+
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadOnlyMany},
296+
},
297+
},
298+
want: true,
299+
},
300+
{
301+
name: "RWO Block volume mode with FSS disabled",
302+
pv: &v1.PersistentVolume{
303+
Spec: v1.PersistentVolumeSpec{
304+
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
305+
VolumeMode: func() *v1.PersistentVolumeMode { m := v1.PersistentVolumeBlock; return &m }(),
306+
},
307+
},
308+
want: false,
309+
},
310+
}
311+
312+
for _, tt := range tests {
313+
t.Run(tt.name, func(t *testing.T) {
314+
k8sOrchestratorInstance = &K8sOrchestrator{}
315+
got := isFileVolume(ctx, tt.pv)
316+
if got != tt.want {
317+
t.Errorf("isFileVolume() = %v, want %v", got, tt.want)
318+
}
319+
})
320+
}
321+
}

pkg/csi/service/common/constants.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ const (
448448
// FileVolumesWithVmService is an FSS to support file volumes with VM service VMs.
449449
FileVolumesWithVmService = "file-volume-with-vm-service"
450450
// SharedDiskFss is an FSS that tells whether shared disks are supported or not
451-
SharedDiskFss = "supports_shared_disks"
451+
SharedDiskFss = "supports_shared_disks_with_VM_service_VMs"
452452
// CSITranSactionSupport is an FSS for transaction support
453453
CSITranSactionSupport = "csi-transaction-support"
454454
// VolFromSnapshotOnTargetDs is a FSS that tells whether creation of volumes from
@@ -466,6 +466,7 @@ var WCPFeatureStates = map[string]struct{}{
466466
WorkloadDomainIsolation: {},
467467
VPCCapabilitySupervisor: {},
468468
VolFromSnapshotOnTargetDs: {},
469+
SharedDiskFss: {},
469470
}
470471

471472
// WCPFeatureStatesSupportsLateEnablement contains capabilities that can be enabled later

0 commit comments

Comments
 (0)