From bfc171ccaa194bc81083b0ec60fb1b8f1ae2c3a0 Mon Sep 17 00:00:00 2001 From: FengyunPan Date: Mon, 18 Sep 2017 14:25:57 +0800 Subject: [PATCH] Improve codes which checks whether sandbox contains containers Currently when evictSandboxes() checks whether sandbox contains containers, it traverses all the containers for every sandbox, but when cluster has many containres, it wastes a lot of time. It is better to use sets in this case. --- pkg/kubelet/kuberuntime/kuberuntime_gc.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/kubelet/kuberuntime/kuberuntime_gc.go b/pkg/kubelet/kuberuntime/kuberuntime_gc.go index 063a89d6b31..35b750d5b1a 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_gc.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_gc.go @@ -25,6 +25,7 @@ import ( "github.com/golang/glog" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/sets" internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri" runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" @@ -258,6 +259,12 @@ func (cgc *containerGC) evictSandboxes(evictNonDeletedPods bool) error { return err } + // collect all the PodSandboxId of container + sandboxIDs := sets.NewString() + for _, container := range containers { + sandboxIDs.Insert(container.PodSandboxId) + } + sandboxes, err := cgc.manager.getKubeletSandboxes(true) if err != nil { return err @@ -277,15 +284,7 @@ func (cgc *containerGC) evictSandboxes(evictNonDeletedPods bool) error { } // Set sandboxes that still have containers to be active. - hasContainers := false - sandboxID := sandbox.Id - for _, container := range containers { - if container.PodSandboxId == sandboxID { - hasContainers = true - break - } - } - if hasContainers { + if sandboxIDs.Has(sandbox.Id) { sandboxInfo.active = true }