Add PodRemovedFromVolume

To know when a volume has been fully unmounted (incl. uncertain mounts).
This commit is contained in:
Jan Safranek 2021-06-07 15:57:33 +02:00
parent ca934b8f5c
commit 2fcb5e9cf7
4 changed files with 40 additions and 3 deletions

View File

@ -71,7 +71,7 @@ func (kl *Kubelet) ListBlockVolumesForPod(podUID types.UID) (map[string]volume.B
} }
// podVolumesExist checks with the volume manager and returns true any of the // podVolumesExist checks with the volume manager and returns true any of the
// pods for the specified volume are mounted. // pods for the specified volume are mounted or are uncertain.
func (kl *Kubelet) podVolumesExist(podUID types.UID) bool { func (kl *Kubelet) podVolumesExist(podUID types.UID) bool {
if mountedVolumes := if mountedVolumes :=
kl.volumeManager.GetPossiblyMountedVolumesForPod( kl.volumeManager.GetPossiblyMountedVolumesForPod(

View File

@ -110,6 +110,14 @@ type ActualStateOfWorld interface {
// volumes that do not need to update contents should not fail. // volumes that do not need to update contents should not fail.
PodExistsInVolume(podName volumetypes.UniquePodName, volumeName v1.UniqueVolumeName) (bool, string, error) PodExistsInVolume(podName volumetypes.UniquePodName, volumeName v1.UniqueVolumeName) (bool, string, error)
// PodRemovedFromVolume returns true if the given pod does not exist in the list of
// mountedPods for the given volume in the cache, indicating that the pod has
// fully unmounted it or it was never mounted the volume.
// If the volume is fully mounted or is in uncertain mount state for the pod, it is
// considered that the pod still exists in volume manager's actual state of the world
// and false is returned.
PodRemovedFromVolume(podName volumetypes.UniquePodName, volumeName v1.UniqueVolumeName) bool
// VolumeExistsWithSpecName returns true if the given volume specified with the // VolumeExistsWithSpecName returns true if the given volume specified with the
// volume spec name (a.k.a., InnerVolumeSpecName) exists in the list of // volume spec name (a.k.a., InnerVolumeSpecName) exists in the list of
// volumes that should be attached to this node. // volumes that should be attached to this node.
@ -686,6 +694,31 @@ func (asw *actualStateOfWorld) PodExistsInVolume(
return podExists, volumeObj.devicePath, nil return podExists, volumeObj.devicePath, nil
} }
func (asw *actualStateOfWorld) PodRemovedFromVolume(
podName volumetypes.UniquePodName,
volumeName v1.UniqueVolumeName) bool {
asw.RLock()
defer asw.RUnlock()
volumeObj, volumeExists := asw.attachedVolumes[volumeName]
if !volumeExists {
return true
}
podObj, podExists := volumeObj.mountedPods[podName]
if podExists {
// if volume mount was uncertain we should keep trying to unmount the volume
if podObj.volumeMountStateForPod == operationexecutor.VolumeMountUncertain {
return false
}
if podObj.volumeMountStateForPod == operationexecutor.VolumeMounted {
return false
}
}
return true
}
func (asw *actualStateOfWorld) VolumeExistsWithSpecName(podName volumetypes.UniquePodName, volumeSpecName string) bool { func (asw *actualStateOfWorld) VolumeExistsWithSpecName(podName volumetypes.UniquePodName, volumeSpecName string) bool {
asw.RLock() asw.RLock()
defer asw.RUnlock() defer asw.RUnlock()

View File

@ -671,6 +671,10 @@ func TestUncertainVolumeMounts(t *testing.T) {
if volExists { if volExists {
t.Fatalf("expected volume %s to not exist in asw", generatedVolumeName1) t.Fatalf("expected volume %s to not exist in asw", generatedVolumeName1)
} }
removed := asw.PodRemovedFromVolume(podName1, generatedVolumeName1)
if removed {
t.Fatalf("expected volume %s not to be removed in asw", generatedVolumeName1)
}
} }
func verifyVolumeExistsInGloballyMountedVolumes( func verifyVolumeExistsInGloballyMountedVolumes(

View File

@ -278,12 +278,12 @@ func (dswp *desiredStateOfWorldPopulator) findAndRemoveDeletedPods() {
klog.V(4).InfoS("Pod still has one or more containers in the non-exited state and will not be removed from desired state", "pod", klog.KObj(volumeToMount.Pod)) klog.V(4).InfoS("Pod still has one or more containers in the non-exited state and will not be removed from desired state", "pod", klog.KObj(volumeToMount.Pod))
continue continue
} }
exists, _, _ := dswp.actualStateOfWorld.PodExistsInVolume(volumeToMount.PodName, volumeToMount.VolumeName)
var volumeToMountSpecName string var volumeToMountSpecName string
if volumeToMount.VolumeSpec != nil { if volumeToMount.VolumeSpec != nil {
volumeToMountSpecName = volumeToMount.VolumeSpec.Name() volumeToMountSpecName = volumeToMount.VolumeSpec.Name()
} }
if !exists && podExists { removed := dswp.actualStateOfWorld.PodRemovedFromVolume(volumeToMount.PodName, volumeToMount.VolumeName)
if removed && podExists {
klog.V(4).InfoS("Actual state does not yet have volume mount information and pod still exists in pod manager, skip removing volume from desired state", "pod", klog.KObj(volumeToMount.Pod), "podUID", volumeToMount.Pod.UID, "volumeName", volumeToMountSpecName) klog.V(4).InfoS("Actual state does not yet have volume mount information and pod still exists in pod manager, skip removing volume from desired state", "pod", klog.KObj(volumeToMount.Pod), "podUID", volumeToMount.Pod.UID, "volumeName", volumeToMountSpecName)
continue continue
} }