From b5f44d5aab9ae4b45c9235fc23d5dcdf0d67f684 Mon Sep 17 00:00:00 2001 From: mengyang02 Date: Thu, 5 Sep 2019 11:23:42 +0800 Subject: [PATCH] optimize gc_controller.gcTerminated --- pkg/controller/podgc/gc_controller.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/controller/podgc/gc_controller.go b/pkg/controller/podgc/gc_controller.go index 14b08eca46c..3495192b33f 100644 --- a/pkg/controller/podgc/gc_controller.go +++ b/pkg/controller/podgc/gc_controller.go @@ -21,7 +21,7 @@ import ( "sync" "time" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -113,8 +113,6 @@ func (gcc *PodGCController) gcTerminated(pods []*v1.Pod) { } terminatedPodCount := len(terminatedPods) - sort.Sort(byCreationTimestamp(terminatedPods)) - deleteCount := terminatedPodCount - gcc.terminatedPodThreshold if deleteCount > terminatedPodCount { @@ -122,8 +120,12 @@ func (gcc *PodGCController) gcTerminated(pods []*v1.Pod) { } if deleteCount > 0 { klog.Infof("garbage collecting %v pods", deleteCount) + } else { + return } + // sort only when necessary + sort.Sort(byCreationTimestamp(terminatedPods)) var wait sync.WaitGroup for i := 0; i < deleteCount; i++ { wait.Add(1)