mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 22:46:12 +00:00
Merge pull request #15429 from mikedanese/gc-fix
Auto commit by PR queue bot
This commit is contained in:
commit
ef33001d16
@ -42,9 +42,9 @@ const (
|
||||
|
||||
type GCController struct {
|
||||
kubeClient client.Interface
|
||||
podControl controller.PodControlInterface
|
||||
podStore cache.StoreToPodLister
|
||||
podStoreSyncer *framework.Controller
|
||||
deletePod func(namespace, name string) error
|
||||
threshold int
|
||||
}
|
||||
|
||||
@ -55,11 +55,10 @@ func New(kubeClient client.Interface, resyncPeriod controller.ResyncPeriodFunc,
|
||||
|
||||
gcc := &GCController{
|
||||
kubeClient: kubeClient,
|
||||
podControl: controller.RealPodControl{
|
||||
Recorder: eventBroadcaster.NewRecorder(api.EventSource{Component: "pod-garbage-collector"}),
|
||||
KubeClient: kubeClient,
|
||||
threshold: threshold,
|
||||
deletePod: func(namespace, name string) error {
|
||||
return kubeClient.Pods(namespace).Delete(name, api.NewDeleteOptions(0))
|
||||
},
|
||||
threshold: threshold,
|
||||
}
|
||||
|
||||
terminatedSelector := compileTerminatedPodSelector()
|
||||
@ -105,7 +104,7 @@ func (gcc *GCController) gc() {
|
||||
wait.Add(1)
|
||||
go func(namespace string, name string) {
|
||||
defer wait.Done()
|
||||
if err := gcc.podControl.DeletePod(namespace, name); err != nil {
|
||||
if err := gcc.deletePod(namespace, name); err != nil {
|
||||
// ignore not founds
|
||||
defer util.HandleError(err)
|
||||
}
|
||||
|
@ -25,41 +25,9 @@ import (
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
|
||||
"k8s.io/kubernetes/pkg/controller"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/util/sets"
|
||||
)
|
||||
|
||||
type FakePodControl struct {
|
||||
podSpec []api.PodTemplateSpec
|
||||
deletePodName []string
|
||||
lock sync.Mutex
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *FakePodControl) CreatePods(namespace string, spec *api.PodTemplateSpec, object runtime.Object) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
func (f *FakePodControl) CreatePodsOnNode(nodeName, namespace string, spec *api.PodTemplateSpec, object runtime.Object) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
func (f *FakePodControl) DeletePod(namespace string, podName string) error {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
if f.err != nil {
|
||||
return f.err
|
||||
}
|
||||
f.deletePodName = append(f.deletePodName, podName)
|
||||
return nil
|
||||
}
|
||||
func (f *FakePodControl) clear() {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
f.deletePodName = []string{}
|
||||
f.podSpec = []api.PodTemplateSpec{}
|
||||
}
|
||||
|
||||
func TestGC(t *testing.T) {
|
||||
type nameToPhase struct {
|
||||
name string
|
||||
@ -100,8 +68,14 @@ func TestGC(t *testing.T) {
|
||||
for i, test := range testCases {
|
||||
client := testclient.NewSimpleFake()
|
||||
gcc := New(client, controller.NoResyncPeriodFunc, test.threshold)
|
||||
fake := &FakePodControl{}
|
||||
gcc.podControl = fake
|
||||
deletedPodNames := make([]string, 0)
|
||||
var lock sync.Mutex
|
||||
gcc.deletePod = func(_, name string) error {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
deletedPodNames = append(deletedPodNames, name)
|
||||
return nil
|
||||
}
|
||||
|
||||
creationTime := time.Unix(0, 0)
|
||||
for _, pod := range test.pods {
|
||||
@ -115,16 +89,16 @@ func TestGC(t *testing.T) {
|
||||
gcc.gc()
|
||||
|
||||
pass := true
|
||||
for _, pod := range fake.deletePodName {
|
||||
for _, pod := range deletedPodNames {
|
||||
if !test.deletedPodNames.Has(pod) {
|
||||
pass = false
|
||||
}
|
||||
}
|
||||
if len(fake.deletePodName) != len(test.deletedPodNames) {
|
||||
if len(deletedPodNames) != len(test.deletedPodNames) {
|
||||
pass = false
|
||||
}
|
||||
if !pass {
|
||||
t.Errorf("[%v]pod's deleted expected and actual did not match.\n\texpected: %v\n\tactual: %v", i, test.deletedPodNames, fake.deletePodName)
|
||||
t.Errorf("[%v]pod's deleted expected and actual did not match.\n\texpected: %v\n\tactual: %v", i, test.deletedPodNames, deletedPodNames)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user