generic_scheduler_test.go 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package core
  14. import (
  15. "context"
  16. "fmt"
  17. "math"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "sync/atomic"
  22. "testing"
  23. "time"
  24. v1 "k8s.io/api/core/v1"
  25. policy "k8s.io/api/policy/v1beta1"
  26. "k8s.io/apimachinery/pkg/api/resource"
  27. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  28. "k8s.io/apimachinery/pkg/runtime"
  29. "k8s.io/apimachinery/pkg/types"
  30. "k8s.io/apimachinery/pkg/util/sets"
  31. "k8s.io/apimachinery/pkg/util/wait"
  32. "k8s.io/client-go/informers"
  33. clientsetfake "k8s.io/client-go/kubernetes/fake"
  34. schedulerapi "k8s.io/kubernetes/pkg/scheduler/apis/config"
  35. extenderv1 "k8s.io/kubernetes/pkg/scheduler/apis/extender/v1"
  36. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/defaultbinder"
  37. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/defaultpodtopologyspread"
  38. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/interpodaffinity"
  39. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodeaffinity"
  40. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodelabel"
  41. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodename"
  42. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources"
  43. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodeunschedulable"
  44. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/podtopologyspread"
  45. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/queuesort"
  46. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/tainttoleration"
  47. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumebinding"
  48. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumerestrictions"
  49. "k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumezone"
  50. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  51. internalcache "k8s.io/kubernetes/pkg/scheduler/internal/cache"
  52. internalqueue "k8s.io/kubernetes/pkg/scheduler/internal/queue"
  53. fakelisters "k8s.io/kubernetes/pkg/scheduler/listers/fake"
  54. "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  55. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  56. st "k8s.io/kubernetes/pkg/scheduler/testing"
  57. schedutil "k8s.io/kubernetes/pkg/scheduler/util"
  58. )
  59. var (
  60. errPrioritize = fmt.Errorf("priority map encounters an error")
  61. )
  62. const ErrReasonFake = "Nodes failed the fake predicate"
  63. type trueFilterPlugin struct{}
  64. // Name returns name of the plugin.
  65. func (pl *trueFilterPlugin) Name() string {
  66. return "TrueFilter"
  67. }
  68. // Filter invoked at the filter extension point.
  69. func (pl *trueFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  70. return nil
  71. }
  72. // NewTrueFilterPlugin initializes a trueFilterPlugin and returns it.
  73. func NewTrueFilterPlugin(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  74. return &trueFilterPlugin{}, nil
  75. }
  76. type falseFilterPlugin struct{}
  77. // Name returns name of the plugin.
  78. func (pl *falseFilterPlugin) Name() string {
  79. return "FalseFilter"
  80. }
  81. // Filter invoked at the filter extension point.
  82. func (pl *falseFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  83. return framework.NewStatus(framework.Unschedulable, ErrReasonFake)
  84. }
  85. // NewFalseFilterPlugin initializes a falseFilterPlugin and returns it.
  86. func NewFalseFilterPlugin(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  87. return &falseFilterPlugin{}, nil
  88. }
  89. type matchFilterPlugin struct{}
  90. // Name returns name of the plugin.
  91. func (pl *matchFilterPlugin) Name() string {
  92. return "MatchFilter"
  93. }
  94. // Filter invoked at the filter extension point.
  95. func (pl *matchFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  96. node := nodeInfo.Node()
  97. if node == nil {
  98. return framework.NewStatus(framework.Error, "node not found")
  99. }
  100. if pod.Name == node.Name {
  101. return nil
  102. }
  103. return framework.NewStatus(framework.Unschedulable, ErrReasonFake)
  104. }
  105. // NewMatchFilterPlugin initializes a matchFilterPlugin and returns it.
  106. func NewMatchFilterPlugin(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  107. return &matchFilterPlugin{}, nil
  108. }
  109. type noPodsFilterPlugin struct{}
  110. // Name returns name of the plugin.
  111. func (pl *noPodsFilterPlugin) Name() string {
  112. return "NoPodsFilter"
  113. }
  114. // Filter invoked at the filter extension point.
  115. func (pl *noPodsFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  116. if len(nodeInfo.Pods()) == 0 {
  117. return nil
  118. }
  119. return framework.NewStatus(framework.Unschedulable, ErrReasonFake)
  120. }
  121. // NewNoPodsFilterPlugin initializes a noPodsFilterPlugin and returns it.
  122. func NewNoPodsFilterPlugin(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  123. return &noPodsFilterPlugin{}, nil
  124. }
  125. // fakeFilterPlugin is a test filter plugin to record how many times its Filter() function have
  126. // been called, and it returns different 'Code' depending on its internal 'failedNodeReturnCodeMap'.
  127. type fakeFilterPlugin struct {
  128. numFilterCalled int32
  129. failedNodeReturnCodeMap map[string]framework.Code
  130. }
  131. // Name returns name of the plugin.
  132. func (pl *fakeFilterPlugin) Name() string {
  133. return "FakeFilter"
  134. }
  135. // Filter invoked at the filter extension point.
  136. func (pl *fakeFilterPlugin) Filter(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
  137. atomic.AddInt32(&pl.numFilterCalled, 1)
  138. if returnCode, ok := pl.failedNodeReturnCodeMap[nodeInfo.Node().Name]; ok {
  139. return framework.NewStatus(returnCode, fmt.Sprintf("injecting failure for pod %v", pod.Name))
  140. }
  141. return nil
  142. }
  143. // NewFakeFilterPlugin initializes a fakeFilterPlugin and returns it.
  144. func NewFakeFilterPlugin(failedNodeReturnCodeMap map[string]framework.Code) framework.PluginFactory {
  145. return func(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  146. return &fakeFilterPlugin{
  147. failedNodeReturnCodeMap: failedNodeReturnCodeMap,
  148. }, nil
  149. }
  150. }
  151. type numericMapPlugin struct{}
  152. func newNumericMapPlugin() framework.PluginFactory {
  153. return func(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  154. return &numericMapPlugin{}, nil
  155. }
  156. }
  157. func (pl *numericMapPlugin) Name() string {
  158. return "NumericMap"
  159. }
  160. func (pl *numericMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeName string) (int64, *framework.Status) {
  161. score, err := strconv.Atoi(nodeName)
  162. if err != nil {
  163. return 0, framework.NewStatus(framework.Error, fmt.Sprintf("Error converting nodename to int: %+v", nodeName))
  164. }
  165. return int64(score), nil
  166. }
  167. func (pl *numericMapPlugin) ScoreExtensions() framework.ScoreExtensions {
  168. return nil
  169. }
  170. type reverseNumericMapPlugin struct{}
  171. func newReverseNumericMapPlugin() framework.PluginFactory {
  172. return func(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  173. return &reverseNumericMapPlugin{}, nil
  174. }
  175. }
  176. func (pl *reverseNumericMapPlugin) Name() string {
  177. return "ReverseNumericMap"
  178. }
  179. func (pl *reverseNumericMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeName string) (int64, *framework.Status) {
  180. score, err := strconv.Atoi(nodeName)
  181. if err != nil {
  182. return 0, framework.NewStatus(framework.Error, fmt.Sprintf("Error converting nodename to int: %+v", nodeName))
  183. }
  184. return int64(score), nil
  185. }
  186. func (pl *reverseNumericMapPlugin) ScoreExtensions() framework.ScoreExtensions {
  187. return pl
  188. }
  189. func (pl *reverseNumericMapPlugin) NormalizeScore(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeScores framework.NodeScoreList) *framework.Status {
  190. var maxScore float64
  191. minScore := math.MaxFloat64
  192. for _, hostPriority := range nodeScores {
  193. maxScore = math.Max(maxScore, float64(hostPriority.Score))
  194. minScore = math.Min(minScore, float64(hostPriority.Score))
  195. }
  196. for i, hostPriority := range nodeScores {
  197. nodeScores[i] = framework.NodeScore{
  198. Name: hostPriority.Name,
  199. Score: int64(maxScore + minScore - float64(hostPriority.Score)),
  200. }
  201. }
  202. return nil
  203. }
  204. type trueMapPlugin struct{}
  205. func newTrueMapPlugin() framework.PluginFactory {
  206. return func(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  207. return &trueMapPlugin{}, nil
  208. }
  209. }
  210. func (pl *trueMapPlugin) Name() string {
  211. return "TrueMap"
  212. }
  213. func (pl *trueMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) (int64, *framework.Status) {
  214. return 1, nil
  215. }
  216. func (pl *trueMapPlugin) ScoreExtensions() framework.ScoreExtensions {
  217. return pl
  218. }
  219. func (pl *trueMapPlugin) NormalizeScore(_ context.Context, _ *framework.CycleState, _ *v1.Pod, nodeScores framework.NodeScoreList) *framework.Status {
  220. for _, host := range nodeScores {
  221. if host.Name == "" {
  222. return framework.NewStatus(framework.Error, "unexpected empty host name")
  223. }
  224. }
  225. return nil
  226. }
  227. type falseMapPlugin struct{}
  228. func newFalseMapPlugin() framework.PluginFactory {
  229. return func(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
  230. return &falseMapPlugin{}, nil
  231. }
  232. }
  233. func (pl *falseMapPlugin) Name() string {
  234. return "FalseMap"
  235. }
  236. func (pl *falseMapPlugin) Score(_ context.Context, _ *framework.CycleState, _ *v1.Pod, _ string) (int64, *framework.Status) {
  237. return 0, framework.NewStatus(framework.Error, errPrioritize.Error())
  238. }
  239. func (pl *falseMapPlugin) ScoreExtensions() framework.ScoreExtensions {
  240. return nil
  241. }
  242. var emptySnapshot = internalcache.NewEmptySnapshot()
  243. func makeNodeList(nodeNames []string) []*v1.Node {
  244. result := make([]*v1.Node, 0, len(nodeNames))
  245. for _, nodeName := range nodeNames {
  246. result = append(result, &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}})
  247. }
  248. return result
  249. }
  250. func TestSelectHost(t *testing.T) {
  251. scheduler := genericScheduler{}
  252. tests := []struct {
  253. name string
  254. list framework.NodeScoreList
  255. possibleHosts sets.String
  256. expectsErr bool
  257. }{
  258. {
  259. name: "unique properly ordered scores",
  260. list: []framework.NodeScore{
  261. {Name: "machine1.1", Score: 1},
  262. {Name: "machine2.1", Score: 2},
  263. },
  264. possibleHosts: sets.NewString("machine2.1"),
  265. expectsErr: false,
  266. },
  267. {
  268. name: "equal scores",
  269. list: []framework.NodeScore{
  270. {Name: "machine1.1", Score: 1},
  271. {Name: "machine1.2", Score: 2},
  272. {Name: "machine1.3", Score: 2},
  273. {Name: "machine2.1", Score: 2},
  274. },
  275. possibleHosts: sets.NewString("machine1.2", "machine1.3", "machine2.1"),
  276. expectsErr: false,
  277. },
  278. {
  279. name: "out of order scores",
  280. list: []framework.NodeScore{
  281. {Name: "machine1.1", Score: 3},
  282. {Name: "machine1.2", Score: 3},
  283. {Name: "machine2.1", Score: 2},
  284. {Name: "machine3.1", Score: 1},
  285. {Name: "machine1.3", Score: 3},
  286. },
  287. possibleHosts: sets.NewString("machine1.1", "machine1.2", "machine1.3"),
  288. expectsErr: false,
  289. },
  290. {
  291. name: "empty priority list",
  292. list: []framework.NodeScore{},
  293. possibleHosts: sets.NewString(),
  294. expectsErr: true,
  295. },
  296. }
  297. for _, test := range tests {
  298. t.Run(test.name, func(t *testing.T) {
  299. // increase the randomness
  300. for i := 0; i < 10; i++ {
  301. got, err := scheduler.selectHost(test.list)
  302. if test.expectsErr {
  303. if err == nil {
  304. t.Error("Unexpected non-error")
  305. }
  306. } else {
  307. if err != nil {
  308. t.Errorf("Unexpected error: %v", err)
  309. }
  310. if !test.possibleHosts.Has(got) {
  311. t.Errorf("got %s is not in the possible map %v", got, test.possibleHosts)
  312. }
  313. }
  314. }
  315. })
  316. }
  317. }
  318. func TestGenericScheduler(t *testing.T) {
  319. tests := []struct {
  320. name string
  321. registerPlugins []st.RegisterPluginFunc
  322. nodes []string
  323. pvcs []v1.PersistentVolumeClaim
  324. pod *v1.Pod
  325. pods []*v1.Pod
  326. expectedHosts sets.String
  327. wErr error
  328. }{
  329. {
  330. registerPlugins: []st.RegisterPluginFunc{
  331. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  332. st.RegisterFilterPlugin("FalseFilter", NewFalseFilterPlugin),
  333. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  334. },
  335. nodes: []string{"machine1", "machine2"},
  336. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")}},
  337. name: "test 1",
  338. wErr: &FitError{
  339. Pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")}},
  340. NumAllNodes: 2,
  341. FilteredNodesStatuses: framework.NodeToStatusMap{
  342. "machine1": framework.NewStatus(framework.Unschedulable, ErrReasonFake),
  343. "machine2": framework.NewStatus(framework.Unschedulable, ErrReasonFake),
  344. },
  345. },
  346. },
  347. {
  348. registerPlugins: []st.RegisterPluginFunc{
  349. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  350. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  351. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  352. },
  353. nodes: []string{"machine1", "machine2"},
  354. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "ignore", UID: types.UID("ignore")}},
  355. expectedHosts: sets.NewString("machine1", "machine2"),
  356. name: "test 2",
  357. wErr: nil,
  358. },
  359. {
  360. // Fits on a machine where the pod ID matches the machine name
  361. registerPlugins: []st.RegisterPluginFunc{
  362. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  363. st.RegisterFilterPlugin("MatchFilter", NewMatchFilterPlugin),
  364. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  365. },
  366. nodes: []string{"machine1", "machine2"},
  367. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine2", UID: types.UID("machine2")}},
  368. expectedHosts: sets.NewString("machine2"),
  369. name: "test 3",
  370. wErr: nil,
  371. },
  372. {
  373. registerPlugins: []st.RegisterPluginFunc{
  374. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  375. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  376. st.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1),
  377. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  378. },
  379. nodes: []string{"3", "2", "1"},
  380. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "ignore", UID: types.UID("ignore")}},
  381. expectedHosts: sets.NewString("3"),
  382. name: "test 4",
  383. wErr: nil,
  384. },
  385. {
  386. registerPlugins: []st.RegisterPluginFunc{
  387. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  388. st.RegisterFilterPlugin("MatchFilter", NewMatchFilterPlugin),
  389. st.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1),
  390. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  391. },
  392. nodes: []string{"3", "2", "1"},
  393. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")}},
  394. expectedHosts: sets.NewString("2"),
  395. name: "test 5",
  396. wErr: nil,
  397. },
  398. {
  399. registerPlugins: []st.RegisterPluginFunc{
  400. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  401. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  402. st.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1),
  403. st.RegisterScorePlugin("ReverseNumericMap", newReverseNumericMapPlugin(), 2),
  404. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  405. },
  406. nodes: []string{"3", "2", "1"},
  407. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")}},
  408. expectedHosts: sets.NewString("1"),
  409. name: "test 6",
  410. wErr: nil,
  411. },
  412. {
  413. registerPlugins: []st.RegisterPluginFunc{
  414. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  415. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  416. st.RegisterFilterPlugin("FalseFilter", NewFalseFilterPlugin),
  417. st.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1),
  418. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  419. },
  420. nodes: []string{"3", "2", "1"},
  421. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")}},
  422. name: "test 7",
  423. wErr: &FitError{
  424. Pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")}},
  425. NumAllNodes: 3,
  426. FilteredNodesStatuses: framework.NodeToStatusMap{
  427. "3": framework.NewStatus(framework.Unschedulable, ErrReasonFake),
  428. "2": framework.NewStatus(framework.Unschedulable, ErrReasonFake),
  429. "1": framework.NewStatus(framework.Unschedulable, ErrReasonFake),
  430. },
  431. },
  432. },
  433. {
  434. registerPlugins: []st.RegisterPluginFunc{
  435. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  436. st.RegisterFilterPlugin("NoPodsFilter", NewNoPodsFilterPlugin),
  437. st.RegisterFilterPlugin("MatchFilter", NewMatchFilterPlugin),
  438. st.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1),
  439. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  440. },
  441. pods: []*v1.Pod{
  442. {
  443. ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")},
  444. Spec: v1.PodSpec{
  445. NodeName: "2",
  446. },
  447. Status: v1.PodStatus{
  448. Phase: v1.PodRunning,
  449. },
  450. },
  451. },
  452. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")}},
  453. nodes: []string{"1", "2"},
  454. name: "test 8",
  455. wErr: &FitError{
  456. Pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2", UID: types.UID("2")}},
  457. NumAllNodes: 2,
  458. FilteredNodesStatuses: framework.NodeToStatusMap{
  459. "1": framework.NewStatus(framework.Unschedulable, ErrReasonFake),
  460. "2": framework.NewStatus(framework.Unschedulable, ErrReasonFake),
  461. },
  462. },
  463. },
  464. {
  465. // Pod with existing PVC
  466. registerPlugins: []st.RegisterPluginFunc{
  467. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  468. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  469. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  470. },
  471. nodes: []string{"machine1", "machine2"},
  472. pvcs: []v1.PersistentVolumeClaim{{ObjectMeta: metav1.ObjectMeta{Name: "existingPVC"}}},
  473. pod: &v1.Pod{
  474. ObjectMeta: metav1.ObjectMeta{Name: "ignore", UID: types.UID("ignore")},
  475. Spec: v1.PodSpec{
  476. Volumes: []v1.Volume{
  477. {
  478. VolumeSource: v1.VolumeSource{
  479. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  480. ClaimName: "existingPVC",
  481. },
  482. },
  483. },
  484. },
  485. },
  486. },
  487. expectedHosts: sets.NewString("machine1", "machine2"),
  488. name: "existing PVC",
  489. wErr: nil,
  490. },
  491. {
  492. // Pod with non existing PVC
  493. registerPlugins: []st.RegisterPluginFunc{
  494. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  495. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  496. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  497. },
  498. nodes: []string{"machine1", "machine2"},
  499. pod: &v1.Pod{
  500. ObjectMeta: metav1.ObjectMeta{Name: "ignore", UID: types.UID("ignore")},
  501. Spec: v1.PodSpec{
  502. Volumes: []v1.Volume{
  503. {
  504. VolumeSource: v1.VolumeSource{
  505. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  506. ClaimName: "unknownPVC",
  507. },
  508. },
  509. },
  510. },
  511. },
  512. },
  513. name: "unknown PVC",
  514. wErr: fmt.Errorf("persistentvolumeclaim \"unknownPVC\" not found"),
  515. },
  516. {
  517. // Pod with deleting PVC
  518. registerPlugins: []st.RegisterPluginFunc{
  519. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  520. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  521. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  522. },
  523. nodes: []string{"machine1", "machine2"},
  524. pvcs: []v1.PersistentVolumeClaim{{ObjectMeta: metav1.ObjectMeta{Name: "existingPVC", DeletionTimestamp: &metav1.Time{}}}},
  525. pod: &v1.Pod{
  526. ObjectMeta: metav1.ObjectMeta{Name: "ignore", UID: types.UID("ignore")},
  527. Spec: v1.PodSpec{
  528. Volumes: []v1.Volume{
  529. {
  530. VolumeSource: v1.VolumeSource{
  531. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  532. ClaimName: "existingPVC",
  533. },
  534. },
  535. },
  536. },
  537. },
  538. },
  539. name: "deleted PVC",
  540. wErr: fmt.Errorf("persistentvolumeclaim \"existingPVC\" is being deleted"),
  541. },
  542. {
  543. registerPlugins: []st.RegisterPluginFunc{
  544. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  545. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  546. st.RegisterScorePlugin("FalseMap", newFalseMapPlugin(), 1),
  547. st.RegisterScorePlugin("TrueMap", newTrueMapPlugin(), 2),
  548. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  549. },
  550. nodes: []string{"2", "1"},
  551. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "2"}},
  552. name: "test error with priority map",
  553. wErr: fmt.Errorf("error while running score plugin for pod \"2\": %+v", errPrioritize),
  554. },
  555. {
  556. name: "test podtopologyspread plugin - 2 nodes with maxskew=1",
  557. registerPlugins: []st.RegisterPluginFunc{
  558. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  559. st.RegisterPluginAsExtensions(
  560. podtopologyspread.Name,
  561. podtopologyspread.New,
  562. "PreFilter",
  563. "Filter",
  564. ),
  565. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  566. },
  567. nodes: []string{"machine1", "machine2"},
  568. pod: &v1.Pod{
  569. ObjectMeta: metav1.ObjectMeta{Name: "p", UID: types.UID("p"), Labels: map[string]string{"foo": ""}},
  570. Spec: v1.PodSpec{
  571. TopologySpreadConstraints: []v1.TopologySpreadConstraint{
  572. {
  573. MaxSkew: 1,
  574. TopologyKey: "hostname",
  575. WhenUnsatisfiable: v1.DoNotSchedule,
  576. LabelSelector: &metav1.LabelSelector{
  577. MatchExpressions: []metav1.LabelSelectorRequirement{
  578. {
  579. Key: "foo",
  580. Operator: metav1.LabelSelectorOpExists,
  581. },
  582. },
  583. },
  584. },
  585. },
  586. },
  587. },
  588. pods: []*v1.Pod{
  589. {
  590. ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1"), Labels: map[string]string{"foo": ""}},
  591. Spec: v1.PodSpec{
  592. NodeName: "machine1",
  593. },
  594. Status: v1.PodStatus{
  595. Phase: v1.PodRunning,
  596. },
  597. },
  598. },
  599. expectedHosts: sets.NewString("machine2"),
  600. wErr: nil,
  601. },
  602. {
  603. name: "test podtopologyspread plugin - 3 nodes with maxskew=2",
  604. registerPlugins: []st.RegisterPluginFunc{
  605. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  606. st.RegisterPluginAsExtensions(
  607. podtopologyspread.Name,
  608. podtopologyspread.New,
  609. "PreFilter",
  610. "Filter",
  611. ),
  612. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  613. },
  614. nodes: []string{"machine1", "machine2", "machine3"},
  615. pod: &v1.Pod{
  616. ObjectMeta: metav1.ObjectMeta{Name: "p", UID: types.UID("p"), Labels: map[string]string{"foo": ""}},
  617. Spec: v1.PodSpec{
  618. TopologySpreadConstraints: []v1.TopologySpreadConstraint{
  619. {
  620. MaxSkew: 2,
  621. TopologyKey: "hostname",
  622. WhenUnsatisfiable: v1.DoNotSchedule,
  623. LabelSelector: &metav1.LabelSelector{
  624. MatchExpressions: []metav1.LabelSelectorRequirement{
  625. {
  626. Key: "foo",
  627. Operator: metav1.LabelSelectorOpExists,
  628. },
  629. },
  630. },
  631. },
  632. },
  633. },
  634. },
  635. pods: []*v1.Pod{
  636. {
  637. ObjectMeta: metav1.ObjectMeta{Name: "pod1a", UID: types.UID("pod1a"), Labels: map[string]string{"foo": ""}},
  638. Spec: v1.PodSpec{
  639. NodeName: "machine1",
  640. },
  641. Status: v1.PodStatus{
  642. Phase: v1.PodRunning,
  643. },
  644. },
  645. {
  646. ObjectMeta: metav1.ObjectMeta{Name: "pod1b", UID: types.UID("pod1b"), Labels: map[string]string{"foo": ""}},
  647. Spec: v1.PodSpec{
  648. NodeName: "machine1",
  649. },
  650. Status: v1.PodStatus{
  651. Phase: v1.PodRunning,
  652. },
  653. },
  654. {
  655. ObjectMeta: metav1.ObjectMeta{Name: "pod2", UID: types.UID("pod2"), Labels: map[string]string{"foo": ""}},
  656. Spec: v1.PodSpec{
  657. NodeName: "machine2",
  658. },
  659. Status: v1.PodStatus{
  660. Phase: v1.PodRunning,
  661. },
  662. },
  663. },
  664. expectedHosts: sets.NewString("machine2", "machine3"),
  665. wErr: nil,
  666. },
  667. {
  668. name: "test with filter plugin returning Unschedulable status",
  669. registerPlugins: []st.RegisterPluginFunc{
  670. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  671. st.RegisterFilterPlugin(
  672. "FakeFilter",
  673. NewFakeFilterPlugin(map[string]framework.Code{"3": framework.Unschedulable}),
  674. ),
  675. st.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1),
  676. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  677. },
  678. nodes: []string{"3"},
  679. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test-filter", UID: types.UID("test-filter")}},
  680. expectedHosts: nil,
  681. wErr: &FitError{
  682. Pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test-filter", UID: types.UID("test-filter")}},
  683. NumAllNodes: 1,
  684. FilteredNodesStatuses: framework.NodeToStatusMap{
  685. "3": framework.NewStatus(framework.Unschedulable, "injecting failure for pod test-filter"),
  686. },
  687. },
  688. },
  689. {
  690. name: "test with filter plugin returning UnschedulableAndUnresolvable status",
  691. registerPlugins: []st.RegisterPluginFunc{
  692. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  693. st.RegisterFilterPlugin(
  694. "FakeFilter",
  695. NewFakeFilterPlugin(map[string]framework.Code{"3": framework.UnschedulableAndUnresolvable}),
  696. ),
  697. st.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1),
  698. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  699. },
  700. nodes: []string{"3"},
  701. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test-filter", UID: types.UID("test-filter")}},
  702. expectedHosts: nil,
  703. wErr: &FitError{
  704. Pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test-filter", UID: types.UID("test-filter")}},
  705. NumAllNodes: 1,
  706. FilteredNodesStatuses: framework.NodeToStatusMap{
  707. "3": framework.NewStatus(framework.UnschedulableAndUnresolvable, "injecting failure for pod test-filter"),
  708. },
  709. },
  710. },
  711. {
  712. name: "test with partial failed filter plugin",
  713. registerPlugins: []st.RegisterPluginFunc{
  714. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  715. st.RegisterFilterPlugin(
  716. "FakeFilter",
  717. NewFakeFilterPlugin(map[string]framework.Code{"1": framework.Unschedulable}),
  718. ),
  719. st.RegisterScorePlugin("NumericMap", newNumericMapPlugin(), 1),
  720. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  721. },
  722. nodes: []string{"1", "2"},
  723. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test-filter", UID: types.UID("test-filter")}},
  724. expectedHosts: nil,
  725. wErr: nil,
  726. },
  727. }
  728. for _, test := range tests {
  729. t.Run(test.name, func(t *testing.T) {
  730. client := clientsetfake.NewSimpleClientset()
  731. informerFactory := informers.NewSharedInformerFactory(client, 0)
  732. cache := internalcache.New(time.Duration(0), wait.NeverStop)
  733. for _, pod := range test.pods {
  734. cache.AddPod(pod)
  735. }
  736. var nodes []*v1.Node
  737. for _, name := range test.nodes {
  738. node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: name, Labels: map[string]string{"hostname": name}}}
  739. nodes = append(nodes, node)
  740. cache.AddNode(node)
  741. }
  742. snapshot := internalcache.NewSnapshot(test.pods, nodes)
  743. fwk, err := st.NewFramework(test.registerPlugins, framework.WithSnapshotSharedLister(snapshot))
  744. if err != nil {
  745. t.Fatal(err)
  746. }
  747. var pvcs []v1.PersistentVolumeClaim
  748. pvcs = append(pvcs, test.pvcs...)
  749. pvcLister := fakelisters.PersistentVolumeClaimLister(pvcs)
  750. scheduler := NewGenericScheduler(
  751. cache,
  752. internalqueue.NewSchedulingQueue(nil),
  753. snapshot,
  754. fwk,
  755. []SchedulerExtender{},
  756. nil,
  757. pvcLister,
  758. informerFactory.Policy().V1beta1().PodDisruptionBudgets().Lister(),
  759. false,
  760. schedulerapi.DefaultPercentageOfNodesToScore,
  761. false)
  762. result, err := scheduler.Schedule(context.Background(), framework.NewCycleState(), test.pod)
  763. if !reflect.DeepEqual(err, test.wErr) {
  764. t.Errorf("Unexpected error: %v, expected: %v", err.Error(), test.wErr)
  765. }
  766. if test.expectedHosts != nil && !test.expectedHosts.Has(result.SuggestedHost) {
  767. t.Errorf("Expected: %s, got: %s", test.expectedHosts, result.SuggestedHost)
  768. }
  769. if test.wErr == nil && len(test.nodes) != result.EvaluatedNodes {
  770. t.Errorf("Expected EvaluatedNodes: %d, got: %d", len(test.nodes), result.EvaluatedNodes)
  771. }
  772. })
  773. }
  774. }
  775. // makeScheduler makes a simple genericScheduler for testing.
  776. func makeScheduler(nodes []*v1.Node, fns ...st.RegisterPluginFunc) *genericScheduler {
  777. cache := internalcache.New(time.Duration(0), wait.NeverStop)
  778. for _, n := range nodes {
  779. cache.AddNode(n)
  780. }
  781. fwk, _ := st.NewFramework(fns)
  782. s := NewGenericScheduler(
  783. cache,
  784. internalqueue.NewSchedulingQueue(nil),
  785. emptySnapshot,
  786. fwk,
  787. nil, nil, nil, nil, false,
  788. schedulerapi.DefaultPercentageOfNodesToScore, false)
  789. cache.UpdateSnapshot(s.(*genericScheduler).nodeInfoSnapshot)
  790. return s.(*genericScheduler)
  791. }
  792. func TestFindFitAllError(t *testing.T) {
  793. nodes := makeNodeList([]string{"3", "2", "1"})
  794. scheduler := makeScheduler(
  795. nodes,
  796. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  797. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  798. st.RegisterFilterPlugin("MatchFilter", NewMatchFilterPlugin),
  799. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  800. )
  801. _, nodeToStatusMap, err := scheduler.findNodesThatFitPod(context.Background(), framework.NewCycleState(), &v1.Pod{})
  802. if err != nil {
  803. t.Errorf("unexpected error: %v", err)
  804. }
  805. if len(nodeToStatusMap) != len(nodes) {
  806. t.Errorf("unexpected failed status map: %v", nodeToStatusMap)
  807. }
  808. for _, node := range nodes {
  809. t.Run(node.Name, func(t *testing.T) {
  810. status, found := nodeToStatusMap[node.Name]
  811. if !found {
  812. t.Errorf("failed to find node %v in %v", node.Name, nodeToStatusMap)
  813. }
  814. reasons := status.Reasons()
  815. if len(reasons) != 1 || reasons[0] != ErrReasonFake {
  816. t.Errorf("unexpected failure reasons: %v", reasons)
  817. }
  818. })
  819. }
  820. }
  821. func TestFindFitSomeError(t *testing.T) {
  822. nodes := makeNodeList([]string{"3", "2", "1"})
  823. scheduler := makeScheduler(
  824. nodes,
  825. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  826. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  827. st.RegisterFilterPlugin("MatchFilter", NewMatchFilterPlugin),
  828. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  829. )
  830. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "1", UID: types.UID("1")}}
  831. _, nodeToStatusMap, err := scheduler.findNodesThatFitPod(context.Background(), framework.NewCycleState(), pod)
  832. if err != nil {
  833. t.Errorf("unexpected error: %v", err)
  834. }
  835. if len(nodeToStatusMap) != len(nodes)-1 {
  836. t.Errorf("unexpected failed status map: %v", nodeToStatusMap)
  837. }
  838. for _, node := range nodes {
  839. if node.Name == pod.Name {
  840. continue
  841. }
  842. t.Run(node.Name, func(t *testing.T) {
  843. status, found := nodeToStatusMap[node.Name]
  844. if !found {
  845. t.Errorf("failed to find node %v in %v", node.Name, nodeToStatusMap)
  846. }
  847. reasons := status.Reasons()
  848. if len(reasons) != 1 || reasons[0] != ErrReasonFake {
  849. t.Errorf("unexpected failures: %v", reasons)
  850. }
  851. })
  852. }
  853. }
  854. func TestFindFitPredicateCallCounts(t *testing.T) {
  855. tests := []struct {
  856. name string
  857. pod *v1.Pod
  858. expectedCount int32
  859. }{
  860. {
  861. name: "nominated pods have lower priority, predicate is called once",
  862. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "1", UID: types.UID("1")}, Spec: v1.PodSpec{Priority: &highPriority}},
  863. expectedCount: 1,
  864. },
  865. {
  866. name: "nominated pods have higher priority, predicate is called twice",
  867. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "1", UID: types.UID("1")}, Spec: v1.PodSpec{Priority: &lowPriority}},
  868. expectedCount: 2,
  869. },
  870. }
  871. for _, test := range tests {
  872. nodes := makeNodeList([]string{"1"})
  873. cache := internalcache.New(time.Duration(0), wait.NeverStop)
  874. for _, n := range nodes {
  875. cache.AddNode(n)
  876. }
  877. plugin := fakeFilterPlugin{}
  878. registerFakeFilterFunc := st.RegisterFilterPlugin(
  879. "FakeFilter",
  880. func(_ *runtime.Unknown, fh framework.FrameworkHandle) (framework.Plugin, error) {
  881. return &plugin, nil
  882. },
  883. )
  884. registerPlugins := []st.RegisterPluginFunc{
  885. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  886. registerFakeFilterFunc,
  887. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  888. }
  889. fwk, err := st.NewFramework(registerPlugins)
  890. if err != nil {
  891. t.Fatal(err)
  892. }
  893. queue := internalqueue.NewSchedulingQueue(nil)
  894. scheduler := NewGenericScheduler(
  895. cache,
  896. queue,
  897. emptySnapshot,
  898. fwk,
  899. nil, nil, nil, nil, false,
  900. schedulerapi.DefaultPercentageOfNodesToScore, false).(*genericScheduler)
  901. cache.UpdateSnapshot(scheduler.nodeInfoSnapshot)
  902. queue.UpdateNominatedPodForNode(&v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("nominated")}, Spec: v1.PodSpec{Priority: &midPriority}}, "1")
  903. _, _, err = scheduler.findNodesThatFitPod(context.Background(), framework.NewCycleState(), test.pod)
  904. if err != nil {
  905. t.Errorf("unexpected error: %v", err)
  906. }
  907. if test.expectedCount != plugin.numFilterCalled {
  908. t.Errorf("predicate was called %d times, expected is %d", plugin.numFilterCalled, test.expectedCount)
  909. }
  910. }
  911. }
  912. func makeNode(node string, milliCPU, memory int64) *v1.Node {
  913. return &v1.Node{
  914. ObjectMeta: metav1.ObjectMeta{Name: node},
  915. Status: v1.NodeStatus{
  916. Capacity: v1.ResourceList{
  917. v1.ResourceCPU: *resource.NewMilliQuantity(milliCPU, resource.DecimalSI),
  918. v1.ResourceMemory: *resource.NewQuantity(memory, resource.BinarySI),
  919. "pods": *resource.NewQuantity(100, resource.DecimalSI),
  920. },
  921. Allocatable: v1.ResourceList{
  922. v1.ResourceCPU: *resource.NewMilliQuantity(milliCPU, resource.DecimalSI),
  923. v1.ResourceMemory: *resource.NewQuantity(memory, resource.BinarySI),
  924. "pods": *resource.NewQuantity(100, resource.DecimalSI),
  925. },
  926. },
  927. }
  928. }
  929. // The point of this test is to show that you:
  930. // - get the same priority for a zero-request pod as for a pod with the defaults requests,
  931. // both when the zero-request pod is already on the machine and when the zero-request pod
  932. // is the one being scheduled.
  933. // - don't get the same score no matter what we schedule.
  934. func TestZeroRequest(t *testing.T) {
  935. // A pod with no resources. We expect spreading to count it as having the default resources.
  936. noResources := v1.PodSpec{
  937. Containers: []v1.Container{
  938. {},
  939. },
  940. }
  941. noResources1 := noResources
  942. noResources1.NodeName = "machine1"
  943. // A pod with the same resources as a 0-request pod gets by default as its resources (for spreading).
  944. small := v1.PodSpec{
  945. Containers: []v1.Container{
  946. {
  947. Resources: v1.ResourceRequirements{
  948. Requests: v1.ResourceList{
  949. v1.ResourceCPU: resource.MustParse(
  950. strconv.FormatInt(schedutil.DefaultMilliCPURequest, 10) + "m"),
  951. v1.ResourceMemory: resource.MustParse(
  952. strconv.FormatInt(schedutil.DefaultMemoryRequest, 10)),
  953. },
  954. },
  955. },
  956. },
  957. }
  958. small2 := small
  959. small2.NodeName = "machine2"
  960. // A larger pod.
  961. large := v1.PodSpec{
  962. Containers: []v1.Container{
  963. {
  964. Resources: v1.ResourceRequirements{
  965. Requests: v1.ResourceList{
  966. v1.ResourceCPU: resource.MustParse(
  967. strconv.FormatInt(schedutil.DefaultMilliCPURequest*3, 10) + "m"),
  968. v1.ResourceMemory: resource.MustParse(
  969. strconv.FormatInt(schedutil.DefaultMemoryRequest*3, 10)),
  970. },
  971. },
  972. },
  973. },
  974. }
  975. large1 := large
  976. large1.NodeName = "machine1"
  977. large2 := large
  978. large2.NodeName = "machine2"
  979. tests := []struct {
  980. pod *v1.Pod
  981. pods []*v1.Pod
  982. nodes []*v1.Node
  983. name string
  984. expectedScore int64
  985. }{
  986. // The point of these next two tests is to show you get the same priority for a zero-request pod
  987. // as for a pod with the defaults requests, both when the zero-request pod is already on the machine
  988. // and when the zero-request pod is the one being scheduled.
  989. {
  990. pod: &v1.Pod{Spec: noResources},
  991. nodes: []*v1.Node{makeNode("machine1", 1000, schedutil.DefaultMemoryRequest*10), makeNode("machine2", 1000, schedutil.DefaultMemoryRequest*10)},
  992. name: "test priority of zero-request pod with machine with zero-request pod",
  993. pods: []*v1.Pod{
  994. {Spec: large1}, {Spec: noResources1},
  995. {Spec: large2}, {Spec: small2},
  996. },
  997. expectedScore: 250,
  998. },
  999. {
  1000. pod: &v1.Pod{Spec: small},
  1001. nodes: []*v1.Node{makeNode("machine1", 1000, schedutil.DefaultMemoryRequest*10), makeNode("machine2", 1000, schedutil.DefaultMemoryRequest*10)},
  1002. name: "test priority of nonzero-request pod with machine with zero-request pod",
  1003. pods: []*v1.Pod{
  1004. {Spec: large1}, {Spec: noResources1},
  1005. {Spec: large2}, {Spec: small2},
  1006. },
  1007. expectedScore: 250,
  1008. },
  1009. // The point of this test is to verify that we're not just getting the same score no matter what we schedule.
  1010. {
  1011. pod: &v1.Pod{Spec: large},
  1012. nodes: []*v1.Node{makeNode("machine1", 1000, schedutil.DefaultMemoryRequest*10), makeNode("machine2", 1000, schedutil.DefaultMemoryRequest*10)},
  1013. name: "test priority of larger pod with machine with zero-request pod",
  1014. pods: []*v1.Pod{
  1015. {Spec: large1}, {Spec: noResources1},
  1016. {Spec: large2}, {Spec: small2},
  1017. },
  1018. expectedScore: 230,
  1019. },
  1020. }
  1021. for _, test := range tests {
  1022. t.Run(test.name, func(t *testing.T) {
  1023. client := clientsetfake.NewSimpleClientset()
  1024. informerFactory := informers.NewSharedInformerFactory(client, 0)
  1025. snapshot := internalcache.NewSnapshot(test.pods, test.nodes)
  1026. pluginRegistrations := []st.RegisterPluginFunc{
  1027. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1028. st.RegisterScorePlugin(noderesources.LeastAllocatedName, noderesources.NewLeastAllocated, 1),
  1029. st.RegisterScorePlugin(noderesources.BalancedAllocationName, noderesources.NewBalancedAllocation, 1),
  1030. st.RegisterScorePlugin(defaultpodtopologyspread.Name, defaultpodtopologyspread.New, 1),
  1031. st.RegisterPreScorePlugin(defaultpodtopologyspread.Name, defaultpodtopologyspread.New),
  1032. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1033. }
  1034. fwk, err := st.NewFramework(
  1035. pluginRegistrations,
  1036. framework.WithInformerFactory(informerFactory),
  1037. framework.WithSnapshotSharedLister(snapshot),
  1038. framework.WithClientSet(client),
  1039. )
  1040. if err != nil {
  1041. t.Fatalf("error creating framework: %+v", err)
  1042. }
  1043. scheduler := NewGenericScheduler(
  1044. nil,
  1045. nil,
  1046. emptySnapshot,
  1047. fwk,
  1048. []SchedulerExtender{},
  1049. nil,
  1050. nil,
  1051. nil,
  1052. false,
  1053. schedulerapi.DefaultPercentageOfNodesToScore,
  1054. false).(*genericScheduler)
  1055. scheduler.nodeInfoSnapshot = snapshot
  1056. ctx := context.Background()
  1057. state := framework.NewCycleState()
  1058. _, _, err = scheduler.findNodesThatFitPod(ctx, state, test.pod)
  1059. if err != nil {
  1060. t.Fatalf("error filtering nodes: %+v", err)
  1061. }
  1062. scheduler.framework.RunPreScorePlugins(ctx, state, test.pod, test.nodes)
  1063. list, err := scheduler.prioritizeNodes(
  1064. ctx,
  1065. state,
  1066. test.pod,
  1067. test.nodes,
  1068. )
  1069. if err != nil {
  1070. t.Errorf("unexpected error: %v", err)
  1071. }
  1072. for _, hp := range list {
  1073. if hp.Score != test.expectedScore {
  1074. t.Errorf("expected %d for all priorities, got list %#v", test.expectedScore, list)
  1075. }
  1076. }
  1077. })
  1078. }
  1079. }
  1080. func printNodeToVictims(nodeToVictims map[*v1.Node]*extenderv1.Victims) string {
  1081. var output string
  1082. for node, victims := range nodeToVictims {
  1083. output += node.Name + ": ["
  1084. for _, pod := range victims.Pods {
  1085. output += pod.Name + ", "
  1086. }
  1087. output += "]"
  1088. }
  1089. return output
  1090. }
  1091. type victims struct {
  1092. pods sets.String
  1093. numPDBViolations int64
  1094. }
  1095. func checkPreemptionVictims(expected map[string]victims, nodeToPods map[*v1.Node]*extenderv1.Victims) error {
  1096. if len(expected) == len(nodeToPods) {
  1097. for k, victims := range nodeToPods {
  1098. if expVictims, ok := expected[k.Name]; ok {
  1099. if len(victims.Pods) != len(expVictims.pods) {
  1100. return fmt.Errorf("unexpected number of pods. expected: %v, got: %v", expected, printNodeToVictims(nodeToPods))
  1101. }
  1102. prevPriority := int32(math.MaxInt32)
  1103. for _, p := range victims.Pods {
  1104. // Check that pods are sorted by their priority.
  1105. if *p.Spec.Priority > prevPriority {
  1106. return fmt.Errorf("pod %v of node %v was not sorted by priority", p.Name, k)
  1107. }
  1108. prevPriority = *p.Spec.Priority
  1109. if !expVictims.pods.Has(p.Name) {
  1110. return fmt.Errorf("pod %v was not expected. Expected: %v", p.Name, expVictims.pods)
  1111. }
  1112. }
  1113. if expVictims.numPDBViolations != victims.NumPDBViolations {
  1114. return fmt.Errorf("unexpected numPDBViolations. expected: %d, got: %d", expVictims.numPDBViolations, victims.NumPDBViolations)
  1115. }
  1116. } else {
  1117. return fmt.Errorf("unexpected machines. expected: %v, got: %v", expected, printNodeToVictims(nodeToPods))
  1118. }
  1119. }
  1120. } else {
  1121. return fmt.Errorf("unexpected number of machines. expected: %v, got: %v", expected, printNodeToVictims(nodeToPods))
  1122. }
  1123. return nil
  1124. }
  1125. var smallContainers = []v1.Container{
  1126. {
  1127. Resources: v1.ResourceRequirements{
  1128. Requests: v1.ResourceList{
  1129. "cpu": resource.MustParse(
  1130. strconv.FormatInt(schedutil.DefaultMilliCPURequest, 10) + "m"),
  1131. "memory": resource.MustParse(
  1132. strconv.FormatInt(schedutil.DefaultMemoryRequest, 10)),
  1133. },
  1134. },
  1135. },
  1136. }
  1137. var mediumContainers = []v1.Container{
  1138. {
  1139. Resources: v1.ResourceRequirements{
  1140. Requests: v1.ResourceList{
  1141. "cpu": resource.MustParse(
  1142. strconv.FormatInt(schedutil.DefaultMilliCPURequest*2, 10) + "m"),
  1143. "memory": resource.MustParse(
  1144. strconv.FormatInt(schedutil.DefaultMemoryRequest*2, 10)),
  1145. },
  1146. },
  1147. },
  1148. }
  1149. var largeContainers = []v1.Container{
  1150. {
  1151. Resources: v1.ResourceRequirements{
  1152. Requests: v1.ResourceList{
  1153. "cpu": resource.MustParse(
  1154. strconv.FormatInt(schedutil.DefaultMilliCPURequest*3, 10) + "m"),
  1155. "memory": resource.MustParse(
  1156. strconv.FormatInt(schedutil.DefaultMemoryRequest*3, 10)),
  1157. },
  1158. },
  1159. },
  1160. }
  1161. var veryLargeContainers = []v1.Container{
  1162. {
  1163. Resources: v1.ResourceRequirements{
  1164. Requests: v1.ResourceList{
  1165. "cpu": resource.MustParse(
  1166. strconv.FormatInt(schedutil.DefaultMilliCPURequest*5, 10) + "m"),
  1167. "memory": resource.MustParse(
  1168. strconv.FormatInt(schedutil.DefaultMemoryRequest*5, 10)),
  1169. },
  1170. },
  1171. },
  1172. }
  1173. var negPriority, lowPriority, midPriority, highPriority, veryHighPriority = int32(-100), int32(0), int32(100), int32(1000), int32(10000)
  1174. var startTime = metav1.Date(2019, 1, 1, 1, 1, 1, 0, time.UTC)
  1175. var startTime20190102 = metav1.Date(2019, 1, 2, 1, 1, 1, 0, time.UTC)
  1176. var startTime20190103 = metav1.Date(2019, 1, 3, 1, 1, 1, 0, time.UTC)
  1177. var startTime20190104 = metav1.Date(2019, 1, 4, 1, 1, 1, 0, time.UTC)
  1178. var startTime20190105 = metav1.Date(2019, 1, 5, 1, 1, 1, 0, time.UTC)
  1179. var startTime20190106 = metav1.Date(2019, 1, 6, 1, 1, 1, 0, time.UTC)
  1180. var startTime20190107 = metav1.Date(2019, 1, 7, 1, 1, 1, 0, time.UTC)
  1181. // TestSelectNodesForPreemption tests selectNodesForPreemption. This test assumes
  1182. // that podsFitsOnNode works correctly and is tested separately.
  1183. func TestSelectNodesForPreemption(t *testing.T) {
  1184. tests := []struct {
  1185. name string
  1186. registerPlugins []st.RegisterPluginFunc
  1187. nodes []string
  1188. pod *v1.Pod
  1189. pods []*v1.Pod
  1190. pdbs []*policy.PodDisruptionBudget
  1191. filterReturnCode framework.Code
  1192. expected map[string]victims
  1193. expectedNumFilterCalled int32
  1194. }{
  1195. {
  1196. name: "a pod that does not fit on any machine",
  1197. registerPlugins: []st.RegisterPluginFunc{
  1198. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1199. st.RegisterFilterPlugin("FalseFilter", NewFalseFilterPlugin),
  1200. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1201. },
  1202. nodes: []string{"machine1", "machine2"},
  1203. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "new", UID: types.UID("new")}, Spec: v1.PodSpec{Priority: &highPriority}},
  1204. pods: []*v1.Pod{
  1205. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Priority: &midPriority, NodeName: "machine1"}},
  1206. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Priority: &midPriority, NodeName: "machine2"}}},
  1207. expected: map[string]victims{},
  1208. expectedNumFilterCalled: 2,
  1209. },
  1210. {
  1211. name: "a pod that fits with no preemption",
  1212. registerPlugins: []st.RegisterPluginFunc{
  1213. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1214. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  1215. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1216. },
  1217. nodes: []string{"machine1", "machine2"},
  1218. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "new", UID: types.UID("new")}, Spec: v1.PodSpec{Priority: &highPriority}},
  1219. pods: []*v1.Pod{
  1220. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Priority: &midPriority, NodeName: "machine1"}},
  1221. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Priority: &midPriority, NodeName: "machine2"}}},
  1222. expected: map[string]victims{"machine1": {}, "machine2": {}},
  1223. expectedNumFilterCalled: 4,
  1224. },
  1225. {
  1226. name: "a pod that fits on one machine with no preemption",
  1227. registerPlugins: []st.RegisterPluginFunc{
  1228. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1229. st.RegisterFilterPlugin("MatchFilter", NewMatchFilterPlugin),
  1230. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1231. },
  1232. nodes: []string{"machine1", "machine2"},
  1233. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Priority: &highPriority}},
  1234. pods: []*v1.Pod{
  1235. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Priority: &midPriority, NodeName: "machine1"}},
  1236. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Priority: &midPriority, NodeName: "machine2"}}},
  1237. expected: map[string]victims{"machine1": {}},
  1238. expectedNumFilterCalled: 3,
  1239. },
  1240. {
  1241. name: "a pod that fits on both machines when lower priority pods are preempted",
  1242. registerPlugins: []st.RegisterPluginFunc{
  1243. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1244. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1245. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1246. },
  1247. nodes: []string{"machine1", "machine2"},
  1248. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
  1249. pods: []*v1.Pod{
  1250. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine1"}},
  1251. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}}},
  1252. expected: map[string]victims{"machine1": {pods: sets.NewString("a")}, "machine2": {pods: sets.NewString("b")}},
  1253. expectedNumFilterCalled: 4,
  1254. },
  1255. {
  1256. name: "a pod that would fit on the machines, but other pods running are higher priority",
  1257. registerPlugins: []st.RegisterPluginFunc{
  1258. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1259. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1260. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1261. },
  1262. nodes: []string{"machine1", "machine2"},
  1263. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &lowPriority}},
  1264. pods: []*v1.Pod{
  1265. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine1"}},
  1266. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}}},
  1267. expected: map[string]victims{},
  1268. expectedNumFilterCalled: 2,
  1269. },
  1270. {
  1271. name: "medium priority pod is preempted, but lower priority one stays as it is small",
  1272. registerPlugins: []st.RegisterPluginFunc{
  1273. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1274. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1275. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1276. },
  1277. nodes: []string{"machine1", "machine2"},
  1278. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
  1279. pods: []*v1.Pod{
  1280. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}},
  1281. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine1"}},
  1282. {ObjectMeta: metav1.ObjectMeta{Name: "c", UID: types.UID("c")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}}},
  1283. expected: map[string]victims{"machine1": {pods: sets.NewString("b")}, "machine2": {pods: sets.NewString("c")}},
  1284. expectedNumFilterCalled: 5,
  1285. },
  1286. {
  1287. name: "mixed priority pods are preempted",
  1288. registerPlugins: []st.RegisterPluginFunc{
  1289. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1290. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1291. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1292. },
  1293. nodes: []string{"machine1", "machine2"},
  1294. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
  1295. pods: []*v1.Pod{
  1296. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}},
  1297. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}},
  1298. {ObjectMeta: metav1.ObjectMeta{Name: "c", UID: types.UID("c")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}},
  1299. {ObjectMeta: metav1.ObjectMeta{Name: "d", UID: types.UID("d")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &highPriority, NodeName: "machine1"}},
  1300. {ObjectMeta: metav1.ObjectMeta{Name: "e", UID: types.UID("e")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}}},
  1301. expected: map[string]victims{"machine1": {pods: sets.NewString("b", "c")}},
  1302. expectedNumFilterCalled: 5,
  1303. },
  1304. {
  1305. name: "mixed priority pods are preempted, pick later StartTime one when priorities are equal",
  1306. registerPlugins: []st.RegisterPluginFunc{
  1307. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1308. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1309. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1310. },
  1311. nodes: []string{"machine1", "machine2"},
  1312. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
  1313. pods: []*v1.Pod{
  1314. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190107}},
  1315. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190106}},
  1316. {ObjectMeta: metav1.ObjectMeta{Name: "c", UID: types.UID("c")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190105}},
  1317. {ObjectMeta: metav1.ObjectMeta{Name: "d", UID: types.UID("d")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &highPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190104}},
  1318. {ObjectMeta: metav1.ObjectMeta{Name: "e", UID: types.UID("e")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime20190103}}},
  1319. expected: map[string]victims{"machine1": {pods: sets.NewString("a", "c")}},
  1320. expectedNumFilterCalled: 5,
  1321. },
  1322. {
  1323. name: "pod with anti-affinity is preempted",
  1324. registerPlugins: []st.RegisterPluginFunc{
  1325. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1326. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1327. st.RegisterPluginAsExtensions(interpodaffinity.Name, interpodaffinity.New, "Filter", "PreFilter"),
  1328. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1329. },
  1330. nodes: []string{"machine1", "machine2"},
  1331. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{
  1332. Name: "machine1",
  1333. Labels: map[string]string{"pod": "preemptor"}}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &highPriority}},
  1334. pods: []*v1.Pod{
  1335. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a"), Labels: map[string]string{"service": "securityscan"}}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1", Affinity: &v1.Affinity{
  1336. PodAntiAffinity: &v1.PodAntiAffinity{
  1337. RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
  1338. {
  1339. LabelSelector: &metav1.LabelSelector{
  1340. MatchExpressions: []metav1.LabelSelectorRequirement{
  1341. {
  1342. Key: "pod",
  1343. Operator: metav1.LabelSelectorOpIn,
  1344. Values: []string{"preemptor", "value2"},
  1345. },
  1346. },
  1347. },
  1348. TopologyKey: "hostname",
  1349. },
  1350. },
  1351. }}}},
  1352. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}},
  1353. {ObjectMeta: metav1.ObjectMeta{Name: "d", UID: types.UID("d")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &highPriority, NodeName: "machine1"}},
  1354. {ObjectMeta: metav1.ObjectMeta{Name: "e", UID: types.UID("e")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}}},
  1355. expected: map[string]victims{"machine1": {pods: sets.NewString("a")}, "machine2": {}},
  1356. expectedNumFilterCalled: 4,
  1357. },
  1358. {
  1359. name: "preemption to resolve even pods spread FitError",
  1360. registerPlugins: []st.RegisterPluginFunc{
  1361. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1362. st.RegisterPluginAsExtensions(
  1363. podtopologyspread.Name,
  1364. podtopologyspread.New,
  1365. "PreFilter",
  1366. "Filter",
  1367. ),
  1368. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1369. },
  1370. nodes: []string{"node-a/zone1", "node-b/zone1", "node-x/zone2"},
  1371. pod: &v1.Pod{
  1372. ObjectMeta: metav1.ObjectMeta{
  1373. Name: "p",
  1374. Labels: map[string]string{"foo": ""},
  1375. },
  1376. Spec: v1.PodSpec{
  1377. Priority: &highPriority,
  1378. TopologySpreadConstraints: []v1.TopologySpreadConstraint{
  1379. {
  1380. MaxSkew: 1,
  1381. TopologyKey: "zone",
  1382. WhenUnsatisfiable: v1.DoNotSchedule,
  1383. LabelSelector: &metav1.LabelSelector{
  1384. MatchExpressions: []metav1.LabelSelectorRequirement{
  1385. {
  1386. Key: "foo",
  1387. Operator: metav1.LabelSelectorOpExists,
  1388. },
  1389. },
  1390. },
  1391. },
  1392. {
  1393. MaxSkew: 1,
  1394. TopologyKey: "hostname",
  1395. WhenUnsatisfiable: v1.DoNotSchedule,
  1396. LabelSelector: &metav1.LabelSelector{
  1397. MatchExpressions: []metav1.LabelSelectorRequirement{
  1398. {
  1399. Key: "foo",
  1400. Operator: metav1.LabelSelectorOpExists,
  1401. },
  1402. },
  1403. },
  1404. },
  1405. },
  1406. },
  1407. },
  1408. pods: []*v1.Pod{
  1409. {
  1410. ObjectMeta: metav1.ObjectMeta{Name: "pod-a1", UID: types.UID("pod-a1"), Labels: map[string]string{"foo": ""}},
  1411. Spec: v1.PodSpec{NodeName: "node-a", Priority: &midPriority},
  1412. Status: v1.PodStatus{Phase: v1.PodRunning},
  1413. },
  1414. {
  1415. ObjectMeta: metav1.ObjectMeta{Name: "pod-a2", UID: types.UID("pod-a2"), Labels: map[string]string{"foo": ""}},
  1416. Spec: v1.PodSpec{NodeName: "node-a", Priority: &lowPriority},
  1417. Status: v1.PodStatus{Phase: v1.PodRunning},
  1418. },
  1419. {
  1420. ObjectMeta: metav1.ObjectMeta{Name: "pod-b1", UID: types.UID("pod-b1"), Labels: map[string]string{"foo": ""}},
  1421. Spec: v1.PodSpec{NodeName: "node-b", Priority: &lowPriority},
  1422. Status: v1.PodStatus{Phase: v1.PodRunning},
  1423. },
  1424. {
  1425. ObjectMeta: metav1.ObjectMeta{Name: "pod-x1", UID: types.UID("pod-x1"), Labels: map[string]string{"foo": ""}},
  1426. Spec: v1.PodSpec{NodeName: "node-x", Priority: &highPriority},
  1427. Status: v1.PodStatus{Phase: v1.PodRunning},
  1428. },
  1429. {
  1430. ObjectMeta: metav1.ObjectMeta{Name: "pod-x2", UID: types.UID("pod-x2"), Labels: map[string]string{"foo": ""}},
  1431. Spec: v1.PodSpec{NodeName: "node-x", Priority: &highPriority},
  1432. Status: v1.PodStatus{Phase: v1.PodRunning},
  1433. },
  1434. },
  1435. expected: map[string]victims{
  1436. "node-a": {pods: sets.NewString("pod-a2")},
  1437. "node-b": {pods: sets.NewString("pod-b1")},
  1438. },
  1439. expectedNumFilterCalled: 6,
  1440. },
  1441. {
  1442. name: "get Unschedulable in the preemption phase when the filter plugins filtering the nodes",
  1443. registerPlugins: []st.RegisterPluginFunc{
  1444. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1445. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1446. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1447. },
  1448. nodes: []string{"machine1", "machine2"},
  1449. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
  1450. pods: []*v1.Pod{
  1451. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine1"}},
  1452. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}}},
  1453. filterReturnCode: framework.Unschedulable,
  1454. expected: map[string]victims{},
  1455. expectedNumFilterCalled: 2,
  1456. },
  1457. {
  1458. name: "preemption with violation of same pdb",
  1459. registerPlugins: []st.RegisterPluginFunc{
  1460. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1461. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1462. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1463. },
  1464. nodes: []string{"machine1"},
  1465. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &highPriority}},
  1466. pods: []*v1.Pod{
  1467. {ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a"), Labels: map[string]string{"app": "foo"}}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}},
  1468. {ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b"), Labels: map[string]string{"app": "foo"}}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}}},
  1469. pdbs: []*policy.PodDisruptionBudget{
  1470. {Spec: policy.PodDisruptionBudgetSpec{Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"app": "foo"}}}, Status: policy.PodDisruptionBudgetStatus{DisruptionsAllowed: 1}}},
  1471. expected: map[string]victims{"machine1": {pods: sets.NewString("a", "b"), numPDBViolations: 1}},
  1472. expectedNumFilterCalled: 3,
  1473. },
  1474. }
  1475. labelKeys := []string{"hostname", "zone", "region"}
  1476. for _, test := range tests {
  1477. t.Run(test.name, func(t *testing.T) {
  1478. client := clientsetfake.NewSimpleClientset()
  1479. informerFactory := informers.NewSharedInformerFactory(client, 0)
  1480. filterFailedNodeReturnCodeMap := map[string]framework.Code{}
  1481. cache := internalcache.New(time.Duration(0), wait.NeverStop)
  1482. for _, pod := range test.pods {
  1483. cache.AddPod(pod)
  1484. }
  1485. for _, name := range test.nodes {
  1486. filterFailedNodeReturnCodeMap[name] = test.filterReturnCode
  1487. cache.AddNode(&v1.Node{ObjectMeta: metav1.ObjectMeta{Name: name, Labels: map[string]string{"hostname": name}}})
  1488. }
  1489. var nodes []*v1.Node
  1490. for _, n := range test.nodes {
  1491. node := makeNode(n, 1000*5, schedutil.DefaultMemoryRequest*5)
  1492. // if possible, split node name by '/' to form labels in a format of
  1493. // {"hostname": node.Name[0], "zone": node.Name[1], "region": node.Name[2]}
  1494. node.ObjectMeta.Labels = make(map[string]string)
  1495. for i, label := range strings.Split(node.Name, "/") {
  1496. node.ObjectMeta.Labels[labelKeys[i]] = label
  1497. }
  1498. node.Name = node.ObjectMeta.Labels["hostname"]
  1499. nodes = append(nodes, node)
  1500. }
  1501. // For each test, prepend a FakeFilterPlugin.
  1502. fakePlugin := fakeFilterPlugin{}
  1503. fakePlugin.failedNodeReturnCodeMap = filterFailedNodeReturnCodeMap
  1504. registerFakeFilterFunc := st.RegisterFilterPlugin(
  1505. "FakeFilter",
  1506. func(_ *runtime.Unknown, fh framework.FrameworkHandle) (framework.Plugin, error) {
  1507. return &fakePlugin, nil
  1508. },
  1509. )
  1510. registerPlugins := append([]st.RegisterPluginFunc{registerFakeFilterFunc}, test.registerPlugins...)
  1511. // Use a real snapshot since it's needed in some Filter Plugin (e.g., PodAffinity)
  1512. snapshot := internalcache.NewSnapshot(test.pods, nodes)
  1513. fwk, err := st.NewFramework(registerPlugins, framework.WithSnapshotSharedLister(snapshot))
  1514. if err != nil {
  1515. t.Fatal(err)
  1516. }
  1517. scheduler := NewGenericScheduler(
  1518. nil,
  1519. internalqueue.NewSchedulingQueue(nil),
  1520. snapshot,
  1521. fwk,
  1522. []SchedulerExtender{},
  1523. nil,
  1524. nil,
  1525. informerFactory.Policy().V1beta1().PodDisruptionBudgets().Lister(),
  1526. false,
  1527. schedulerapi.DefaultPercentageOfNodesToScore,
  1528. false)
  1529. g := scheduler.(*genericScheduler)
  1530. assignDefaultStartTime(test.pods)
  1531. state := framework.NewCycleState()
  1532. // Some tests rely on PreFilter plugin to compute its CycleState.
  1533. preFilterStatus := fwk.RunPreFilterPlugins(context.Background(), state, test.pod)
  1534. if !preFilterStatus.IsSuccess() {
  1535. t.Errorf("Unexpected preFilterStatus: %v", preFilterStatus)
  1536. }
  1537. nodeInfos, err := nodesToNodeInfos(nodes, snapshot)
  1538. if err != nil {
  1539. t.Fatal(err)
  1540. }
  1541. nodeToPods, err := g.selectNodesForPreemption(context.Background(), state, test.pod, nodeInfos, test.pdbs)
  1542. if err != nil {
  1543. t.Error(err)
  1544. }
  1545. if test.expectedNumFilterCalled != fakePlugin.numFilterCalled {
  1546. t.Errorf("expected fakePlugin.numFilterCalled is %d, but got %d", test.expectedNumFilterCalled, fakePlugin.numFilterCalled)
  1547. }
  1548. if err := checkPreemptionVictims(test.expected, nodeToPods); err != nil {
  1549. t.Error(err)
  1550. }
  1551. })
  1552. }
  1553. }
  1554. // TestPickOneNodeForPreemption tests pickOneNodeForPreemption.
  1555. func TestPickOneNodeForPreemption(t *testing.T) {
  1556. tests := []struct {
  1557. name string
  1558. registerPlugins []st.RegisterPluginFunc
  1559. nodes []string
  1560. pod *v1.Pod
  1561. pods []*v1.Pod
  1562. expected []string // any of the items is valid
  1563. }{
  1564. {
  1565. name: "No node needs preemption",
  1566. registerPlugins: []st.RegisterPluginFunc{
  1567. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1568. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1569. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1570. },
  1571. nodes: []string{"machine1"},
  1572. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
  1573. pods: []*v1.Pod{
  1574. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}}},
  1575. expected: []string{"machine1"},
  1576. },
  1577. {
  1578. name: "a pod that fits on both machines when lower priority pods are preempted",
  1579. registerPlugins: []st.RegisterPluginFunc{
  1580. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1581. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1582. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1583. },
  1584. nodes: []string{"machine1", "machine2"},
  1585. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
  1586. pods: []*v1.Pod{
  1587. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1588. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}}},
  1589. expected: []string{"machine1", "machine2"},
  1590. },
  1591. {
  1592. name: "a pod that fits on a machine with no preemption",
  1593. registerPlugins: []st.RegisterPluginFunc{
  1594. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1595. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1596. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1597. },
  1598. nodes: []string{"machine1", "machine2", "machine3"},
  1599. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
  1600. pods: []*v1.Pod{
  1601. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1602. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}}},
  1603. expected: []string{"machine3"},
  1604. },
  1605. {
  1606. name: "machine with min highest priority pod is picked",
  1607. registerPlugins: []st.RegisterPluginFunc{
  1608. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1609. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1610. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1611. },
  1612. nodes: []string{"machine1", "machine2", "machine3"},
  1613. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &highPriority}},
  1614. pods: []*v1.Pod{
  1615. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1616. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1617. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1618. {ObjectMeta: metav1.ObjectMeta{Name: "m2.2", UID: types.UID("m2.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &lowPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1619. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &lowPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1620. {ObjectMeta: metav1.ObjectMeta{Name: "m3.2", UID: types.UID("m3.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &lowPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1621. },
  1622. expected: []string{"machine3"},
  1623. },
  1624. {
  1625. name: "when highest priorities are the same, minimum sum of priorities is picked",
  1626. registerPlugins: []st.RegisterPluginFunc{
  1627. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1628. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1629. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1630. },
  1631. nodes: []string{"machine1", "machine2", "machine3"},
  1632. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &highPriority}},
  1633. pods: []*v1.Pod{
  1634. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1635. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1636. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1637. {ObjectMeta: metav1.ObjectMeta{Name: "m2.2", UID: types.UID("m2.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &lowPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1638. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1639. {ObjectMeta: metav1.ObjectMeta{Name: "m3.2", UID: types.UID("m3.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1640. },
  1641. expected: []string{"machine2"},
  1642. },
  1643. {
  1644. name: "when highest priority and sum are the same, minimum number of pods is picked",
  1645. registerPlugins: []st.RegisterPluginFunc{
  1646. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1647. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1648. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1649. },
  1650. nodes: []string{"machine1", "machine2", "machine3"},
  1651. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &highPriority}},
  1652. pods: []*v1.Pod{
  1653. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1654. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &negPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1655. {ObjectMeta: metav1.ObjectMeta{Name: "m1.3", UID: types.UID("m1.3")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1656. {ObjectMeta: metav1.ObjectMeta{Name: "m1.4", UID: types.UID("m1.4")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &negPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1657. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1658. {ObjectMeta: metav1.ObjectMeta{Name: "m2.2", UID: types.UID("m2.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &negPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1659. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1660. {ObjectMeta: metav1.ObjectMeta{Name: "m3.2", UID: types.UID("m3.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &negPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1661. {ObjectMeta: metav1.ObjectMeta{Name: "m3.3", UID: types.UID("m3.3")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1662. },
  1663. expected: []string{"machine2"},
  1664. },
  1665. {
  1666. // pickOneNodeForPreemption adjusts pod priorities when finding the sum of the victims. This
  1667. // test ensures that the logic works correctly.
  1668. name: "sum of adjusted priorities is considered",
  1669. registerPlugins: []st.RegisterPluginFunc{
  1670. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1671. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1672. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1673. },
  1674. nodes: []string{"machine1", "machine2", "machine3"},
  1675. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &highPriority}},
  1676. pods: []*v1.Pod{
  1677. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1678. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &negPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1679. {ObjectMeta: metav1.ObjectMeta{Name: "m1.3", UID: types.UID("m1.3")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &negPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1680. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1681. {ObjectMeta: metav1.ObjectMeta{Name: "m2.2", UID: types.UID("m2.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &negPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1682. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1683. {ObjectMeta: metav1.ObjectMeta{Name: "m3.2", UID: types.UID("m3.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &negPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1684. {ObjectMeta: metav1.ObjectMeta{Name: "m3.3", UID: types.UID("m3.3")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1685. },
  1686. expected: []string{"machine2"},
  1687. },
  1688. {
  1689. name: "non-overlapping lowest high priority, sum priorities, and number of pods",
  1690. registerPlugins: []st.RegisterPluginFunc{
  1691. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1692. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1693. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1694. },
  1695. nodes: []string{"machine1", "machine2", "machine3", "machine4"},
  1696. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &veryHighPriority}},
  1697. pods: []*v1.Pod{
  1698. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1699. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1700. {ObjectMeta: metav1.ObjectMeta{Name: "m1.3", UID: types.UID("m1.3")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime}},
  1701. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime}},
  1702. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1703. {ObjectMeta: metav1.ObjectMeta{Name: "m3.2", UID: types.UID("m3.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1704. {ObjectMeta: metav1.ObjectMeta{Name: "m3.3", UID: types.UID("m3.3")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1705. {ObjectMeta: metav1.ObjectMeta{Name: "m3.4", UID: types.UID("m3.4")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &lowPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime}},
  1706. {ObjectMeta: metav1.ObjectMeta{Name: "m4.1", UID: types.UID("m4.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine4"}, Status: v1.PodStatus{StartTime: &startTime}},
  1707. {ObjectMeta: metav1.ObjectMeta{Name: "m4.2", UID: types.UID("m4.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine4"}, Status: v1.PodStatus{StartTime: &startTime}},
  1708. {ObjectMeta: metav1.ObjectMeta{Name: "m4.3", UID: types.UID("m4.3")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine4"}, Status: v1.PodStatus{StartTime: &startTime}},
  1709. {ObjectMeta: metav1.ObjectMeta{Name: "m4.4", UID: types.UID("m4.4")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &negPriority, NodeName: "machine4"}, Status: v1.PodStatus{StartTime: &startTime}},
  1710. },
  1711. expected: []string{"machine1"},
  1712. },
  1713. {
  1714. name: "same priority, same number of victims, different start time for each machine's pod",
  1715. registerPlugins: []st.RegisterPluginFunc{
  1716. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1717. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1718. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1719. },
  1720. nodes: []string{"machine1", "machine2", "machine3"},
  1721. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &highPriority}},
  1722. pods: []*v1.Pod{
  1723. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190103}},
  1724. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190103}},
  1725. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime20190104}},
  1726. {ObjectMeta: metav1.ObjectMeta{Name: "m2.2", UID: types.UID("m2.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime20190104}},
  1727. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime20190102}},
  1728. {ObjectMeta: metav1.ObjectMeta{Name: "m3.2", UID: types.UID("m3.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime20190102}},
  1729. },
  1730. expected: []string{"machine2"},
  1731. },
  1732. {
  1733. name: "same priority, same number of victims, different start time for all pods",
  1734. registerPlugins: []st.RegisterPluginFunc{
  1735. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1736. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1737. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1738. },
  1739. nodes: []string{"machine1", "machine2", "machine3"},
  1740. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &highPriority}},
  1741. pods: []*v1.Pod{
  1742. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190105}},
  1743. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190103}},
  1744. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime20190106}},
  1745. {ObjectMeta: metav1.ObjectMeta{Name: "m2.2", UID: types.UID("m2.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime20190102}},
  1746. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime20190104}},
  1747. {ObjectMeta: metav1.ObjectMeta{Name: "m3.2", UID: types.UID("m3.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime20190107}},
  1748. },
  1749. expected: []string{"machine3"},
  1750. },
  1751. {
  1752. name: "different priority, same number of victims, different start time for all pods",
  1753. registerPlugins: []st.RegisterPluginFunc{
  1754. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1755. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1756. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1757. },
  1758. nodes: []string{"machine1", "machine2", "machine3"},
  1759. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: veryLargeContainers, Priority: &highPriority}},
  1760. pods: []*v1.Pod{
  1761. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190105}},
  1762. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190103}},
  1763. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime20190107}},
  1764. {ObjectMeta: metav1.ObjectMeta{Name: "m2.2", UID: types.UID("m2.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &lowPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime20190102}},
  1765. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &lowPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime20190104}},
  1766. {ObjectMeta: metav1.ObjectMeta{Name: "m3.2", UID: types.UID("m3.2")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{StartTime: &startTime20190106}},
  1767. },
  1768. expected: []string{"machine2"},
  1769. },
  1770. }
  1771. for _, test := range tests {
  1772. t.Run(test.name, func(t *testing.T) {
  1773. var nodes []*v1.Node
  1774. for _, n := range test.nodes {
  1775. nodes = append(nodes, makeNode(n, schedutil.DefaultMilliCPURequest*5, schedutil.DefaultMemoryRequest*5))
  1776. }
  1777. snapshot := internalcache.NewSnapshot(test.pods, nodes)
  1778. fwk, err := st.NewFramework(test.registerPlugins, framework.WithSnapshotSharedLister(snapshot))
  1779. if err != nil {
  1780. t.Fatal(err)
  1781. }
  1782. g := &genericScheduler{
  1783. framework: fwk,
  1784. nodeInfoSnapshot: snapshot,
  1785. }
  1786. assignDefaultStartTime(test.pods)
  1787. nodeInfos, err := nodesToNodeInfos(nodes, snapshot)
  1788. if err != nil {
  1789. t.Fatal(err)
  1790. }
  1791. state := framework.NewCycleState()
  1792. // Some tests rely on PreFilter plugin to compute its CycleState.
  1793. preFilterStatus := fwk.RunPreFilterPlugins(context.Background(), state, test.pod)
  1794. if !preFilterStatus.IsSuccess() {
  1795. t.Errorf("Unexpected preFilterStatus: %v", preFilterStatus)
  1796. }
  1797. candidateNodes, _ := g.selectNodesForPreemption(context.Background(), state, test.pod, nodeInfos, nil)
  1798. node := pickOneNodeForPreemption(candidateNodes)
  1799. found := false
  1800. for _, nodeName := range test.expected {
  1801. if node.Name == nodeName {
  1802. found = true
  1803. break
  1804. }
  1805. }
  1806. if !found {
  1807. t.Errorf("unexpected node: %v", node)
  1808. }
  1809. })
  1810. }
  1811. }
  1812. func TestNodesWherePreemptionMightHelp(t *testing.T) {
  1813. // Prepare 4 node names.
  1814. nodeNames := make([]string, 0, 4)
  1815. for i := 1; i < 5; i++ {
  1816. nodeNames = append(nodeNames, fmt.Sprintf("machine%d", i))
  1817. }
  1818. tests := []struct {
  1819. name string
  1820. nodesStatuses framework.NodeToStatusMap
  1821. expected map[string]bool // set of expected node names. Value is ignored.
  1822. }{
  1823. {
  1824. name: "No node should be attempted",
  1825. nodesStatuses: framework.NodeToStatusMap{
  1826. "machine1": framework.NewStatus(framework.UnschedulableAndUnresolvable, nodeaffinity.ErrReason),
  1827. "machine2": framework.NewStatus(framework.UnschedulableAndUnresolvable, nodename.ErrReason),
  1828. "machine3": framework.NewStatus(framework.UnschedulableAndUnresolvable, tainttoleration.ErrReasonNotMatch),
  1829. "machine4": framework.NewStatus(framework.UnschedulableAndUnresolvable, nodelabel.ErrReasonPresenceViolated),
  1830. },
  1831. expected: map[string]bool{},
  1832. },
  1833. {
  1834. name: "ErrReasonAffinityNotMatch should be tried as it indicates that the pod is unschedulable due to inter-pod affinity or anti-affinity",
  1835. nodesStatuses: framework.NodeToStatusMap{
  1836. "machine1": framework.NewStatus(framework.Unschedulable, interpodaffinity.ErrReasonAffinityNotMatch),
  1837. "machine2": framework.NewStatus(framework.UnschedulableAndUnresolvable, nodename.ErrReason),
  1838. "machine3": framework.NewStatus(framework.UnschedulableAndUnresolvable, nodeunschedulable.ErrReasonUnschedulable),
  1839. },
  1840. expected: map[string]bool{"machine1": true, "machine4": true},
  1841. },
  1842. {
  1843. name: "pod with both pod affinity and anti-affinity should be tried",
  1844. nodesStatuses: framework.NodeToStatusMap{
  1845. "machine1": framework.NewStatus(framework.Unschedulable, interpodaffinity.ErrReasonAffinityNotMatch),
  1846. "machine2": framework.NewStatus(framework.UnschedulableAndUnresolvable, nodename.ErrReason),
  1847. },
  1848. expected: map[string]bool{"machine1": true, "machine3": true, "machine4": true},
  1849. },
  1850. {
  1851. name: "ErrReasonAffinityRulesNotMatch should not be tried as it indicates that the pod is unschedulable due to inter-pod affinity, but ErrReasonAffinityNotMatch should be tried as it indicates that the pod is unschedulable due to inter-pod affinity or anti-affinity",
  1852. nodesStatuses: framework.NodeToStatusMap{
  1853. "machine1": framework.NewStatus(framework.UnschedulableAndUnresolvable, interpodaffinity.ErrReasonAffinityRulesNotMatch),
  1854. "machine2": framework.NewStatus(framework.Unschedulable, interpodaffinity.ErrReasonAffinityNotMatch),
  1855. },
  1856. expected: map[string]bool{"machine2": true, "machine3": true, "machine4": true},
  1857. },
  1858. {
  1859. name: "Mix of failed predicates works fine",
  1860. nodesStatuses: framework.NodeToStatusMap{
  1861. "machine1": framework.NewStatus(framework.UnschedulableAndUnresolvable, volumerestrictions.ErrReasonDiskConflict),
  1862. "machine2": framework.NewStatus(framework.Unschedulable, fmt.Sprintf("Insufficient %v", v1.ResourceMemory)),
  1863. },
  1864. expected: map[string]bool{"machine2": true, "machine3": true, "machine4": true},
  1865. },
  1866. {
  1867. name: "Node condition errors should be considered unresolvable",
  1868. nodesStatuses: framework.NodeToStatusMap{
  1869. "machine1": framework.NewStatus(framework.UnschedulableAndUnresolvable, nodeunschedulable.ErrReasonUnknownCondition),
  1870. },
  1871. expected: map[string]bool{"machine2": true, "machine3": true, "machine4": true},
  1872. },
  1873. {
  1874. name: "ErrVolume... errors should not be tried as it indicates that the pod is unschedulable due to no matching volumes for pod on node",
  1875. nodesStatuses: framework.NodeToStatusMap{
  1876. "machine1": framework.NewStatus(framework.UnschedulableAndUnresolvable, volumezone.ErrReasonConflict),
  1877. "machine2": framework.NewStatus(framework.UnschedulableAndUnresolvable, volumebinding.ErrReasonNodeConflict),
  1878. "machine3": framework.NewStatus(framework.UnschedulableAndUnresolvable, volumebinding.ErrReasonBindConflict),
  1879. },
  1880. expected: map[string]bool{"machine4": true},
  1881. },
  1882. {
  1883. name: "ErrTopologySpreadConstraintsNotMatch should be tried as it indicates that the pod is unschedulable due to topology spread constraints",
  1884. nodesStatuses: framework.NodeToStatusMap{
  1885. "machine1": framework.NewStatus(framework.Unschedulable, podtopologyspread.ErrReasonConstraintsNotMatch),
  1886. "machine2": framework.NewStatus(framework.UnschedulableAndUnresolvable, nodename.ErrReason),
  1887. "machine3": framework.NewStatus(framework.Unschedulable, podtopologyspread.ErrReasonConstraintsNotMatch),
  1888. },
  1889. expected: map[string]bool{"machine1": true, "machine3": true, "machine4": true},
  1890. },
  1891. {
  1892. name: "UnschedulableAndUnresolvable status should be skipped but Unschedulable should be tried",
  1893. nodesStatuses: framework.NodeToStatusMap{
  1894. "machine2": framework.NewStatus(framework.UnschedulableAndUnresolvable, ""),
  1895. "machine3": framework.NewStatus(framework.Unschedulable, ""),
  1896. "machine4": framework.NewStatus(framework.UnschedulableAndUnresolvable, ""),
  1897. },
  1898. expected: map[string]bool{"machine1": true, "machine3": true},
  1899. },
  1900. }
  1901. for _, test := range tests {
  1902. t.Run(test.name, func(t *testing.T) {
  1903. fitErr := FitError{
  1904. FilteredNodesStatuses: test.nodesStatuses,
  1905. }
  1906. var nodeInfos []*schedulernodeinfo.NodeInfo
  1907. for _, n := range makeNodeList(nodeNames) {
  1908. ni := schedulernodeinfo.NewNodeInfo()
  1909. ni.SetNode(n)
  1910. nodeInfos = append(nodeInfos, ni)
  1911. }
  1912. nodes := nodesWherePreemptionMightHelp(nodeInfos, &fitErr)
  1913. if len(test.expected) != len(nodes) {
  1914. t.Errorf("number of nodes is not the same as expected. exptectd: %d, got: %d. Nodes: %v", len(test.expected), len(nodes), nodes)
  1915. }
  1916. for _, node := range nodes {
  1917. name := node.Node().Name
  1918. if _, found := test.expected[name]; !found {
  1919. t.Errorf("node %v is not expected.", name)
  1920. }
  1921. }
  1922. })
  1923. }
  1924. }
  1925. func TestPreempt(t *testing.T) {
  1926. defaultFailedNodeToStatusMap := framework.NodeToStatusMap{
  1927. "machine1": framework.NewStatus(framework.Unschedulable, fmt.Sprintf("Insufficient %v", v1.ResourceMemory)),
  1928. "machine2": framework.NewStatus(framework.Unschedulable, volumerestrictions.ErrReasonDiskConflict),
  1929. "machine3": framework.NewStatus(framework.Unschedulable, fmt.Sprintf("Insufficient %v", v1.ResourceMemory)),
  1930. }
  1931. // Prepare 3 node names.
  1932. var defaultNodeNames []string
  1933. for i := 1; i < 4; i++ {
  1934. defaultNodeNames = append(defaultNodeNames, fmt.Sprintf("machine%d", i))
  1935. }
  1936. var (
  1937. preemptLowerPriority = v1.PreemptLowerPriority
  1938. preemptNever = v1.PreemptNever
  1939. )
  1940. tests := []struct {
  1941. name string
  1942. pod *v1.Pod
  1943. pods []*v1.Pod
  1944. extenders []*FakeExtender
  1945. failedNodeToStatusMap framework.NodeToStatusMap
  1946. nodeNames []string
  1947. registerPlugins []st.RegisterPluginFunc
  1948. expectedNode string
  1949. expectedPods []string // list of preempted pods
  1950. }{
  1951. {
  1952. name: "basic preemption logic",
  1953. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{
  1954. Containers: veryLargeContainers,
  1955. Priority: &highPriority,
  1956. PreemptionPolicy: &preemptLowerPriority},
  1957. },
  1958. pods: []*v1.Pod{
  1959. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  1960. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  1961. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  1962. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  1963. },
  1964. registerPlugins: []st.RegisterPluginFunc{
  1965. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1966. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1967. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1968. },
  1969. expectedNode: "machine1",
  1970. expectedPods: []string{"m1.1", "m1.2"},
  1971. },
  1972. {
  1973. name: "One node doesn't need any preemption",
  1974. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{
  1975. Containers: veryLargeContainers,
  1976. Priority: &highPriority,
  1977. PreemptionPolicy: &preemptLowerPriority},
  1978. },
  1979. pods: []*v1.Pod{
  1980. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  1981. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  1982. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  1983. },
  1984. registerPlugins: []st.RegisterPluginFunc{
  1985. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  1986. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  1987. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  1988. },
  1989. expectedNode: "machine3",
  1990. expectedPods: []string{},
  1991. },
  1992. {
  1993. name: "preemption for topology spread constraints",
  1994. pod: &v1.Pod{
  1995. ObjectMeta: metav1.ObjectMeta{
  1996. Name: "p",
  1997. Labels: map[string]string{"foo": ""},
  1998. },
  1999. Spec: v1.PodSpec{
  2000. Priority: &highPriority,
  2001. TopologySpreadConstraints: []v1.TopologySpreadConstraint{
  2002. {
  2003. MaxSkew: 1,
  2004. TopologyKey: "zone",
  2005. WhenUnsatisfiable: v1.DoNotSchedule,
  2006. LabelSelector: &metav1.LabelSelector{
  2007. MatchExpressions: []metav1.LabelSelectorRequirement{
  2008. {
  2009. Key: "foo",
  2010. Operator: metav1.LabelSelectorOpExists,
  2011. },
  2012. },
  2013. },
  2014. },
  2015. {
  2016. MaxSkew: 1,
  2017. TopologyKey: "hostname",
  2018. WhenUnsatisfiable: v1.DoNotSchedule,
  2019. LabelSelector: &metav1.LabelSelector{
  2020. MatchExpressions: []metav1.LabelSelectorRequirement{
  2021. {
  2022. Key: "foo",
  2023. Operator: metav1.LabelSelectorOpExists,
  2024. },
  2025. },
  2026. },
  2027. },
  2028. },
  2029. },
  2030. },
  2031. pods: []*v1.Pod{
  2032. {
  2033. ObjectMeta: metav1.ObjectMeta{Name: "pod-a1", UID: types.UID("pod-a1"), Labels: map[string]string{"foo": ""}},
  2034. Spec: v1.PodSpec{NodeName: "node-a", Priority: &highPriority},
  2035. Status: v1.PodStatus{Phase: v1.PodRunning},
  2036. },
  2037. {
  2038. ObjectMeta: metav1.ObjectMeta{Name: "pod-a2", UID: types.UID("pod-a2"), Labels: map[string]string{"foo": ""}},
  2039. Spec: v1.PodSpec{NodeName: "node-a", Priority: &highPriority},
  2040. Status: v1.PodStatus{Phase: v1.PodRunning},
  2041. },
  2042. {
  2043. ObjectMeta: metav1.ObjectMeta{Name: "pod-b1", UID: types.UID("pod-b1"), Labels: map[string]string{"foo": ""}},
  2044. Spec: v1.PodSpec{NodeName: "node-b", Priority: &lowPriority},
  2045. Status: v1.PodStatus{Phase: v1.PodRunning},
  2046. },
  2047. {
  2048. ObjectMeta: metav1.ObjectMeta{Name: "pod-x1", UID: types.UID("pod-x1"), Labels: map[string]string{"foo": ""}},
  2049. Spec: v1.PodSpec{NodeName: "node-x", Priority: &highPriority},
  2050. Status: v1.PodStatus{Phase: v1.PodRunning},
  2051. },
  2052. {
  2053. ObjectMeta: metav1.ObjectMeta{Name: "pod-x2", UID: types.UID("pod-x2"), Labels: map[string]string{"foo": ""}},
  2054. Spec: v1.PodSpec{NodeName: "node-x", Priority: &highPriority},
  2055. Status: v1.PodStatus{Phase: v1.PodRunning},
  2056. },
  2057. },
  2058. failedNodeToStatusMap: framework.NodeToStatusMap{
  2059. "node-a": framework.NewStatus(framework.Unschedulable, podtopologyspread.ErrReasonConstraintsNotMatch),
  2060. "node-b": framework.NewStatus(framework.Unschedulable, podtopologyspread.ErrReasonConstraintsNotMatch),
  2061. "node-x": framework.NewStatus(framework.Unschedulable, podtopologyspread.ErrReasonConstraintsNotMatch),
  2062. },
  2063. nodeNames: []string{"node-a/zone1", "node-b/zone1", "node-x/zone2"},
  2064. registerPlugins: []st.RegisterPluginFunc{
  2065. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  2066. st.RegisterPluginAsExtensions(
  2067. podtopologyspread.Name,
  2068. podtopologyspread.New,
  2069. "PreFilter",
  2070. "Filter",
  2071. ),
  2072. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  2073. },
  2074. expectedNode: "node-b",
  2075. expectedPods: []string{"pod-b1"},
  2076. },
  2077. {
  2078. name: "Scheduler extenders allow only machine1, otherwise machine3 would have been chosen",
  2079. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{
  2080. Containers: veryLargeContainers,
  2081. Priority: &highPriority,
  2082. PreemptionPolicy: &preemptLowerPriority},
  2083. },
  2084. pods: []*v1.Pod{
  2085. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2086. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2087. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2088. },
  2089. extenders: []*FakeExtender{
  2090. {
  2091. predicates: []fitPredicate{truePredicateExtender},
  2092. },
  2093. {
  2094. predicates: []fitPredicate{machine1PredicateExtender},
  2095. },
  2096. },
  2097. registerPlugins: []st.RegisterPluginFunc{
  2098. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  2099. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  2100. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  2101. },
  2102. expectedNode: "machine1",
  2103. expectedPods: []string{"m1.1", "m1.2"},
  2104. },
  2105. {
  2106. name: "Scheduler extenders do not allow any preemption",
  2107. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{
  2108. Containers: veryLargeContainers,
  2109. Priority: &highPriority,
  2110. PreemptionPolicy: &preemptLowerPriority},
  2111. },
  2112. pods: []*v1.Pod{
  2113. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2114. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2115. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2116. },
  2117. extenders: []*FakeExtender{
  2118. {
  2119. predicates: []fitPredicate{falsePredicateExtender},
  2120. },
  2121. },
  2122. registerPlugins: []st.RegisterPluginFunc{
  2123. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  2124. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  2125. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  2126. },
  2127. expectedNode: "",
  2128. expectedPods: []string{},
  2129. },
  2130. {
  2131. name: "One scheduler extender allows only machine1, the other returns error but ignorable. Only machine1 would be chosen",
  2132. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{
  2133. Containers: veryLargeContainers,
  2134. Priority: &highPriority,
  2135. PreemptionPolicy: &preemptLowerPriority},
  2136. },
  2137. pods: []*v1.Pod{
  2138. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2139. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2140. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2141. },
  2142. extenders: []*FakeExtender{
  2143. {
  2144. predicates: []fitPredicate{errorPredicateExtender},
  2145. ignorable: true,
  2146. },
  2147. {
  2148. predicates: []fitPredicate{machine1PredicateExtender},
  2149. },
  2150. },
  2151. registerPlugins: []st.RegisterPluginFunc{
  2152. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  2153. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  2154. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  2155. },
  2156. expectedNode: "machine1",
  2157. expectedPods: []string{"m1.1", "m1.2"},
  2158. },
  2159. {
  2160. name: "One scheduler extender allows only machine1, but it is not interested in given pod, otherwise machine1 would have been chosen",
  2161. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{
  2162. Containers: veryLargeContainers,
  2163. Priority: &highPriority,
  2164. PreemptionPolicy: &preemptLowerPriority},
  2165. },
  2166. pods: []*v1.Pod{
  2167. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2168. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2169. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &midPriority, NodeName: "machine2"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2170. },
  2171. extenders: []*FakeExtender{
  2172. {
  2173. predicates: []fitPredicate{machine1PredicateExtender},
  2174. unInterested: true,
  2175. },
  2176. {
  2177. predicates: []fitPredicate{truePredicateExtender},
  2178. },
  2179. },
  2180. registerPlugins: []st.RegisterPluginFunc{
  2181. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  2182. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  2183. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  2184. },
  2185. expectedNode: "machine3",
  2186. expectedPods: []string{},
  2187. },
  2188. {
  2189. name: "no preempting in pod",
  2190. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{
  2191. Containers: veryLargeContainers,
  2192. Priority: &highPriority,
  2193. PreemptionPolicy: &preemptNever},
  2194. },
  2195. pods: []*v1.Pod{
  2196. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2197. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2198. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2199. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2200. },
  2201. registerPlugins: []st.RegisterPluginFunc{
  2202. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  2203. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  2204. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  2205. },
  2206. expectedNode: "",
  2207. expectedPods: nil,
  2208. },
  2209. {
  2210. name: "PreemptionPolicy is nil",
  2211. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", UID: types.UID("pod1")}, Spec: v1.PodSpec{
  2212. Containers: veryLargeContainers,
  2213. Priority: &highPriority,
  2214. PreemptionPolicy: nil},
  2215. },
  2216. pods: []*v1.Pod{
  2217. {ObjectMeta: metav1.ObjectMeta{Name: "m1.1", UID: types.UID("m1.1")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2218. {ObjectMeta: metav1.ObjectMeta{Name: "m1.2", UID: types.UID("m1.2")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2219. {ObjectMeta: metav1.ObjectMeta{Name: "m2.1", UID: types.UID("m2.1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2220. {ObjectMeta: metav1.ObjectMeta{Name: "m3.1", UID: types.UID("m3.1")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine3"}, Status: v1.PodStatus{Phase: v1.PodRunning}},
  2221. },
  2222. registerPlugins: []st.RegisterPluginFunc{
  2223. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  2224. st.RegisterPluginAsExtensions(noderesources.FitName, noderesources.NewFit, "Filter", "PreFilter"),
  2225. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  2226. },
  2227. expectedNode: "machine1",
  2228. expectedPods: []string{"m1.1", "m1.2"},
  2229. },
  2230. }
  2231. labelKeys := []string{"hostname", "zone", "region"}
  2232. for _, test := range tests {
  2233. t.Run(test.name, func(t *testing.T) {
  2234. client := clientsetfake.NewSimpleClientset()
  2235. informerFactory := informers.NewSharedInformerFactory(client, 0)
  2236. stop := make(chan struct{})
  2237. cache := internalcache.New(time.Duration(0), stop)
  2238. for _, pod := range test.pods {
  2239. cache.AddPod(pod)
  2240. }
  2241. cachedNodeInfoMap := map[string]*schedulernodeinfo.NodeInfo{}
  2242. nodeNames := defaultNodeNames
  2243. if len(test.nodeNames) != 0 {
  2244. nodeNames = test.nodeNames
  2245. }
  2246. var nodes []*v1.Node
  2247. for i, name := range nodeNames {
  2248. node := makeNode(name, 1000*5, schedutil.DefaultMemoryRequest*5)
  2249. // if possible, split node name by '/' to form labels in a format of
  2250. // {"hostname": node.Name[0], "zone": node.Name[1], "region": node.Name[2]}
  2251. node.ObjectMeta.Labels = make(map[string]string)
  2252. for i, label := range strings.Split(node.Name, "/") {
  2253. node.ObjectMeta.Labels[labelKeys[i]] = label
  2254. }
  2255. node.Name = node.ObjectMeta.Labels["hostname"]
  2256. cache.AddNode(node)
  2257. nodes = append(nodes, node)
  2258. nodeNames[i] = node.Name
  2259. // Set nodeInfo to extenders to mock extenders' cache for preemption.
  2260. cachedNodeInfo := schedulernodeinfo.NewNodeInfo()
  2261. cachedNodeInfo.SetNode(node)
  2262. cachedNodeInfoMap[node.Name] = cachedNodeInfo
  2263. }
  2264. var extenders []SchedulerExtender
  2265. for _, extender := range test.extenders {
  2266. // Set nodeInfoMap as extenders cached node information.
  2267. extender.cachedNodeNameToInfo = cachedNodeInfoMap
  2268. extenders = append(extenders, extender)
  2269. }
  2270. snapshot := internalcache.NewSnapshot(test.pods, nodes)
  2271. fwk, err := st.NewFramework(test.registerPlugins, framework.WithSnapshotSharedLister(snapshot))
  2272. if err != nil {
  2273. t.Fatal(err)
  2274. }
  2275. scheduler := NewGenericScheduler(
  2276. cache,
  2277. internalqueue.NewSchedulingQueue(nil),
  2278. snapshot,
  2279. fwk,
  2280. extenders,
  2281. nil,
  2282. informerFactory.Core().V1().PersistentVolumeClaims().Lister(),
  2283. informerFactory.Policy().V1beta1().PodDisruptionBudgets().Lister(),
  2284. false,
  2285. schedulerapi.DefaultPercentageOfNodesToScore,
  2286. true)
  2287. state := framework.NewCycleState()
  2288. // Some tests rely on PreFilter plugin to compute its CycleState.
  2289. preFilterStatus := fwk.RunPreFilterPlugins(context.Background(), state, test.pod)
  2290. if !preFilterStatus.IsSuccess() {
  2291. t.Errorf("Unexpected preFilterStatus: %v", preFilterStatus)
  2292. }
  2293. // Call Preempt and check the expected results.
  2294. failedNodeToStatusMap := defaultFailedNodeToStatusMap
  2295. if test.failedNodeToStatusMap != nil {
  2296. failedNodeToStatusMap = test.failedNodeToStatusMap
  2297. }
  2298. node, victims, _, err := scheduler.Preempt(context.Background(), state, test.pod, error(&FitError{Pod: test.pod, FilteredNodesStatuses: failedNodeToStatusMap}))
  2299. if err != nil {
  2300. t.Errorf("unexpected error in preemption: %v", err)
  2301. }
  2302. if node != nil && node.Name != test.expectedNode {
  2303. t.Errorf("expected node: %v, got: %v", test.expectedNode, node.GetName())
  2304. }
  2305. if node == nil && len(test.expectedNode) != 0 {
  2306. t.Errorf("expected node: %v, got: nothing", test.expectedNode)
  2307. }
  2308. if len(victims) != len(test.expectedPods) {
  2309. t.Errorf("expected %v pods, got %v.", len(test.expectedPods), len(victims))
  2310. }
  2311. for _, victim := range victims {
  2312. found := false
  2313. for _, expPod := range test.expectedPods {
  2314. if expPod == victim.Name {
  2315. found = true
  2316. break
  2317. }
  2318. }
  2319. if !found {
  2320. t.Errorf("pod %v is not expected to be a victim.", victim.Name)
  2321. }
  2322. // Mark the victims for deletion and record the preemptor's nominated node name.
  2323. now := metav1.Now()
  2324. victim.DeletionTimestamp = &now
  2325. test.pod.Status.NominatedNodeName = node.Name
  2326. }
  2327. // Call preempt again and make sure it doesn't preempt any more pods.
  2328. node, victims, _, err = scheduler.Preempt(context.Background(), state, test.pod, error(&FitError{Pod: test.pod, FilteredNodesStatuses: failedNodeToStatusMap}))
  2329. if err != nil {
  2330. t.Errorf("unexpected error in preemption: %v", err)
  2331. }
  2332. if node != nil && len(victims) > 0 {
  2333. t.Errorf("didn't expect any more preemption. Node %v is selected for preemption.", node)
  2334. }
  2335. close(stop)
  2336. })
  2337. }
  2338. }
  2339. func TestNumFeasibleNodesToFind(t *testing.T) {
  2340. tests := []struct {
  2341. name string
  2342. percentageOfNodesToScore int32
  2343. numAllNodes int32
  2344. wantNumNodes int32
  2345. }{
  2346. {
  2347. name: "not set percentageOfNodesToScore and nodes number not more than 50",
  2348. numAllNodes: 10,
  2349. wantNumNodes: 10,
  2350. },
  2351. {
  2352. name: "set percentageOfNodesToScore and nodes number not more than 50",
  2353. percentageOfNodesToScore: 40,
  2354. numAllNodes: 10,
  2355. wantNumNodes: 10,
  2356. },
  2357. {
  2358. name: "not set percentageOfNodesToScore and nodes number more than 50",
  2359. numAllNodes: 1000,
  2360. wantNumNodes: 420,
  2361. },
  2362. {
  2363. name: "set percentageOfNodesToScore and nodes number more than 50",
  2364. percentageOfNodesToScore: 40,
  2365. numAllNodes: 1000,
  2366. wantNumNodes: 400,
  2367. },
  2368. {
  2369. name: "not set percentageOfNodesToScore and nodes number more than 50*125",
  2370. numAllNodes: 6000,
  2371. wantNumNodes: 300,
  2372. },
  2373. {
  2374. name: "set percentageOfNodesToScore and nodes number more than 50*125",
  2375. percentageOfNodesToScore: 40,
  2376. numAllNodes: 6000,
  2377. wantNumNodes: 2400,
  2378. },
  2379. }
  2380. for _, tt := range tests {
  2381. t.Run(tt.name, func(t *testing.T) {
  2382. g := &genericScheduler{
  2383. percentageOfNodesToScore: tt.percentageOfNodesToScore,
  2384. }
  2385. if gotNumNodes := g.numFeasibleNodesToFind(tt.numAllNodes); gotNumNodes != tt.wantNumNodes {
  2386. t.Errorf("genericScheduler.numFeasibleNodesToFind() = %v, want %v", gotNumNodes, tt.wantNumNodes)
  2387. }
  2388. })
  2389. }
  2390. }
  2391. func assignDefaultStartTime(pods []*v1.Pod) {
  2392. now := metav1.Now()
  2393. for i := range pods {
  2394. pod := pods[i]
  2395. if pod.Status.StartTime == nil {
  2396. pod.Status.StartTime = &now
  2397. }
  2398. }
  2399. }
  2400. func TestFairEvaluationForNodes(t *testing.T) {
  2401. numAllNodes := 500
  2402. nodeNames := make([]string, 0, numAllNodes)
  2403. for i := 0; i < numAllNodes; i++ {
  2404. nodeNames = append(nodeNames, strconv.Itoa(i))
  2405. }
  2406. nodes := makeNodeList(nodeNames)
  2407. g := makeScheduler(
  2408. nodes,
  2409. st.RegisterQueueSortPlugin(queuesort.Name, queuesort.New),
  2410. st.RegisterFilterPlugin("TrueFilter", NewTrueFilterPlugin),
  2411. st.RegisterBindPlugin(defaultbinder.Name, defaultbinder.New),
  2412. )
  2413. // To make numAllNodes % nodesToFind != 0
  2414. g.percentageOfNodesToScore = 30
  2415. nodesToFind := int(g.numFeasibleNodesToFind(int32(numAllNodes)))
  2416. // Iterating over all nodes more than twice
  2417. for i := 0; i < 2*(numAllNodes/nodesToFind+1); i++ {
  2418. nodesThatFit, _, err := g.findNodesThatFitPod(context.Background(), framework.NewCycleState(), &v1.Pod{})
  2419. if err != nil {
  2420. t.Errorf("unexpected error: %v", err)
  2421. }
  2422. if len(nodesThatFit) != nodesToFind {
  2423. t.Errorf("got %d nodes filtered, want %d", len(nodesThatFit), nodesToFind)
  2424. }
  2425. if g.nextStartNodeIndex != (i+1)*nodesToFind%numAllNodes {
  2426. t.Errorf("got %d lastProcessedNodeIndex, want %d", g.nextStartNodeIndex, (i+1)*nodesToFind%numAllNodes)
  2427. }
  2428. }
  2429. }
  2430. func nodesToNodeInfos(nodes []*v1.Node, snapshot *internalcache.Snapshot) ([]*schedulernodeinfo.NodeInfo, error) {
  2431. var nodeInfos []*schedulernodeinfo.NodeInfo
  2432. for _, n := range nodes {
  2433. nodeInfo, err := snapshot.NodeInfos().Get(n.Name)
  2434. if err != nil {
  2435. return nil, err
  2436. }
  2437. nodeInfos = append(nodeInfos, nodeInfo)
  2438. }
  2439. return nodeInfos, nil
  2440. }