diff --git a/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator.go b/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator.go index 80ef98aafb2..d66acf63af3 100644 --- a/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator.go +++ b/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator.go @@ -207,7 +207,9 @@ func (dswp *desiredStateOfWorldPopulator) findAndAddNewPods() { // Iterate through all pods in desired state of world, and remove if they no // longer exist func (dswp *desiredStateOfWorldPopulator) findAndRemoveDeletedPods() { + podsFromCache := make(map[volumetypes.UniquePodName]struct{}) for _, volumeToMount := range dswp.desiredStateOfWorld.GetVolumesToMount() { + podsFromCache[volumetypes.UniquePodName(volumeToMount.Pod.UID)] = struct{}{} pod, podExists := dswp.podManager.GetPodByUID(volumeToMount.Pod.UID) if podExists { @@ -256,6 +258,23 @@ func (dswp *desiredStateOfWorldPopulator) findAndRemoveDeletedPods() { dswp.deleteProcessedPod(volumeToMount.PodName) } + // Cleanup orphanded entries from processedPods + dswp.pods.Lock() + orphanedPods := make([]volumetypes.UniquePodName, 0, len(dswp.pods.processedPods)) + for k := range dswp.pods.processedPods { + if _, ok := podsFromCache[k]; !ok { + orphanedPods = append(orphanedPods, k) + } + } + dswp.pods.Unlock() + for _, orphanedPod := range orphanedPods { + uid := types.UID(orphanedPod) + _, podExists := dswp.podManager.GetPodByUID(uid) + if !podExists && dswp.podStateProvider.ShouldPodRuntimeBeRemoved(uid) { + dswp.deleteProcessedPod(orphanedPod) + } + } + podsWithError := dswp.desiredStateOfWorld.GetPodsWithErrors() for _, podName := range podsWithError { if _, podExists := dswp.podManager.GetPodByUID(types.UID(podName)); !podExists { diff --git a/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator_test.go b/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator_test.go index 3f96add355f..01e48b335fa 100644 --- a/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator_test.go +++ b/pkg/kubelet/volumemanager/populator/desired_state_of_world_populator_test.go @@ -350,6 +350,15 @@ func TestFindAndAddNewPods_FindAndRemoveDeletedPods(t *testing.T) { t.Fatalf("Failed to remove pods from desired state of world since they no longer exist") } + // podWorker may call volume_manager WaitForUnmount() after we processed the pod in findAndRemoveDeletedPods() + dswp.ReprocessPod(podName) + dswp.findAndRemoveDeletedPods() + + // findAndRemoveDeletedPods() above must detect orphaned pod and delete it from the map + if _, ok := dswp.pods.processedPods[podName]; ok { + t.Fatalf("Failed to remove orphanded pods from internal map") + } + volumeExists := dswp.desiredStateOfWorld.VolumeExists(expectedVolumeName, "" /* SELinuxContext */) if volumeExists { t.Fatalf(