From 593654eaa3b79348233f3718ccd3bcee3f789e5c Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Wed, 31 Jan 2024 15:09:29 +0100 Subject: [PATCH] Fix cleanup of file subpaths Allow kubelet orphan cleanup to delete both files and empty directories as subpath mount points. A pod does not need to use a directory as a subpath, it can use a file (unix domain socket, pipe, ...) too. Therefore the cleanup should use `os.Remove` that deletes both files and empty directories. --- pkg/kubelet/kubelet_volumes.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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)