@@ -125,6 +125,9 @@ func (cgc *containerGC) enforceMaxContainersPerEvictUnit(evictUnits containersBy
125
125
func (cgc * containerGC ) removeOldestN (containers []containerGCInfo , toRemove int ) []containerGCInfo {
126
126
// Remove from oldest to newest (last to first).
127
127
numToKeep := len (containers ) - toRemove
128
+ if numToKeep > 0 {
129
+ sort .Sort (byCreated (containers ))
130
+ }
128
131
for i := len (containers ) - 1 ; i >= numToKeep ; i -- {
129
132
if containers [i ].unknown {
130
133
// Containers in known state could be running, we should try
@@ -151,8 +154,11 @@ func (cgc *containerGC) removeOldestN(containers []containerGCInfo, toRemove int
151
154
// removeOldestNSandboxes removes the oldest inactive toRemove sandboxes and
152
155
// returns the resulting slice.
153
156
func (cgc * containerGC ) removeOldestNSandboxes (sandboxes []sandboxGCInfo , toRemove int ) {
154
- // Remove from oldest to newest (last to first).
155
157
numToKeep := len (sandboxes ) - toRemove
158
+ if numToKeep > 0 {
159
+ sort .Sort (sandboxByCreated (sandboxes ))
160
+ }
161
+ // Remove from oldest to newest (last to first).
156
162
for i := len (sandboxes ) - 1 ; i >= numToKeep ; i -- {
157
163
if ! sandboxes [i ].active {
158
164
cgc .removeSandbox (sandboxes [i ].id )
@@ -210,11 +216,6 @@ func (cgc *containerGC) evictableContainers(minAge time.Duration) (containersByE
210
216
evictUnits [key ] = append (evictUnits [key ], containerInfo )
211
217
}
212
218
213
- // Sort the containers by age.
214
- for uid := range evictUnits {
215
- sort .Sort (byCreated (evictUnits [uid ]))
216
- }
217
-
218
219
return evictUnits , nil
219
220
}
220
221
@@ -309,11 +310,6 @@ func (cgc *containerGC) evictSandboxes(evictTerminatedPods bool) error {
309
310
sandboxesByPod [podUID ] = append (sandboxesByPod [podUID ], sandboxInfo )
310
311
}
311
312
312
- // Sort the sandboxes by age.
313
- for uid := range sandboxesByPod {
314
- sort .Sort (sandboxByCreated (sandboxesByPod [uid ]))
315
- }
316
-
317
313
for podUID , sandboxes := range sandboxesByPod {
318
314
if cgc .podStateProvider .IsPodDeleted (podUID ) || (cgc .podStateProvider .IsPodTerminated (podUID ) && evictTerminatedPods ) {
319
315
// Remove all evictable sandboxes if the pod has been removed.
0 commit comments