Skip to content

Commit d4da7b0

Browse files
committed
chore: create voume/snapshot should respect mountOptions in secret
1 parent 5ef58d3 commit d4da7b0

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

pkg/nfs/controllerserver.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
163163
return nil, status.Error(codes.InvalidArgument, err.Error())
164164
}
165165

166-
var volCap *csi.VolumeCapability
166+
volCap := getVolumeCapabilityFromSecret(name, req.GetSecrets())
167167
if len(req.GetVolumeCapabilities()) > 0 {
168168
volCap = req.GetVolumeCapabilities()[0]
169169
}
@@ -220,19 +220,6 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
220220
return &csi.DeleteVolumeResponse{}, nil
221221
}
222222

223-
var volCap *csi.VolumeCapability
224-
mountOptions := getMountOptions(req.GetSecrets())
225-
if mountOptions != "" {
226-
klog.V(2).Infof("DeleteVolume: found mountOptions(%s) for volume(%s)", mountOptions, volumeID)
227-
volCap = &csi.VolumeCapability{
228-
AccessType: &csi.VolumeCapability_Mount{
229-
Mount: &csi.VolumeCapability_MountVolume{
230-
MountFlags: []string{mountOptions},
231-
},
232-
},
233-
}
234-
}
235-
236223
if nfsVol.onDelete == "" {
237224
nfsVol.onDelete = cs.Driver.defaultOnDeletePolicy
238225
}
@@ -253,6 +240,7 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
253240
return &csi.DeleteVolumeResponse{}, nil
254241
}
255242
// mount nfs base share so we can delete the subdirectory
243+
volCap := getVolumeCapabilityFromSecret(volumeID, req.GetSecrets())
256244
if err = cs.internalMount(ctx, nfsVol, nil, volCap); err != nil {
257245
return nil, status.Errorf(codes.Internal, "failed to mount nfs server: %v", err)
258246
}
@@ -376,7 +364,8 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
376364
return nil, status.Errorf(codes.NotFound, "failed to create nfsSnapshot: %v", err)
377365
}
378366
snapVol := volumeFromSnapshot(snapshot)
379-
if err = cs.internalMount(ctx, snapVol, req.GetParameters(), nil); err != nil {
367+
volCap := getVolumeCapabilityFromSecret(req.GetSourceVolumeId(), req.GetSecrets())
368+
if err = cs.internalMount(ctx, snapVol, req.GetParameters(), volCap); err != nil {
380369
return nil, status.Errorf(codes.Internal, "failed to mount snapshot nfs server: %v", err)
381370
}
382371
defer func() {
@@ -392,7 +381,7 @@ func (cs *ControllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
392381
return nil, err
393382
}
394383

395-
if err = cs.internalMount(ctx, srcVol, req.GetParameters(), nil); err != nil {
384+
if err = cs.internalMount(ctx, srcVol, req.GetParameters(), volCap); err != nil {
396385
return nil, status.Errorf(codes.Internal, "failed to mount src nfs server: %v", err)
397386
}
398387
defer func() {
@@ -445,18 +434,7 @@ func (cs *ControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
445434
return &csi.DeleteSnapshotResponse{}, nil
446435
}
447436

448-
var volCap *csi.VolumeCapability
449-
mountOptions := getMountOptions(req.GetSecrets())
450-
if mountOptions != "" {
451-
klog.V(2).Infof("DeleteSnapshot: found mountOptions(%s) for snapshot(%s)", mountOptions, req.GetSnapshotId())
452-
volCap = &csi.VolumeCapability{
453-
AccessType: &csi.VolumeCapability_Mount{
454-
Mount: &csi.VolumeCapability_MountVolume{
455-
MountFlags: []string{mountOptions},
456-
},
457-
},
458-
}
459-
}
437+
volCap := getVolumeCapabilityFromSecret(req.SnapshotId, req.GetSecrets())
460438
vol := volumeFromSnapshot(snap)
461439
if err = cs.internalMount(ctx, vol, nil, volCap); err != nil {
462440
return nil, status.Errorf(codes.Internal, "failed to mount nfs server for snapshot deletion: %v", err)

pkg/nfs/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,21 @@ func WaitUntilTimeout(timeout time.Duration, execFunc ExecFunc, timeoutFunc Time
289289
return timeoutFunc()
290290
}
291291
}
292+
293+
// getVolumeCapabilityFromSecret retrieves the volume capability from the secret
294+
// if secret contains mountOptions, it will return the volume capability
295+
// if secret does not contain mountOptions, it will return nil
296+
func getVolumeCapabilityFromSecret(volumeID string, secret map[string]string) *csi.VolumeCapability {
297+
mountOptions := getMountOptions(secret)
298+
if mountOptions != "" {
299+
klog.V(2).Infof("found mountOptions(%s) for volume(%s)", mountOptions, volumeID)
300+
return &csi.VolumeCapability{
301+
AccessType: &csi.VolumeCapability_Mount{
302+
Mount: &csi.VolumeCapability_MountVolume{
303+
MountFlags: []string{mountOptions},
304+
},
305+
},
306+
}
307+
}
308+
return nil
309+
}

pkg/nfs/utils_test.go

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

27+
"github.com/container-storage-interface/spec/lib/go/csi"
2728
"go.uber.org/goleak"
2829
)
2930

@@ -505,3 +506,51 @@ func TestWaitUntilTimeout(t *testing.T) {
505506
}
506507
}
507508
}
509+
func TestGetVolumeCapabilityFromSecret(t *testing.T) {
510+
tests := []struct {
511+
desc string
512+
volumeID string
513+
secret map[string]string
514+
expected *csi.VolumeCapability
515+
}{
516+
{
517+
desc: "secret contains mountOptions",
518+
volumeID: "vol-123",
519+
secret: map[string]string{"mountOptions": "nfsvers=3"},
520+
expected: &csi.VolumeCapability{
521+
AccessType: &csi.VolumeCapability_Mount{
522+
Mount: &csi.VolumeCapability_MountVolume{
523+
MountFlags: []string{"nfsvers=3"},
524+
},
525+
},
526+
},
527+
},
528+
{
529+
desc: "secret does not contain mountOptions",
530+
volumeID: "vol-456",
531+
secret: map[string]string{"otherKey": "otherValue"},
532+
expected: nil,
533+
},
534+
{
535+
desc: "empty secret",
536+
volumeID: "vol-789",
537+
secret: map[string]string{},
538+
expected: nil,
539+
},
540+
{
541+
desc: "nil secret",
542+
volumeID: "vol-000",
543+
secret: nil,
544+
expected: nil,
545+
},
546+
}
547+
548+
for _, test := range tests {
549+
t.Run(test.desc, func(t *testing.T) {
550+
result := getVolumeCapabilityFromSecret(test.volumeID, test.secret)
551+
if !reflect.DeepEqual(result, test.expected) {
552+
t.Errorf("test[%s]: unexpected result: %v, expected: %v", test.desc, result, test.expected)
553+
}
554+
})
555+
}
556+
}

0 commit comments

Comments
 (0)