Skip to content

Commit

Permalink
Merge pull request #1019 from nirmalaagash/master
Browse files Browse the repository at this point in the history
Node Publish Mount Idempotent
  • Loading branch information
k8s-ci-robot authored Aug 27, 2021
2 parents 8c6c7e0 + 38d60d9 commit edb4fbe
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/cloud/aws_metrics.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !providerless
// +build !providerless

/*
Expand Down
14 changes: 14 additions & 0 deletions pkg/driver/mocks/mock_mount.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/driver/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Mounter interface {
mountInterface

FormatAndMount(source string, target string, fstype string, options []string) error

IsCorruptedMnt(err error) bool
GetDeviceNameFromMount(mountPath string) (string, int, error)
MakeFile(path string) error
MakeDir(path string) error
Expand Down
6 changes: 6 additions & 0 deletions pkg/driver/mount_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand Down Expand Up @@ -33,6 +34,11 @@ func (m NodeMounter) GetDeviceNameFromMount(mountPath string) (string, int, erro
return mountutils.GetDeviceNameFromMount(m, mountPath)
}

// IsCorruptedMnt return true if err is about corrupted mount point
func (m NodeMounter) IsCorruptedMnt(err error) bool {
return mountutils.IsCorruptedMnt(err)
}

// This function is mirrored in ./sanity_test.go to make sure sanity test covered this block of code
// Please mirror the change to func MakeFile in ./sanity_test.go
func (m *NodeMounter) MakeFile(path string) error {
Expand Down
1 change: 1 addition & 0 deletions pkg/driver/mount_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

/*
Expand Down
65 changes: 56 additions & 9 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,17 +586,57 @@ func (d *nodeService) nodePublishVolumeForBlock(req *csi.NodePublishVolumeReques
return status.Errorf(codes.Internal, "Could not create file %q: %v", target, err)
}

klog.V(4).Infof("NodePublishVolume [block]: mounting %s at %s", source, target)
if err := d.mounter.Mount(source, target, "", mountOptions); err != nil {
if removeErr := os.Remove(target); removeErr != nil {
return status.Errorf(codes.Internal, "Could not remove mount target %q: %v", target, removeErr)
}
//Checking if the target file is already mounted with a device.
mounted, err := d.isMounted(source, target)
if err != nil {
return status.Errorf(codes.Internal, "Could not mount %q at %q: %v", source, target, err)
}

if !mounted {
klog.V(4).Infof("NodePublishVolume [block]: mounting %s at %s", source, target)
if err := d.mounter.Mount(source, target, "", mountOptions); err != nil {
if removeErr := os.Remove(target); removeErr != nil {
return status.Errorf(codes.Internal, "Could not remove mount target %q: %v", target, removeErr)
}
return status.Errorf(codes.Internal, "Could not mount %q at %q: %v", source, target, err)
}
} else {
klog.V(4).Infof("NodePublishVolume [block]: Target path %q is already mounted", target)
}

return nil
}

func (d *nodeService) isMounted(source string, target string) (bool, error) {
/*
Checking if it's a mount point using IsLikelyNotMountPoint. There are three different return values,
1. true, err when the directory does not exist or corrupted.
2. false, nil when the path is already mounted with a device.
3. true, nil when the path is not mounted with any device.
*/
notMnt, err := d.mounter.IsLikelyNotMountPoint(target)
if err != nil && !os.IsNotExist(err) {
//Checking if the path exists and error is related to Corrupted Mount, in that case, the system could unmount and mount.
_, pathErr := d.mounter.PathExists(target)
if pathErr != nil && d.mounter.IsCorruptedMnt(pathErr) {
klog.V(4).Infof("NodePublishVolume: Target path %q is a corrupted mount. Trying to unmount.", target)
if mntErr := d.mounter.Unmount(target); mntErr != nil {
return !notMnt, status.Errorf(codes.Internal, "Unable to unmount the target %q : %v", target, mntErr)
}
//After successful unmount, the device is ready to be mounted.
return !notMnt, nil
}
return !notMnt, status.Errorf(codes.Internal, "Could not mount %q at %q: %v", source, target, err)
}

if !notMnt {
klog.V(4).Infof("NodePublishVolume: Target path %q is already mounted", target)
return !notMnt, nil
}

return !notMnt, err
}

func (d *nodeService) nodePublishVolumeForFileSystem(req *csi.NodePublishVolumeRequest, mountOptions []string, mode *csi.VolumeCapability_Mount) error {
target := req.GetTargetPath()
source := req.GetStagingTargetPath()
Expand All @@ -620,13 +660,20 @@ func (d *nodeService) nodePublishVolumeForFileSystem(req *csi.NodePublishVolumeR
mountOptions = collectMountOptions(fsType, mountOptions)

klog.V(4).Infof("NodePublishVolume: mounting %s at %s with option %s as fstype %s", source, target, mountOptions, fsType)
if err := d.mounter.Mount(source, target, fsType, mountOptions); err != nil {
if removeErr := os.Remove(target); removeErr != nil {
return status.Errorf(codes.Internal, "Could not remove mount target %q: %v", target, err)
}

//Checking if the target directory is already mounted with a device.
mounted, err := d.isMounted(source, target)

if err != nil {
return status.Errorf(codes.Internal, "Could not mount %q at %q: %v", source, target, err)
}

if !mounted {
if err := d.mounter.Mount(source, target, fsType, mountOptions); err != nil {
return status.Errorf(codes.Internal, "Could not mount %q at %q: %v", source, target, err)
}
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/driver/node_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand Down
Loading

0 comments on commit edb4fbe

Please sign in to comment.