mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
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.
This commit is contained in:
parent
1a44e26670
commit
bfc171ccaa
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user