critical_pod_test.go 5.6 KB

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