Merge pull request #52634 from FengyunPan/improve-containerGC

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Improve codes which checks whether sandbox contains containers

Currently 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.

**Release note**:
```release-note
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-09-27 20:10:24 -07:00 committed by GitHub
commit 8be101ecb7
1 changed files with 8 additions and 9 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri" internalapi "k8s.io/kubernetes/pkg/kubelet/apis/cri"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime" runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
@ -258,6 +259,12 @@ func (cgc *containerGC) evictSandboxes(evictNonDeletedPods bool) error {
return err 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) sandboxes, err := cgc.manager.getKubeletSandboxes(true)
if err != nil { if err != nil {
return err return err
@ -277,15 +284,7 @@ func (cgc *containerGC) evictSandboxes(evictNonDeletedPods bool) error {
} }
// Set sandboxes that still have containers to be active. // Set sandboxes that still have containers to be active.
hasContainers := false if sandboxIDs.Has(sandbox.Id) {
sandboxID := sandbox.Id
for _, container := range containers {
if container.PodSandboxId == sandboxID {
hasContainers = true
break
}
}
if hasContainers {
sandboxInfo.active = true sandboxInfo.active = true
} }