limit_range.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 e2e
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/resource"
  18. "k8s.io/kubernetes/test/e2e/framework"
  19. . "github.com/onsi/ginkgo"
  20. . "github.com/onsi/gomega"
  21. )
  22. var _ = framework.KubeDescribe("LimitRange", func() {
  23. f := framework.NewDefaultFramework("limitrange")
  24. It("should create a LimitRange with defaults and ensure pod has those defaults applied.", func() {
  25. By("Creating a LimitRange")
  26. min := getResourceList("50m", "100Mi")
  27. max := getResourceList("500m", "500Mi")
  28. defaultLimit := getResourceList("500m", "500Mi")
  29. defaultRequest := getResourceList("100m", "200Mi")
  30. maxLimitRequestRatio := api.ResourceList{}
  31. limitRange := newLimitRange("limit-range", api.LimitTypeContainer,
  32. min, max,
  33. defaultLimit, defaultRequest,
  34. maxLimitRequestRatio)
  35. limitRange, err := f.Client.LimitRanges(f.Namespace.Name).Create(limitRange)
  36. Expect(err).NotTo(HaveOccurred())
  37. By("Fetching the LimitRange to ensure it has proper values")
  38. limitRange, err = f.Client.LimitRanges(f.Namespace.Name).Get(limitRange.Name)
  39. expected := api.ResourceRequirements{Requests: defaultRequest, Limits: defaultLimit}
  40. actual := api.ResourceRequirements{Requests: limitRange.Spec.Limits[0].DefaultRequest, Limits: limitRange.Spec.Limits[0].Default}
  41. err = equalResourceRequirement(expected, actual)
  42. Expect(err).NotTo(HaveOccurred())
  43. By("Creating a Pod with no resource requirements")
  44. pod := newTestPod(f, "pod-no-resources", api.ResourceList{}, api.ResourceList{})
  45. pod, err = f.Client.Pods(f.Namespace.Name).Create(pod)
  46. Expect(err).NotTo(HaveOccurred())
  47. By("Ensuring Pod has resource requirements applied from LimitRange")
  48. pod, err = f.Client.Pods(f.Namespace.Name).Get(pod.Name)
  49. Expect(err).NotTo(HaveOccurred())
  50. for i := range pod.Spec.Containers {
  51. err = equalResourceRequirement(expected, pod.Spec.Containers[i].Resources)
  52. if err != nil {
  53. // Print the pod to help in debugging.
  54. framework.Logf("Pod %+v does not have the expected requirements", pod)
  55. Expect(err).NotTo(HaveOccurred())
  56. }
  57. }
  58. By("Creating a Pod with partial resource requirements")
  59. pod = newTestPod(f, "pod-partial-resources", getResourceList("", "150Mi"), getResourceList("300m", ""))
  60. pod, err = f.Client.Pods(f.Namespace.Name).Create(pod)
  61. Expect(err).NotTo(HaveOccurred())
  62. By("Ensuring Pod has merged resource requirements applied from LimitRange")
  63. pod, err = f.Client.Pods(f.Namespace.Name).Get(pod.Name)
  64. Expect(err).NotTo(HaveOccurred())
  65. // This is an interesting case, so it's worth a comment
  66. // If you specify a Limit, and no Request, the Limit will default to the Request
  67. // This means that the LimitRange.DefaultRequest will ONLY take affect if a container.resources.limit is not supplied
  68. expected = api.ResourceRequirements{Requests: getResourceList("300m", "150Mi"), Limits: getResourceList("300m", "500Mi")}
  69. for i := range pod.Spec.Containers {
  70. err = equalResourceRequirement(expected, pod.Spec.Containers[i].Resources)
  71. if err != nil {
  72. // Print the pod to help in debugging.
  73. framework.Logf("Pod %+v does not have the expected requirements", pod)
  74. Expect(err).NotTo(HaveOccurred())
  75. }
  76. }
  77. By("Failing to create a Pod with less than min resources")
  78. pod = newTestPod(f, podName, getResourceList("10m", "50Mi"), api.ResourceList{})
  79. pod, err = f.Client.Pods(f.Namespace.Name).Create(pod)
  80. Expect(err).To(HaveOccurred())
  81. By("Failing to create a Pod with more than max resources")
  82. pod = newTestPod(f, podName, getResourceList("600m", "600Mi"), api.ResourceList{})
  83. pod, err = f.Client.Pods(f.Namespace.Name).Create(pod)
  84. Expect(err).To(HaveOccurred())
  85. })
  86. })
  87. func equalResourceRequirement(expected api.ResourceRequirements, actual api.ResourceRequirements) error {
  88. framework.Logf("Verifying requests: expected %v with actual %v", expected.Requests, actual.Requests)
  89. err := equalResourceList(expected.Requests, actual.Requests)
  90. if err != nil {
  91. return err
  92. }
  93. framework.Logf("Verifying limits: expected %v with actual %v", expected.Limits, actual.Limits)
  94. err = equalResourceList(expected.Limits, actual.Limits)
  95. if err != nil {
  96. return err
  97. }
  98. return nil
  99. }
  100. func equalResourceList(expected api.ResourceList, actual api.ResourceList) error {
  101. for k, v := range expected {
  102. if actualValue, found := actual[k]; !found || (v.Cmp(actualValue) != 0) {
  103. return fmt.Errorf("resource %v expected %v actual %v", k, v.String(), actualValue.String())
  104. }
  105. }
  106. for k, v := range actual {
  107. if expectedValue, found := expected[k]; !found || (v.Cmp(expectedValue) != 0) {
  108. return fmt.Errorf("resource %v expected %v actual %v", k, expectedValue.String(), v.String())
  109. }
  110. }
  111. return nil
  112. }
  113. func getResourceList(cpu, memory string) api.ResourceList {
  114. res := api.ResourceList{}
  115. if cpu != "" {
  116. res[api.ResourceCPU] = resource.MustParse(cpu)
  117. }
  118. if memory != "" {
  119. res[api.ResourceMemory] = resource.MustParse(memory)
  120. }
  121. return res
  122. }
  123. // newLimitRange returns a limit range with specified data
  124. func newLimitRange(name string, limitType api.LimitType,
  125. min, max,
  126. defaultLimit, defaultRequest,
  127. maxLimitRequestRatio api.ResourceList) *api.LimitRange {
  128. return &api.LimitRange{
  129. ObjectMeta: api.ObjectMeta{
  130. Name: name,
  131. },
  132. Spec: api.LimitRangeSpec{
  133. Limits: []api.LimitRangeItem{
  134. {
  135. Type: limitType,
  136. Min: min,
  137. Max: max,
  138. Default: defaultLimit,
  139. DefaultRequest: defaultRequest,
  140. MaxLimitRequestRatio: maxLimitRequestRatio,
  141. },
  142. },
  143. },
  144. }
  145. }
  146. // newTestPod returns a pod that has the specified requests and limits
  147. func newTestPod(f *framework.Framework, name string, requests api.ResourceList, limits api.ResourceList) *api.Pod {
  148. return &api.Pod{
  149. ObjectMeta: api.ObjectMeta{
  150. Name: name,
  151. },
  152. Spec: api.PodSpec{
  153. Containers: []api.Container{
  154. {
  155. Name: "pause",
  156. Image: framework.GetPauseImageName(f.Client),
  157. Resources: api.ResourceRequirements{
  158. Requests: requests,
  159. Limits: limits,
  160. },
  161. },
  162. },
  163. },
  164. }
  165. }