predicates.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. "context"
  16. "fmt"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/resource"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/sets"
  22. "k8s.io/apimachinery/pkg/util/uuid"
  23. utilversion "k8s.io/apimachinery/pkg/util/version"
  24. clientset "k8s.io/client-go/kubernetes"
  25. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. e2eevents "k8s.io/kubernetes/test/e2e/framework/events"
  28. e2ekubelet "k8s.io/kubernetes/test/e2e/framework/kubelet"
  29. e2enode "k8s.io/kubernetes/test/e2e/framework/node"
  30. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  31. e2erc "k8s.io/kubernetes/test/e2e/framework/rc"
  32. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  33. testutils "k8s.io/kubernetes/test/utils"
  34. imageutils "k8s.io/kubernetes/test/utils/image"
  35. k8utilnet "k8s.io/utils/net"
  36. "github.com/onsi/ginkgo"
  37. // ensure libs have a chance to initialize
  38. _ "github.com/stretchr/testify/assert"
  39. )
  40. const (
  41. maxNumberOfPods int64 = 10
  42. defaultTimeout = 3 * time.Minute
  43. )
  44. var localStorageVersion = utilversion.MustParseSemantic("v1.8.0-beta.0")
  45. // variable set in BeforeEach, never modified afterwards
  46. var masterNodes sets.String
  47. type pausePodConfig struct {
  48. Name string
  49. Namespace string
  50. Affinity *v1.Affinity
  51. Annotations, Labels, NodeSelector map[string]string
  52. Resources *v1.ResourceRequirements
  53. Tolerations []v1.Toleration
  54. NodeName string
  55. Ports []v1.ContainerPort
  56. OwnerReferences []metav1.OwnerReference
  57. PriorityClassName string
  58. DeletionGracePeriodSeconds *int64
  59. }
  60. var _ = SIGDescribe("SchedulerPredicates [Serial]", func() {
  61. var cs clientset.Interface
  62. var nodeList *v1.NodeList
  63. var RCName string
  64. var ns string
  65. f := framework.NewDefaultFramework("sched-pred")
  66. ginkgo.AfterEach(func() {
  67. rc, err := cs.CoreV1().ReplicationControllers(ns).Get(context.TODO(), RCName, metav1.GetOptions{})
  68. if err == nil && *(rc.Spec.Replicas) != 0 {
  69. ginkgo.By("Cleaning up the replication controller")
  70. err := e2erc.DeleteRCAndWaitForGC(f.ClientSet, ns, RCName)
  71. framework.ExpectNoError(err)
  72. }
  73. })
  74. ginkgo.BeforeEach(func() {
  75. cs = f.ClientSet
  76. ns = f.Namespace.Name
  77. nodeList = &v1.NodeList{}
  78. var err error
  79. framework.AllNodesReady(cs, time.Minute)
  80. // NOTE: Here doesn't get nodeList for supporting a master nodes which can host workload pods.
  81. masterNodes, _, err = e2enode.GetMasterAndWorkerNodes(cs)
  82. if err != nil {
  83. framework.Logf("Unexpected error occurred: %v", err)
  84. }
  85. nodeList, err = e2enode.GetReadySchedulableNodes(cs)
  86. if err != nil {
  87. framework.Logf("Unexpected error occurred: %v", err)
  88. }
  89. // TODO: write a wrapper for ExpectNoErrorWithOffset()
  90. framework.ExpectNoErrorWithOffset(0, err)
  91. err = framework.CheckTestingNSDeletedExcept(cs, ns)
  92. framework.ExpectNoError(err)
  93. for _, node := range nodeList.Items {
  94. framework.Logf("\nLogging pods the kubelet thinks is on node %v before test", node.Name)
  95. printAllKubeletPods(cs, node.Name)
  96. }
  97. })
  98. // This test verifies we don't allow scheduling of pods in a way that sum of local ephemeral storage resource requests of pods is greater than machines capacity.
  99. // It assumes that cluster add-on pods stay stable and cannot be run in parallel with any other test that touches Nodes or Pods.
  100. // It is so because we need to have precise control on what's running in the cluster.
  101. ginkgo.It("validates local ephemeral storage resource limits of pods that are allowed to run [Feature:LocalStorageCapacityIsolation]", func() {
  102. e2eskipper.SkipUnlessServerVersionGTE(localStorageVersion, f.ClientSet.Discovery())
  103. nodeMaxAllocatable := int64(0)
  104. nodeToAllocatableMap := make(map[string]int64)
  105. for _, node := range nodeList.Items {
  106. allocatable, found := node.Status.Allocatable[v1.ResourceEphemeralStorage]
  107. framework.ExpectEqual(found, true)
  108. nodeToAllocatableMap[node.Name] = allocatable.Value()
  109. if nodeMaxAllocatable < allocatable.Value() {
  110. nodeMaxAllocatable = allocatable.Value()
  111. }
  112. }
  113. WaitForStableCluster(cs, masterNodes)
  114. pods, err := cs.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
  115. framework.ExpectNoError(err)
  116. for _, pod := range pods.Items {
  117. _, found := nodeToAllocatableMap[pod.Spec.NodeName]
  118. if found && pod.Status.Phase != v1.PodSucceeded && pod.Status.Phase != v1.PodFailed {
  119. framework.Logf("Pod %v requesting local ephemeral resource =%v on Node %v", pod.Name, getRequestedStorageEphemeralStorage(pod), pod.Spec.NodeName)
  120. nodeToAllocatableMap[pod.Spec.NodeName] -= getRequestedStorageEphemeralStorage(pod)
  121. }
  122. }
  123. var podsNeededForSaturation int
  124. var ephemeralStoragePerPod int64
  125. ephemeralStoragePerPod = nodeMaxAllocatable / maxNumberOfPods
  126. framework.Logf("Using pod capacity: %v", ephemeralStoragePerPod)
  127. for name, leftAllocatable := range nodeToAllocatableMap {
  128. framework.Logf("Node: %v has local ephemeral resource allocatable: %v", name, leftAllocatable)
  129. podsNeededForSaturation += (int)(leftAllocatable / ephemeralStoragePerPod)
  130. }
  131. ginkgo.By(fmt.Sprintf("Starting additional %v Pods to fully saturate the cluster local ephemeral resource and trying to start another one", podsNeededForSaturation))
  132. // As the pods are distributed randomly among nodes,
  133. // it can easily happen that all nodes are saturated
  134. // and there is no need to create additional pods.
  135. // StartPods requires at least one pod to replicate.
  136. if podsNeededForSaturation > 0 {
  137. framework.ExpectNoError(testutils.StartPods(cs, podsNeededForSaturation, ns, "overcommit",
  138. *initPausePod(f, pausePodConfig{
  139. Name: "",
  140. Labels: map[string]string{"name": ""},
  141. Resources: &v1.ResourceRequirements{
  142. Limits: v1.ResourceList{
  143. v1.ResourceEphemeralStorage: *resource.NewQuantity(ephemeralStoragePerPod, "DecimalSI"),
  144. },
  145. Requests: v1.ResourceList{
  146. v1.ResourceEphemeralStorage: *resource.NewQuantity(ephemeralStoragePerPod, "DecimalSI"),
  147. },
  148. },
  149. }), true, framework.Logf))
  150. }
  151. podName := "additional-pod"
  152. conf := pausePodConfig{
  153. Name: podName,
  154. Labels: map[string]string{"name": "additional"},
  155. Resources: &v1.ResourceRequirements{
  156. Limits: v1.ResourceList{
  157. v1.ResourceEphemeralStorage: *resource.NewQuantity(ephemeralStoragePerPod, "DecimalSI"),
  158. },
  159. Requests: v1.ResourceList{
  160. v1.ResourceEphemeralStorage: *resource.NewQuantity(ephemeralStoragePerPod, "DecimalSI"),
  161. },
  162. },
  163. }
  164. WaitForSchedulerAfterAction(f, createPausePodAction(f, conf), ns, podName, false)
  165. verifyResult(cs, podsNeededForSaturation, 1, ns)
  166. })
  167. // This test verifies we don't allow scheduling of pods in a way that sum of
  168. // resource requests of pods is greater than machines capacity.
  169. // It assumes that cluster add-on pods stay stable and cannot be run in parallel
  170. // with any other test that touches Nodes or Pods.
  171. // It is so because we need to have precise control on what's running in the cluster.
  172. // Test scenario:
  173. // 1. Find the amount CPU resources on each node.
  174. // 2. Create one pod with affinity to each node that uses 70% of the node CPU.
  175. // 3. Wait for the pods to be scheduled.
  176. // 4. Create another pod with no affinity to any node that need 50% of the largest node CPU.
  177. // 5. Make sure this additional pod is not scheduled.
  178. /*
  179. Release : v1.9
  180. Testname: Scheduler, resource limits
  181. Description: Scheduling Pods MUST fail if the resource requests exceed Machine capacity.
  182. */
  183. framework.ConformanceIt("validates resource limits of pods that are allowed to run ", func() {
  184. WaitForStableCluster(cs, masterNodes)
  185. nodeMaxAllocatable := int64(0)
  186. nodeToAllocatableMap := make(map[string]int64)
  187. for _, node := range nodeList.Items {
  188. nodeReady := false
  189. for _, condition := range node.Status.Conditions {
  190. if condition.Type == v1.NodeReady && condition.Status == v1.ConditionTrue {
  191. nodeReady = true
  192. break
  193. }
  194. }
  195. if !nodeReady {
  196. continue
  197. }
  198. // Apply node label to each node
  199. framework.AddOrUpdateLabelOnNode(cs, node.Name, "node", node.Name)
  200. framework.ExpectNodeHasLabel(cs, node.Name, "node", node.Name)
  201. // Find allocatable amount of CPU.
  202. allocatable, found := node.Status.Allocatable[v1.ResourceCPU]
  203. framework.ExpectEqual(found, true)
  204. nodeToAllocatableMap[node.Name] = allocatable.MilliValue()
  205. if nodeMaxAllocatable < allocatable.MilliValue() {
  206. nodeMaxAllocatable = allocatable.MilliValue()
  207. }
  208. }
  209. // Clean up added labels after this test.
  210. defer func() {
  211. for nodeName := range nodeToAllocatableMap {
  212. framework.RemoveLabelOffNode(cs, nodeName, "node")
  213. }
  214. }()
  215. pods, err := cs.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
  216. framework.ExpectNoError(err)
  217. for _, pod := range pods.Items {
  218. _, found := nodeToAllocatableMap[pod.Spec.NodeName]
  219. if found && pod.Status.Phase != v1.PodSucceeded && pod.Status.Phase != v1.PodFailed {
  220. framework.Logf("Pod %v requesting resource cpu=%vm on Node %v", pod.Name, getRequestedCPU(pod), pod.Spec.NodeName)
  221. nodeToAllocatableMap[pod.Spec.NodeName] -= getRequestedCPU(pod)
  222. }
  223. }
  224. ginkgo.By("Starting Pods to consume most of the cluster CPU.")
  225. // Create one pod per node that requires 70% of the node remaining CPU.
  226. fillerPods := []*v1.Pod{}
  227. for nodeName, cpu := range nodeToAllocatableMap {
  228. requestedCPU := cpu * 7 / 10
  229. framework.Logf("Creating a pod which consumes cpu=%vm on Node %v", requestedCPU, nodeName)
  230. fillerPods = append(fillerPods, createPausePod(f, pausePodConfig{
  231. Name: "filler-pod-" + string(uuid.NewUUID()),
  232. Resources: &v1.ResourceRequirements{
  233. Limits: v1.ResourceList{
  234. v1.ResourceCPU: *resource.NewMilliQuantity(requestedCPU, "DecimalSI"),
  235. },
  236. Requests: v1.ResourceList{
  237. v1.ResourceCPU: *resource.NewMilliQuantity(requestedCPU, "DecimalSI"),
  238. },
  239. },
  240. Affinity: &v1.Affinity{
  241. NodeAffinity: &v1.NodeAffinity{
  242. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  243. NodeSelectorTerms: []v1.NodeSelectorTerm{
  244. {
  245. MatchExpressions: []v1.NodeSelectorRequirement{
  246. {
  247. Key: "node",
  248. Operator: v1.NodeSelectorOpIn,
  249. Values: []string{nodeName},
  250. },
  251. },
  252. },
  253. },
  254. },
  255. },
  256. },
  257. }))
  258. }
  259. // Wait for filler pods to schedule.
  260. for _, pod := range fillerPods {
  261. framework.ExpectNoError(e2epod.WaitForPodRunningInNamespace(cs, pod))
  262. }
  263. ginkgo.By("Creating another pod that requires unavailable amount of CPU.")
  264. // Create another pod that requires 50% of the largest node CPU resources.
  265. // This pod should remain pending as at least 70% of CPU of other nodes in
  266. // the cluster are already consumed.
  267. podName := "additional-pod"
  268. conf := pausePodConfig{
  269. Name: podName,
  270. Labels: map[string]string{"name": "additional"},
  271. Resources: &v1.ResourceRequirements{
  272. Limits: v1.ResourceList{
  273. v1.ResourceCPU: *resource.NewMilliQuantity(nodeMaxAllocatable*5/10, "DecimalSI"),
  274. },
  275. Requests: v1.ResourceList{
  276. v1.ResourceCPU: *resource.NewMilliQuantity(nodeMaxAllocatable*5/10, "DecimalSI"),
  277. },
  278. },
  279. }
  280. WaitForSchedulerAfterAction(f, createPausePodAction(f, conf), ns, podName, false)
  281. verifyResult(cs, len(fillerPods), 1, ns)
  282. })
  283. // Test Nodes does not have any label, hence it should be impossible to schedule Pod with
  284. // nonempty Selector set.
  285. /*
  286. Release : v1.9
  287. Testname: Scheduler, node selector not matching
  288. Description: Create a Pod with a NodeSelector set to a value that does not match a node in the cluster. Since there are no nodes matching the criteria the Pod MUST not be scheduled.
  289. */
  290. framework.ConformanceIt("validates that NodeSelector is respected if not matching ", func() {
  291. ginkgo.By("Trying to schedule Pod with nonempty NodeSelector.")
  292. podName := "restricted-pod"
  293. WaitForStableCluster(cs, masterNodes)
  294. conf := pausePodConfig{
  295. Name: podName,
  296. Labels: map[string]string{"name": "restricted"},
  297. NodeSelector: map[string]string{
  298. "label": "nonempty",
  299. },
  300. }
  301. WaitForSchedulerAfterAction(f, createPausePodAction(f, conf), ns, podName, false)
  302. verifyResult(cs, 0, 1, ns)
  303. })
  304. /*
  305. Release : v1.9
  306. Testname: Scheduler, node selector matching
  307. Description: Create a label on the node {k: v}. Then create a Pod with a NodeSelector set to {k: v}. Check to see if the Pod is scheduled. When the NodeSelector matches then Pod MUST be scheduled on that node.
  308. */
  309. framework.ConformanceIt("validates that NodeSelector is respected if matching ", func() {
  310. nodeName := GetNodeThatCanRunPod(f)
  311. ginkgo.By("Trying to apply a random label on the found node.")
  312. k := fmt.Sprintf("kubernetes.io/e2e-%s", string(uuid.NewUUID()))
  313. v := "42"
  314. framework.AddOrUpdateLabelOnNode(cs, nodeName, k, v)
  315. framework.ExpectNodeHasLabel(cs, nodeName, k, v)
  316. defer framework.RemoveLabelOffNode(cs, nodeName, k)
  317. ginkgo.By("Trying to relaunch the pod, now with labels.")
  318. labelPodName := "with-labels"
  319. createPausePod(f, pausePodConfig{
  320. Name: labelPodName,
  321. NodeSelector: map[string]string{
  322. k: v,
  323. },
  324. })
  325. // check that pod got scheduled. We intentionally DO NOT check that the
  326. // pod is running because this will create a race condition with the
  327. // kubelet and the scheduler: the scheduler might have scheduled a pod
  328. // already when the kubelet does not know about its new label yet. The
  329. // kubelet will then refuse to launch the pod.
  330. framework.ExpectNoError(e2epod.WaitForPodNotPending(cs, ns, labelPodName))
  331. labelPod, err := cs.CoreV1().Pods(ns).Get(context.TODO(), labelPodName, metav1.GetOptions{})
  332. framework.ExpectNoError(err)
  333. framework.ExpectEqual(labelPod.Spec.NodeName, nodeName)
  334. })
  335. // Test Nodes does not have any label, hence it should be impossible to schedule Pod with
  336. // non-nil NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.
  337. ginkgo.It("validates that NodeAffinity is respected if not matching", func() {
  338. ginkgo.By("Trying to schedule Pod with nonempty NodeSelector.")
  339. podName := "restricted-pod"
  340. WaitForStableCluster(cs, masterNodes)
  341. conf := pausePodConfig{
  342. Name: podName,
  343. Affinity: &v1.Affinity{
  344. NodeAffinity: &v1.NodeAffinity{
  345. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  346. NodeSelectorTerms: []v1.NodeSelectorTerm{
  347. {
  348. MatchExpressions: []v1.NodeSelectorRequirement{
  349. {
  350. Key: "foo",
  351. Operator: v1.NodeSelectorOpIn,
  352. Values: []string{"bar", "value2"},
  353. },
  354. },
  355. }, {
  356. MatchExpressions: []v1.NodeSelectorRequirement{
  357. {
  358. Key: "diffkey",
  359. Operator: v1.NodeSelectorOpIn,
  360. Values: []string{"wrong", "value2"},
  361. },
  362. },
  363. },
  364. },
  365. },
  366. },
  367. },
  368. Labels: map[string]string{"name": "restricted"},
  369. }
  370. WaitForSchedulerAfterAction(f, createPausePodAction(f, conf), ns, podName, false)
  371. verifyResult(cs, 0, 1, ns)
  372. })
  373. // Keep the same steps with the test on NodeSelector,
  374. // but specify Affinity in Pod.Spec.Affinity, instead of NodeSelector.
  375. ginkgo.It("validates that required NodeAffinity setting is respected if matching", func() {
  376. nodeName := GetNodeThatCanRunPod(f)
  377. ginkgo.By("Trying to apply a random label on the found node.")
  378. k := fmt.Sprintf("kubernetes.io/e2e-%s", string(uuid.NewUUID()))
  379. v := "42"
  380. framework.AddOrUpdateLabelOnNode(cs, nodeName, k, v)
  381. framework.ExpectNodeHasLabel(cs, nodeName, k, v)
  382. defer framework.RemoveLabelOffNode(cs, nodeName, k)
  383. ginkgo.By("Trying to relaunch the pod, now with labels.")
  384. labelPodName := "with-labels"
  385. createPausePod(f, pausePodConfig{
  386. Name: labelPodName,
  387. Affinity: &v1.Affinity{
  388. NodeAffinity: &v1.NodeAffinity{
  389. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  390. NodeSelectorTerms: []v1.NodeSelectorTerm{
  391. {
  392. MatchExpressions: []v1.NodeSelectorRequirement{
  393. {
  394. Key: k,
  395. Operator: v1.NodeSelectorOpIn,
  396. Values: []string{v},
  397. },
  398. },
  399. },
  400. },
  401. },
  402. },
  403. },
  404. })
  405. // check that pod got scheduled. We intentionally DO NOT check that the
  406. // pod is running because this will create a race condition with the
  407. // kubelet and the scheduler: the scheduler might have scheduled a pod
  408. // already when the kubelet does not know about its new label yet. The
  409. // kubelet will then refuse to launch the pod.
  410. framework.ExpectNoError(e2epod.WaitForPodNotPending(cs, ns, labelPodName))
  411. labelPod, err := cs.CoreV1().Pods(ns).Get(context.TODO(), labelPodName, metav1.GetOptions{})
  412. framework.ExpectNoError(err)
  413. framework.ExpectEqual(labelPod.Spec.NodeName, nodeName)
  414. })
  415. // 1. Run a pod to get an available node, then delete the pod
  416. // 2. Taint the node with a random taint
  417. // 3. Try to relaunch the pod with tolerations tolerate the taints on node,
  418. // and the pod's nodeName specified to the name of node found in step 1
  419. ginkgo.It("validates that taints-tolerations is respected if matching", func() {
  420. nodeName := getNodeThatCanRunPodWithoutToleration(f)
  421. ginkgo.By("Trying to apply a random taint on the found node.")
  422. testTaint := v1.Taint{
  423. Key: fmt.Sprintf("kubernetes.io/e2e-taint-key-%s", string(uuid.NewUUID())),
  424. Value: "testing-taint-value",
  425. Effect: v1.TaintEffectNoSchedule,
  426. }
  427. framework.AddOrUpdateTaintOnNode(cs, nodeName, testTaint)
  428. framework.ExpectNodeHasTaint(cs, nodeName, &testTaint)
  429. defer framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  430. ginkgo.By("Trying to apply a random label on the found node.")
  431. labelKey := fmt.Sprintf("kubernetes.io/e2e-label-key-%s", string(uuid.NewUUID()))
  432. labelValue := "testing-label-value"
  433. framework.AddOrUpdateLabelOnNode(cs, nodeName, labelKey, labelValue)
  434. framework.ExpectNodeHasLabel(cs, nodeName, labelKey, labelValue)
  435. defer framework.RemoveLabelOffNode(cs, nodeName, labelKey)
  436. ginkgo.By("Trying to relaunch the pod, now with tolerations.")
  437. tolerationPodName := "with-tolerations"
  438. createPausePod(f, pausePodConfig{
  439. Name: tolerationPodName,
  440. Tolerations: []v1.Toleration{{Key: testTaint.Key, Value: testTaint.Value, Effect: testTaint.Effect}},
  441. NodeSelector: map[string]string{labelKey: labelValue},
  442. })
  443. // check that pod got scheduled. We intentionally DO NOT check that the
  444. // pod is running because this will create a race condition with the
  445. // kubelet and the scheduler: the scheduler might have scheduled a pod
  446. // already when the kubelet does not know about its new taint yet. The
  447. // kubelet will then refuse to launch the pod.
  448. framework.ExpectNoError(e2epod.WaitForPodNotPending(cs, ns, tolerationPodName))
  449. deployedPod, err := cs.CoreV1().Pods(ns).Get(context.TODO(), tolerationPodName, metav1.GetOptions{})
  450. framework.ExpectNoError(err)
  451. framework.ExpectEqual(deployedPod.Spec.NodeName, nodeName)
  452. })
  453. // 1. Run a pod to get an available node, then delete the pod
  454. // 2. Taint the node with a random taint
  455. // 3. Try to relaunch the pod still no tolerations,
  456. // and the pod's nodeName specified to the name of node found in step 1
  457. ginkgo.It("validates that taints-tolerations is respected if not matching", func() {
  458. nodeName := getNodeThatCanRunPodWithoutToleration(f)
  459. ginkgo.By("Trying to apply a random taint on the found node.")
  460. testTaint := v1.Taint{
  461. Key: fmt.Sprintf("kubernetes.io/e2e-taint-key-%s", string(uuid.NewUUID())),
  462. Value: "testing-taint-value",
  463. Effect: v1.TaintEffectNoSchedule,
  464. }
  465. framework.AddOrUpdateTaintOnNode(cs, nodeName, testTaint)
  466. framework.ExpectNodeHasTaint(cs, nodeName, &testTaint)
  467. defer framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  468. ginkgo.By("Trying to apply a random label on the found node.")
  469. labelKey := fmt.Sprintf("kubernetes.io/e2e-label-key-%s", string(uuid.NewUUID()))
  470. labelValue := "testing-label-value"
  471. framework.AddOrUpdateLabelOnNode(cs, nodeName, labelKey, labelValue)
  472. framework.ExpectNodeHasLabel(cs, nodeName, labelKey, labelValue)
  473. defer framework.RemoveLabelOffNode(cs, nodeName, labelKey)
  474. ginkgo.By("Trying to relaunch the pod, still no tolerations.")
  475. podNameNoTolerations := "still-no-tolerations"
  476. conf := pausePodConfig{
  477. Name: podNameNoTolerations,
  478. NodeSelector: map[string]string{labelKey: labelValue},
  479. }
  480. WaitForSchedulerAfterAction(f, createPausePodAction(f, conf), ns, podNameNoTolerations, false)
  481. verifyResult(cs, 0, 1, ns)
  482. ginkgo.By("Removing taint off the node")
  483. WaitForSchedulerAfterAction(f, removeTaintFromNodeAction(cs, nodeName, testTaint), ns, podNameNoTolerations, true)
  484. verifyResult(cs, 1, 0, ns)
  485. })
  486. /*
  487. Release : v1.16
  488. Testname: Scheduling, HostPort matching and HostIP and Protocol not-matching
  489. Description: Pods with the same HostPort value MUST be able to be scheduled to the same node
  490. if the HostIP or Protocol is different.
  491. */
  492. framework.ConformanceIt("validates that there is no conflict between pods with same hostPort but different hostIP and protocol", func() {
  493. nodeName := GetNodeThatCanRunPod(f)
  494. // use nodeSelector to make sure the testing pods get assigned on the same node to explicitly verify there exists conflict or not
  495. ginkgo.By("Trying to apply a random label on the found node.")
  496. k := fmt.Sprintf("kubernetes.io/e2e-%s", string(uuid.NewUUID()))
  497. v := "90"
  498. nodeSelector := make(map[string]string)
  499. nodeSelector[k] = v
  500. framework.AddOrUpdateLabelOnNode(cs, nodeName, k, v)
  501. framework.ExpectNodeHasLabel(cs, nodeName, k, v)
  502. defer framework.RemoveLabelOffNode(cs, nodeName, k)
  503. port := int32(54321)
  504. ginkgo.By(fmt.Sprintf("Trying to create a pod(pod1) with hostport %v and hostIP 127.0.0.1 and expect scheduled", port))
  505. createHostPortPodOnNode(f, "pod1", ns, "127.0.0.1", port, v1.ProtocolTCP, nodeSelector, true)
  506. ginkgo.By(fmt.Sprintf("Trying to create another pod(pod2) with hostport %v but hostIP 127.0.0.2 on the node which pod1 resides and expect scheduled", port))
  507. createHostPortPodOnNode(f, "pod2", ns, "127.0.0.2", port, v1.ProtocolTCP, nodeSelector, true)
  508. ginkgo.By(fmt.Sprintf("Trying to create a third pod(pod3) with hostport %v, hostIP 127.0.0.2 but use UDP protocol on the node which pod2 resides", port))
  509. createHostPortPodOnNode(f, "pod3", ns, "127.0.0.2", port, v1.ProtocolUDP, nodeSelector, true)
  510. })
  511. /*
  512. Release : v1.16
  513. Testname: Scheduling, HostPort and Protocol match, HostIPs different but one is default HostIP (0.0.0.0)
  514. Description: Pods with the same HostPort and Protocol, but different HostIPs, MUST NOT schedule to the
  515. same node if one of those IPs is the default HostIP of 0.0.0.0, which represents all IPs on the host.
  516. */
  517. framework.ConformanceIt("validates that there exists conflict between pods with same hostPort and protocol but one using 0.0.0.0 hostIP", func() {
  518. nodeName := GetNodeThatCanRunPod(f)
  519. // use nodeSelector to make sure the testing pods get assigned on the same node to explicitly verify there exists conflict or not
  520. ginkgo.By("Trying to apply a random label on the found node.")
  521. k := fmt.Sprintf("kubernetes.io/e2e-%s", string(uuid.NewUUID()))
  522. v := "95"
  523. nodeSelector := make(map[string]string)
  524. nodeSelector[k] = v
  525. framework.AddOrUpdateLabelOnNode(cs, nodeName, k, v)
  526. framework.ExpectNodeHasLabel(cs, nodeName, k, v)
  527. defer framework.RemoveLabelOffNode(cs, nodeName, k)
  528. port := int32(54322)
  529. ginkgo.By(fmt.Sprintf("Trying to create a pod(pod4) with hostport %v and hostIP 0.0.0.0(empty string here) and expect scheduled", port))
  530. createHostPortPodOnNode(f, "pod4", ns, "", port, v1.ProtocolTCP, nodeSelector, true)
  531. ginkgo.By(fmt.Sprintf("Trying to create another pod(pod5) with hostport %v but hostIP 127.0.0.1 on the node which pod4 resides and expect not scheduled", port))
  532. createHostPortPodOnNode(f, "pod5", ns, "127.0.0.1", port, v1.ProtocolTCP, nodeSelector, false)
  533. })
  534. })
  535. // printAllKubeletPods outputs status of all kubelet pods into log.
  536. func printAllKubeletPods(c clientset.Interface, nodeName string) {
  537. podList, err := e2ekubelet.GetKubeletPods(c, nodeName)
  538. if err != nil {
  539. framework.Logf("Unable to retrieve kubelet pods for node %v: %v", nodeName, err)
  540. return
  541. }
  542. for _, p := range podList.Items {
  543. framework.Logf("%v from %v started at %v (%d container statuses recorded)", p.Name, p.Namespace, p.Status.StartTime, len(p.Status.ContainerStatuses))
  544. for _, c := range p.Status.ContainerStatuses {
  545. framework.Logf("\tContainer %v ready: %v, restart count %v",
  546. c.Name, c.Ready, c.RestartCount)
  547. }
  548. }
  549. }
  550. func initPausePod(f *framework.Framework, conf pausePodConfig) *v1.Pod {
  551. var gracePeriod = int64(1)
  552. pod := &v1.Pod{
  553. ObjectMeta: metav1.ObjectMeta{
  554. Name: conf.Name,
  555. Namespace: conf.Namespace,
  556. Labels: conf.Labels,
  557. Annotations: conf.Annotations,
  558. OwnerReferences: conf.OwnerReferences,
  559. },
  560. Spec: v1.PodSpec{
  561. NodeSelector: conf.NodeSelector,
  562. Affinity: conf.Affinity,
  563. Containers: []v1.Container{
  564. {
  565. Name: conf.Name,
  566. Image: imageutils.GetPauseImageName(),
  567. Ports: conf.Ports,
  568. },
  569. },
  570. Tolerations: conf.Tolerations,
  571. NodeName: conf.NodeName,
  572. PriorityClassName: conf.PriorityClassName,
  573. TerminationGracePeriodSeconds: &gracePeriod,
  574. },
  575. }
  576. if conf.Resources != nil {
  577. pod.Spec.Containers[0].Resources = *conf.Resources
  578. }
  579. if conf.DeletionGracePeriodSeconds != nil {
  580. pod.ObjectMeta.DeletionGracePeriodSeconds = conf.DeletionGracePeriodSeconds
  581. }
  582. return pod
  583. }
  584. func createPausePod(f *framework.Framework, conf pausePodConfig) *v1.Pod {
  585. namespace := conf.Namespace
  586. if len(namespace) == 0 {
  587. namespace = f.Namespace.Name
  588. }
  589. pod, err := f.ClientSet.CoreV1().Pods(namespace).Create(context.TODO(), initPausePod(f, conf), metav1.CreateOptions{})
  590. framework.ExpectNoError(err)
  591. return pod
  592. }
  593. func runPausePod(f *framework.Framework, conf pausePodConfig) *v1.Pod {
  594. pod := createPausePod(f, conf)
  595. framework.ExpectNoError(e2epod.WaitForPodRunningInNamespace(f.ClientSet, pod))
  596. pod, err := f.ClientSet.CoreV1().Pods(pod.Namespace).Get(context.TODO(), conf.Name, metav1.GetOptions{})
  597. framework.ExpectNoError(err)
  598. return pod
  599. }
  600. func runPodAndGetNodeName(f *framework.Framework, conf pausePodConfig) string {
  601. // launch a pod to find a node which can launch a pod. We intentionally do
  602. // not just take the node list and choose the first of them. Depending on the
  603. // cluster and the scheduler it might be that a "normal" pod cannot be
  604. // scheduled onto it.
  605. pod := runPausePod(f, conf)
  606. ginkgo.By("Explicitly delete pod here to free the resource it takes.")
  607. err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  608. framework.ExpectNoError(err)
  609. return pod.Spec.NodeName
  610. }
  611. func getRequestedCPU(pod v1.Pod) int64 {
  612. var result int64
  613. for _, container := range pod.Spec.Containers {
  614. result += container.Resources.Requests.Cpu().MilliValue()
  615. }
  616. return result
  617. }
  618. func getRequestedStorageEphemeralStorage(pod v1.Pod) int64 {
  619. var result int64
  620. for _, container := range pod.Spec.Containers {
  621. result += container.Resources.Requests.StorageEphemeral().Value()
  622. }
  623. return result
  624. }
  625. // removeTaintFromNodeAction returns a closure that removes the given taint
  626. // from the given node upon invocation.
  627. func removeTaintFromNodeAction(cs clientset.Interface, nodeName string, testTaint v1.Taint) e2eevents.Action {
  628. return func() error {
  629. framework.RemoveTaintOffNode(cs, nodeName, testTaint)
  630. return nil
  631. }
  632. }
  633. // createPausePodAction returns a closure that creates a pause pod upon invocation.
  634. func createPausePodAction(f *framework.Framework, conf pausePodConfig) e2eevents.Action {
  635. return func() error {
  636. _, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), initPausePod(f, conf), metav1.CreateOptions{})
  637. return err
  638. }
  639. }
  640. // WaitForSchedulerAfterAction performs the provided action and then waits for
  641. // scheduler to act on the given pod.
  642. func WaitForSchedulerAfterAction(f *framework.Framework, action e2eevents.Action, ns, podName string, expectSuccess bool) {
  643. predicate := scheduleFailureEvent(podName)
  644. if expectSuccess {
  645. predicate = scheduleSuccessEvent(ns, podName, "" /* any node */)
  646. }
  647. success, err := e2eevents.ObserveEventAfterAction(f.ClientSet, f.Namespace.Name, predicate, action)
  648. framework.ExpectNoError(err)
  649. framework.ExpectEqual(success, true)
  650. }
  651. // TODO: upgrade calls in PodAffinity tests when we're able to run them
  652. func verifyResult(c clientset.Interface, expectedScheduled int, expectedNotScheduled int, ns string) {
  653. allPods, err := c.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{})
  654. framework.ExpectNoError(err)
  655. scheduledPods, notScheduledPods := GetPodsScheduled(masterNodes, allPods)
  656. framework.ExpectEqual(len(notScheduledPods), expectedNotScheduled, fmt.Sprintf("Not scheduled Pods: %#v", notScheduledPods))
  657. framework.ExpectEqual(len(scheduledPods), expectedScheduled, fmt.Sprintf("Scheduled Pods: %#v", scheduledPods))
  658. }
  659. // GetNodeThatCanRunPod trying to launch a pod without a label to get a node which can launch it
  660. func GetNodeThatCanRunPod(f *framework.Framework) string {
  661. ginkgo.By("Trying to launch a pod without a label to get a node which can launch it.")
  662. return runPodAndGetNodeName(f, pausePodConfig{Name: "without-label"})
  663. }
  664. func getNodeThatCanRunPodWithoutToleration(f *framework.Framework) string {
  665. ginkgo.By("Trying to launch a pod without a toleration to get a node which can launch it.")
  666. return runPodAndGetNodeName(f, pausePodConfig{Name: "without-toleration"})
  667. }
  668. // CreateHostPortPods creates RC with host port 4321
  669. func CreateHostPortPods(f *framework.Framework, id string, replicas int, expectRunning bool) {
  670. ginkgo.By(fmt.Sprintf("Running RC which reserves host port"))
  671. config := &testutils.RCConfig{
  672. Client: f.ClientSet,
  673. Name: id,
  674. Namespace: f.Namespace.Name,
  675. Timeout: defaultTimeout,
  676. Image: imageutils.GetPauseImageName(),
  677. Replicas: replicas,
  678. HostPorts: map[string]int{"port1": 4321},
  679. }
  680. err := e2erc.RunRC(*config)
  681. if expectRunning {
  682. framework.ExpectNoError(err)
  683. }
  684. }
  685. // CreateNodeSelectorPods creates RC with host port 4321 and defines node selector
  686. func CreateNodeSelectorPods(f *framework.Framework, id string, replicas int, nodeSelector map[string]string, expectRunning bool) error {
  687. ginkgo.By(fmt.Sprintf("Running RC which reserves host port and defines node selector"))
  688. config := &testutils.RCConfig{
  689. Client: f.ClientSet,
  690. Name: id,
  691. Namespace: f.Namespace.Name,
  692. Timeout: defaultTimeout,
  693. Image: imageutils.GetPauseImageName(),
  694. Replicas: replicas,
  695. HostPorts: map[string]int{"port1": 4321},
  696. NodeSelector: nodeSelector,
  697. }
  698. err := e2erc.RunRC(*config)
  699. if expectRunning {
  700. return err
  701. }
  702. return nil
  703. }
  704. // create pod which using hostport on the specified node according to the nodeSelector
  705. func createHostPortPodOnNode(f *framework.Framework, podName, ns, hostIP string, port int32, protocol v1.Protocol, nodeSelector map[string]string, expectScheduled bool) {
  706. hostIP = translateIPv4ToIPv6(hostIP)
  707. createPausePod(f, pausePodConfig{
  708. Name: podName,
  709. Ports: []v1.ContainerPort{
  710. {
  711. HostPort: port,
  712. ContainerPort: 80,
  713. Protocol: protocol,
  714. HostIP: hostIP,
  715. },
  716. },
  717. NodeSelector: nodeSelector,
  718. })
  719. err := e2epod.WaitForPodNotPending(f.ClientSet, ns, podName)
  720. if expectScheduled {
  721. framework.ExpectNoError(err)
  722. }
  723. }
  724. // translateIPv4ToIPv6 maps an IPv4 address into a valid IPv6 address
  725. // adding the well known prefix "0::ffff:" https://tools.ietf.org/html/rfc2765
  726. // if the ip is IPv4 and the cluster IPFamily is IPv6, otherwise returns the same ip
  727. func translateIPv4ToIPv6(ip string) string {
  728. if framework.TestContext.IPFamily == "ipv6" && !k8utilnet.IsIPv6String(ip) && ip != "" {
  729. ip = "0::ffff:" + ip
  730. }
  731. return ip
  732. }
  733. // GetPodsScheduled returns a number of currently scheduled and not scheduled Pods.
  734. func GetPodsScheduled(masterNodes sets.String, pods *v1.PodList) (scheduledPods, notScheduledPods []v1.Pod) {
  735. for _, pod := range pods.Items {
  736. if !masterNodes.Has(pod.Spec.NodeName) {
  737. if pod.Spec.NodeName != "" {
  738. _, scheduledCondition := podutil.GetPodCondition(&pod.Status, v1.PodScheduled)
  739. framework.ExpectEqual(scheduledCondition != nil, true)
  740. framework.ExpectEqual(scheduledCondition.Status, v1.ConditionTrue)
  741. scheduledPods = append(scheduledPods, pod)
  742. } else {
  743. _, scheduledCondition := podutil.GetPodCondition(&pod.Status, v1.PodScheduled)
  744. framework.ExpectEqual(scheduledCondition != nil, true)
  745. framework.ExpectEqual(scheduledCondition.Status, v1.ConditionFalse)
  746. if scheduledCondition.Reason == "Unschedulable" {
  747. notScheduledPods = append(notScheduledPods, pod)
  748. }
  749. }
  750. }
  751. }
  752. return
  753. }