Skip to content

Commit 59aa8fd

Browse files
authored
Merge pull request kubernetes#82698 from janario/fix/umount-subpath-warns
Unmount subpath should only scan the first level of files/directories
2 parents d366d2e + c9e9715 commit 59aa8fd

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

pkg/util/mount/fake.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ type FakeMounter struct {
3232
MountCheckErrors map[string]error
3333
// Some tests run things in parallel, make sure the mounter does not produce
3434
// any golang's DATA RACE warnings.
35-
mutex sync.Mutex
35+
mutex sync.Mutex
36+
UnmountFunc UnmountFunc
3637
}
3738

39+
type UnmountFunc func(path string) error
40+
3841
var _ Interface = &FakeMounter{}
3942

4043
const (
@@ -117,6 +120,12 @@ func (f *FakeMounter) Unmount(target string) error {
117120
newMountpoints := []MountPoint{}
118121
for _, mp := range f.MountPoints {
119122
if mp.Path == absTarget {
123+
if f.UnmountFunc != nil {
124+
err := f.UnmountFunc(absTarget)
125+
if err != nil {
126+
return err
127+
}
128+
}
120129
klog.V(5).Infof("Fake mounter: unmounted %s from %s", mp.Device, absTarget)
121130
// Don't copy it to newMountpoints
122131
continue

pkg/volume/util/subpath/subpath_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ func doCleanSubPaths(mounter mount.Interface, podDir string, volumeName string)
241241
if err = doCleanSubPath(mounter, fullContainerDirPath, filepath.Base(path)); err != nil {
242242
return err
243243
}
244+
245+
if info.IsDir() {
246+
// skip subdirs of the volume: it only matters the first level to unmount, otherwise it would try to unmount subdir of the volume
247+
return filepath.SkipDir
248+
}
249+
244250
return nil
245251
})
246252
if err != nil {

pkg/volume/util/subpath/subpath_linux_test.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ func TestCleanSubPaths(t *testing.T) {
409409
// Function that validates directory structure after the test
410410
validate func(base string) error
411411
expectError bool
412+
unmount func(path string) error
412413
}{
413414
{
414415
name: "not-exists",
@@ -539,6 +540,64 @@ func TestCleanSubPaths(t *testing.T) {
539540
return validateDirExists(baseSubdir)
540541
},
541542
},
543+
{
544+
name: "subpath-with-files",
545+
prepare: func(base string) ([]mount.MountPoint, error) {
546+
containerPath := filepath.Join(base, containerSubPathDirectoryName, testVol, "container1")
547+
if err := os.MkdirAll(containerPath, defaultPerm); err != nil {
548+
return nil, err
549+
}
550+
551+
file0 := filepath.Join(containerPath, "0")
552+
if err := ioutil.WriteFile(file0, []byte{}, defaultPerm); err != nil {
553+
return nil, err
554+
}
555+
556+
dir1 := filepath.Join(containerPath, "1")
557+
if err := os.MkdirAll(filepath.Join(dir1, "my-dir-1"), defaultPerm); err != nil {
558+
return nil, err
559+
}
560+
561+
dir2 := filepath.Join(containerPath, "2")
562+
if err := os.MkdirAll(filepath.Join(dir2, "my-dir-2"), defaultPerm); err != nil {
563+
return nil, err
564+
}
565+
566+
file3 := filepath.Join(containerPath, "3")
567+
if err := ioutil.WriteFile(file3, []byte{}, defaultPerm); err != nil {
568+
return nil, err
569+
}
570+
571+
mounts := []mount.MountPoint{
572+
{Device: "/dev/sdb", Path: file0},
573+
{Device: "/dev/sdc", Path: dir1},
574+
{Device: "/dev/sdd", Path: dir2},
575+
{Device: "/dev/sde", Path: file3},
576+
}
577+
return mounts, nil
578+
},
579+
unmount: func(mountpath string) error {
580+
err := filepath.Walk(mountpath, func(path string, info os.FileInfo, err error) error {
581+
if path == mountpath {
582+
// Skip top level directory
583+
return nil
584+
}
585+
586+
if err = os.Remove(path); err != nil {
587+
return err
588+
}
589+
return filepath.SkipDir
590+
})
591+
if err != nil {
592+
return fmt.Errorf("error processing %s: %s", mountpath, err)
593+
}
594+
595+
return nil
596+
},
597+
validate: func(base string) error {
598+
return validateDirNotExists(filepath.Join(base, containerSubPathDirectoryName))
599+
},
600+
},
542601
}
543602

544603
for _, test := range tests {
@@ -553,7 +612,7 @@ func TestCleanSubPaths(t *testing.T) {
553612
t.Fatalf("failed to prepare test %q: %v", test.name, err.Error())
554613
}
555614

556-
fm := &mount.FakeMounter{MountPoints: mounts}
615+
fm := &mount.FakeMounter{MountPoints: mounts, UnmountFunc: test.unmount}
557616

558617
err = doCleanSubPaths(fm, base, testVol)
559618
if err != nil && !test.expectError {

0 commit comments

Comments
 (0)