Skip to content

Commit 96e13de

Browse files
authored
Merge pull request kubernetes#88980 from tedyu/evict-delay-sorting
Delay sorting of evictUnits slice in kuberuntime_gc
2 parents c096a37 + 78e2668 commit 96e13de

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

pkg/kubelet/kuberuntime/kuberuntime_gc.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func (cgc *containerGC) enforceMaxContainersPerEvictUnit(evictUnits containersBy
125125
func (cgc *containerGC) removeOldestN(containers []containerGCInfo, toRemove int) []containerGCInfo {
126126
// Remove from oldest to newest (last to first).
127127
numToKeep := len(containers) - toRemove
128+
if numToKeep > 0 {
129+
sort.Sort(byCreated(containers))
130+
}
128131
for i := len(containers) - 1; i >= numToKeep; i-- {
129132
if containers[i].unknown {
130133
// Containers in known state could be running, we should try
@@ -151,8 +154,11 @@ func (cgc *containerGC) removeOldestN(containers []containerGCInfo, toRemove int
151154
// removeOldestNSandboxes removes the oldest inactive toRemove sandboxes and
152155
// returns the resulting slice.
153156
func (cgc *containerGC) removeOldestNSandboxes(sandboxes []sandboxGCInfo, toRemove int) {
154-
// Remove from oldest to newest (last to first).
155157
numToKeep := len(sandboxes) - toRemove
158+
if numToKeep > 0 {
159+
sort.Sort(sandboxByCreated(sandboxes))
160+
}
161+
// Remove from oldest to newest (last to first).
156162
for i := len(sandboxes) - 1; i >= numToKeep; i-- {
157163
if !sandboxes[i].active {
158164
cgc.removeSandbox(sandboxes[i].id)
@@ -210,11 +216,6 @@ func (cgc *containerGC) evictableContainers(minAge time.Duration) (containersByE
210216
evictUnits[key] = append(evictUnits[key], containerInfo)
211217
}
212218

213-
// Sort the containers by age.
214-
for uid := range evictUnits {
215-
sort.Sort(byCreated(evictUnits[uid]))
216-
}
217-
218219
return evictUnits, nil
219220
}
220221

@@ -309,11 +310,6 @@ func (cgc *containerGC) evictSandboxes(evictTerminatedPods bool) error {
309310
sandboxesByPod[podUID] = append(sandboxesByPod[podUID], sandboxInfo)
310311
}
311312

312-
// Sort the sandboxes by age.
313-
for uid := range sandboxesByPod {
314-
sort.Sort(sandboxByCreated(sandboxesByPod[uid]))
315-
}
316-
317313
for podUID, sandboxes := range sandboxesByPod {
318314
if cgc.podStateProvider.IsPodDeleted(podUID) || (cgc.podStateProvider.IsPodTerminated(podUID) && evictTerminatedPods) {
319315
// Remove all evictable sandboxes if the pod has been removed.

0 commit comments

Comments
 (0)