critical_pod_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright 2016 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 e2enode
  14. import (
  15. "context"
  16. "fmt"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/api/resource"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. kubeapi "k8s.io/kubernetes/pkg/apis/core"
  21. "k8s.io/kubernetes/pkg/apis/scheduling"
  22. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  23. kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  26. imageutils "k8s.io/kubernetes/test/utils/image"
  27. "github.com/onsi/ginkgo"
  28. )
  29. const (
  30. criticalPodName = "static-critical-pod"
  31. guaranteedPodName = "guaranteed"
  32. burstablePodName = "burstable"
  33. bestEffortPodName = "best-effort"
  34. )
  35. var _ = framework.KubeDescribe("CriticalPod [Serial] [Disruptive] [NodeFeature:CriticalPod]", func() {
  36. f := framework.NewDefaultFramework("critical-pod-test")
  37. ginkgo.Context("when we need to admit a critical pod", func() {
  38. tempSetCurrentKubeletConfig(f, func(initialConfig *kubeletconfig.KubeletConfiguration) {
  39. if initialConfig.FeatureGates == nil {
  40. initialConfig.FeatureGates = make(map[string]bool)
  41. }
  42. })
  43. ginkgo.It("should be able to create and delete a critical pod", func() {
  44. configEnabled, err := isKubeletConfigEnabled(f)
  45. framework.ExpectNoError(err)
  46. if !configEnabled {
  47. e2eskipper.Skipf("unable to run test without dynamic kubelet config enabled.")
  48. }
  49. // because adminssion Priority enable, If the priority class is not found, the Pod is rejected.
  50. node := getNodeName(f)
  51. // Define test pods
  52. nonCriticalGuaranteed := getTestPod(false, guaranteedPodName, v1.ResourceRequirements{
  53. Requests: v1.ResourceList{
  54. v1.ResourceCPU: resource.MustParse("100m"),
  55. v1.ResourceMemory: resource.MustParse("100Mi"),
  56. },
  57. Limits: v1.ResourceList{
  58. v1.ResourceCPU: resource.MustParse("100m"),
  59. v1.ResourceMemory: resource.MustParse("100Mi"),
  60. },
  61. }, node)
  62. nonCriticalBurstable := getTestPod(false, burstablePodName, v1.ResourceRequirements{
  63. Requests: v1.ResourceList{
  64. v1.ResourceCPU: resource.MustParse("100m"),
  65. v1.ResourceMemory: resource.MustParse("100Mi"),
  66. },
  67. }, node)
  68. nonCriticalBestEffort := getTestPod(false, bestEffortPodName, v1.ResourceRequirements{}, node)
  69. criticalPod := getTestPod(true, criticalPodName, v1.ResourceRequirements{
  70. // request the entire resource capacity of the node, so that
  71. // admitting this pod requires the other pod to be preempted
  72. Requests: getNodeCPUAndMemoryCapacity(f),
  73. }, node)
  74. // Create pods, starting with non-critical so that the critical preempts the other pods.
  75. f.PodClient().CreateBatch([]*v1.Pod{nonCriticalBestEffort, nonCriticalBurstable, nonCriticalGuaranteed})
  76. f.PodClientNS(kubeapi.NamespaceSystem).CreateSync(criticalPod)
  77. // Check that non-critical pods other than the besteffort have been evicted
  78. updatedPodList, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{})
  79. framework.ExpectNoError(err)
  80. for _, p := range updatedPodList.Items {
  81. if p.Name == nonCriticalBestEffort.Name {
  82. framework.ExpectEqual(p.Status.Phase, v1.PodRunning, fmt.Sprintf("pod: %v should not be preempted with status: %#v", p.Name, p.Status))
  83. } else {
  84. framework.ExpectEqual(p.Status.Phase, v1.PodFailed, fmt.Sprintf("pod: %v should be preempted with status: %#v", p.Name, p.Status))
  85. }
  86. }
  87. })
  88. ginkgo.AfterEach(func() {
  89. // Delete Pods
  90. f.PodClient().DeleteSync(guaranteedPodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  91. f.PodClient().DeleteSync(burstablePodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  92. f.PodClient().DeleteSync(bestEffortPodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  93. f.PodClientNS(kubeapi.NamespaceSystem).DeleteSync(criticalPodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
  94. // Log Events
  95. logPodEvents(f)
  96. logNodeEvents(f)
  97. })
  98. })
  99. })
  100. func getNodeCPUAndMemoryCapacity(f *framework.Framework) v1.ResourceList {
  101. nodeList, err := f.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
  102. framework.ExpectNoError(err)
  103. // Assuming that there is only one node, because this is a node e2e test.
  104. framework.ExpectEqual(len(nodeList.Items), 1)
  105. capacity := nodeList.Items[0].Status.Allocatable
  106. return v1.ResourceList{
  107. v1.ResourceCPU: capacity[v1.ResourceCPU],
  108. v1.ResourceMemory: capacity[v1.ResourceMemory],
  109. }
  110. }
  111. func getNodeName(f *framework.Framework) string {
  112. nodeList, err := f.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
  113. framework.ExpectNoError(err)
  114. // Assuming that there is only one node, because this is a node e2e test.
  115. framework.ExpectEqual(len(nodeList.Items), 1)
  116. return nodeList.Items[0].GetName()
  117. }
  118. func getTestPod(critical bool, name string, resources v1.ResourceRequirements, node string) *v1.Pod {
  119. pod := &v1.Pod{
  120. TypeMeta: metav1.TypeMeta{
  121. Kind: "Pod",
  122. APIVersion: "v1",
  123. },
  124. ObjectMeta: metav1.ObjectMeta{Name: name},
  125. Spec: v1.PodSpec{
  126. Containers: []v1.Container{
  127. {
  128. Name: "container",
  129. Image: imageutils.GetPauseImageName(),
  130. Resources: resources,
  131. },
  132. },
  133. NodeName: node,
  134. },
  135. }
  136. if critical {
  137. pod.ObjectMeta.Namespace = kubeapi.NamespaceSystem
  138. pod.ObjectMeta.Annotations = map[string]string{
  139. kubelettypes.ConfigSourceAnnotationKey: kubelettypes.FileSource,
  140. }
  141. pod.Spec.PriorityClassName = scheduling.SystemNodeCritical
  142. framework.ExpectEqual(kubelettypes.IsCriticalPod(pod), true, "pod should be a critical pod")
  143. } else {
  144. framework.ExpectEqual(kubelettypes.IsCriticalPod(pod), false, "pod should not be a critical pod")
  145. }
  146. return pod
  147. }