1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package podcapacityofnode
import (
"context"
"fmt"
"testing"
"time"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/informers"
clientsetfake "k8s.io/client-go/kubernetes/fake"
"k8s.io/kubernetes/pkg/scheduler/framework"
fakeframework "k8s.io/kubernetes/pkg/scheduler/framework/fake"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/defaultbinder"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/queuesort"
frameworkruntime "k8s.io/kubernetes/pkg/scheduler/framework/runtime"
st "k8s.io/kubernetes/pkg/scheduler/testing"
testutil "sigs.k8s.io/scheduler-plugins/test/util"
"k8s.io/apimachinery/pkg/api/resource"
)
func TestPodCapacityOfNode(t *testing.T) {
tests := []struct {
nodeInfos []*framework.NodeInfo
wantErr string
expectedList framework.NodeScoreList
name string
}{
{
nodeInfos: []*framework.NodeInfo{makeNodeInfo("node1", 0, 0, 110, 110), makeNodeInfo("node2", 3, 0, 10, 110), makeNodeInfo("node3", 0, 0, 0, 110)},
expectedList: []framework.NodeScore{{Name: "node1", Score: framework.MinNodeScore}, {Name: "node2", Score: 88}, {Name: "node3", Score: framework.MaxNodeScore}},
name: "pod capacity of node score.",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cs := clientsetfake.NewSimpleClientset()
informerFactory := informers.NewSharedInformerFactory(cs, 0)
registeredPlugins := []st.RegisterPluginFunc{
st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
st.RegisterPluginAsExtensions(Name, New, "Score"),
}
fakeSharedLister := &fakeSharedLister{nodes: test.nodeInfos}
fh, err := st.NewFramework(
registeredPlugins,
frameworkruntime.WithClientSet(cs),
frameworkruntime.WithInformerFactory(informerFactory),
frameworkruntime.WithSnapshotSharedLister(fakeSharedLister),
frameworkruntime.WithPodNominator(testutil.NewPodNominator()),
)
if err != nil {
t.Fatalf("fail to create framework: %s", err)
}
// initialize nominated pod by adding nominated pods into nominatedPodMap
for _, n := range test.nodeInfos {
for _, pi := range n.Pods {
if pi.Pod.Status.NominatedNodeName != "" {
addNominatedPod(pi.Pod, n.Node().Name, fh.PreemptHandle())
}
}
}
pe, _ := New(nil, fh)
var gotList framework.NodeScoreList
plugin := pe.(framework.ScorePlugin)
for i, n := range test.nodeInfos {
score, err := plugin.Score(context.Background(), nil, nil, n.Node().Name)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
gotList = append(gotList, framework.NodeScore{Name: test.nodeInfos[i].Node().Name, Score: score})
}
status := plugin.ScoreExtensions().NormalizeScore(context.Background(), nil, nil, gotList)
if !status.IsSuccess() {
t.Errorf("unexpected error: %v", status)
}
for i := range gotList {
if test.expectedList[i].Score != gotList[i].Score {
t.Errorf("expected %#v, got %#v", test.expectedList[i].Score, gotList[i].Score)
}
}
})
}
}
func makeNodeInfo(node string, terminatingPodNumber, nominatedPodNumber, regularPodNumber, pods int) *framework.NodeInfo {
ni := framework.NewNodeInfo()
for i := 0; i < terminatingPodNumber; i++ {
podInfo := &framework.PodInfo{
Pod: makeTerminatingPod(fmt.Sprintf("tpod_%s_%v", node, i+1)),
}
ni.Pods = append(ni.Pods, podInfo)
}
for i := 0; i < nominatedPodNumber; i++ {
podInfo := &framework.PodInfo{
Pod: makeNominatedPod(fmt.Sprintf("npod_%s_%v", node, i+1), node),
}
ni.Pods = append(ni.Pods, podInfo)
}
for i := 0; i < regularPodNumber; i++ {
podInfo := &framework.PodInfo{
Pod: makeRegularPod(fmt.Sprintf("rpod_%s_%v", node, i+1)),
}
ni.Pods = append(ni.Pods, podInfo)
}
ni.SetNode(&v1.Node{
ObjectMeta: metav1.ObjectMeta{Name: node},
Status: v1.NodeStatus{
Capacity: v1.ResourceList{
v1.ResourcePods: *resource.NewQuantity(int64(pods), resource.DecimalExponent),
},
},
})
return ni
}
func makeTerminatingPod(name string) *v1.Pod {
deletionTimestamp := metav1.Time{Time: time.Now()}
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
DeletionTimestamp: &deletionTimestamp,
},
}
}
func makeNominatedPod(podName string, nodeName string) *v1.Pod {
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
UID: types.UID(podName),
},
Status: v1.PodStatus{
NominatedNodeName: nodeName,
},
}
}
func makeRegularPod(name string) *v1.Pod {
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}
func addNominatedPod(pod *v1.Pod, nodeName string, ph framework.PreemptHandle) *v1.Pod {
ph.AddNominatedPod(pod, nodeName)
return pod
}
var _ framework.SharedLister = &fakeSharedLister{}
type fakeSharedLister struct {
nodes []*framework.NodeInfo
}
func (f *fakeSharedLister) NodeInfos() framework.NodeInfoLister {
return fakeframework.NodeInfoLister(f.nodes)
}
|