1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-26 05:14:13 +01:00

fitsRequest check pod num

This commit is contained in:
sunxiaofei
2023-02-16 20:20:38 +08:00
parent 463158f7dc
commit b9ddbf12ed
2 changed files with 65 additions and 0 deletions

View File

@@ -222,6 +222,12 @@ func fitsRequest(nodeIndexer podutil.GetPodsAssignedToNodeFunc, pod *v1.Pod, nod
podFitsOnNode = false
}
}
// check pod num, at least one pod number is avaibalbe
if availableResources[v1.ResourcePods].MilliValue() <= 0 {
insufficientResources = append(insufficientResources, fmt.Errorf("insufficient %v", v1.ResourcePods))
podFitsOnNode = false
}
return podFitsOnNode, insufficientResources
}

View File

@@ -18,6 +18,7 @@ package node
import (
"context"
"errors"
"testing"
v1 "k8s.io/api/core/v1"
@@ -752,6 +753,64 @@ func TestPodFitsAnyOtherNode(t *testing.T) {
}
}
func TestNodeFit(t *testing.T) {
node := test.BuildTestNode("node", 64000, 128*1000*1000*1000, 2, nil)
tests := []struct {
description string
pod *v1.Pod
node *v1.Node
podsOnNode []*v1.Pod
err error
}{
{
description: "insufficient cpu",
pod: test.BuildTestPod("p1", 10000, 2*1000*1000*1000, "", nil),
node: node,
podsOnNode: []*v1.Pod{
test.BuildTestPod("p2", 60000, 60*1000*1000*1000, "node", nil),
},
err: errors.New("insufficient cpu"),
},
{
description: "insufficient pod num",
pod: test.BuildTestPod("p1", 1000, 2*1000*1000*1000, "", nil),
node: node,
podsOnNode: []*v1.Pod{
test.BuildTestPod("p2", 1000, 2*1000*1000*1000, "node", nil),
test.BuildTestPod("p3", 1000, 2*1000*1000*1000, "node", nil),
},
err: errors.New("insufficient pods"),
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
objs := []runtime.Object{tc.node, tc.pod}
for _, pod := range tc.podsOnNode {
objs = append(objs, pod)
}
fakeClient := fake.NewSimpleClientset(objs...)
sharedInformerFactory := informers.NewSharedInformerFactory(fakeClient, 0)
podInformer := sharedInformerFactory.Core().V1().Pods().Informer()
getPodsAssignedToNode, err := podutil.BuildGetPodsAssignedToNodeFunc(podInformer)
if err != nil {
t.Errorf("Build get pods assigned to node function error: %v", err)
}
sharedInformerFactory.Start(ctx.Done())
sharedInformerFactory.WaitForCacheSync(ctx.Done())
if errs := NodeFit(getPodsAssignedToNode, tc.pod, tc.node); (len(errs) == 0 && tc.err != nil) || errs[0].Error() != tc.err.Error() {
t.Errorf("Test %#v failed, got %v, expect %v", tc.description, errs, tc.err)
}
})
}
}
// createResourceList builds a small resource list of core resources
func createResourceList(cpu, memory, ephemeralStorage int64) v1.ResourceList {
resourceList := make(map[v1.ResourceName]resource.Quantity)