taint_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. Copyright 2017 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 scheduler
  14. // This file tests the Taint feature.
  15. import (
  16. "fmt"
  17. "testing"
  18. "time"
  19. "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/api/resource"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime/schema"
  23. utilfeature "k8s.io/apiserver/pkg/util/feature"
  24. "k8s.io/client-go/informers"
  25. "k8s.io/client-go/kubernetes"
  26. restclient "k8s.io/client-go/rest"
  27. featuregatetesting "k8s.io/component-base/featuregate/testing"
  28. "k8s.io/kubernetes/pkg/controller/nodelifecycle"
  29. "k8s.io/kubernetes/pkg/features"
  30. "k8s.io/kubernetes/pkg/scheduler/algorithmprovider"
  31. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  32. "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction"
  33. pluginapi "k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction/apis/podtolerationrestriction"
  34. )
  35. func newPod(nsName, name string, req, limit v1.ResourceList) *v1.Pod {
  36. return &v1.Pod{
  37. ObjectMeta: metav1.ObjectMeta{
  38. Name: name,
  39. Namespace: nsName,
  40. },
  41. Spec: v1.PodSpec{
  42. Containers: []v1.Container{
  43. {
  44. Name: "busybox",
  45. Image: "busybox",
  46. Resources: v1.ResourceRequirements{
  47. Requests: req,
  48. Limits: limit,
  49. },
  50. },
  51. },
  52. },
  53. }
  54. }
  55. // TestTaintNodeByCondition tests related cases for TaintNodeByCondition feature.
  56. func TestTaintNodeByCondition(t *testing.T) {
  57. // Enable TaintNodeByCondition
  58. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.TaintNodesByCondition, true)()
  59. // Build PodToleration Admission.
  60. admission := podtolerationrestriction.NewPodTolerationsPlugin(&pluginapi.Configuration{})
  61. context := initTestMaster(t, "default", admission)
  62. // Build clientset and informers for controllers.
  63. externalClientset := kubernetes.NewForConfigOrDie(&restclient.Config{
  64. QPS: -1,
  65. Host: context.httpServer.URL,
  66. ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
  67. externalInformers := informers.NewSharedInformerFactory(externalClientset, time.Second)
  68. admission.SetExternalKubeClientSet(externalClientset)
  69. admission.SetExternalKubeInformerFactory(externalInformers)
  70. // Apply feature gates to enable TaintNodesByCondition
  71. algorithmprovider.ApplyFeatureGates()
  72. context = initTestScheduler(t, context, false, nil)
  73. cs := context.clientSet
  74. informers := context.informerFactory
  75. nsName := context.ns.Name
  76. // Start NodeLifecycleController for taint.
  77. nc, err := nodelifecycle.NewNodeLifecycleController(
  78. informers.Coordination().V1beta1().Leases(),
  79. informers.Core().V1().Pods(),
  80. informers.Core().V1().Nodes(),
  81. informers.Apps().V1().DaemonSets(),
  82. cs,
  83. time.Hour, // Node monitor grace period
  84. time.Second, // Node startup grace period
  85. time.Second, // Node monitor period
  86. time.Second, // Pod eviction timeout
  87. 100, // Eviction limiter QPS
  88. 100, // Secondary eviction limiter QPS
  89. 100, // Large cluster threshold
  90. 100, // Unhealthy zone threshold
  91. true, // Run taint manager
  92. true, // Use taint based evictions
  93. true, // Enabled TaintNodeByCondition feature
  94. )
  95. if err != nil {
  96. t.Errorf("Failed to create node controller: %v", err)
  97. return
  98. }
  99. go nc.Run(context.stopCh)
  100. // Waiting for all controller sync.
  101. externalInformers.Start(context.stopCh)
  102. externalInformers.WaitForCacheSync(context.stopCh)
  103. informers.Start(context.stopCh)
  104. informers.WaitForCacheSync(context.stopCh)
  105. // -------------------------------------------
  106. // Test TaintNodeByCondition feature.
  107. // -------------------------------------------
  108. nodeRes := v1.ResourceList{
  109. v1.ResourceCPU: resource.MustParse("4000m"),
  110. v1.ResourceMemory: resource.MustParse("16Gi"),
  111. v1.ResourcePods: resource.MustParse("110"),
  112. }
  113. podRes := v1.ResourceList{
  114. v1.ResourceCPU: resource.MustParse("100m"),
  115. v1.ResourceMemory: resource.MustParse("100Mi"),
  116. }
  117. notReadyToleration := v1.Toleration{
  118. Key: schedulerapi.TaintNodeNotReady,
  119. Operator: v1.TolerationOpExists,
  120. Effect: v1.TaintEffectNoSchedule,
  121. }
  122. unschedulableToleration := v1.Toleration{
  123. Key: schedulerapi.TaintNodeUnschedulable,
  124. Operator: v1.TolerationOpExists,
  125. Effect: v1.TaintEffectNoSchedule,
  126. }
  127. memoryPressureToleration := v1.Toleration{
  128. Key: schedulerapi.TaintNodeMemoryPressure,
  129. Operator: v1.TolerationOpExists,
  130. Effect: v1.TaintEffectNoSchedule,
  131. }
  132. diskPressureToleration := v1.Toleration{
  133. Key: schedulerapi.TaintNodeDiskPressure,
  134. Operator: v1.TolerationOpExists,
  135. Effect: v1.TaintEffectNoSchedule,
  136. }
  137. networkUnavailableToleration := v1.Toleration{
  138. Key: schedulerapi.TaintNodeNetworkUnavailable,
  139. Operator: v1.TolerationOpExists,
  140. Effect: v1.TaintEffectNoSchedule,
  141. }
  142. pidPressureToleration := v1.Toleration{
  143. Key: schedulerapi.TaintNodePIDPressure,
  144. Operator: v1.TolerationOpExists,
  145. Effect: v1.TaintEffectNoSchedule,
  146. }
  147. bestEffortPod := newPod(nsName, "besteffort-pod", nil, nil)
  148. burstablePod := newPod(nsName, "burstable-pod", podRes, nil)
  149. guaranteePod := newPod(nsName, "guarantee-pod", podRes, podRes)
  150. type podCase struct {
  151. pod *v1.Pod
  152. tolerations []v1.Toleration
  153. fits bool
  154. }
  155. // switch to table driven testings
  156. tests := []struct {
  157. name string
  158. existingTaints []v1.Taint
  159. nodeConditions []v1.NodeCondition
  160. unschedulable bool
  161. expectedTaints []v1.Taint
  162. pods []podCase
  163. }{
  164. {
  165. name: "not-ready node",
  166. nodeConditions: []v1.NodeCondition{
  167. {
  168. Type: v1.NodeReady,
  169. Status: v1.ConditionFalse,
  170. },
  171. },
  172. expectedTaints: []v1.Taint{
  173. {
  174. Key: schedulerapi.TaintNodeNotReady,
  175. Effect: v1.TaintEffectNoSchedule,
  176. },
  177. },
  178. pods: []podCase{
  179. {
  180. pod: bestEffortPod,
  181. fits: false,
  182. },
  183. {
  184. pod: burstablePod,
  185. fits: false,
  186. },
  187. {
  188. pod: guaranteePod,
  189. fits: false,
  190. },
  191. {
  192. pod: bestEffortPod,
  193. tolerations: []v1.Toleration{notReadyToleration},
  194. fits: true,
  195. },
  196. },
  197. },
  198. {
  199. name: "unschedulable node",
  200. unschedulable: true, // node.spec.unschedulable = true
  201. nodeConditions: []v1.NodeCondition{
  202. {
  203. Type: v1.NodeReady,
  204. Status: v1.ConditionTrue,
  205. },
  206. },
  207. expectedTaints: []v1.Taint{
  208. {
  209. Key: schedulerapi.TaintNodeUnschedulable,
  210. Effect: v1.TaintEffectNoSchedule,
  211. },
  212. },
  213. pods: []podCase{
  214. {
  215. pod: bestEffortPod,
  216. fits: false,
  217. },
  218. {
  219. pod: burstablePod,
  220. fits: false,
  221. },
  222. {
  223. pod: guaranteePod,
  224. fits: false,
  225. },
  226. {
  227. pod: bestEffortPod,
  228. tolerations: []v1.Toleration{unschedulableToleration},
  229. fits: true,
  230. },
  231. },
  232. },
  233. {
  234. name: "memory pressure node",
  235. nodeConditions: []v1.NodeCondition{
  236. {
  237. Type: v1.NodeMemoryPressure,
  238. Status: v1.ConditionTrue,
  239. },
  240. {
  241. Type: v1.NodeReady,
  242. Status: v1.ConditionTrue,
  243. },
  244. },
  245. expectedTaints: []v1.Taint{
  246. {
  247. Key: schedulerapi.TaintNodeMemoryPressure,
  248. Effect: v1.TaintEffectNoSchedule,
  249. },
  250. },
  251. // In MemoryPressure condition, both Burstable and Guarantee pods are scheduled;
  252. // BestEffort pod with toleration are also scheduled.
  253. pods: []podCase{
  254. {
  255. pod: bestEffortPod,
  256. fits: false,
  257. },
  258. {
  259. pod: bestEffortPod,
  260. tolerations: []v1.Toleration{memoryPressureToleration},
  261. fits: true,
  262. },
  263. {
  264. pod: bestEffortPod,
  265. tolerations: []v1.Toleration{diskPressureToleration},
  266. fits: false,
  267. },
  268. {
  269. pod: burstablePod,
  270. fits: true,
  271. },
  272. {
  273. pod: guaranteePod,
  274. fits: true,
  275. },
  276. },
  277. },
  278. {
  279. name: "disk pressure node",
  280. nodeConditions: []v1.NodeCondition{
  281. {
  282. Type: v1.NodeDiskPressure,
  283. Status: v1.ConditionTrue,
  284. },
  285. {
  286. Type: v1.NodeReady,
  287. Status: v1.ConditionTrue,
  288. },
  289. },
  290. expectedTaints: []v1.Taint{
  291. {
  292. Key: schedulerapi.TaintNodeDiskPressure,
  293. Effect: v1.TaintEffectNoSchedule,
  294. },
  295. },
  296. // In DiskPressure condition, only pods with toleration can be scheduled.
  297. pods: []podCase{
  298. {
  299. pod: bestEffortPod,
  300. fits: false,
  301. },
  302. {
  303. pod: burstablePod,
  304. fits: false,
  305. },
  306. {
  307. pod: guaranteePod,
  308. fits: false,
  309. },
  310. {
  311. pod: bestEffortPod,
  312. tolerations: []v1.Toleration{diskPressureToleration},
  313. fits: true,
  314. },
  315. {
  316. pod: bestEffortPod,
  317. tolerations: []v1.Toleration{memoryPressureToleration},
  318. fits: false,
  319. },
  320. },
  321. },
  322. {
  323. name: "network unavailable and node is ready",
  324. nodeConditions: []v1.NodeCondition{
  325. {
  326. Type: v1.NodeNetworkUnavailable,
  327. Status: v1.ConditionTrue,
  328. },
  329. {
  330. Type: v1.NodeReady,
  331. Status: v1.ConditionTrue,
  332. },
  333. },
  334. expectedTaints: []v1.Taint{
  335. {
  336. Key: schedulerapi.TaintNodeNetworkUnavailable,
  337. Effect: v1.TaintEffectNoSchedule,
  338. },
  339. },
  340. pods: []podCase{
  341. {
  342. pod: bestEffortPod,
  343. fits: false,
  344. },
  345. {
  346. pod: burstablePod,
  347. fits: false,
  348. },
  349. {
  350. pod: guaranteePod,
  351. fits: false,
  352. },
  353. {
  354. pod: burstablePod,
  355. tolerations: []v1.Toleration{
  356. networkUnavailableToleration,
  357. },
  358. fits: true,
  359. },
  360. },
  361. },
  362. {
  363. name: "network unavailable and node is not ready",
  364. nodeConditions: []v1.NodeCondition{
  365. {
  366. Type: v1.NodeNetworkUnavailable,
  367. Status: v1.ConditionTrue,
  368. },
  369. {
  370. Type: v1.NodeReady,
  371. Status: v1.ConditionFalse,
  372. },
  373. },
  374. expectedTaints: []v1.Taint{
  375. {
  376. Key: schedulerapi.TaintNodeNetworkUnavailable,
  377. Effect: v1.TaintEffectNoSchedule,
  378. },
  379. {
  380. Key: schedulerapi.TaintNodeNotReady,
  381. Effect: v1.TaintEffectNoSchedule,
  382. },
  383. },
  384. pods: []podCase{
  385. {
  386. pod: bestEffortPod,
  387. fits: false,
  388. },
  389. {
  390. pod: burstablePod,
  391. fits: false,
  392. },
  393. {
  394. pod: guaranteePod,
  395. fits: false,
  396. },
  397. {
  398. pod: burstablePod,
  399. tolerations: []v1.Toleration{
  400. networkUnavailableToleration,
  401. },
  402. fits: false,
  403. },
  404. {
  405. pod: burstablePod,
  406. tolerations: []v1.Toleration{
  407. networkUnavailableToleration,
  408. notReadyToleration,
  409. },
  410. fits: true,
  411. },
  412. },
  413. },
  414. {
  415. name: "pid pressure node",
  416. nodeConditions: []v1.NodeCondition{
  417. {
  418. Type: v1.NodePIDPressure,
  419. Status: v1.ConditionTrue,
  420. },
  421. {
  422. Type: v1.NodeReady,
  423. Status: v1.ConditionTrue,
  424. },
  425. },
  426. expectedTaints: []v1.Taint{
  427. {
  428. Key: schedulerapi.TaintNodePIDPressure,
  429. Effect: v1.TaintEffectNoSchedule,
  430. },
  431. },
  432. pods: []podCase{
  433. {
  434. pod: bestEffortPod,
  435. fits: false,
  436. },
  437. {
  438. pod: burstablePod,
  439. fits: false,
  440. },
  441. {
  442. pod: guaranteePod,
  443. fits: false,
  444. },
  445. {
  446. pod: bestEffortPod,
  447. tolerations: []v1.Toleration{pidPressureToleration},
  448. fits: true,
  449. },
  450. },
  451. },
  452. {
  453. name: "multi taints on node",
  454. nodeConditions: []v1.NodeCondition{
  455. {
  456. Type: v1.NodePIDPressure,
  457. Status: v1.ConditionTrue,
  458. },
  459. {
  460. Type: v1.NodeMemoryPressure,
  461. Status: v1.ConditionTrue,
  462. },
  463. {
  464. Type: v1.NodeDiskPressure,
  465. Status: v1.ConditionTrue,
  466. },
  467. {
  468. Type: v1.NodeReady,
  469. Status: v1.ConditionTrue,
  470. },
  471. },
  472. expectedTaints: []v1.Taint{
  473. {
  474. Key: schedulerapi.TaintNodeDiskPressure,
  475. Effect: v1.TaintEffectNoSchedule,
  476. },
  477. {
  478. Key: schedulerapi.TaintNodeMemoryPressure,
  479. Effect: v1.TaintEffectNoSchedule,
  480. },
  481. {
  482. Key: schedulerapi.TaintNodePIDPressure,
  483. Effect: v1.TaintEffectNoSchedule,
  484. },
  485. },
  486. },
  487. }
  488. for _, test := range tests {
  489. t.Run(test.name, func(t *testing.T) {
  490. node := &v1.Node{
  491. ObjectMeta: metav1.ObjectMeta{
  492. Name: "node-1",
  493. },
  494. Spec: v1.NodeSpec{
  495. Unschedulable: test.unschedulable,
  496. Taints: test.existingTaints,
  497. },
  498. Status: v1.NodeStatus{
  499. Capacity: nodeRes,
  500. Allocatable: nodeRes,
  501. Conditions: test.nodeConditions,
  502. },
  503. }
  504. if _, err := cs.CoreV1().Nodes().Create(node); err != nil {
  505. t.Errorf("Failed to create node, err: %v", err)
  506. }
  507. if err := waitForNodeTaints(cs, node, test.expectedTaints); err != nil {
  508. t.Errorf("Failed to taint node <%s>, err: %v", node.Name, err)
  509. }
  510. var pods []*v1.Pod
  511. for i, p := range test.pods {
  512. pod := p.pod.DeepCopy()
  513. pod.Name = fmt.Sprintf("%s-%d", pod.Name, i)
  514. pod.Spec.Tolerations = p.tolerations
  515. createdPod, err := cs.CoreV1().Pods(pod.Namespace).Create(pod)
  516. if err != nil {
  517. t.Fatalf("Failed to create pod %s/%s, error: %v",
  518. pod.Namespace, pod.Name, err)
  519. }
  520. pods = append(pods, createdPod)
  521. if p.fits {
  522. if err := waitForPodToSchedule(cs, createdPod); err != nil {
  523. t.Errorf("Failed to schedule pod %s/%s on the node, err: %v",
  524. pod.Namespace, pod.Name, err)
  525. }
  526. } else {
  527. if err := waitForPodUnschedulable(cs, createdPod); err != nil {
  528. t.Errorf("Unschedulable pod %s/%s gets scheduled on the node, err: %v",
  529. pod.Namespace, pod.Name, err)
  530. }
  531. }
  532. }
  533. cleanupPods(cs, t, pods)
  534. cleanupNodes(cs, t)
  535. waitForSchedulerCacheCleanup(context.scheduler, t)
  536. })
  537. }
  538. }