Commit 8be101ec authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

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 ```
parents acbfde39 bfc171cc
...@@ -25,6 +25,7 @@ import ( ...@@ -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 { ...@@ -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 { ...@@ -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
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment