diff --git a/pkg/kubelet/kubelet_volumes.go b/pkg/kubelet/kubelet_volumes.go index 2eb91338e24..27c82ae430e 100644 --- a/pkg/kubelet/kubelet_volumes.go +++ b/pkg/kubelet/kubelet_volumes.go @@ -116,7 +116,8 @@ func (kl *Kubelet) newVolumeMounterFromPlugins(spec *volume.Spec, pod *v1.Pod, o // removeOrphanedPodVolumeDirs attempts to remove the pod volumes directory and // its subdirectories. There should be no files left under normal conditions // when this is called, so it effectively does a recursive rmdir instead of -// RemoveAll to ensure it only removes directories and not regular files. +// RemoveAll to ensure it only removes empty directories and files that were +// used as mount points, but not content of the mount points. func (kl *Kubelet) removeOrphanedPodVolumeDirs(uid types.UID) []error { orphanVolumeErrors := []error{} @@ -136,7 +137,7 @@ func (kl *Kubelet) removeOrphanedPodVolumeDirs(uid types.UID) []error { } } - // If there are any volume-subpaths, attempt to rmdir them + // If there are any volume-subpaths, attempt to remove them subpathVolumePaths, err := kl.getPodVolumeSubpathListFromDisk(uid) if err != nil { orphanVolumeErrors = append(orphanVolumeErrors, fmt.Errorf("orphaned pod %q found, but error occurred during reading of volume-subpaths dir from disk: %v", uid, err)) @@ -144,7 +145,8 @@ func (kl *Kubelet) removeOrphanedPodVolumeDirs(uid types.UID) []error { } if len(subpathVolumePaths) > 0 { for _, subpathVolumePath := range subpathVolumePaths { - if err := syscall.Rmdir(subpathVolumePath); err != nil { + // Remove both files and empty directories here, as the subpath may have been a bind-mount of a file or a directory. + if err := os.Remove(subpathVolumePath); err != nil { orphanVolumeErrors = append(orphanVolumeErrors, fmt.Errorf("orphaned pod %q found, but failed to rmdir() subpath at path %v: %v", uid, subpathVolumePath, err)) } else { klog.InfoS("Cleaned up orphaned volume subpath from pod", "podUID", uid, "path", subpathVolumePath)