Fix code for desired state of the world populator

This commit is contained in:
Hemant Kumar 2022-03-08 16:03:58 -05:00
parent e4f62d6c41
commit c0fbd83cde
3 changed files with 11 additions and 25 deletions

View File

@ -194,6 +194,7 @@ type volumeToMount struct {
desiredSizeLimit *resource.Quantity desiredSizeLimit *resource.Quantity
// persistentVolumeSize records desired size of a persistent volume. // persistentVolumeSize records desired size of a persistent volume.
// Usually this value reflects size recorded in pv.Spec.Capacity
persistentVolumeSize *resource.Quantity persistentVolumeSize *resource.Quantity
} }

View File

@ -195,13 +195,12 @@ func (dswp *desiredStateOfWorldPopulator) findAndAddNewPods() {
mountedVolumes[mountedVolume.OuterVolumeSpecName] = mountedVolume mountedVolumes[mountedVolume.OuterVolumeSpecName] = mountedVolume
} }
processedVolumesForFSResize := sets.NewString()
for _, pod := range dswp.podManager.GetPods() { for _, pod := range dswp.podManager.GetPods() {
if dswp.podStateProvider.ShouldPodContainersBeTerminating(pod.UID) { if dswp.podStateProvider.ShouldPodContainersBeTerminating(pod.UID) {
// Do not (re)add volumes for pods that can't also be starting containers // Do not (re)add volumes for pods that can't also be starting containers
continue continue
} }
dswp.processPodVolumes(pod, mountedVolumesForPod, processedVolumesForFSResize) dswp.processPodVolumes(pod, mountedVolumesForPod)
} }
} }
@ -270,8 +269,7 @@ func (dswp *desiredStateOfWorldPopulator) findAndRemoveDeletedPods() {
// desired state of the world. // desired state of the world.
func (dswp *desiredStateOfWorldPopulator) processPodVolumes( func (dswp *desiredStateOfWorldPopulator) processPodVolumes(
pod *v1.Pod, pod *v1.Pod,
mountedVolumesForPod map[volumetypes.UniquePodName]map[string]cache.MountedVolume, mountedVolumesForPod map[volumetypes.UniquePodName]map[string]cache.MountedVolume) {
processedVolumesForFSResize sets.String) {
if pod == nil { if pod == nil {
return return
} }
@ -314,9 +312,7 @@ func (dswp *desiredStateOfWorldPopulator) processPodVolumes(
// sync reconstructed volume // sync reconstructed volume
dswp.actualStateOfWorld.SyncReconstructedVolume(uniqueVolumeName, uniquePodName, podVolume.Name) dswp.actualStateOfWorld.SyncReconstructedVolume(uniqueVolumeName, uniquePodName, podVolume.Name)
dswp.checkVolumeFSResize(pod, podVolume, pvc, volumeSpec, dswp.checkVolumeFSResize(pod, podVolume, pvc, volumeSpec, uniquePodName, mountedVolumesForPod)
uniquePodName, mountedVolumesForPod, processedVolumesForFSResize)
} }
// some of the volume additions may have failed, should not mark this pod as fully processed // some of the volume additions may have failed, should not mark this pod as fully processed
@ -350,8 +346,7 @@ func (dswp *desiredStateOfWorldPopulator) checkVolumeFSResize(
pvc *v1.PersistentVolumeClaim, pvc *v1.PersistentVolumeClaim,
volumeSpec *volume.Spec, volumeSpec *volume.Spec,
uniquePodName volumetypes.UniquePodName, uniquePodName volumetypes.UniquePodName,
mountedVolumesForPod map[volumetypes.UniquePodName]map[string]cache.MountedVolume, mountedVolumesForPod map[volumetypes.UniquePodName]map[string]cache.MountedVolume) {
processedVolumesForFSResize sets.String) {
if podVolume.PersistentVolumeClaim == nil { if podVolume.PersistentVolumeClaim == nil {
// Only PVC supports resize operation. // Only PVC supports resize operation.
return return
@ -363,11 +358,6 @@ func (dswp *desiredStateOfWorldPopulator) checkVolumeFSResize(
// or online resize in subsequent loop(after we confirm it has been mounted). // or online resize in subsequent loop(after we confirm it has been mounted).
return return
} }
if processedVolumesForFSResize.Has(string(uniqueVolumeName)) {
// File system resize operation is a global operation for volume,
// so we only need to check it once if more than one pod use it.
return
}
// volumeSpec.ReadOnly is the value that determines if volume could be formatted when being mounted. // volumeSpec.ReadOnly is the value that determines if volume could be formatted when being mounted.
// This is the same flag that determines filesystem resizing behaviour for offline resizing and hence // This is the same flag that determines filesystem resizing behaviour for offline resizing and hence
// we should use it here. This value comes from Pod.spec.volumes.persistentVolumeClaim.readOnly. // we should use it here. This value comes from Pod.spec.volumes.persistentVolumeClaim.readOnly.
@ -376,10 +366,12 @@ func (dswp *desiredStateOfWorldPopulator) checkVolumeFSResize(
klog.V(5).InfoS("Skip file system resize check for the volume, as the volume is mounted as readonly", "pod", klog.KObj(pod), "volumeName", podVolume.Name) klog.V(5).InfoS("Skip file system resize check for the volume, as the volume is mounted as readonly", "pod", klog.KObj(pod), "volumeName", podVolume.Name)
return return
} }
if volumeRequiresFSResize(pvc, volumeSpec.PersistentVolume) { pvCap := volumeSpec.PersistentVolume.Spec.Capacity.Storage()
dswp.actualStateOfWorld.MarkFSResizeRequired(uniqueVolumeName, uniquePodName) pvcStatusCap := pvc.Status.Capacity.Storage()
} dswp.desiredStateOfWorld.UpdatePersistentVolumeSize(uniqueVolumeName, pvCap)
processedVolumesForFSResize.Insert(string(uniqueVolumeName))
// in case the actualStateOfWorld was rebuild after kubelet restart ensure that claimSize is set to accurate value
dswp.actualStateOfWorld.SetVolumeClaimSize(uniqueVolumeName, pvcStatusCap)
} }
func getUniqueVolumeName( func getUniqueVolumeName(
@ -397,12 +389,6 @@ func getUniqueVolumeName(
return mountedVolume.VolumeName, true return mountedVolume.VolumeName, true
} }
func volumeRequiresFSResize(pvc *v1.PersistentVolumeClaim, pv *v1.PersistentVolume) bool {
capacity := pvc.Status.Capacity[v1.ResourceStorage]
requested := pv.Spec.Capacity[v1.ResourceStorage]
return requested.Cmp(capacity) > 0
}
// podPreviouslyProcessed returns true if the volumes for this pod have already // podPreviouslyProcessed returns true if the volumes for this pod have already
// been processed/reprocessed by the populator. Otherwise, the volumes for this pod need to // been processed/reprocessed by the populator. Otherwise, the volumes for this pod need to
// be reprocessed. // be reprocessed.

View File

@ -1127,7 +1127,6 @@ func verifyVolumeExistsInVolumesToMount(t *testing.T, expectedVolumeName v1.Uniq
expectReportedInUse, expectReportedInUse,
volume.ReportedInUse) volume.ReportedInUse)
} }
return return
} }
} }