Skip to content

Commit 83a1310

Browse files
authored
Merge pull request kubernetes#126575 from Lucaber/volume-attach-memory-allocations
Reduce memory usage/allocations during wait for volume attachment
2 parents 7478a30 + 90906ab commit 83a1310

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

pkg/kubelet/volumemanager/cache/actual_state_of_world.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ type ActualStateOfWorld interface {
120120
// and false is returned.
121121
PodRemovedFromVolume(podName volumetypes.UniquePodName, volumeName v1.UniqueVolumeName) bool
122122

123+
// PodHasMountedVolumes returns true if any volume is mounted on the given pod
124+
PodHasMountedVolumes(podName volumetypes.UniquePodName) bool
125+
123126
// VolumeExistsWithSpecName returns true if the given volume specified with the
124127
// volume spec name (a.k.a., InnerVolumeSpecName) exists in the list of
125128
// volumes that should be attached to this node.
@@ -146,6 +149,10 @@ type ActualStateOfWorld interface {
146149
// current actual state of the world.
147150
GetMountedVolumesForPod(podName volumetypes.UniquePodName) []MountedVolume
148151

152+
// GetMountedVolumeForPodByOuterVolumeSpecName returns the volume and true if
153+
// the given outerVolumeSpecName is mounted on the given pod.
154+
GetMountedVolumeForPodByOuterVolumeSpecName(podName volumetypes.UniquePodName, outerVolumeSpecName string) (MountedVolume, bool)
155+
149156
// GetPossiblyMountedVolumesForPod generates and returns a list of volumes for
150157
// the specified pod that either are attached and mounted or are "uncertain",
151158
// i.e. a volume plugin may be mounting the volume right now.
@@ -948,6 +955,20 @@ func (asw *actualStateOfWorld) PodExistsInVolume(podName volumetypes.UniquePodNa
948955
return podExists, volumeObj.devicePath, nil
949956
}
950957

958+
func (asw *actualStateOfWorld) PodHasMountedVolumes(podName volumetypes.UniquePodName) bool {
959+
asw.RLock()
960+
defer asw.RUnlock()
961+
for _, volumeObj := range asw.attachedVolumes {
962+
if podObj, hasPod := volumeObj.mountedPods[podName]; hasPod {
963+
if podObj.volumeMountStateForPod == operationexecutor.VolumeMounted {
964+
return true
965+
}
966+
}
967+
}
968+
969+
return false
970+
}
971+
951972
func (asw *actualStateOfWorld) volumeNeedsExpansion(volumeObj attachedVolume, desiredVolumeSize resource.Quantity) (resource.Quantity, bool) {
952973
currentSize := resource.Quantity{}
953974
if volumeObj.persistentVolumeSize != nil {
@@ -1063,7 +1084,7 @@ func (asw *actualStateOfWorld) GetMountedVolumesForPod(
10631084
podName volumetypes.UniquePodName) []MountedVolume {
10641085
asw.RLock()
10651086
defer asw.RUnlock()
1066-
mountedVolume := make([]MountedVolume, 0 /* len */, len(asw.attachedVolumes) /* cap */)
1087+
mountedVolume := make([]MountedVolume, 0 /* len */)
10671088
for _, volumeObj := range asw.attachedVolumes {
10681089
for mountedPodName, podObj := range volumeObj.mountedPods {
10691090
if mountedPodName == podName && podObj.volumeMountStateForPod == operationexecutor.VolumeMounted {
@@ -1077,6 +1098,21 @@ func (asw *actualStateOfWorld) GetMountedVolumesForPod(
10771098
return mountedVolume
10781099
}
10791100

1101+
func (asw *actualStateOfWorld) GetMountedVolumeForPodByOuterVolumeSpecName(
1102+
podName volumetypes.UniquePodName, outerVolumeSpecName string) (MountedVolume, bool) {
1103+
asw.RLock()
1104+
defer asw.RUnlock()
1105+
for _, volumeObj := range asw.attachedVolumes {
1106+
if podObj, hasPod := volumeObj.mountedPods[podName]; hasPod {
1107+
if podObj.volumeMountStateForPod == operationexecutor.VolumeMounted && podObj.outerVolumeSpecName == outerVolumeSpecName {
1108+
return getMountedVolume(&podObj, &volumeObj), true
1109+
}
1110+
}
1111+
}
1112+
1113+
return MountedVolume{}, false
1114+
}
1115+
10801116
func (asw *actualStateOfWorld) GetPossiblyMountedVolumesForPod(
10811117
podName volumetypes.UniquePodName) []MountedVolume {
10821118
asw.RLock()

pkg/kubelet/volumemanager/volume_manager.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,13 @@ func (vm *volumeManager) verifyVolumesMountedFunc(podName types.UniquePodName, e
540540
if errs := vm.desiredStateOfWorld.PopPodErrors(podName); len(errs) > 0 {
541541
return true, errors.New(strings.Join(errs, "; "))
542542
}
543-
return len(vm.getUnmountedVolumes(podName, expectedVolumes)) == 0, nil
543+
for _, expectedVolume := range expectedVolumes {
544+
_, found := vm.actualStateOfWorld.GetMountedVolumeForPodByOuterVolumeSpecName(podName, expectedVolume)
545+
if !found {
546+
return false, nil
547+
}
548+
}
549+
return true, nil
544550
}
545551
}
546552

@@ -551,7 +557,7 @@ func (vm *volumeManager) verifyVolumesUnmountedFunc(podName types.UniquePodName)
551557
if errs := vm.desiredStateOfWorld.PopPodErrors(podName); len(errs) > 0 {
552558
return true, errors.New(strings.Join(errs, "; "))
553559
}
554-
return len(vm.actualStateOfWorld.GetMountedVolumesForPod(podName)) == 0, nil
560+
return !vm.actualStateOfWorld.PodHasMountedVolumes(podName), nil
555561
}
556562
}
557563

0 commit comments

Comments
 (0)