diff --git a/pkg/kubelet/kuberuntime/kuberuntime_gc.go b/pkg/kubelet/kuberuntime/kuberuntime_gc.go index 063a89d6b3..35b750d5b1 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 }