Skip to content

Commit

Permalink
Merge pull request #430 from bertinatto/mountoptions
Browse files Browse the repository at this point in the history
Handle mount flags in NodeStageVolume
  • Loading branch information
k8s-ci-robot authored Dec 26, 2019
2 parents 1369dec + d52d41b commit 282df6a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
32 changes: 22 additions & 10 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,19 @@ func (d *nodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
if mount == nil {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume: mount is nil within volume capability")
}

fsType := mount.GetFsType()
if len(fsType) == 0 {
fsType = defaultFsType
}

var mountOptions []string
for _, f := range mount.MountFlags {
if !hasMountOption(mountOptions, f) {
mountOptions = append(mountOptions, f)
}
}

if ok := d.inFlight.Insert(req); !ok {
msg := fmt.Sprintf("request to stage volume=%q is already in progress", volumeID)
return nil, status.Error(codes.Internal, msg)
Expand Down Expand Up @@ -181,7 +189,7 @@ func (d *nodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol

// FormatAndMount will format only if needed
klog.V(5).Infof("NodeStageVolume: formatting %s and mounting at %s with fstype %s", source, target, fsType)
err = d.mounter.FormatAndMount(source, target, fsType, nil)
err = d.mounter.FormatAndMount(source, target, fsType, mountOptions)
if err != nil {
msg := fmt.Sprintf("could not format %q and mount it at %q", source, target)
return nil, status.Error(codes.Internal, msg)
Expand Down Expand Up @@ -420,16 +428,8 @@ func (d *nodeService) nodePublishVolumeForFileSystem(req *csi.NodePublishVolumeR
target := req.GetTargetPath()
source := req.GetStagingTargetPath()
if m := mode.Mount; m != nil {
hasOption := func(options []string, opt string) bool {
for _, o := range options {
if o == opt {
return true
}
}
return false
}
for _, f := range m.MountFlags {
if !hasOption(mountOptions, f) {
if !hasMountOption(mountOptions, f) {
mountOptions = append(mountOptions, f)
}
}
Expand Down Expand Up @@ -519,3 +519,15 @@ func (d *nodeService) getVolumesLimit() int64 {
}
return defaultMaxEBSVolumes
}

// hasMountOption returns a boolean indicating whether the given
// slice already contains a mount option. This is used to prevent
// passing duplicate option to the mount command.
func hasMountOption(options []string, opt string) bool {
for _, o := range options {
if o == opt {
return true
}
}
return false
}
47 changes: 46 additions & 1 deletion pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,52 @@ func TestNodeStageVolume(t *testing.T) {
},
},
{
name: "success mount options fsType ext3",
name: "success with mount options",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockMetadata := mocks.NewMockMetadataService(mockCtl)
mockMounter := mocks.NewMockMounter(mockCtl)

awsDriver := nodeService{
metadata: mockMetadata,
mounter: mockMounter,
inFlight: internal.NewInFlight(),
}

req := &csi.NodeStageVolumeRequest{
PublishContext: map[string]string{DevicePathKey: devicePath},
StagingTargetPath: targetPath,
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{
MountFlags: []string{"dirsync", "noexec"},
},
},
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
},
VolumeId: "vol-test",
}

gomock.InOrder(
mockMounter.EXPECT().ExistsPath(gomock.Eq(devicePath)).Return(true, nil),
mockMounter.EXPECT().ExistsPath(gomock.Eq(targetPath)).Return(false, nil),
)

mockMounter.EXPECT().MakeDir(targetPath).Return(nil)
mockMounter.EXPECT().GetDeviceName(targetPath).Return("", 1, nil)
mockMounter.EXPECT().FormatAndMount(gomock.Eq(devicePath), gomock.Eq(targetPath), gomock.Eq(FSTypeExt4), gomock.Eq([]string{"dirsync", "noexec"}))
_, err := awsDriver.NodeStageVolume(context.TODO(), req)
if err != nil {
t.Fatalf("Expect no error but got: %v", err)
}
},
},
{
name: "success fsType ext3",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
Expand Down

0 comments on commit 282df6a

Please sign in to comment.