taints.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. Copyright 2015 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 scheduling
  14. import (
  15. "time"
  16. "github.com/onsi/ginkgo"
  17. // ensure libs have a chance to initialize
  18. _ "github.com/stretchr/testify/assert"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/labels"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/watch"
  24. clientset "k8s.io/client-go/kubernetes"
  25. "k8s.io/client-go/tools/cache"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  28. testutils "k8s.io/kubernetes/test/utils"
  29. )
  30. func getTestTaint() v1.Taint {
  31. now := metav1.Now()
  32. return v1.Taint{
  33. Key: "kubernetes.io/e2e-evict-taint-key",
  34. Value: "evictTaintVal",
  35. Effect: v1.TaintEffectNoExecute,
  36. TimeAdded: &now,
  37. }
  38. }
  39. // Create a default pod for this test, with argument saying if the Pod should have
  40. // toleration for Taits used in this test.
  41. func createPodForTaintsTest(hasToleration bool, tolerationSeconds int, podName, podLabel, ns string) *v1.Pod {
  42. grace := int64(1)
  43. if !hasToleration {
  44. return &v1.Pod{
  45. ObjectMeta: metav1.ObjectMeta{
  46. Name: podName,
  47. Namespace: ns,
  48. Labels: map[string]string{"group": podLabel},
  49. DeletionGracePeriodSeconds: &grace,
  50. },
  51. Spec: v1.PodSpec{
  52. Containers: []v1.Container{
  53. {
  54. Name: "pause",
  55. Image: "k8s.gcr.io/pause:3.1",
  56. },
  57. },
  58. },
  59. }
  60. }
  61. if tolerationSeconds <= 0 {
  62. return &v1.Pod{
  63. ObjectMeta: metav1.ObjectMeta{
  64. Name: podName,
  65. Namespace: ns,
  66. Labels: map[string]string{"group": podLabel},
  67. DeletionGracePeriodSeconds: &grace,
  68. // default - tolerate forever
  69. },
  70. Spec: v1.PodSpec{
  71. Containers: []v1.Container{
  72. {
  73. Name: "pause",
  74. Image: "k8s.gcr.io/pause:3.1",
  75. },
  76. },
  77. Tolerations: []v1.Toleration{{Key: "kubernetes.io/e2e-evict-taint-key", Value: "evictTaintVal", Effect: v1.TaintEffectNoExecute}},
  78. },
  79. }
  80. }
  81. ts := int64(tolerationSeconds)
  82. return &v1.Pod{
  83. ObjectMeta: metav1.ObjectMeta{
  84. Name: podName,
  85. Namespace: ns,
  86. Labels: map[string]string{"group": podLabel},
  87. DeletionGracePeriodSeconds: &grace,
  88. },
  89. Spec: v1.PodSpec{
  90. Containers: []v1.Container{
  91. {
  92. Name: "pause",
  93. Image: "k8s.gcr.io/pause:3.1",
  94. },
  95. },
  96. // default - tolerate forever
  97. Tolerations: []v1.Toleration{{Key: "kubernetes.io/e2e-evict-taint-key", Value: "evictTaintVal", Effect: v1.TaintEffectNoExecute, TolerationSeconds: &ts}},
  98. },
  99. }
  100. }
  101. // Creates and starts a controller (informer) that watches updates on a pod in given namespace with given name. It puts a new
  102. // struct into observedDeletion channel for every deletion it sees.
  103. func createTestController(cs clientset.Interface, observedDeletions chan string, stopCh chan struct{}, podLabel, ns string) {
  104. _, controller := cache.NewInformer(
  105. &cache.ListWatch{
  106. ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
  107. options.LabelSelector = labels.SelectorFromSet(labels.Set{"group": podLabel}).String()
  108. obj, err := cs.CoreV1().Pods(ns).List(options)
  109. return runtime.Object(obj), err
  110. },
  111. WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
  112. options.LabelSelector = labels.SelectorFromSet(labels.Set{"group": podLabel}).String()
  113. return cs.CoreV1().Pods(ns).Watch(options)
  114. },
  115. },
  116. &v1.Pod{},
  117. 0,
  118. cache.ResourceEventHandlerFuncs{
  119. DeleteFunc: func(oldObj interface{}) {
  120. if delPod, ok := oldObj.(*v1.Pod); ok {
  121. observedDeletions <- delPod.Name
  122. } else {
  123. observedDeletions <- ""
  124. }
  125. },
  126. },
  127. )
  128. e2elog.Logf("Starting informer...")
  129. go controller.Run(stopCh)
  130. }
  131. const (
  132. kubeletPodDeletionDelaySeconds = 60
  133. additionalWaitPerDeleteSeconds = 5
  134. )
  135. // Tests the behavior of NoExecuteTaintManager. Following scenarios are included:
  136. // - eviction of non-tolerating pods from a tainted node,
  137. // - lack of eviction of tolerating pods from a tainted node,
  138. // - delayed eviction of short-tolerating pod from a tainted node,
  139. // - lack of eviction of short-tolerating pod after taint removal.
  140. var _ = SIGDescribe("NoExecuteTaintManager Single Pod [Serial]", func() {
  141. var cs clientset.Interface
  142. var ns string
  143. f := framework.NewDefaultFramework("taint-single-pod")
  144. ginkgo.BeforeEach(func() {
  145. cs = f.ClientSet
  146. ns = f.Namespace.Name
  147. framework.WaitForAllNodesHealthy(cs, time.Minute)
  148. err := framework.CheckTestingNSDeletedExcept(cs, ns)
  149. framework.ExpectNoError(err)
  150. })
  151. // 1. Run a pod
  152. // 2. Taint the node running this pod with a no-execute taint
  153. // 3. See if pod will get evicted
  154. ginkgo.It("evicts pods from tainted nodes", func() {
  155. podName := "taint-eviction-1"
  156. pod := createPodForTaintsTest(false, 0, podName, podName, ns)
  157. observedDeletions := make(chan string, 100)
  158. stopCh := make(chan struct{})
  159. createTestController(cs, observedDeletions, stopCh, podName, ns)
  160. ginkgo.By("Starting pod...")
  161. nodeName, err := testutils.RunPodAndGetNodeName(cs, pod, 2*time.Minute)
  162. framework.ExpectNoError(err)
  163. e2elog.Logf("Pod is running on %v. Tainting Node", nodeName)
  164. ginkgo.By("Trying to apply a taint on the Node")
  165. testTaint := getTestTaint()
  166. framework.AddOrUpdateTaintOnNode(cs, nodeName, testTaint)
  167. framework.ExpectNodeHasTaint(cs, nodeName, &testTaint)
  168. defer framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  169. // Wait a bit
  170. ginkgo.By("Waiting for Pod to be deleted")
  171. timeoutChannel := time.NewTimer(time.Duration(kubeletPodDeletionDelaySeconds+additionalWaitPerDeleteSeconds) * time.Second).C
  172. select {
  173. case <-timeoutChannel:
  174. framework.Failf("Failed to evict Pod")
  175. case <-observedDeletions:
  176. e2elog.Logf("Noticed Pod eviction. Test successful")
  177. }
  178. })
  179. // 1. Run a pod with toleration
  180. // 2. Taint the node running this pod with a no-execute taint
  181. // 3. See if pod won't get evicted
  182. ginkgo.It("doesn't evict pod with tolerations from tainted nodes", func() {
  183. podName := "taint-eviction-2"
  184. pod := createPodForTaintsTest(true, 0, podName, podName, ns)
  185. observedDeletions := make(chan string, 100)
  186. stopCh := make(chan struct{})
  187. createTestController(cs, observedDeletions, stopCh, podName, ns)
  188. ginkgo.By("Starting pod...")
  189. nodeName, err := testutils.RunPodAndGetNodeName(cs, pod, 2*time.Minute)
  190. framework.ExpectNoError(err)
  191. e2elog.Logf("Pod is running on %v. Tainting Node", nodeName)
  192. ginkgo.By("Trying to apply a taint on the Node")
  193. testTaint := getTestTaint()
  194. framework.AddOrUpdateTaintOnNode(cs, nodeName, testTaint)
  195. framework.ExpectNodeHasTaint(cs, nodeName, &testTaint)
  196. defer framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  197. // Wait a bit
  198. ginkgo.By("Waiting for Pod to be deleted")
  199. timeoutChannel := time.NewTimer(time.Duration(kubeletPodDeletionDelaySeconds+additionalWaitPerDeleteSeconds) * time.Second).C
  200. select {
  201. case <-timeoutChannel:
  202. e2elog.Logf("Pod wasn't evicted. Test successful")
  203. case <-observedDeletions:
  204. framework.Failf("Pod was evicted despite toleration")
  205. }
  206. })
  207. // 1. Run a pod with a finite toleration
  208. // 2. Taint the node running this pod with a no-execute taint
  209. // 3. See if pod won't get evicted before toleration time runs out
  210. // 4. See if pod will get evicted after toleration time runs out
  211. ginkgo.It("eventually evict pod with finite tolerations from tainted nodes", func() {
  212. podName := "taint-eviction-3"
  213. pod := createPodForTaintsTest(true, kubeletPodDeletionDelaySeconds+2*additionalWaitPerDeleteSeconds, podName, podName, ns)
  214. observedDeletions := make(chan string, 100)
  215. stopCh := make(chan struct{})
  216. createTestController(cs, observedDeletions, stopCh, podName, ns)
  217. ginkgo.By("Starting pod...")
  218. nodeName, err := testutils.RunPodAndGetNodeName(cs, pod, 2*time.Minute)
  219. framework.ExpectNoError(err)
  220. e2elog.Logf("Pod is running on %v. Tainting Node", nodeName)
  221. ginkgo.By("Trying to apply a taint on the Node")
  222. testTaint := getTestTaint()
  223. framework.AddOrUpdateTaintOnNode(cs, nodeName, testTaint)
  224. framework.ExpectNodeHasTaint(cs, nodeName, &testTaint)
  225. defer framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  226. // Wait a bit
  227. ginkgo.By("Waiting to see if a Pod won't be deleted")
  228. timeoutChannel := time.NewTimer(time.Duration(kubeletPodDeletionDelaySeconds+additionalWaitPerDeleteSeconds) * time.Second).C
  229. select {
  230. case <-timeoutChannel:
  231. e2elog.Logf("Pod wasn't evicted")
  232. case <-observedDeletions:
  233. framework.Failf("Pod was evicted despite toleration")
  234. return
  235. }
  236. ginkgo.By("Waiting for Pod to be deleted")
  237. timeoutChannel = time.NewTimer(time.Duration(kubeletPodDeletionDelaySeconds+additionalWaitPerDeleteSeconds) * time.Second).C
  238. select {
  239. case <-timeoutChannel:
  240. framework.Failf("Pod wasn't evicted")
  241. case <-observedDeletions:
  242. e2elog.Logf("Pod was evicted after toleration time run out. Test successful")
  243. return
  244. }
  245. })
  246. // 1. Run a pod with short toleration
  247. // 2. Taint the node running this pod with a no-execute taint
  248. // 3. Wait some time
  249. // 4. Remove the taint
  250. // 5. See if Pod won't be evicted.
  251. ginkgo.It("removing taint cancels eviction", func() {
  252. podName := "taint-eviction-4"
  253. pod := createPodForTaintsTest(true, 2*additionalWaitPerDeleteSeconds, podName, podName, ns)
  254. observedDeletions := make(chan string, 100)
  255. stopCh := make(chan struct{})
  256. createTestController(cs, observedDeletions, stopCh, podName, ns)
  257. ginkgo.By("Starting pod...")
  258. nodeName, err := testutils.RunPodAndGetNodeName(cs, pod, 2*time.Minute)
  259. framework.ExpectNoError(err)
  260. e2elog.Logf("Pod is running on %v. Tainting Node", nodeName)
  261. ginkgo.By("Trying to apply a taint on the Node")
  262. testTaint := getTestTaint()
  263. framework.AddOrUpdateTaintOnNode(cs, nodeName, testTaint)
  264. framework.ExpectNodeHasTaint(cs, nodeName, &testTaint)
  265. taintRemoved := false
  266. defer func() {
  267. if !taintRemoved {
  268. framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  269. }
  270. }()
  271. // Wait a bit
  272. ginkgo.By("Waiting short time to make sure Pod is queued for deletion")
  273. timeoutChannel := time.NewTimer(additionalWaitPerDeleteSeconds).C
  274. select {
  275. case <-timeoutChannel:
  276. e2elog.Logf("Pod wasn't evicted. Proceeding")
  277. case <-observedDeletions:
  278. framework.Failf("Pod was evicted despite toleration")
  279. return
  280. }
  281. e2elog.Logf("Removing taint from Node")
  282. framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  283. taintRemoved = true
  284. ginkgo.By("Waiting some time to make sure that toleration time passed.")
  285. timeoutChannel = time.NewTimer(time.Duration(kubeletPodDeletionDelaySeconds+3*additionalWaitPerDeleteSeconds) * time.Second).C
  286. select {
  287. case <-timeoutChannel:
  288. e2elog.Logf("Pod wasn't evicted. Test successful")
  289. case <-observedDeletions:
  290. framework.Failf("Pod was evicted despite toleration")
  291. }
  292. })
  293. })
  294. var _ = SIGDescribe("NoExecuteTaintManager Multiple Pods [Serial]", func() {
  295. var cs clientset.Interface
  296. var ns string
  297. f := framework.NewDefaultFramework("taint-multiple-pods")
  298. ginkgo.BeforeEach(func() {
  299. cs = f.ClientSet
  300. ns = f.Namespace.Name
  301. framework.WaitForAllNodesHealthy(cs, time.Minute)
  302. err := framework.CheckTestingNSDeletedExcept(cs, ns)
  303. framework.ExpectNoError(err)
  304. })
  305. // 1. Run two pods; one with toleration, one without toleration
  306. // 2. Taint the nodes running those pods with a no-execute taint
  307. // 3. See if pod-without-toleration get evicted, and pod-with-toleration is kept
  308. ginkgo.It("only evicts pods without tolerations from tainted nodes", func() {
  309. podGroup := "taint-eviction-a"
  310. observedDeletions := make(chan string, 100)
  311. stopCh := make(chan struct{})
  312. createTestController(cs, observedDeletions, stopCh, podGroup, ns)
  313. pod1 := createPodForTaintsTest(false, 0, podGroup+"1", podGroup, ns)
  314. pod2 := createPodForTaintsTest(true, 0, podGroup+"2", podGroup, ns)
  315. ginkgo.By("Starting pods...")
  316. nodeName1, err := testutils.RunPodAndGetNodeName(cs, pod1, 2*time.Minute)
  317. framework.ExpectNoError(err)
  318. e2elog.Logf("Pod1 is running on %v. Tainting Node", nodeName1)
  319. nodeName2, err := testutils.RunPodAndGetNodeName(cs, pod2, 2*time.Minute)
  320. framework.ExpectNoError(err)
  321. e2elog.Logf("Pod2 is running on %v. Tainting Node", nodeName2)
  322. ginkgo.By("Trying to apply a taint on the Nodes")
  323. testTaint := getTestTaint()
  324. framework.AddOrUpdateTaintOnNode(cs, nodeName1, testTaint)
  325. framework.ExpectNodeHasTaint(cs, nodeName1, &testTaint)
  326. defer framework.RemoveTaintOffNode(cs, nodeName1, testTaint)
  327. if nodeName2 != nodeName1 {
  328. framework.AddOrUpdateTaintOnNode(cs, nodeName2, testTaint)
  329. framework.ExpectNodeHasTaint(cs, nodeName2, &testTaint)
  330. defer framework.RemoveTaintOffNode(cs, nodeName2, testTaint)
  331. }
  332. // Wait a bit
  333. ginkgo.By("Waiting for Pod1 to be deleted")
  334. timeoutChannel := time.NewTimer(time.Duration(kubeletPodDeletionDelaySeconds+additionalWaitPerDeleteSeconds) * time.Second).C
  335. var evicted int
  336. for {
  337. select {
  338. case <-timeoutChannel:
  339. if evicted == 0 {
  340. framework.Failf("Failed to evict Pod1.")
  341. } else if evicted == 2 {
  342. framework.Failf("Pod1 is evicted. But unexpected Pod2 also get evicted.")
  343. }
  344. return
  345. case podName := <-observedDeletions:
  346. evicted++
  347. if podName == podGroup+"1" {
  348. e2elog.Logf("Noticed Pod %q gets evicted.", podName)
  349. } else if podName == podGroup+"2" {
  350. framework.Failf("Unexepected Pod %q gets evicted.", podName)
  351. return
  352. }
  353. }
  354. }
  355. })
  356. // 1. Run two pods both with toleration; one with tolerationSeconds=5, the other with 25
  357. // 2. Taint the nodes running those pods with a no-execute taint
  358. // 3. See if both pods get evicted in between [5, 25] seconds
  359. ginkgo.It("evicts pods with minTolerationSeconds", func() {
  360. podGroup := "taint-eviction-b"
  361. observedDeletions := make(chan string, 100)
  362. stopCh := make(chan struct{})
  363. createTestController(cs, observedDeletions, stopCh, podGroup, ns)
  364. pod1 := createPodForTaintsTest(true, additionalWaitPerDeleteSeconds, podGroup+"1", podGroup, ns)
  365. pod2 := createPodForTaintsTest(true, 5*additionalWaitPerDeleteSeconds, podGroup+"2", podGroup, ns)
  366. ginkgo.By("Starting pods...")
  367. nodeName, err := testutils.RunPodAndGetNodeName(cs, pod1, 2*time.Minute)
  368. node, err := cs.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
  369. framework.ExpectNoError(err)
  370. nodeHostNameLabel, ok := node.GetObjectMeta().GetLabels()["kubernetes.io/hostname"]
  371. if !ok {
  372. framework.Failf("error getting kubernetes.io/hostname label on node %s", nodeName)
  373. }
  374. framework.ExpectNoError(err)
  375. e2elog.Logf("Pod1 is running on %v. Tainting Node", nodeName)
  376. // ensure pod2 lands on the same node as pod1
  377. pod2.Spec.NodeSelector = map[string]string{"kubernetes.io/hostname": nodeHostNameLabel}
  378. _, err = testutils.RunPodAndGetNodeName(cs, pod2, 2*time.Minute)
  379. framework.ExpectNoError(err)
  380. e2elog.Logf("Pod2 is running on %v. Tainting Node", nodeName)
  381. ginkgo.By("Trying to apply a taint on the Node")
  382. testTaint := getTestTaint()
  383. framework.AddOrUpdateTaintOnNode(cs, nodeName, testTaint)
  384. framework.ExpectNodeHasTaint(cs, nodeName, &testTaint)
  385. defer framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  386. // Wait a bit
  387. ginkgo.By("Waiting for Pod1 and Pod2 to be deleted")
  388. timeoutChannel := time.NewTimer(time.Duration(kubeletPodDeletionDelaySeconds+3*additionalWaitPerDeleteSeconds) * time.Second).C
  389. var evicted int
  390. for evicted != 2 {
  391. select {
  392. case <-timeoutChannel:
  393. framework.Failf("Failed to evict all Pods. %d pod(s) is not evicted.", 2-evicted)
  394. return
  395. case podName := <-observedDeletions:
  396. e2elog.Logf("Noticed Pod %q gets evicted.", podName)
  397. evicted++
  398. }
  399. }
  400. })
  401. })