node_affinity_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. Copyright 2019 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 nodeaffinity
  14. import (
  15. "context"
  16. "reflect"
  17. "testing"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  22. "k8s.io/kubernetes/pkg/scheduler/internal/cache"
  23. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  24. )
  25. // TODO: Add test case for RequiredDuringSchedulingRequiredDuringExecution after it's implemented.
  26. func TestNodeAffinity(t *testing.T) {
  27. tests := []struct {
  28. pod *v1.Pod
  29. labels map[string]string
  30. nodeName string
  31. name string
  32. wantStatus *framework.Status
  33. }{
  34. {
  35. pod: &v1.Pod{},
  36. name: "no selector",
  37. },
  38. {
  39. pod: &v1.Pod{
  40. Spec: v1.PodSpec{
  41. NodeSelector: map[string]string{
  42. "foo": "bar",
  43. },
  44. },
  45. },
  46. name: "missing labels",
  47. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  48. },
  49. {
  50. pod: &v1.Pod{
  51. Spec: v1.PodSpec{
  52. NodeSelector: map[string]string{
  53. "foo": "bar",
  54. },
  55. },
  56. },
  57. labels: map[string]string{
  58. "foo": "bar",
  59. },
  60. name: "same labels",
  61. },
  62. {
  63. pod: &v1.Pod{
  64. Spec: v1.PodSpec{
  65. NodeSelector: map[string]string{
  66. "foo": "bar",
  67. },
  68. },
  69. },
  70. labels: map[string]string{
  71. "foo": "bar",
  72. "baz": "blah",
  73. },
  74. name: "node labels are superset",
  75. },
  76. {
  77. pod: &v1.Pod{
  78. Spec: v1.PodSpec{
  79. NodeSelector: map[string]string{
  80. "foo": "bar",
  81. "baz": "blah",
  82. },
  83. },
  84. },
  85. labels: map[string]string{
  86. "foo": "bar",
  87. },
  88. name: "node labels are subset",
  89. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  90. },
  91. {
  92. pod: &v1.Pod{
  93. Spec: v1.PodSpec{
  94. Affinity: &v1.Affinity{
  95. NodeAffinity: &v1.NodeAffinity{
  96. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  97. NodeSelectorTerms: []v1.NodeSelectorTerm{
  98. {
  99. MatchExpressions: []v1.NodeSelectorRequirement{
  100. {
  101. Key: "foo",
  102. Operator: v1.NodeSelectorOpIn,
  103. Values: []string{"bar", "value2"},
  104. },
  105. },
  106. },
  107. },
  108. },
  109. },
  110. },
  111. },
  112. },
  113. labels: map[string]string{
  114. "foo": "bar",
  115. },
  116. name: "Pod with matchExpressions using In operator that matches the existing node",
  117. },
  118. {
  119. pod: &v1.Pod{
  120. Spec: v1.PodSpec{
  121. Affinity: &v1.Affinity{
  122. NodeAffinity: &v1.NodeAffinity{
  123. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  124. NodeSelectorTerms: []v1.NodeSelectorTerm{
  125. {
  126. MatchExpressions: []v1.NodeSelectorRequirement{
  127. {
  128. Key: "kernel-version",
  129. Operator: v1.NodeSelectorOpGt,
  130. Values: []string{"0204"},
  131. },
  132. },
  133. },
  134. },
  135. },
  136. },
  137. },
  138. },
  139. },
  140. labels: map[string]string{
  141. // We use two digit to denote major version and two digit for minor version.
  142. "kernel-version": "0206",
  143. },
  144. name: "Pod with matchExpressions using Gt operator that matches the existing node",
  145. },
  146. {
  147. pod: &v1.Pod{
  148. Spec: v1.PodSpec{
  149. Affinity: &v1.Affinity{
  150. NodeAffinity: &v1.NodeAffinity{
  151. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  152. NodeSelectorTerms: []v1.NodeSelectorTerm{
  153. {
  154. MatchExpressions: []v1.NodeSelectorRequirement{
  155. {
  156. Key: "mem-type",
  157. Operator: v1.NodeSelectorOpNotIn,
  158. Values: []string{"DDR", "DDR2"},
  159. },
  160. },
  161. },
  162. },
  163. },
  164. },
  165. },
  166. },
  167. },
  168. labels: map[string]string{
  169. "mem-type": "DDR3",
  170. },
  171. name: "Pod with matchExpressions using NotIn operator that matches the existing node",
  172. },
  173. {
  174. pod: &v1.Pod{
  175. Spec: v1.PodSpec{
  176. Affinity: &v1.Affinity{
  177. NodeAffinity: &v1.NodeAffinity{
  178. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  179. NodeSelectorTerms: []v1.NodeSelectorTerm{
  180. {
  181. MatchExpressions: []v1.NodeSelectorRequirement{
  182. {
  183. Key: "GPU",
  184. Operator: v1.NodeSelectorOpExists,
  185. },
  186. },
  187. },
  188. },
  189. },
  190. },
  191. },
  192. },
  193. },
  194. labels: map[string]string{
  195. "GPU": "NVIDIA-GRID-K1",
  196. },
  197. name: "Pod with matchExpressions using Exists operator that matches the existing node",
  198. },
  199. {
  200. pod: &v1.Pod{
  201. Spec: v1.PodSpec{
  202. Affinity: &v1.Affinity{
  203. NodeAffinity: &v1.NodeAffinity{
  204. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  205. NodeSelectorTerms: []v1.NodeSelectorTerm{
  206. {
  207. MatchExpressions: []v1.NodeSelectorRequirement{
  208. {
  209. Key: "foo",
  210. Operator: v1.NodeSelectorOpIn,
  211. Values: []string{"value1", "value2"},
  212. },
  213. },
  214. },
  215. },
  216. },
  217. },
  218. },
  219. },
  220. },
  221. labels: map[string]string{
  222. "foo": "bar",
  223. },
  224. name: "Pod with affinity that don't match node's labels won't schedule onto the node",
  225. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  226. },
  227. {
  228. pod: &v1.Pod{
  229. Spec: v1.PodSpec{
  230. Affinity: &v1.Affinity{
  231. NodeAffinity: &v1.NodeAffinity{
  232. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  233. NodeSelectorTerms: nil,
  234. },
  235. },
  236. },
  237. },
  238. },
  239. labels: map[string]string{
  240. "foo": "bar",
  241. },
  242. name: "Pod with a nil []NodeSelectorTerm in affinity, can't match the node's labels and won't schedule onto the node",
  243. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  244. },
  245. {
  246. pod: &v1.Pod{
  247. Spec: v1.PodSpec{
  248. Affinity: &v1.Affinity{
  249. NodeAffinity: &v1.NodeAffinity{
  250. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  251. NodeSelectorTerms: []v1.NodeSelectorTerm{},
  252. },
  253. },
  254. },
  255. },
  256. },
  257. labels: map[string]string{
  258. "foo": "bar",
  259. },
  260. name: "Pod with an empty []NodeSelectorTerm in affinity, can't match the node's labels and won't schedule onto the node",
  261. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  262. },
  263. {
  264. pod: &v1.Pod{
  265. Spec: v1.PodSpec{
  266. Affinity: &v1.Affinity{
  267. NodeAffinity: &v1.NodeAffinity{
  268. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  269. NodeSelectorTerms: []v1.NodeSelectorTerm{
  270. {
  271. MatchExpressions: []v1.NodeSelectorRequirement{},
  272. },
  273. },
  274. },
  275. },
  276. },
  277. },
  278. },
  279. labels: map[string]string{
  280. "foo": "bar",
  281. },
  282. name: "Pod with empty MatchExpressions is not a valid value will match no objects and won't schedule onto the node",
  283. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  284. },
  285. {
  286. pod: &v1.Pod{},
  287. labels: map[string]string{
  288. "foo": "bar",
  289. },
  290. name: "Pod with no Affinity will schedule onto a node",
  291. },
  292. {
  293. pod: &v1.Pod{
  294. Spec: v1.PodSpec{
  295. Affinity: &v1.Affinity{
  296. NodeAffinity: &v1.NodeAffinity{
  297. RequiredDuringSchedulingIgnoredDuringExecution: nil,
  298. },
  299. },
  300. },
  301. },
  302. labels: map[string]string{
  303. "foo": "bar",
  304. },
  305. name: "Pod with Affinity but nil NodeSelector will schedule onto a node",
  306. },
  307. {
  308. pod: &v1.Pod{
  309. Spec: v1.PodSpec{
  310. Affinity: &v1.Affinity{
  311. NodeAffinity: &v1.NodeAffinity{
  312. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  313. NodeSelectorTerms: []v1.NodeSelectorTerm{
  314. {
  315. MatchExpressions: []v1.NodeSelectorRequirement{
  316. {
  317. Key: "GPU",
  318. Operator: v1.NodeSelectorOpExists,
  319. }, {
  320. Key: "GPU",
  321. Operator: v1.NodeSelectorOpNotIn,
  322. Values: []string{"AMD", "INTER"},
  323. },
  324. },
  325. },
  326. },
  327. },
  328. },
  329. },
  330. },
  331. },
  332. labels: map[string]string{
  333. "GPU": "NVIDIA-GRID-K1",
  334. },
  335. name: "Pod with multiple matchExpressions ANDed that matches the existing node",
  336. },
  337. {
  338. pod: &v1.Pod{
  339. Spec: v1.PodSpec{
  340. Affinity: &v1.Affinity{
  341. NodeAffinity: &v1.NodeAffinity{
  342. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  343. NodeSelectorTerms: []v1.NodeSelectorTerm{
  344. {
  345. MatchExpressions: []v1.NodeSelectorRequirement{
  346. {
  347. Key: "GPU",
  348. Operator: v1.NodeSelectorOpExists,
  349. }, {
  350. Key: "GPU",
  351. Operator: v1.NodeSelectorOpIn,
  352. Values: []string{"AMD", "INTER"},
  353. },
  354. },
  355. },
  356. },
  357. },
  358. },
  359. },
  360. },
  361. },
  362. labels: map[string]string{
  363. "GPU": "NVIDIA-GRID-K1",
  364. },
  365. name: "Pod with multiple matchExpressions ANDed that doesn't match the existing node",
  366. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  367. },
  368. {
  369. pod: &v1.Pod{
  370. Spec: v1.PodSpec{
  371. Affinity: &v1.Affinity{
  372. NodeAffinity: &v1.NodeAffinity{
  373. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  374. NodeSelectorTerms: []v1.NodeSelectorTerm{
  375. {
  376. MatchExpressions: []v1.NodeSelectorRequirement{
  377. {
  378. Key: "foo",
  379. Operator: v1.NodeSelectorOpIn,
  380. Values: []string{"bar", "value2"},
  381. },
  382. },
  383. },
  384. {
  385. MatchExpressions: []v1.NodeSelectorRequirement{
  386. {
  387. Key: "diffkey",
  388. Operator: v1.NodeSelectorOpIn,
  389. Values: []string{"wrong", "value2"},
  390. },
  391. },
  392. },
  393. },
  394. },
  395. },
  396. },
  397. },
  398. },
  399. labels: map[string]string{
  400. "foo": "bar",
  401. },
  402. name: "Pod with multiple NodeSelectorTerms ORed in affinity, matches the node's labels and will schedule onto the node",
  403. },
  404. {
  405. pod: &v1.Pod{
  406. Spec: v1.PodSpec{
  407. NodeSelector: map[string]string{
  408. "foo": "bar",
  409. },
  410. Affinity: &v1.Affinity{
  411. NodeAffinity: &v1.NodeAffinity{
  412. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  413. NodeSelectorTerms: []v1.NodeSelectorTerm{
  414. {
  415. MatchExpressions: []v1.NodeSelectorRequirement{
  416. {
  417. Key: "foo",
  418. Operator: v1.NodeSelectorOpExists,
  419. },
  420. },
  421. },
  422. },
  423. },
  424. },
  425. },
  426. },
  427. },
  428. labels: map[string]string{
  429. "foo": "bar",
  430. },
  431. name: "Pod with an Affinity and a PodSpec.NodeSelector(the old thing that we are deprecating) " +
  432. "both are satisfied, will schedule onto the node",
  433. },
  434. {
  435. pod: &v1.Pod{
  436. Spec: v1.PodSpec{
  437. NodeSelector: map[string]string{
  438. "foo": "bar",
  439. },
  440. Affinity: &v1.Affinity{
  441. NodeAffinity: &v1.NodeAffinity{
  442. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  443. NodeSelectorTerms: []v1.NodeSelectorTerm{
  444. {
  445. MatchExpressions: []v1.NodeSelectorRequirement{
  446. {
  447. Key: "foo",
  448. Operator: v1.NodeSelectorOpExists,
  449. },
  450. },
  451. },
  452. },
  453. },
  454. },
  455. },
  456. },
  457. },
  458. labels: map[string]string{
  459. "foo": "barrrrrr",
  460. },
  461. name: "Pod with an Affinity matches node's labels but the PodSpec.NodeSelector(the old thing that we are deprecating) " +
  462. "is not satisfied, won't schedule onto the node",
  463. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  464. },
  465. {
  466. pod: &v1.Pod{
  467. Spec: v1.PodSpec{
  468. Affinity: &v1.Affinity{
  469. NodeAffinity: &v1.NodeAffinity{
  470. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  471. NodeSelectorTerms: []v1.NodeSelectorTerm{
  472. {
  473. MatchExpressions: []v1.NodeSelectorRequirement{
  474. {
  475. Key: "foo",
  476. Operator: v1.NodeSelectorOpNotIn,
  477. Values: []string{"invalid value: ___@#$%^"},
  478. },
  479. },
  480. },
  481. },
  482. },
  483. },
  484. },
  485. },
  486. },
  487. labels: map[string]string{
  488. "foo": "bar",
  489. },
  490. name: "Pod with an invalid value in Affinity term won't be scheduled onto the node",
  491. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  492. },
  493. {
  494. pod: &v1.Pod{
  495. Spec: v1.PodSpec{
  496. Affinity: &v1.Affinity{
  497. NodeAffinity: &v1.NodeAffinity{
  498. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  499. NodeSelectorTerms: []v1.NodeSelectorTerm{
  500. {
  501. MatchFields: []v1.NodeSelectorRequirement{
  502. {
  503. Key: api.ObjectNameField,
  504. Operator: v1.NodeSelectorOpIn,
  505. Values: []string{"node_1"},
  506. },
  507. },
  508. },
  509. },
  510. },
  511. },
  512. },
  513. },
  514. },
  515. nodeName: "node_1",
  516. name: "Pod with matchFields using In operator that matches the existing node",
  517. },
  518. {
  519. pod: &v1.Pod{
  520. Spec: v1.PodSpec{
  521. Affinity: &v1.Affinity{
  522. NodeAffinity: &v1.NodeAffinity{
  523. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  524. NodeSelectorTerms: []v1.NodeSelectorTerm{
  525. {
  526. MatchFields: []v1.NodeSelectorRequirement{
  527. {
  528. Key: api.ObjectNameField,
  529. Operator: v1.NodeSelectorOpIn,
  530. Values: []string{"node_1"},
  531. },
  532. },
  533. },
  534. },
  535. },
  536. },
  537. },
  538. },
  539. },
  540. nodeName: "node_2",
  541. name: "Pod with matchFields using In operator that does not match the existing node",
  542. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  543. },
  544. {
  545. pod: &v1.Pod{
  546. Spec: v1.PodSpec{
  547. Affinity: &v1.Affinity{
  548. NodeAffinity: &v1.NodeAffinity{
  549. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  550. NodeSelectorTerms: []v1.NodeSelectorTerm{
  551. {
  552. MatchFields: []v1.NodeSelectorRequirement{
  553. {
  554. Key: api.ObjectNameField,
  555. Operator: v1.NodeSelectorOpIn,
  556. Values: []string{"node_1"},
  557. },
  558. },
  559. },
  560. {
  561. MatchExpressions: []v1.NodeSelectorRequirement{
  562. {
  563. Key: "foo",
  564. Operator: v1.NodeSelectorOpIn,
  565. Values: []string{"bar"},
  566. },
  567. },
  568. },
  569. },
  570. },
  571. },
  572. },
  573. },
  574. },
  575. nodeName: "node_2",
  576. labels: map[string]string{"foo": "bar"},
  577. name: "Pod with two terms: matchFields does not match, but matchExpressions matches",
  578. },
  579. {
  580. pod: &v1.Pod{
  581. Spec: v1.PodSpec{
  582. Affinity: &v1.Affinity{
  583. NodeAffinity: &v1.NodeAffinity{
  584. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  585. NodeSelectorTerms: []v1.NodeSelectorTerm{
  586. {
  587. MatchFields: []v1.NodeSelectorRequirement{
  588. {
  589. Key: api.ObjectNameField,
  590. Operator: v1.NodeSelectorOpIn,
  591. Values: []string{"node_1"},
  592. },
  593. },
  594. MatchExpressions: []v1.NodeSelectorRequirement{
  595. {
  596. Key: "foo",
  597. Operator: v1.NodeSelectorOpIn,
  598. Values: []string{"bar"},
  599. },
  600. },
  601. },
  602. },
  603. },
  604. },
  605. },
  606. },
  607. },
  608. nodeName: "node_2",
  609. labels: map[string]string{"foo": "bar"},
  610. name: "Pod with one term: matchFields does not match, but matchExpressions matches",
  611. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  612. },
  613. {
  614. pod: &v1.Pod{
  615. Spec: v1.PodSpec{
  616. Affinity: &v1.Affinity{
  617. NodeAffinity: &v1.NodeAffinity{
  618. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  619. NodeSelectorTerms: []v1.NodeSelectorTerm{
  620. {
  621. MatchFields: []v1.NodeSelectorRequirement{
  622. {
  623. Key: api.ObjectNameField,
  624. Operator: v1.NodeSelectorOpIn,
  625. Values: []string{"node_1"},
  626. },
  627. },
  628. MatchExpressions: []v1.NodeSelectorRequirement{
  629. {
  630. Key: "foo",
  631. Operator: v1.NodeSelectorOpIn,
  632. Values: []string{"bar"},
  633. },
  634. },
  635. },
  636. },
  637. },
  638. },
  639. },
  640. },
  641. },
  642. nodeName: "node_1",
  643. labels: map[string]string{"foo": "bar"},
  644. name: "Pod with one term: both matchFields and matchExpressions match",
  645. },
  646. {
  647. pod: &v1.Pod{
  648. Spec: v1.PodSpec{
  649. Affinity: &v1.Affinity{
  650. NodeAffinity: &v1.NodeAffinity{
  651. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  652. NodeSelectorTerms: []v1.NodeSelectorTerm{
  653. {
  654. MatchFields: []v1.NodeSelectorRequirement{
  655. {
  656. Key: api.ObjectNameField,
  657. Operator: v1.NodeSelectorOpIn,
  658. Values: []string{"node_1"},
  659. },
  660. },
  661. },
  662. {
  663. MatchExpressions: []v1.NodeSelectorRequirement{
  664. {
  665. Key: "foo",
  666. Operator: v1.NodeSelectorOpIn,
  667. Values: []string{"not-match-to-bar"},
  668. },
  669. },
  670. },
  671. },
  672. },
  673. },
  674. },
  675. },
  676. },
  677. nodeName: "node_2",
  678. labels: map[string]string{"foo": "bar"},
  679. name: "Pod with two terms: both matchFields and matchExpressions do not match",
  680. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReason),
  681. },
  682. }
  683. for _, test := range tests {
  684. t.Run(test.name, func(t *testing.T) {
  685. node := v1.Node{ObjectMeta: metav1.ObjectMeta{
  686. Name: test.nodeName,
  687. Labels: test.labels,
  688. }}
  689. nodeInfo := schedulernodeinfo.NewNodeInfo()
  690. nodeInfo.SetNode(&node)
  691. p, _ := New(nil, nil)
  692. gotStatus := p.(framework.FilterPlugin).Filter(context.Background(), nil, test.pod, nodeInfo)
  693. if !reflect.DeepEqual(gotStatus, test.wantStatus) {
  694. t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
  695. }
  696. })
  697. }
  698. }
  699. func TestNodeAffinityPriority(t *testing.T) {
  700. label1 := map[string]string{"foo": "bar"}
  701. label2 := map[string]string{"key": "value"}
  702. label3 := map[string]string{"az": "az1"}
  703. label4 := map[string]string{"abc": "az11", "def": "az22"}
  704. label5 := map[string]string{"foo": "bar", "key": "value", "az": "az1"}
  705. affinity1 := &v1.Affinity{
  706. NodeAffinity: &v1.NodeAffinity{
  707. PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{{
  708. Weight: 2,
  709. Preference: v1.NodeSelectorTerm{
  710. MatchExpressions: []v1.NodeSelectorRequirement{{
  711. Key: "foo",
  712. Operator: v1.NodeSelectorOpIn,
  713. Values: []string{"bar"},
  714. }},
  715. },
  716. }},
  717. },
  718. }
  719. affinity2 := &v1.Affinity{
  720. NodeAffinity: &v1.NodeAffinity{
  721. PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{
  722. {
  723. Weight: 2,
  724. Preference: v1.NodeSelectorTerm{
  725. MatchExpressions: []v1.NodeSelectorRequirement{
  726. {
  727. Key: "foo",
  728. Operator: v1.NodeSelectorOpIn,
  729. Values: []string{"bar"},
  730. },
  731. },
  732. },
  733. },
  734. {
  735. Weight: 4,
  736. Preference: v1.NodeSelectorTerm{
  737. MatchExpressions: []v1.NodeSelectorRequirement{
  738. {
  739. Key: "key",
  740. Operator: v1.NodeSelectorOpIn,
  741. Values: []string{"value"},
  742. },
  743. },
  744. },
  745. },
  746. {
  747. Weight: 5,
  748. Preference: v1.NodeSelectorTerm{
  749. MatchExpressions: []v1.NodeSelectorRequirement{
  750. {
  751. Key: "foo",
  752. Operator: v1.NodeSelectorOpIn,
  753. Values: []string{"bar"},
  754. },
  755. {
  756. Key: "key",
  757. Operator: v1.NodeSelectorOpIn,
  758. Values: []string{"value"},
  759. },
  760. {
  761. Key: "az",
  762. Operator: v1.NodeSelectorOpIn,
  763. Values: []string{"az1"},
  764. },
  765. },
  766. },
  767. },
  768. },
  769. },
  770. }
  771. tests := []struct {
  772. pod *v1.Pod
  773. nodes []*v1.Node
  774. expectedList framework.NodeScoreList
  775. name string
  776. }{
  777. {
  778. pod: &v1.Pod{
  779. ObjectMeta: metav1.ObjectMeta{
  780. Annotations: map[string]string{},
  781. },
  782. },
  783. nodes: []*v1.Node{
  784. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  785. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  786. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  787. },
  788. expectedList: []framework.NodeScore{{Name: "machine1", Score: 0}, {Name: "machine2", Score: 0}, {Name: "machine3", Score: 0}},
  789. name: "all machines are same priority as NodeAffinity is nil",
  790. },
  791. {
  792. pod: &v1.Pod{
  793. Spec: v1.PodSpec{
  794. Affinity: affinity1,
  795. },
  796. },
  797. nodes: []*v1.Node{
  798. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label4}},
  799. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  800. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  801. },
  802. expectedList: []framework.NodeScore{{Name: "machine1", Score: 0}, {Name: "machine2", Score: 0}, {Name: "machine3", Score: 0}},
  803. name: "no machine macthes preferred scheduling requirements in NodeAffinity of pod so all machines' priority is zero",
  804. },
  805. {
  806. pod: &v1.Pod{
  807. Spec: v1.PodSpec{
  808. Affinity: affinity1,
  809. },
  810. },
  811. nodes: []*v1.Node{
  812. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  813. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  814. {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
  815. },
  816. expectedList: []framework.NodeScore{{Name: "machine1", Score: framework.MaxNodeScore}, {Name: "machine2", Score: 0}, {Name: "machine3", Score: 0}},
  817. name: "only machine1 matches the preferred scheduling requirements of pod",
  818. },
  819. {
  820. pod: &v1.Pod{
  821. Spec: v1.PodSpec{
  822. Affinity: affinity2,
  823. },
  824. },
  825. nodes: []*v1.Node{
  826. {ObjectMeta: metav1.ObjectMeta{Name: "machine1", Labels: label1}},
  827. {ObjectMeta: metav1.ObjectMeta{Name: "machine5", Labels: label5}},
  828. {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
  829. },
  830. expectedList: []framework.NodeScore{{Name: "machine1", Score: 18}, {Name: "machine5", Score: framework.MaxNodeScore}, {Name: "machine2", Score: 36}},
  831. name: "all machines matches the preferred scheduling requirements of pod but with different priorities ",
  832. },
  833. }
  834. for _, test := range tests {
  835. t.Run(test.name, func(t *testing.T) {
  836. state := framework.NewCycleState()
  837. fh, _ := framework.NewFramework(nil, nil, nil, framework.WithSnapshotSharedLister(cache.NewSnapshot(nil, test.nodes)))
  838. p, _ := New(nil, fh)
  839. var gotList framework.NodeScoreList
  840. for _, n := range test.nodes {
  841. nodeName := n.ObjectMeta.Name
  842. score, status := p.(framework.ScorePlugin).Score(context.Background(), state, test.pod, nodeName)
  843. if !status.IsSuccess() {
  844. t.Errorf("unexpected error: %v", status)
  845. }
  846. gotList = append(gotList, framework.NodeScore{Name: nodeName, Score: score})
  847. }
  848. status := p.(framework.ScorePlugin).ScoreExtensions().NormalizeScore(context.Background(), state, test.pod, gotList)
  849. if !status.IsSuccess() {
  850. t.Errorf("unexpected error: %v", status)
  851. }
  852. if !reflect.DeepEqual(test.expectedList, gotList) {
  853. t.Errorf("expected:\n\t%+v,\ngot:\n\t%+v", test.expectedList, gotList)
  854. }
  855. })
  856. }
  857. }