Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.1] Automated cherry pick of #1019: Added Mount Idempotency #1039

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

9 changes: 8 additions & 1 deletion pkg/driver/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package driver

import (
"fmt"
"k8s.io/klog"
"os"
"strconv"
"strings"

"k8s.io/klog"

"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/mounter"
mountutils "k8s.io/mount-utils"
utilexec "k8s.io/utils/exec"
Expand All @@ -38,6 +39,7 @@ type Mounter interface {
utilexec.Interface

// Implemented by NodeMounter below
IsCorruptedMnt(err error) bool
GetDeviceNameFromMount(mountPath string) (string, int, error)
// TODO this won't make sense on Windows with csi-proxy
MakeFile(path string) error
Expand All @@ -64,6 +66,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
65 changes: 56 additions & 9 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,17 +591,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 @@ -624,13 +664,20 @@ func (d *nodeService) nodePublishVolumeForFileSystem(req *csi.NodePublishVolumeR
}

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