diff --git a/pkg/util/mount/fake.go b/pkg/util/mount/fake.go index 66b877064d0..063973527f1 100644 --- a/pkg/util/mount/fake.go +++ b/pkg/util/mount/fake.go @@ -32,9 +32,12 @@ type FakeMounter struct { MountCheckErrors map[string]error // Some tests run things in parallel, make sure the mounter does not produce // any golang's DATA RACE warnings. - mutex sync.Mutex + mutex sync.Mutex + UmountFunc UmountFunc } +type UmountFunc func(path string) error + var _ Interface = &FakeMounter{} const ( @@ -117,6 +120,12 @@ func (f *FakeMounter) Unmount(target string) error { newMountpoints := []MountPoint{} for _, mp := range f.MountPoints { if mp.Path == absTarget { + if f.UmountFunc != nil { + err := f.UmountFunc(absTarget) + if err != nil { + return err + } + } klog.V(5).Infof("Fake mounter: unmounted %s from %s", mp.Device, absTarget) // Don't copy it to newMountpoints continue diff --git a/pkg/volume/util/subpath/subpath_linux.go b/pkg/volume/util/subpath/subpath_linux.go index b497a810dc2..59c8128409e 100644 --- a/pkg/volume/util/subpath/subpath_linux.go +++ b/pkg/volume/util/subpath/subpath_linux.go @@ -241,7 +241,8 @@ func doCleanSubPaths(mounter mount.Interface, podDir string, volumeName string) if err = doCleanSubPath(mounter, fullContainerDirPath, filepath.Base(path)); err != nil { return err } - return nil + // skip subdirs of the volume: it only matters the first level to unmount, otherwise it would try to unmount subdir of the volume + return filepath.SkipDir }) if err != nil { return fmt.Errorf("error processing %s: %s", fullContainerDirPath, err) diff --git a/pkg/volume/util/subpath/subpath_linux_test.go b/pkg/volume/util/subpath/subpath_linux_test.go index 954a134157a..f950ed8f821 100644 --- a/pkg/volume/util/subpath/subpath_linux_test.go +++ b/pkg/volume/util/subpath/subpath_linux_test.go @@ -409,6 +409,7 @@ func TestCleanSubPaths(t *testing.T) { // Function that validates directory structure after the test validate func(base string) error expectError bool + umount func(path string) error }{ { name: "not-exists", @@ -539,6 +540,37 @@ func TestCleanSubPaths(t *testing.T) { return validateDirExists(baseSubdir) }, }, + { + name: "subpath-with-files", + prepare: func(base string) ([]mount.MountPoint, error) { + path := filepath.Join(base, containerSubPathDirectoryName, testVol, "container1", "0") + path2 := filepath.Join(base, containerSubPathDirectoryName, testVol, "container1", "1") + if err := os.MkdirAll(filepath.Join(path, "my-dir-1"), defaultPerm); err != nil { + return nil, err + } + if err := os.MkdirAll(filepath.Join(path2, "my-dir-2"), defaultPerm); err != nil { + return nil, err + } + mounts := []mount.MountPoint{ + {Device: "/dev/sdb", Path: path}, + {Device: "/dev/sdc", Path: path2}, + } + return mounts, nil + }, + umount: func(mountpath string) error { + fileInfo, err := ioutil.ReadDir(mountpath) + for _, file := range fileInfo { + if err = os.RemoveAll(filepath.Join(mountpath, file.Name())); err != nil { + return err + } + } + + return nil + }, + validate: func(base string) error { + return validateDirNotExists(filepath.Join(base, containerSubPathDirectoryName)) + }, + }, } for _, test := range tests { @@ -553,7 +585,7 @@ func TestCleanSubPaths(t *testing.T) { t.Fatalf("failed to prepare test %q: %v", test.name, err.Error()) } - fm := &mount.FakeMounter{MountPoints: mounts} + fm := &mount.FakeMounter{MountPoints: mounts, UmountFunc: test.umount} err = doCleanSubPaths(fm, base, testVol) if err != nil && !test.expectError {