From 78e26685391f19340c7b80b058c78b33b1118623 Mon Sep 17 00:00:00 2001 From: zyu Date: Mon, 9 Mar 2020 12:24:42 -0700 Subject: [PATCH] Delay sorting of evictUnits slice in kuberuntime_gc Signed-off-by: zyu --- pkg/kubelet/kuberuntime/kuberuntime_gc.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/pkg/kubelet/kuberuntime/kuberuntime_gc.go b/pkg/kubelet/kuberuntime/kuberuntime_gc.go index b139dba9272..3ccd756c1b1 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_gc.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_gc.go @@ -125,6 +125,9 @@ func (cgc *containerGC) enforceMaxContainersPerEvictUnit(evictUnits containersBy func (cgc *containerGC) removeOldestN(containers []containerGCInfo, toRemove int) []containerGCInfo { // Remove from oldest to newest (last to first). numToKeep := len(containers) - toRemove + if numToKeep > 0 { + sort.Sort(byCreated(containers)) + } for i := len(containers) - 1; i >= numToKeep; i-- { if containers[i].unknown { // Containers in known state could be running, we should try @@ -151,8 +154,11 @@ func (cgc *containerGC) removeOldestN(containers []containerGCInfo, toRemove int // removeOldestNSandboxes removes the oldest inactive toRemove sandboxes and // returns the resulting slice. func (cgc *containerGC) removeOldestNSandboxes(sandboxes []sandboxGCInfo, toRemove int) { - // Remove from oldest to newest (last to first). numToKeep := len(sandboxes) - toRemove + if numToKeep > 0 { + sort.Sort(sandboxByCreated(sandboxes)) + } + // Remove from oldest to newest (last to first). for i := len(sandboxes) - 1; i >= numToKeep; i-- { if !sandboxes[i].active { cgc.removeSandbox(sandboxes[i].id) @@ -210,11 +216,6 @@ func (cgc *containerGC) evictableContainers(minAge time.Duration) (containersByE evictUnits[key] = append(evictUnits[key], containerInfo) } - // Sort the containers by age. - for uid := range evictUnits { - sort.Sort(byCreated(evictUnits[uid])) - } - return evictUnits, nil } @@ -309,11 +310,6 @@ func (cgc *containerGC) evictSandboxes(evictTerminatedPods bool) error { sandboxesByPod[podUID] = append(sandboxesByPod[podUID], sandboxInfo) } - // Sort the sandboxes by age. - for uid := range sandboxesByPod { - sort.Sort(sandboxByCreated(sandboxesByPod[uid])) - } - for podUID, sandboxes := range sandboxesByPod { if cgc.podStateProvider.IsPodDeleted(podUID) || (cgc.podStateProvider.IsPodTerminated(podUID) && evictTerminatedPods) { // Remove all evictable sandboxes if the pod has been removed.