resource_quota.go 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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 apimachinery
  14. import (
  15. "context"
  16. "fmt"
  17. "strconv"
  18. "time"
  19. appsv1 "k8s.io/api/apps/v1"
  20. v1 "k8s.io/api/core/v1"
  21. schedulingv1 "k8s.io/api/scheduling/v1"
  22. apierrors "k8s.io/apimachinery/pkg/api/errors"
  23. "k8s.io/apimachinery/pkg/api/resource"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  26. "k8s.io/apimachinery/pkg/util/intstr"
  27. "k8s.io/apimachinery/pkg/util/wait"
  28. clientset "k8s.io/client-go/kubernetes"
  29. "k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
  30. "k8s.io/kubernetes/test/e2e/framework"
  31. "k8s.io/kubernetes/test/utils/crd"
  32. imageutils "k8s.io/kubernetes/test/utils/image"
  33. "github.com/onsi/ginkgo"
  34. )
  35. const (
  36. // how long to wait for a resource quota update to occur
  37. resourceQuotaTimeout = 30 * time.Second
  38. podName = "pfpod"
  39. )
  40. var classGold = "gold"
  41. var extendedResourceName = "example.com/dongle"
  42. var _ = SIGDescribe("ResourceQuota", func() {
  43. f := framework.NewDefaultFramework("resourcequota")
  44. /*
  45. Release: v1.16
  46. Testname: ResourceQuota, object count quota, resourcequotas
  47. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  48. */
  49. framework.ConformanceIt("should create a ResourceQuota and ensure its status is promptly calculated.", func() {
  50. ginkgo.By("Counting existing ResourceQuota")
  51. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  52. framework.ExpectNoError(err)
  53. ginkgo.By("Creating a ResourceQuota")
  54. quotaName := "test-quota"
  55. resourceQuota := newTestResourceQuota(quotaName)
  56. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  57. framework.ExpectNoError(err)
  58. ginkgo.By("Ensuring resource quota status is calculated")
  59. usedResources := v1.ResourceList{}
  60. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  61. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  62. framework.ExpectNoError(err)
  63. })
  64. /*
  65. Release: v1.16
  66. Testname: ResourceQuota, object count quota, service
  67. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  68. Create a Service. Its creation MUST be successful and resource usage count against the Service object and resourceQuota object MUST be captured in ResourceQuotaStatus of the ResourceQuota.
  69. Delete the Service. Deletion MUST succeed and resource usage count against the Service object MUST be released from ResourceQuotaStatus of the ResourceQuota.
  70. */
  71. framework.ConformanceIt("should create a ResourceQuota and capture the life of a service.", func() {
  72. ginkgo.By("Counting existing ResourceQuota")
  73. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  74. framework.ExpectNoError(err)
  75. ginkgo.By("Creating a ResourceQuota")
  76. quotaName := "test-quota"
  77. resourceQuota := newTestResourceQuota(quotaName)
  78. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  79. framework.ExpectNoError(err)
  80. ginkgo.By("Ensuring resource quota status is calculated")
  81. usedResources := v1.ResourceList{}
  82. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  83. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  84. framework.ExpectNoError(err)
  85. ginkgo.By("Creating a Service")
  86. service := newTestServiceForQuota("test-service", v1.ServiceTypeClusterIP)
  87. service, err = f.ClientSet.CoreV1().Services(f.Namespace.Name).Create(context.TODO(), service, metav1.CreateOptions{})
  88. framework.ExpectNoError(err)
  89. ginkgo.By("Ensuring resource quota status captures service creation")
  90. usedResources = v1.ResourceList{}
  91. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  92. usedResources[v1.ResourceServices] = resource.MustParse("1")
  93. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  94. framework.ExpectNoError(err)
  95. ginkgo.By("Deleting a Service")
  96. err = f.ClientSet.CoreV1().Services(f.Namespace.Name).Delete(context.TODO(), service.Name, nil)
  97. framework.ExpectNoError(err)
  98. ginkgo.By("Ensuring resource quota status released usage")
  99. usedResources[v1.ResourceServices] = resource.MustParse("0")
  100. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  101. framework.ExpectNoError(err)
  102. })
  103. /*
  104. Release: v1.16
  105. Testname: ResourceQuota, object count quota, secret
  106. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  107. Create a Secret. Its creation MUST be successful and resource usage count against the Secret object and resourceQuota object MUST be captured in ResourceQuotaStatus of the ResourceQuota.
  108. Delete the Secret. Deletion MUST succeed and resource usage count against the Secret object MUST be released from ResourceQuotaStatus of the ResourceQuota.
  109. */
  110. framework.ConformanceIt("should create a ResourceQuota and capture the life of a secret.", func() {
  111. ginkgo.By("Discovering how many secrets are in namespace by default")
  112. found, unchanged := 0, 0
  113. // On contended servers the service account controller can slow down, leading to the count changing during a run.
  114. // Wait up to 5s for the count to stabilize, assuming that updates come at a consistent rate, and are not held indefinitely.
  115. wait.Poll(1*time.Second, 30*time.Second, func() (bool, error) {
  116. secrets, err := f.ClientSet.CoreV1().Secrets(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{})
  117. framework.ExpectNoError(err)
  118. if len(secrets.Items) == found {
  119. // loop until the number of secrets has stabilized for 5 seconds
  120. unchanged++
  121. return unchanged > 4, nil
  122. }
  123. unchanged = 0
  124. found = len(secrets.Items)
  125. return false, nil
  126. })
  127. defaultSecrets := fmt.Sprintf("%d", found)
  128. hardSecrets := fmt.Sprintf("%d", found+1)
  129. ginkgo.By("Counting existing ResourceQuota")
  130. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  131. framework.ExpectNoError(err)
  132. ginkgo.By("Creating a ResourceQuota")
  133. quotaName := "test-quota"
  134. resourceQuota := newTestResourceQuota(quotaName)
  135. resourceQuota.Spec.Hard[v1.ResourceSecrets] = resource.MustParse(hardSecrets)
  136. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  137. framework.ExpectNoError(err)
  138. ginkgo.By("Ensuring resource quota status is calculated")
  139. usedResources := v1.ResourceList{}
  140. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  141. usedResources[v1.ResourceSecrets] = resource.MustParse(defaultSecrets)
  142. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  143. framework.ExpectNoError(err)
  144. ginkgo.By("Creating a Secret")
  145. secret := newTestSecretForQuota("test-secret")
  146. secret, err = f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Create(context.TODO(), secret, metav1.CreateOptions{})
  147. framework.ExpectNoError(err)
  148. ginkgo.By("Ensuring resource quota status captures secret creation")
  149. usedResources = v1.ResourceList{}
  150. usedResources[v1.ResourceSecrets] = resource.MustParse(hardSecrets)
  151. // we expect there to be two secrets because each namespace will receive
  152. // a service account token secret by default
  153. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  154. framework.ExpectNoError(err)
  155. ginkgo.By("Deleting a secret")
  156. err = f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Delete(context.TODO(), secret.Name, nil)
  157. framework.ExpectNoError(err)
  158. ginkgo.By("Ensuring resource quota status released usage")
  159. usedResources[v1.ResourceSecrets] = resource.MustParse(defaultSecrets)
  160. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  161. framework.ExpectNoError(err)
  162. })
  163. /*
  164. Release: v1.16
  165. Testname: ResourceQuota, object count quota, pod
  166. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  167. Create a Pod with resource request count for CPU, Memory, EphemeralStorage and ExtendedResourceName. Pod creation MUST be successful and respective resource usage count MUST be captured in ResourceQuotaStatus of the ResourceQuota.
  168. Create another Pod with resource request exceeding remaining quota. Pod creation MUST fail as the request exceeds ResourceQuota limits.
  169. Update the successfully created pod's resource requests. Updation MUST fail as a Pod can not dynamically update its resource requirements.
  170. Delete the successfully created Pod. Pod Deletion MUST be scuccessful and it MUST release the allocated resource counts from ResourceQuotaStatus of the ResourceQuota.
  171. */
  172. framework.ConformanceIt("should create a ResourceQuota and capture the life of a pod.", func() {
  173. ginkgo.By("Counting existing ResourceQuota")
  174. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  175. framework.ExpectNoError(err)
  176. ginkgo.By("Creating a ResourceQuota")
  177. quotaName := "test-quota"
  178. resourceQuota := newTestResourceQuota(quotaName)
  179. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  180. framework.ExpectNoError(err)
  181. ginkgo.By("Ensuring resource quota status is calculated")
  182. usedResources := v1.ResourceList{}
  183. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  184. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  185. framework.ExpectNoError(err)
  186. ginkgo.By("Creating a Pod that fits quota")
  187. podName := "test-pod"
  188. requests := v1.ResourceList{}
  189. limits := v1.ResourceList{}
  190. requests[v1.ResourceCPU] = resource.MustParse("500m")
  191. requests[v1.ResourceMemory] = resource.MustParse("252Mi")
  192. requests[v1.ResourceEphemeralStorage] = resource.MustParse("30Gi")
  193. requests[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
  194. limits[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
  195. pod := newTestPodForQuota(f, podName, requests, limits)
  196. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  197. framework.ExpectNoError(err)
  198. podToUpdate := pod
  199. ginkgo.By("Ensuring ResourceQuota status captures the pod usage")
  200. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  201. usedResources[v1.ResourcePods] = resource.MustParse("1")
  202. usedResources[v1.ResourceCPU] = requests[v1.ResourceCPU]
  203. usedResources[v1.ResourceMemory] = requests[v1.ResourceMemory]
  204. usedResources[v1.ResourceEphemeralStorage] = requests[v1.ResourceEphemeralStorage]
  205. usedResources[v1.ResourceName(v1.DefaultResourceRequestsPrefix+extendedResourceName)] = requests[v1.ResourceName(extendedResourceName)]
  206. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  207. framework.ExpectNoError(err)
  208. ginkgo.By("Not allowing a pod to be created that exceeds remaining quota")
  209. requests = v1.ResourceList{}
  210. requests[v1.ResourceCPU] = resource.MustParse("600m")
  211. requests[v1.ResourceMemory] = resource.MustParse("100Mi")
  212. pod = newTestPodForQuota(f, "fail-pod", requests, v1.ResourceList{})
  213. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  214. framework.ExpectError(err)
  215. ginkgo.By("Not allowing a pod to be created that exceeds remaining quota(validation on extended resources)")
  216. requests = v1.ResourceList{}
  217. limits = v1.ResourceList{}
  218. requests[v1.ResourceCPU] = resource.MustParse("500m")
  219. requests[v1.ResourceMemory] = resource.MustParse("100Mi")
  220. requests[v1.ResourceEphemeralStorage] = resource.MustParse("30Gi")
  221. requests[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
  222. limits[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
  223. pod = newTestPodForQuota(f, "fail-pod-for-extended-resource", requests, limits)
  224. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  225. framework.ExpectError(err)
  226. ginkgo.By("Ensuring a pod cannot update its resource requirements")
  227. // a pod cannot dynamically update its resource requirements.
  228. requests = v1.ResourceList{}
  229. requests[v1.ResourceCPU] = resource.MustParse("100m")
  230. requests[v1.ResourceMemory] = resource.MustParse("100Mi")
  231. requests[v1.ResourceEphemeralStorage] = resource.MustParse("10Gi")
  232. podToUpdate.Spec.Containers[0].Resources.Requests = requests
  233. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Update(context.TODO(), podToUpdate, metav1.UpdateOptions{})
  234. framework.ExpectError(err)
  235. ginkgo.By("Ensuring attempts to update pod resource requirements did not change quota usage")
  236. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  237. framework.ExpectNoError(err)
  238. ginkgo.By("Deleting the pod")
  239. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), podName, metav1.NewDeleteOptions(0))
  240. framework.ExpectNoError(err)
  241. ginkgo.By("Ensuring resource quota status released the pod usage")
  242. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  243. usedResources[v1.ResourcePods] = resource.MustParse("0")
  244. usedResources[v1.ResourceCPU] = resource.MustParse("0")
  245. usedResources[v1.ResourceMemory] = resource.MustParse("0")
  246. usedResources[v1.ResourceEphemeralStorage] = resource.MustParse("0")
  247. usedResources[v1.ResourceName(v1.DefaultResourceRequestsPrefix+extendedResourceName)] = resource.MustParse("0")
  248. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  249. framework.ExpectNoError(err)
  250. })
  251. /*
  252. Release: v1.16
  253. Testname: ResourceQuota, object count quota, configmap
  254. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  255. Create a ConfigMap. Its creation MUST be successful and resource usage count against the ConfigMap object MUST be captured in ResourceQuotaStatus of the ResourceQuota.
  256. Delete the ConfigMap. Deletion MUST succeed and resource usage count against the ConfigMap object MUST be released from ResourceQuotaStatus of the ResourceQuota.
  257. */
  258. framework.ConformanceIt("should create a ResourceQuota and capture the life of a configMap.", func() {
  259. found, unchanged := 0, 0
  260. // On contended servers the service account controller can slow down, leading to the count changing during a run.
  261. // Wait up to 5s for the count to stabilize, assuming that updates come at a consistent rate, and are not held indefinitely.
  262. wait.Poll(1*time.Second, 30*time.Second, func() (bool, error) {
  263. configmaps, err := f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{})
  264. framework.ExpectNoError(err)
  265. if len(configmaps.Items) == found {
  266. // loop until the number of configmaps has stabilized for 5 seconds
  267. unchanged++
  268. return unchanged > 4, nil
  269. }
  270. unchanged = 0
  271. found = len(configmaps.Items)
  272. return false, nil
  273. })
  274. defaultConfigMaps := fmt.Sprintf("%d", found)
  275. hardConfigMaps := fmt.Sprintf("%d", found+1)
  276. ginkgo.By("Counting existing ResourceQuota")
  277. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  278. framework.ExpectNoError(err)
  279. ginkgo.By("Creating a ResourceQuota")
  280. quotaName := "test-quota"
  281. resourceQuota := newTestResourceQuota(quotaName)
  282. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  283. framework.ExpectNoError(err)
  284. ginkgo.By("Ensuring resource quota status is calculated")
  285. usedResources := v1.ResourceList{}
  286. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  287. usedResources[v1.ResourceConfigMaps] = resource.MustParse(defaultConfigMaps)
  288. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  289. framework.ExpectNoError(err)
  290. ginkgo.By("Creating a ConfigMap")
  291. configMap := newTestConfigMapForQuota("test-configmap")
  292. configMap, err = f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Create(context.TODO(), configMap, metav1.CreateOptions{})
  293. framework.ExpectNoError(err)
  294. ginkgo.By("Ensuring resource quota status captures configMap creation")
  295. usedResources = v1.ResourceList{}
  296. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  297. // we expect there to be two configmaps because each namespace will receive
  298. // a ca.crt configmap by default.
  299. // ref:https://github.com/kubernetes/kubernetes/pull/68812
  300. usedResources[v1.ResourceConfigMaps] = resource.MustParse(hardConfigMaps)
  301. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  302. framework.ExpectNoError(err)
  303. ginkgo.By("Deleting a ConfigMap")
  304. err = f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Delete(context.TODO(), configMap.Name, nil)
  305. framework.ExpectNoError(err)
  306. ginkgo.By("Ensuring resource quota status released usage")
  307. usedResources[v1.ResourceConfigMaps] = resource.MustParse(defaultConfigMaps)
  308. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  309. framework.ExpectNoError(err)
  310. })
  311. /*
  312. Release: v1.16
  313. Testname: ResourceQuota, object count quota, replicationController
  314. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  315. Create a ReplicationController. Its creation MUST be successful and resource usage count against the ReplicationController object MUST be captured in ResourceQuotaStatus of the ResourceQuota.
  316. Delete the ReplicationController. Deletion MUST succeed and resource usage count against the ReplicationController object MUST be released from ResourceQuotaStatus of the ResourceQuota.
  317. */
  318. framework.ConformanceIt("should create a ResourceQuota and capture the life of a replication controller.", func() {
  319. ginkgo.By("Counting existing ResourceQuota")
  320. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  321. framework.ExpectNoError(err)
  322. ginkgo.By("Creating a ResourceQuota")
  323. quotaName := "test-quota"
  324. resourceQuota := newTestResourceQuota(quotaName)
  325. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  326. framework.ExpectNoError(err)
  327. ginkgo.By("Ensuring resource quota status is calculated")
  328. usedResources := v1.ResourceList{}
  329. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  330. usedResources[v1.ResourceReplicationControllers] = resource.MustParse("0")
  331. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  332. framework.ExpectNoError(err)
  333. ginkgo.By("Creating a ReplicationController")
  334. replicationController := newTestReplicationControllerForQuota("test-rc", "nginx", 0)
  335. replicationController, err = f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Create(context.TODO(), replicationController, metav1.CreateOptions{})
  336. framework.ExpectNoError(err)
  337. ginkgo.By("Ensuring resource quota status captures replication controller creation")
  338. usedResources = v1.ResourceList{}
  339. usedResources[v1.ResourceReplicationControllers] = resource.MustParse("1")
  340. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  341. framework.ExpectNoError(err)
  342. ginkgo.By("Deleting a ReplicationController")
  343. // Without the delete options, the object isn't actually
  344. // removed until the GC verifies that all children have been
  345. // detached. ReplicationControllers default to "orphan", which
  346. // is different from most resources. (Why? To preserve a common
  347. // workflow from prior to the GC's introduction.)
  348. err = f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Delete(context.TODO(), replicationController.Name, &metav1.DeleteOptions{
  349. PropagationPolicy: func() *metav1.DeletionPropagation {
  350. p := metav1.DeletePropagationBackground
  351. return &p
  352. }(),
  353. })
  354. framework.ExpectNoError(err)
  355. ginkgo.By("Ensuring resource quota status released usage")
  356. usedResources[v1.ResourceReplicationControllers] = resource.MustParse("0")
  357. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  358. framework.ExpectNoError(err)
  359. })
  360. /*
  361. Release: v1.16
  362. Testname: ResourceQuota, object count quota, replicaSet
  363. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  364. Create a ReplicaSet. Its creation MUST be successful and resource usage count against the ReplicaSet object MUST be captured in ResourceQuotaStatus of the ResourceQuota.
  365. Delete the ReplicaSet. Deletion MUST succeed and resource usage count against the ReplicaSet object MUST be released from ResourceQuotaStatus of the ResourceQuota.
  366. */
  367. framework.ConformanceIt("should create a ResourceQuota and capture the life of a replica set.", func() {
  368. ginkgo.By("Counting existing ResourceQuota")
  369. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  370. framework.ExpectNoError(err)
  371. ginkgo.By("Creating a ResourceQuota")
  372. quotaName := "test-quota"
  373. resourceQuota := newTestResourceQuota(quotaName)
  374. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  375. framework.ExpectNoError(err)
  376. ginkgo.By("Ensuring resource quota status is calculated")
  377. usedResources := v1.ResourceList{}
  378. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  379. usedResources[v1.ResourceName("count/replicasets.apps")] = resource.MustParse("0")
  380. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  381. framework.ExpectNoError(err)
  382. ginkgo.By("Creating a ReplicaSet")
  383. replicaSet := newTestReplicaSetForQuota("test-rs", "nginx", 0)
  384. replicaSet, err = f.ClientSet.AppsV1().ReplicaSets(f.Namespace.Name).Create(context.TODO(), replicaSet, metav1.CreateOptions{})
  385. framework.ExpectNoError(err)
  386. ginkgo.By("Ensuring resource quota status captures replicaset creation")
  387. usedResources = v1.ResourceList{}
  388. usedResources[v1.ResourceName("count/replicasets.apps")] = resource.MustParse("1")
  389. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  390. framework.ExpectNoError(err)
  391. ginkgo.By("Deleting a ReplicaSet")
  392. err = f.ClientSet.AppsV1().ReplicaSets(f.Namespace.Name).Delete(context.TODO(), replicaSet.Name, nil)
  393. framework.ExpectNoError(err)
  394. ginkgo.By("Ensuring resource quota status released usage")
  395. usedResources[v1.ResourceName("count/replicasets.apps")] = resource.MustParse("0")
  396. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  397. framework.ExpectNoError(err)
  398. })
  399. /*
  400. Release: v1.16
  401. Testname: ResourceQuota, object count quota, pvc
  402. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  403. Create PersistentVolumeClaim (PVC) to request storage capacity of 1G. PVC creation MUST be successful and resource usage count against the PVC and storage object MUST be captured in ResourceQuotaStatus of the ResourceQuota.
  404. Delete the PVC. Deletion MUST succeed and resource usage count against its PVC and storage object MUST be released from ResourceQuotaStatus of the ResourceQuota.
  405. [NotConformancePromotable] as test suite do not have any e2e at this moment which are explicitly verifying PV and PVC behaviour.
  406. */
  407. ginkgo.It("should create a ResourceQuota and capture the life of a persistent volume claim. [sig-storage]", func() {
  408. ginkgo.By("Counting existing ResourceQuota")
  409. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  410. framework.ExpectNoError(err)
  411. ginkgo.By("Creating a ResourceQuota")
  412. quotaName := "test-quota"
  413. resourceQuota := newTestResourceQuota(quotaName)
  414. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  415. framework.ExpectNoError(err)
  416. ginkgo.By("Ensuring resource quota status is calculated")
  417. usedResources := v1.ResourceList{}
  418. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  419. usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("0")
  420. usedResources[v1.ResourceRequestsStorage] = resource.MustParse("0")
  421. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  422. framework.ExpectNoError(err)
  423. ginkgo.By("Creating a PersistentVolumeClaim")
  424. pvc := newTestPersistentVolumeClaimForQuota("test-claim")
  425. pvc, err = f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Create(context.TODO(), pvc, metav1.CreateOptions{})
  426. framework.ExpectNoError(err)
  427. ginkgo.By("Ensuring resource quota status captures persistent volume claim creation")
  428. usedResources = v1.ResourceList{}
  429. usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("1")
  430. usedResources[v1.ResourceRequestsStorage] = resource.MustParse("1Gi")
  431. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  432. framework.ExpectNoError(err)
  433. ginkgo.By("Deleting a PersistentVolumeClaim")
  434. err = f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Delete(context.TODO(), pvc.Name, nil)
  435. framework.ExpectNoError(err)
  436. ginkgo.By("Ensuring resource quota status released usage")
  437. usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("0")
  438. usedResources[v1.ResourceRequestsStorage] = resource.MustParse("0")
  439. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  440. framework.ExpectNoError(err)
  441. })
  442. /*
  443. Release: v1.16
  444. Testname: ResourceQuota, object count quota, storageClass
  445. Description: Create a ResourceQuota. Creation MUST be successful and its ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  446. Create PersistentVolumeClaim (PVC) with specified storageClass to request storage capacity of 1G. PVC creation MUST be successful and resource usage count against PVC, storageClass and storage object MUST be captured in ResourceQuotaStatus of the ResourceQuota.
  447. Delete the PVC. Deletion MUST succeed and resource usage count against PVC, storageClass and storage object MUST be released from ResourceQuotaStatus of the ResourceQuota.
  448. [NotConformancePromotable] as test suite do not have any e2e at this moment which are explicitly verifying PV and PVC behaviour.
  449. */
  450. ginkgo.It("should create a ResourceQuota and capture the life of a persistent volume claim with a storage class. [sig-storage]", func() {
  451. ginkgo.By("Counting existing ResourceQuota")
  452. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  453. framework.ExpectNoError(err)
  454. ginkgo.By("Creating a ResourceQuota")
  455. quotaName := "test-quota"
  456. resourceQuota := newTestResourceQuota(quotaName)
  457. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  458. framework.ExpectNoError(err)
  459. ginkgo.By("Ensuring resource quota status is calculated")
  460. usedResources := v1.ResourceList{}
  461. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  462. usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("0")
  463. usedResources[v1.ResourceRequestsStorage] = resource.MustParse("0")
  464. usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourcePersistentVolumeClaims)] = resource.MustParse("0")
  465. usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourceRequestsStorage)] = resource.MustParse("0")
  466. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  467. framework.ExpectNoError(err)
  468. ginkgo.By("Creating a PersistentVolumeClaim with storage class")
  469. pvc := newTestPersistentVolumeClaimForQuota("test-claim")
  470. pvc.Spec.StorageClassName = &classGold
  471. pvc, err = f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Create(context.TODO(), pvc, metav1.CreateOptions{})
  472. framework.ExpectNoError(err)
  473. ginkgo.By("Ensuring resource quota status captures persistent volume claim creation")
  474. usedResources = v1.ResourceList{}
  475. usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("1")
  476. usedResources[v1.ResourceRequestsStorage] = resource.MustParse("1Gi")
  477. usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourcePersistentVolumeClaims)] = resource.MustParse("1")
  478. usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourceRequestsStorage)] = resource.MustParse("1Gi")
  479. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  480. framework.ExpectNoError(err)
  481. ginkgo.By("Deleting a PersistentVolumeClaim")
  482. err = f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Delete(context.TODO(), pvc.Name, nil)
  483. framework.ExpectNoError(err)
  484. ginkgo.By("Ensuring resource quota status released usage")
  485. usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("0")
  486. usedResources[v1.ResourceRequestsStorage] = resource.MustParse("0")
  487. usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourcePersistentVolumeClaims)] = resource.MustParse("0")
  488. usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourceRequestsStorage)] = resource.MustParse("0")
  489. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  490. framework.ExpectNoError(err)
  491. })
  492. ginkgo.It("should create a ResourceQuota and capture the life of a custom resource.", func() {
  493. ginkgo.By("Creating a Custom Resource Definition")
  494. testcrd, err := crd.CreateTestCRD(f)
  495. framework.ExpectNoError(err)
  496. defer testcrd.CleanUp()
  497. countResourceName := "count/" + testcrd.Crd.Spec.Names.Plural + "." + testcrd.Crd.Spec.Group
  498. // resourcequota controller needs to take 30 seconds at most to detect the new custom resource.
  499. // in order to make sure the resourcequota controller knows this resource, we create one test
  500. // resourcequota object, and triggering updates on it until the status is updated.
  501. quotaName := "quota-for-" + testcrd.Crd.Spec.Names.Plural
  502. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, &v1.ResourceQuota{
  503. ObjectMeta: metav1.ObjectMeta{Name: quotaName},
  504. Spec: v1.ResourceQuotaSpec{
  505. Hard: v1.ResourceList{
  506. v1.ResourceName(countResourceName): resource.MustParse("0"),
  507. },
  508. },
  509. })
  510. framework.ExpectNoError(err)
  511. err = updateResourceQuotaUntilUsageAppears(f.ClientSet, f.Namespace.Name, quotaName, v1.ResourceName(countResourceName))
  512. framework.ExpectNoError(err)
  513. err = f.ClientSet.CoreV1().ResourceQuotas(f.Namespace.Name).Delete(context.TODO(), quotaName, nil)
  514. framework.ExpectNoError(err)
  515. ginkgo.By("Counting existing ResourceQuota")
  516. c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
  517. framework.ExpectNoError(err)
  518. ginkgo.By("Creating a ResourceQuota")
  519. quotaName = "test-quota"
  520. resourceQuota := newTestResourceQuota(quotaName)
  521. resourceQuota.Spec.Hard[v1.ResourceName(countResourceName)] = resource.MustParse("1")
  522. _, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
  523. framework.ExpectNoError(err)
  524. ginkgo.By("Ensuring resource quota status is calculated")
  525. usedResources := v1.ResourceList{}
  526. usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
  527. usedResources[v1.ResourceName(countResourceName)] = resource.MustParse("0")
  528. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  529. framework.ExpectNoError(err)
  530. ginkgo.By("Creating a custom resource")
  531. resourceClient := testcrd.DynamicClients["v1"]
  532. testcr, err := instantiateCustomResource(&unstructured.Unstructured{
  533. Object: map[string]interface{}{
  534. "apiVersion": testcrd.Crd.Spec.Group + "/" + testcrd.Crd.Spec.Versions[0].Name,
  535. "kind": testcrd.Crd.Spec.Names.Kind,
  536. "metadata": map[string]interface{}{
  537. "name": "test-cr-1",
  538. },
  539. },
  540. }, resourceClient, testcrd.Crd)
  541. framework.ExpectNoError(err)
  542. ginkgo.By("Ensuring resource quota status captures custom resource creation")
  543. usedResources = v1.ResourceList{}
  544. usedResources[v1.ResourceName(countResourceName)] = resource.MustParse("1")
  545. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  546. framework.ExpectNoError(err)
  547. ginkgo.By("Creating a second custom resource")
  548. _, err = instantiateCustomResource(&unstructured.Unstructured{
  549. Object: map[string]interface{}{
  550. "apiVersion": testcrd.Crd.Spec.Group + "/" + testcrd.Crd.Spec.Versions[0].Name,
  551. "kind": testcrd.Crd.Spec.Names.Kind,
  552. "metadata": map[string]interface{}{
  553. "name": "test-cr-2",
  554. },
  555. },
  556. }, resourceClient, testcrd.Crd)
  557. // since we only give one quota, this creation should fail.
  558. framework.ExpectError(err)
  559. ginkgo.By("Deleting a custom resource")
  560. err = deleteCustomResource(resourceClient, testcr.GetName())
  561. framework.ExpectNoError(err)
  562. ginkgo.By("Ensuring resource quota status released usage")
  563. usedResources[v1.ResourceName(countResourceName)] = resource.MustParse("0")
  564. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
  565. framework.ExpectNoError(err)
  566. })
  567. /*
  568. Release: v1.16
  569. Testname: ResourceQuota, quota scope, Terminating and NotTerminating scope
  570. Description: Create two ResourceQuotas, one with 'Terminating' scope and another 'NotTerminating' scope. Request and the limit counts for CPU and Memory resources are set for the ResourceQuota. Creation MUST be successful and their ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  571. Create a Pod with specified CPU and Memory ResourceRequirements fall within quota limits. Pod creation MUST be successful and usage count MUST be captured in ResourceQuotaStatus of 'NotTerminating' scoped ResourceQuota but MUST NOT in 'Terminating' scoped ResourceQuota.
  572. Delete the Pod. Pod deletion MUST succeed and Pod resource usage count MUST be released from ResourceQuotaStatus of 'NotTerminating' scoped ResourceQuota.
  573. Create a pod with specified activeDeadlineSeconds and resourceRequirements for CPU and Memory fall within quota limits. Pod creation MUST be successful and usage count MUST be captured in ResourceQuotaStatus of 'Terminating' scoped ResourceQuota but MUST NOT in 'NotTerminating' scoped ResourceQuota.
  574. Delete the Pod. Pod deletion MUST succeed and Pod resource usage count MUST be released from ResourceQuotaStatus of 'Terminating' scoped ResourceQuota.
  575. */
  576. framework.ConformanceIt("should verify ResourceQuota with terminating scopes.", func() {
  577. ginkgo.By("Creating a ResourceQuota with terminating scope")
  578. quotaTerminatingName := "quota-terminating"
  579. resourceQuotaTerminating, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScope(quotaTerminatingName, v1.ResourceQuotaScopeTerminating))
  580. framework.ExpectNoError(err)
  581. ginkgo.By("Ensuring ResourceQuota status is calculated")
  582. usedResources := v1.ResourceList{}
  583. usedResources[v1.ResourcePods] = resource.MustParse("0")
  584. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
  585. framework.ExpectNoError(err)
  586. ginkgo.By("Creating a ResourceQuota with not terminating scope")
  587. quotaNotTerminatingName := "quota-not-terminating"
  588. resourceQuotaNotTerminating, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScope(quotaNotTerminatingName, v1.ResourceQuotaScopeNotTerminating))
  589. framework.ExpectNoError(err)
  590. ginkgo.By("Ensuring ResourceQuota status is calculated")
  591. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
  592. framework.ExpectNoError(err)
  593. ginkgo.By("Creating a long running pod")
  594. podName := "test-pod"
  595. requests := v1.ResourceList{}
  596. requests[v1.ResourceCPU] = resource.MustParse("500m")
  597. requests[v1.ResourceMemory] = resource.MustParse("200Mi")
  598. limits := v1.ResourceList{}
  599. limits[v1.ResourceCPU] = resource.MustParse("1")
  600. limits[v1.ResourceMemory] = resource.MustParse("400Mi")
  601. pod := newTestPodForQuota(f, podName, requests, limits)
  602. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  603. framework.ExpectNoError(err)
  604. ginkgo.By("Ensuring resource quota with not terminating scope captures the pod usage")
  605. usedResources[v1.ResourcePods] = resource.MustParse("1")
  606. usedResources[v1.ResourceRequestsCPU] = requests[v1.ResourceCPU]
  607. usedResources[v1.ResourceRequestsMemory] = requests[v1.ResourceMemory]
  608. usedResources[v1.ResourceLimitsCPU] = limits[v1.ResourceCPU]
  609. usedResources[v1.ResourceLimitsMemory] = limits[v1.ResourceMemory]
  610. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
  611. framework.ExpectNoError(err)
  612. ginkgo.By("Ensuring resource quota with terminating scope ignored the pod usage")
  613. usedResources[v1.ResourcePods] = resource.MustParse("0")
  614. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  615. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
  616. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  617. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
  618. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
  619. framework.ExpectNoError(err)
  620. ginkgo.By("Deleting the pod")
  621. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), podName, metav1.NewDeleteOptions(0))
  622. framework.ExpectNoError(err)
  623. ginkgo.By("Ensuring resource quota status released the pod usage")
  624. usedResources[v1.ResourcePods] = resource.MustParse("0")
  625. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  626. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
  627. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  628. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
  629. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
  630. framework.ExpectNoError(err)
  631. ginkgo.By("Creating a terminating pod")
  632. podName = "terminating-pod"
  633. pod = newTestPodForQuota(f, podName, requests, limits)
  634. activeDeadlineSeconds := int64(3600)
  635. pod.Spec.ActiveDeadlineSeconds = &activeDeadlineSeconds
  636. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  637. framework.ExpectNoError(err)
  638. ginkgo.By("Ensuring resource quota with terminating scope captures the pod usage")
  639. usedResources[v1.ResourcePods] = resource.MustParse("1")
  640. usedResources[v1.ResourceRequestsCPU] = requests[v1.ResourceCPU]
  641. usedResources[v1.ResourceRequestsMemory] = requests[v1.ResourceMemory]
  642. usedResources[v1.ResourceLimitsCPU] = limits[v1.ResourceCPU]
  643. usedResources[v1.ResourceLimitsMemory] = limits[v1.ResourceMemory]
  644. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
  645. framework.ExpectNoError(err)
  646. ginkgo.By("Ensuring resource quota with not terminating scope ignored the pod usage")
  647. usedResources[v1.ResourcePods] = resource.MustParse("0")
  648. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  649. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
  650. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  651. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
  652. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
  653. framework.ExpectNoError(err)
  654. ginkgo.By("Deleting the pod")
  655. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), podName, metav1.NewDeleteOptions(0))
  656. framework.ExpectNoError(err)
  657. ginkgo.By("Ensuring resource quota status released the pod usage")
  658. usedResources[v1.ResourcePods] = resource.MustParse("0")
  659. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  660. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
  661. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  662. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
  663. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
  664. framework.ExpectNoError(err)
  665. })
  666. /*
  667. Release: v1.16
  668. Testname: ResourceQuota, quota scope, BestEffort and NotBestEffort scope
  669. Description: Create two ResourceQuotas, one with 'BestEffort' scope and another with 'NotBestEffort' scope. Creation MUST be successful and their ResourceQuotaStatus MUST match to expected used and total allowed resource quota count within namespace.
  670. Create a 'BestEffort' Pod by not explicitly specifying resource limits and requests. Pod creation MUST be successful and usage count MUST be captured in ResourceQuotaStatus of 'BestEffort' scoped ResourceQuota but MUST NOT in 'NotBestEffort' scoped ResourceQuota.
  671. Delete the Pod. Pod deletion MUST succeed and Pod resource usage count MUST be released from ResourceQuotaStatus of 'BestEffort' scoped ResourceQuota.
  672. Create a 'NotBestEffort' Pod by explicitly specifying resource limits and requests. Pod creation MUST be successful and usage count MUST be captured in ResourceQuotaStatus of 'NotBestEffort' scoped ResourceQuota but MUST NOT in 'BestEffort' scoped ResourceQuota.
  673. Delete the Pod. Pod deletion MUST succeed and Pod resource usage count MUST be released from ResourceQuotaStatus of 'NotBestEffort' scoped ResourceQuota.
  674. */
  675. framework.ConformanceIt("should verify ResourceQuota with best effort scope.", func() {
  676. ginkgo.By("Creating a ResourceQuota with best effort scope")
  677. resourceQuotaBestEffort, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScope("quota-besteffort", v1.ResourceQuotaScopeBestEffort))
  678. framework.ExpectNoError(err)
  679. ginkgo.By("Ensuring ResourceQuota status is calculated")
  680. usedResources := v1.ResourceList{}
  681. usedResources[v1.ResourcePods] = resource.MustParse("0")
  682. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
  683. framework.ExpectNoError(err)
  684. ginkgo.By("Creating a ResourceQuota with not best effort scope")
  685. resourceQuotaNotBestEffort, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScope("quota-not-besteffort", v1.ResourceQuotaScopeNotBestEffort))
  686. framework.ExpectNoError(err)
  687. ginkgo.By("Ensuring ResourceQuota status is calculated")
  688. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
  689. framework.ExpectNoError(err)
  690. ginkgo.By("Creating a best-effort pod")
  691. pod := newTestPodForQuota(f, podName, v1.ResourceList{}, v1.ResourceList{})
  692. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  693. framework.ExpectNoError(err)
  694. ginkgo.By("Ensuring resource quota with best effort scope captures the pod usage")
  695. usedResources[v1.ResourcePods] = resource.MustParse("1")
  696. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
  697. framework.ExpectNoError(err)
  698. ginkgo.By("Ensuring resource quota with not best effort ignored the pod usage")
  699. usedResources[v1.ResourcePods] = resource.MustParse("0")
  700. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
  701. framework.ExpectNoError(err)
  702. ginkgo.By("Deleting the pod")
  703. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  704. framework.ExpectNoError(err)
  705. ginkgo.By("Ensuring resource quota status released the pod usage")
  706. usedResources[v1.ResourcePods] = resource.MustParse("0")
  707. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
  708. framework.ExpectNoError(err)
  709. ginkgo.By("Creating a not best-effort pod")
  710. requests := v1.ResourceList{}
  711. requests[v1.ResourceCPU] = resource.MustParse("500m")
  712. requests[v1.ResourceMemory] = resource.MustParse("200Mi")
  713. limits := v1.ResourceList{}
  714. limits[v1.ResourceCPU] = resource.MustParse("1")
  715. limits[v1.ResourceMemory] = resource.MustParse("400Mi")
  716. pod = newTestPodForQuota(f, "burstable-pod", requests, limits)
  717. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  718. framework.ExpectNoError(err)
  719. ginkgo.By("Ensuring resource quota with not best effort scope captures the pod usage")
  720. usedResources[v1.ResourcePods] = resource.MustParse("1")
  721. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
  722. framework.ExpectNoError(err)
  723. ginkgo.By("Ensuring resource quota with best effort scope ignored the pod usage")
  724. usedResources[v1.ResourcePods] = resource.MustParse("0")
  725. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
  726. framework.ExpectNoError(err)
  727. ginkgo.By("Deleting the pod")
  728. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  729. framework.ExpectNoError(err)
  730. ginkgo.By("Ensuring resource quota status released the pod usage")
  731. usedResources[v1.ResourcePods] = resource.MustParse("0")
  732. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
  733. framework.ExpectNoError(err)
  734. })
  735. /*
  736. Release: v1.16
  737. Testname: ResourceQuota, update and delete
  738. Description: Create a ResourceQuota for CPU and Memory quota limits. Creation MUST be successful.
  739. When ResourceQuota is updated to modify CPU and Memory quota limits, update MUST succeed with updated values for CPU and Memory limits.
  740. When ResourceQuota is deleted, it MUST not be available in the namespace.
  741. */
  742. framework.ConformanceIt("should be able to update and delete ResourceQuota.", func() {
  743. client := f.ClientSet
  744. ns := f.Namespace.Name
  745. ginkgo.By("Creating a ResourceQuota")
  746. quotaName := "test-quota"
  747. resourceQuota := &v1.ResourceQuota{
  748. Spec: v1.ResourceQuotaSpec{
  749. Hard: v1.ResourceList{},
  750. },
  751. }
  752. resourceQuota.ObjectMeta.Name = quotaName
  753. resourceQuota.Spec.Hard[v1.ResourceCPU] = resource.MustParse("1")
  754. resourceQuota.Spec.Hard[v1.ResourceMemory] = resource.MustParse("500Mi")
  755. _, err := createResourceQuota(client, ns, resourceQuota)
  756. framework.ExpectNoError(err)
  757. ginkgo.By("Getting a ResourceQuota")
  758. resourceQuotaResult, err := client.CoreV1().ResourceQuotas(ns).Get(context.TODO(), quotaName, metav1.GetOptions{})
  759. framework.ExpectNoError(err)
  760. framework.ExpectEqual(resourceQuotaResult.Spec.Hard[v1.ResourceCPU], resource.MustParse("1"))
  761. framework.ExpectEqual(resourceQuotaResult.Spec.Hard[v1.ResourceMemory], resource.MustParse("500Mi"))
  762. ginkgo.By("Updating a ResourceQuota")
  763. resourceQuota.Spec.Hard[v1.ResourceCPU] = resource.MustParse("2")
  764. resourceQuota.Spec.Hard[v1.ResourceMemory] = resource.MustParse("1Gi")
  765. resourceQuotaResult, err = client.CoreV1().ResourceQuotas(ns).Update(context.TODO(), resourceQuota, metav1.UpdateOptions{})
  766. framework.ExpectNoError(err)
  767. framework.ExpectEqual(resourceQuotaResult.Spec.Hard[v1.ResourceCPU], resource.MustParse("2"))
  768. framework.ExpectEqual(resourceQuotaResult.Spec.Hard[v1.ResourceMemory], resource.MustParse("1Gi"))
  769. ginkgo.By("Verifying a ResourceQuota was modified")
  770. resourceQuotaResult, err = client.CoreV1().ResourceQuotas(ns).Get(context.TODO(), quotaName, metav1.GetOptions{})
  771. framework.ExpectNoError(err)
  772. framework.ExpectEqual(resourceQuotaResult.Spec.Hard[v1.ResourceCPU], resource.MustParse("2"))
  773. framework.ExpectEqual(resourceQuotaResult.Spec.Hard[v1.ResourceMemory], resource.MustParse("1Gi"))
  774. ginkgo.By("Deleting a ResourceQuota")
  775. err = deleteResourceQuota(client, ns, quotaName)
  776. framework.ExpectNoError(err)
  777. ginkgo.By("Verifying the deleted ResourceQuota")
  778. _, err = client.CoreV1().ResourceQuotas(ns).Get(context.TODO(), quotaName, metav1.GetOptions{})
  779. framework.ExpectEqual(apierrors.IsNotFound(err), true)
  780. })
  781. })
  782. var _ = SIGDescribe("ResourceQuota [Feature:ScopeSelectors]", func() {
  783. f := framework.NewDefaultFramework("scope-selectors")
  784. ginkgo.It("should verify ResourceQuota with best effort scope using scope-selectors.", func() {
  785. ginkgo.By("Creating a ResourceQuota with best effort scope")
  786. resourceQuotaBestEffort, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeSelector("quota-besteffort", v1.ResourceQuotaScopeBestEffort))
  787. framework.ExpectNoError(err)
  788. ginkgo.By("Ensuring ResourceQuota status is calculated")
  789. usedResources := v1.ResourceList{}
  790. usedResources[v1.ResourcePods] = resource.MustParse("0")
  791. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
  792. framework.ExpectNoError(err)
  793. ginkgo.By("Creating a ResourceQuota with not best effort scope")
  794. resourceQuotaNotBestEffort, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeSelector("quota-not-besteffort", v1.ResourceQuotaScopeNotBestEffort))
  795. framework.ExpectNoError(err)
  796. ginkgo.By("Ensuring ResourceQuota status is calculated")
  797. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
  798. framework.ExpectNoError(err)
  799. ginkgo.By("Creating a best-effort pod")
  800. pod := newTestPodForQuota(f, podName, v1.ResourceList{}, v1.ResourceList{})
  801. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  802. framework.ExpectNoError(err)
  803. ginkgo.By("Ensuring resource quota with best effort scope captures the pod usage")
  804. usedResources[v1.ResourcePods] = resource.MustParse("1")
  805. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
  806. framework.ExpectNoError(err)
  807. ginkgo.By("Ensuring resource quota with not best effort ignored the pod usage")
  808. usedResources[v1.ResourcePods] = resource.MustParse("0")
  809. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
  810. framework.ExpectNoError(err)
  811. ginkgo.By("Deleting the pod")
  812. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  813. framework.ExpectNoError(err)
  814. ginkgo.By("Ensuring resource quota status released the pod usage")
  815. usedResources[v1.ResourcePods] = resource.MustParse("0")
  816. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
  817. framework.ExpectNoError(err)
  818. ginkgo.By("Creating a not best-effort pod")
  819. requests := v1.ResourceList{}
  820. requests[v1.ResourceCPU] = resource.MustParse("500m")
  821. requests[v1.ResourceMemory] = resource.MustParse("200Mi")
  822. limits := v1.ResourceList{}
  823. limits[v1.ResourceCPU] = resource.MustParse("1")
  824. limits[v1.ResourceMemory] = resource.MustParse("400Mi")
  825. pod = newTestPodForQuota(f, "burstable-pod", requests, limits)
  826. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  827. framework.ExpectNoError(err)
  828. ginkgo.By("Ensuring resource quota with not best effort scope captures the pod usage")
  829. usedResources[v1.ResourcePods] = resource.MustParse("1")
  830. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
  831. framework.ExpectNoError(err)
  832. ginkgo.By("Ensuring resource quota with best effort scope ignored the pod usage")
  833. usedResources[v1.ResourcePods] = resource.MustParse("0")
  834. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
  835. framework.ExpectNoError(err)
  836. ginkgo.By("Deleting the pod")
  837. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  838. framework.ExpectNoError(err)
  839. ginkgo.By("Ensuring resource quota status released the pod usage")
  840. usedResources[v1.ResourcePods] = resource.MustParse("0")
  841. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
  842. framework.ExpectNoError(err)
  843. })
  844. ginkgo.It("should verify ResourceQuota with terminating scopes through scope selectors.", func() {
  845. ginkgo.By("Creating a ResourceQuota with terminating scope")
  846. quotaTerminatingName := "quota-terminating"
  847. resourceQuotaTerminating, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeSelector(quotaTerminatingName, v1.ResourceQuotaScopeTerminating))
  848. framework.ExpectNoError(err)
  849. ginkgo.By("Ensuring ResourceQuota status is calculated")
  850. usedResources := v1.ResourceList{}
  851. usedResources[v1.ResourcePods] = resource.MustParse("0")
  852. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
  853. framework.ExpectNoError(err)
  854. ginkgo.By("Creating a ResourceQuota with not terminating scope")
  855. quotaNotTerminatingName := "quota-not-terminating"
  856. resourceQuotaNotTerminating, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeSelector(quotaNotTerminatingName, v1.ResourceQuotaScopeNotTerminating))
  857. framework.ExpectNoError(err)
  858. ginkgo.By("Ensuring ResourceQuota status is calculated")
  859. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
  860. framework.ExpectNoError(err)
  861. ginkgo.By("Creating a long running pod")
  862. podName := "test-pod"
  863. requests := v1.ResourceList{}
  864. requests[v1.ResourceCPU] = resource.MustParse("500m")
  865. requests[v1.ResourceMemory] = resource.MustParse("200Mi")
  866. limits := v1.ResourceList{}
  867. limits[v1.ResourceCPU] = resource.MustParse("1")
  868. limits[v1.ResourceMemory] = resource.MustParse("400Mi")
  869. pod := newTestPodForQuota(f, podName, requests, limits)
  870. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  871. framework.ExpectNoError(err)
  872. ginkgo.By("Ensuring resource quota with not terminating scope captures the pod usage")
  873. usedResources[v1.ResourcePods] = resource.MustParse("1")
  874. usedResources[v1.ResourceRequestsCPU] = requests[v1.ResourceCPU]
  875. usedResources[v1.ResourceRequestsMemory] = requests[v1.ResourceMemory]
  876. usedResources[v1.ResourceLimitsCPU] = limits[v1.ResourceCPU]
  877. usedResources[v1.ResourceLimitsMemory] = limits[v1.ResourceMemory]
  878. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
  879. framework.ExpectNoError(err)
  880. ginkgo.By("Ensuring resource quota with terminating scope ignored the pod usage")
  881. usedResources[v1.ResourcePods] = resource.MustParse("0")
  882. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  883. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
  884. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  885. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
  886. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
  887. framework.ExpectNoError(err)
  888. ginkgo.By("Deleting the pod")
  889. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), podName, metav1.NewDeleteOptions(0))
  890. framework.ExpectNoError(err)
  891. ginkgo.By("Ensuring resource quota status released the pod usage")
  892. usedResources[v1.ResourcePods] = resource.MustParse("0")
  893. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  894. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
  895. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  896. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
  897. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
  898. framework.ExpectNoError(err)
  899. ginkgo.By("Creating a terminating pod")
  900. podName = "terminating-pod"
  901. pod = newTestPodForQuota(f, podName, requests, limits)
  902. activeDeadlineSeconds := int64(3600)
  903. pod.Spec.ActiveDeadlineSeconds = &activeDeadlineSeconds
  904. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  905. framework.ExpectNoError(err)
  906. ginkgo.By("Ensuring resource quota with terminating scope captures the pod usage")
  907. usedResources[v1.ResourcePods] = resource.MustParse("1")
  908. usedResources[v1.ResourceRequestsCPU] = requests[v1.ResourceCPU]
  909. usedResources[v1.ResourceRequestsMemory] = requests[v1.ResourceMemory]
  910. usedResources[v1.ResourceLimitsCPU] = limits[v1.ResourceCPU]
  911. usedResources[v1.ResourceLimitsMemory] = limits[v1.ResourceMemory]
  912. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
  913. framework.ExpectNoError(err)
  914. ginkgo.By("Ensuring resource quota with not terminating scope ignored the pod usage")
  915. usedResources[v1.ResourcePods] = resource.MustParse("0")
  916. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  917. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
  918. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  919. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
  920. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
  921. framework.ExpectNoError(err)
  922. ginkgo.By("Deleting the pod")
  923. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), podName, metav1.NewDeleteOptions(0))
  924. framework.ExpectNoError(err)
  925. ginkgo.By("Ensuring resource quota status released the pod usage")
  926. usedResources[v1.ResourcePods] = resource.MustParse("0")
  927. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  928. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
  929. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  930. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
  931. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
  932. framework.ExpectNoError(err)
  933. })
  934. })
  935. var _ = SIGDescribe("ResourceQuota [Feature:PodPriority]", func() {
  936. f := framework.NewDefaultFramework("resourcequota-priorityclass")
  937. ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with same priority class.", func() {
  938. _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(context.TODO(), &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass1"}, Value: int32(1000)}, metav1.CreateOptions{})
  939. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  940. hard := v1.ResourceList{}
  941. hard[v1.ResourcePods] = resource.MustParse("1")
  942. ginkgo.By("Creating a ResourceQuota with priority class scope")
  943. resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass1"}))
  944. framework.ExpectNoError(err)
  945. ginkgo.By("Ensuring ResourceQuota status is calculated")
  946. usedResources := v1.ResourceList{}
  947. usedResources[v1.ResourcePods] = resource.MustParse("0")
  948. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  949. framework.ExpectNoError(err)
  950. ginkgo.By("Creating a pod with priority class")
  951. podName := "testpod-pclass1"
  952. pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass1")
  953. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  954. framework.ExpectNoError(err)
  955. ginkgo.By("Ensuring resource quota with priority class scope captures the pod usage")
  956. usedResources[v1.ResourcePods] = resource.MustParse("1")
  957. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  958. framework.ExpectNoError(err)
  959. ginkgo.By("Deleting the pod")
  960. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  961. framework.ExpectNoError(err)
  962. ginkgo.By("Ensuring resource quota status released the pod usage")
  963. usedResources[v1.ResourcePods] = resource.MustParse("0")
  964. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  965. framework.ExpectNoError(err)
  966. })
  967. ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against 2 pods with same priority class.", func() {
  968. _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(context.TODO(), &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass2"}, Value: int32(1000)}, metav1.CreateOptions{})
  969. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  970. hard := v1.ResourceList{}
  971. hard[v1.ResourcePods] = resource.MustParse("1")
  972. ginkgo.By("Creating a ResourceQuota with priority class scope")
  973. resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass2"}))
  974. framework.ExpectNoError(err)
  975. ginkgo.By("Ensuring ResourceQuota status is calculated")
  976. usedResources := v1.ResourceList{}
  977. usedResources[v1.ResourcePods] = resource.MustParse("0")
  978. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  979. framework.ExpectNoError(err)
  980. ginkgo.By("Creating first pod with priority class should pass")
  981. podName := "testpod-pclass2-1"
  982. pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass2")
  983. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  984. framework.ExpectNoError(err)
  985. ginkgo.By("Ensuring resource quota with priority class scope captures the pod usage")
  986. usedResources[v1.ResourcePods] = resource.MustParse("1")
  987. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  988. framework.ExpectNoError(err)
  989. ginkgo.By("Creating 2nd pod with priority class should fail")
  990. podName2 := "testpod-pclass2-2"
  991. pod2 := newTestPodForQuotaWithPriority(f, podName2, v1.ResourceList{}, v1.ResourceList{}, "pclass2")
  992. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod2, metav1.CreateOptions{})
  993. framework.ExpectError(err)
  994. ginkgo.By("Deleting first pod")
  995. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  996. framework.ExpectNoError(err)
  997. ginkgo.By("Ensuring resource quota status released the pod usage")
  998. usedResources[v1.ResourcePods] = resource.MustParse("0")
  999. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1000. framework.ExpectNoError(err)
  1001. })
  1002. ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against 2 pods with different priority class.", func() {
  1003. _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(context.TODO(), &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass3"}, Value: int32(1000)}, metav1.CreateOptions{})
  1004. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  1005. hard := v1.ResourceList{}
  1006. hard[v1.ResourcePods] = resource.MustParse("1")
  1007. ginkgo.By("Creating a ResourceQuota with priority class scope")
  1008. resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass4"}))
  1009. framework.ExpectNoError(err)
  1010. ginkgo.By("Ensuring ResourceQuota status is calculated")
  1011. usedResources := v1.ResourceList{}
  1012. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1013. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1014. framework.ExpectNoError(err)
  1015. ginkgo.By("Creating a pod with priority class with pclass3")
  1016. podName := "testpod-pclass3-1"
  1017. pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass3")
  1018. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  1019. framework.ExpectNoError(err)
  1020. ginkgo.By("Ensuring resource quota with priority class scope remains same")
  1021. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1022. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1023. framework.ExpectNoError(err)
  1024. ginkgo.By("Creating a 2nd pod with priority class pclass3")
  1025. podName2 := "testpod-pclass2-2"
  1026. pod2 := newTestPodForQuotaWithPriority(f, podName2, v1.ResourceList{}, v1.ResourceList{}, "pclass3")
  1027. pod2, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod2, metav1.CreateOptions{})
  1028. framework.ExpectNoError(err)
  1029. ginkgo.By("Ensuring resource quota with priority class scope remains same")
  1030. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1031. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1032. framework.ExpectNoError(err)
  1033. ginkgo.By("Deleting both pods")
  1034. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  1035. framework.ExpectNoError(err)
  1036. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod2.Name, metav1.NewDeleteOptions(0))
  1037. framework.ExpectNoError(err)
  1038. })
  1039. ginkgo.It("should verify ResourceQuota's multiple priority class scope (quota set to pod count: 2) against 2 pods with same priority classes.", func() {
  1040. _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(context.TODO(), &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass5"}, Value: int32(1000)}, metav1.CreateOptions{})
  1041. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  1042. _, err = f.ClientSet.SchedulingV1().PriorityClasses().Create(context.TODO(), &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass6"}, Value: int32(1000)}, metav1.CreateOptions{})
  1043. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  1044. hard := v1.ResourceList{}
  1045. hard[v1.ResourcePods] = resource.MustParse("2")
  1046. ginkgo.By("Creating a ResourceQuota with priority class scope")
  1047. resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass5", "pclass6"}))
  1048. framework.ExpectNoError(err)
  1049. ginkgo.By("Ensuring ResourceQuota status is calculated")
  1050. usedResources := v1.ResourceList{}
  1051. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1052. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1053. framework.ExpectNoError(err)
  1054. ginkgo.By("Creating a pod with priority class pclass5")
  1055. podName := "testpod-pclass5"
  1056. pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass5")
  1057. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  1058. framework.ExpectNoError(err)
  1059. ginkgo.By("Ensuring resource quota with priority class is updated with the pod usage")
  1060. usedResources[v1.ResourcePods] = resource.MustParse("1")
  1061. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1062. framework.ExpectNoError(err)
  1063. ginkgo.By("Creating 2nd pod with priority class pclass6")
  1064. podName2 := "testpod-pclass6"
  1065. pod2 := newTestPodForQuotaWithPriority(f, podName2, v1.ResourceList{}, v1.ResourceList{}, "pclass6")
  1066. pod2, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod2, metav1.CreateOptions{})
  1067. framework.ExpectNoError(err)
  1068. ginkgo.By("Ensuring resource quota with priority class scope is updated with the pod usage")
  1069. usedResources[v1.ResourcePods] = resource.MustParse("2")
  1070. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1071. framework.ExpectNoError(err)
  1072. ginkgo.By("Deleting both pods")
  1073. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  1074. framework.ExpectNoError(err)
  1075. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod2.Name, metav1.NewDeleteOptions(0))
  1076. framework.ExpectNoError(err)
  1077. ginkgo.By("Ensuring resource quota status released the pod usage")
  1078. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1079. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1080. framework.ExpectNoError(err)
  1081. })
  1082. ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with different priority class (ScopeSelectorOpNotIn).", func() {
  1083. _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(context.TODO(), &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass7"}, Value: int32(1000)}, metav1.CreateOptions{})
  1084. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  1085. hard := v1.ResourceList{}
  1086. hard[v1.ResourcePods] = resource.MustParse("1")
  1087. ginkgo.By("Creating a ResourceQuota with priority class scope")
  1088. resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpNotIn, []string{"pclass7"}))
  1089. framework.ExpectNoError(err)
  1090. ginkgo.By("Ensuring ResourceQuota status is calculated")
  1091. usedResources := v1.ResourceList{}
  1092. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1093. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1094. framework.ExpectNoError(err)
  1095. ginkgo.By("Creating a pod with priority class pclass7")
  1096. podName := "testpod-pclass7"
  1097. pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass7")
  1098. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  1099. framework.ExpectNoError(err)
  1100. ginkgo.By("Ensuring resource quota with priority class is not used")
  1101. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1102. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1103. framework.ExpectNoError(err)
  1104. ginkgo.By("Deleting the pod")
  1105. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  1106. framework.ExpectNoError(err)
  1107. })
  1108. ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with different priority class (ScopeSelectorOpExists).", func() {
  1109. _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(context.TODO(), &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass8"}, Value: int32(1000)}, metav1.CreateOptions{})
  1110. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  1111. hard := v1.ResourceList{}
  1112. hard[v1.ResourcePods] = resource.MustParse("1")
  1113. ginkgo.By("Creating a ResourceQuota with priority class scope")
  1114. resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpExists, []string{}))
  1115. framework.ExpectNoError(err)
  1116. ginkgo.By("Ensuring ResourceQuota status is calculated")
  1117. usedResources := v1.ResourceList{}
  1118. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1119. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1120. framework.ExpectNoError(err)
  1121. ginkgo.By("Creating a pod with priority class pclass8")
  1122. podName := "testpod-pclass8"
  1123. pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass8")
  1124. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  1125. framework.ExpectNoError(err)
  1126. ginkgo.By("Ensuring resource quota with priority class is updated with the pod usage")
  1127. usedResources[v1.ResourcePods] = resource.MustParse("1")
  1128. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1129. framework.ExpectNoError(err)
  1130. ginkgo.By("Deleting the pod")
  1131. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  1132. framework.ExpectNoError(err)
  1133. ginkgo.By("Ensuring resource quota status released the pod usage")
  1134. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1135. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1136. framework.ExpectNoError(err)
  1137. })
  1138. ginkgo.It("should verify ResourceQuota's priority class scope (cpu, memory quota set) against a pod with same priority class.", func() {
  1139. _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(context.TODO(), &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass9"}, Value: int32(1000)}, metav1.CreateOptions{})
  1140. framework.ExpectEqual(err == nil || apierrors.IsAlreadyExists(err), true)
  1141. hard := v1.ResourceList{}
  1142. hard[v1.ResourcePods] = resource.MustParse("1")
  1143. hard[v1.ResourceRequestsCPU] = resource.MustParse("1")
  1144. hard[v1.ResourceRequestsMemory] = resource.MustParse("1Gi")
  1145. hard[v1.ResourceLimitsCPU] = resource.MustParse("3")
  1146. hard[v1.ResourceLimitsMemory] = resource.MustParse("3Gi")
  1147. ginkgo.By("Creating a ResourceQuota with priority class scope")
  1148. resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass9"}))
  1149. framework.ExpectNoError(err)
  1150. ginkgo.By("Ensuring ResourceQuota status is calculated")
  1151. usedResources := v1.ResourceList{}
  1152. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1153. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  1154. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0Gi")
  1155. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  1156. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0Gi")
  1157. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1158. framework.ExpectNoError(err)
  1159. ginkgo.By("Creating a pod with priority class")
  1160. podName := "testpod-pclass9"
  1161. request := v1.ResourceList{}
  1162. request[v1.ResourceCPU] = resource.MustParse("1")
  1163. request[v1.ResourceMemory] = resource.MustParse("1Gi")
  1164. limit := v1.ResourceList{}
  1165. limit[v1.ResourceCPU] = resource.MustParse("2")
  1166. limit[v1.ResourceMemory] = resource.MustParse("2Gi")
  1167. pod := newTestPodForQuotaWithPriority(f, podName, request, limit, "pclass9")
  1168. pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  1169. framework.ExpectNoError(err)
  1170. ginkgo.By("Ensuring resource quota with priority class scope captures the pod usage")
  1171. usedResources[v1.ResourcePods] = resource.MustParse("1")
  1172. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("1")
  1173. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("1Gi")
  1174. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("2")
  1175. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("2Gi")
  1176. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1177. framework.ExpectNoError(err)
  1178. ginkgo.By("Deleting the pod")
  1179. err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(context.TODO(), pod.Name, metav1.NewDeleteOptions(0))
  1180. framework.ExpectNoError(err)
  1181. ginkgo.By("Ensuring resource quota status released the pod usage")
  1182. usedResources[v1.ResourcePods] = resource.MustParse("0")
  1183. usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
  1184. usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0Gi")
  1185. usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
  1186. usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0Gi")
  1187. err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
  1188. framework.ExpectNoError(err)
  1189. })
  1190. })
  1191. // newTestResourceQuotaWithScopeSelector returns a quota that enforces default constraints for testing with scopeSelectors
  1192. func newTestResourceQuotaWithScopeSelector(name string, scope v1.ResourceQuotaScope) *v1.ResourceQuota {
  1193. hard := v1.ResourceList{}
  1194. hard[v1.ResourcePods] = resource.MustParse("5")
  1195. switch scope {
  1196. case v1.ResourceQuotaScopeTerminating, v1.ResourceQuotaScopeNotTerminating:
  1197. hard[v1.ResourceRequestsCPU] = resource.MustParse("1")
  1198. hard[v1.ResourceRequestsMemory] = resource.MustParse("500Mi")
  1199. hard[v1.ResourceLimitsCPU] = resource.MustParse("2")
  1200. hard[v1.ResourceLimitsMemory] = resource.MustParse("1Gi")
  1201. }
  1202. return &v1.ResourceQuota{
  1203. ObjectMeta: metav1.ObjectMeta{Name: name},
  1204. Spec: v1.ResourceQuotaSpec{Hard: hard,
  1205. ScopeSelector: &v1.ScopeSelector{
  1206. MatchExpressions: []v1.ScopedResourceSelectorRequirement{
  1207. {
  1208. ScopeName: scope,
  1209. Operator: v1.ScopeSelectorOpExists},
  1210. },
  1211. },
  1212. },
  1213. }
  1214. }
  1215. // newTestResourceQuotaWithScope returns a quota that enforces default constraints for testing with scopes
  1216. func newTestResourceQuotaWithScope(name string, scope v1.ResourceQuotaScope) *v1.ResourceQuota {
  1217. hard := v1.ResourceList{}
  1218. hard[v1.ResourcePods] = resource.MustParse("5")
  1219. switch scope {
  1220. case v1.ResourceQuotaScopeTerminating, v1.ResourceQuotaScopeNotTerminating:
  1221. hard[v1.ResourceRequestsCPU] = resource.MustParse("1")
  1222. hard[v1.ResourceRequestsMemory] = resource.MustParse("500Mi")
  1223. hard[v1.ResourceLimitsCPU] = resource.MustParse("2")
  1224. hard[v1.ResourceLimitsMemory] = resource.MustParse("1Gi")
  1225. }
  1226. return &v1.ResourceQuota{
  1227. ObjectMeta: metav1.ObjectMeta{Name: name},
  1228. Spec: v1.ResourceQuotaSpec{Hard: hard, Scopes: []v1.ResourceQuotaScope{scope}},
  1229. }
  1230. }
  1231. // newTestResourceQuotaWithScopeForPriorityClass returns a quota
  1232. // that enforces default constraints for testing with ResourceQuotaScopePriorityClass scope
  1233. func newTestResourceQuotaWithScopeForPriorityClass(name string, hard v1.ResourceList, op v1.ScopeSelectorOperator, values []string) *v1.ResourceQuota {
  1234. return &v1.ResourceQuota{
  1235. ObjectMeta: metav1.ObjectMeta{Name: name},
  1236. Spec: v1.ResourceQuotaSpec{Hard: hard,
  1237. ScopeSelector: &v1.ScopeSelector{
  1238. MatchExpressions: []v1.ScopedResourceSelectorRequirement{
  1239. {
  1240. ScopeName: v1.ResourceQuotaScopePriorityClass,
  1241. Operator: op,
  1242. Values: values,
  1243. },
  1244. },
  1245. },
  1246. },
  1247. }
  1248. }
  1249. // newTestResourceQuota returns a quota that enforces default constraints for testing
  1250. func newTestResourceQuota(name string) *v1.ResourceQuota {
  1251. hard := v1.ResourceList{}
  1252. hard[v1.ResourcePods] = resource.MustParse("5")
  1253. hard[v1.ResourceServices] = resource.MustParse("10")
  1254. hard[v1.ResourceServicesNodePorts] = resource.MustParse("1")
  1255. hard[v1.ResourceServicesLoadBalancers] = resource.MustParse("1")
  1256. hard[v1.ResourceReplicationControllers] = resource.MustParse("10")
  1257. hard[v1.ResourceQuotas] = resource.MustParse("1")
  1258. hard[v1.ResourceCPU] = resource.MustParse("1")
  1259. hard[v1.ResourceMemory] = resource.MustParse("500Mi")
  1260. hard[v1.ResourceConfigMaps] = resource.MustParse("2")
  1261. hard[v1.ResourceSecrets] = resource.MustParse("10")
  1262. hard[v1.ResourcePersistentVolumeClaims] = resource.MustParse("10")
  1263. hard[v1.ResourceRequestsStorage] = resource.MustParse("10Gi")
  1264. hard[v1.ResourceEphemeralStorage] = resource.MustParse("50Gi")
  1265. hard[core.V1ResourceByStorageClass(classGold, v1.ResourcePersistentVolumeClaims)] = resource.MustParse("10")
  1266. hard[core.V1ResourceByStorageClass(classGold, v1.ResourceRequestsStorage)] = resource.MustParse("10Gi")
  1267. // test quota on discovered resource type
  1268. hard[v1.ResourceName("count/replicasets.apps")] = resource.MustParse("5")
  1269. // test quota on extended resource
  1270. hard[v1.ResourceName(v1.DefaultResourceRequestsPrefix+extendedResourceName)] = resource.MustParse("3")
  1271. return &v1.ResourceQuota{
  1272. ObjectMeta: metav1.ObjectMeta{Name: name},
  1273. Spec: v1.ResourceQuotaSpec{Hard: hard},
  1274. }
  1275. }
  1276. // newTestPodForQuota returns a pod that has the specified requests and limits
  1277. func newTestPodForQuota(f *framework.Framework, name string, requests v1.ResourceList, limits v1.ResourceList) *v1.Pod {
  1278. return &v1.Pod{
  1279. ObjectMeta: metav1.ObjectMeta{
  1280. Name: name,
  1281. },
  1282. Spec: v1.PodSpec{
  1283. Containers: []v1.Container{
  1284. {
  1285. Name: "pause",
  1286. Image: imageutils.GetPauseImageName(),
  1287. Resources: v1.ResourceRequirements{
  1288. Requests: requests,
  1289. Limits: limits,
  1290. },
  1291. },
  1292. },
  1293. },
  1294. }
  1295. }
  1296. // newTestPodForQuotaWithPriority returns a pod that has the specified requests, limits and priority class
  1297. func newTestPodForQuotaWithPriority(f *framework.Framework, name string, requests v1.ResourceList, limits v1.ResourceList, pclass string) *v1.Pod {
  1298. return &v1.Pod{
  1299. ObjectMeta: metav1.ObjectMeta{
  1300. Name: name,
  1301. },
  1302. Spec: v1.PodSpec{
  1303. Containers: []v1.Container{
  1304. {
  1305. Name: "pause",
  1306. Image: imageutils.GetPauseImageName(),
  1307. Resources: v1.ResourceRequirements{
  1308. Requests: requests,
  1309. Limits: limits,
  1310. },
  1311. },
  1312. },
  1313. PriorityClassName: pclass,
  1314. },
  1315. }
  1316. }
  1317. // newTestPersistentVolumeClaimForQuota returns a simple persistent volume claim
  1318. func newTestPersistentVolumeClaimForQuota(name string) *v1.PersistentVolumeClaim {
  1319. return &v1.PersistentVolumeClaim{
  1320. ObjectMeta: metav1.ObjectMeta{
  1321. Name: name,
  1322. },
  1323. Spec: v1.PersistentVolumeClaimSpec{
  1324. AccessModes: []v1.PersistentVolumeAccessMode{
  1325. v1.ReadWriteOnce,
  1326. v1.ReadOnlyMany,
  1327. v1.ReadWriteMany,
  1328. },
  1329. Resources: v1.ResourceRequirements{
  1330. Requests: v1.ResourceList{
  1331. v1.ResourceName(v1.ResourceStorage): resource.MustParse("1Gi"),
  1332. },
  1333. },
  1334. },
  1335. }
  1336. }
  1337. // newTestReplicationControllerForQuota returns a simple replication controller
  1338. func newTestReplicationControllerForQuota(name, image string, replicas int32) *v1.ReplicationController {
  1339. return &v1.ReplicationController{
  1340. ObjectMeta: metav1.ObjectMeta{
  1341. Name: name,
  1342. },
  1343. Spec: v1.ReplicationControllerSpec{
  1344. Replicas: func(i int32) *int32 { return &i }(replicas),
  1345. Selector: map[string]string{
  1346. "name": name,
  1347. },
  1348. Template: &v1.PodTemplateSpec{
  1349. ObjectMeta: metav1.ObjectMeta{
  1350. Labels: map[string]string{"name": name},
  1351. },
  1352. Spec: v1.PodSpec{
  1353. Containers: []v1.Container{
  1354. {
  1355. Name: name,
  1356. Image: image,
  1357. },
  1358. },
  1359. },
  1360. },
  1361. },
  1362. }
  1363. }
  1364. // newTestReplicaSetForQuota returns a simple replica set
  1365. func newTestReplicaSetForQuota(name, image string, replicas int32) *appsv1.ReplicaSet {
  1366. zero := int64(0)
  1367. return &appsv1.ReplicaSet{
  1368. ObjectMeta: metav1.ObjectMeta{
  1369. Name: name,
  1370. },
  1371. Spec: appsv1.ReplicaSetSpec{
  1372. Replicas: &replicas,
  1373. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"name": name}},
  1374. Template: v1.PodTemplateSpec{
  1375. ObjectMeta: metav1.ObjectMeta{
  1376. Labels: map[string]string{"name": name},
  1377. },
  1378. Spec: v1.PodSpec{
  1379. TerminationGracePeriodSeconds: &zero,
  1380. Containers: []v1.Container{
  1381. {
  1382. Name: name,
  1383. Image: image,
  1384. },
  1385. },
  1386. },
  1387. },
  1388. },
  1389. }
  1390. }
  1391. // newTestServiceForQuota returns a simple service
  1392. func newTestServiceForQuota(name string, serviceType v1.ServiceType) *v1.Service {
  1393. return &v1.Service{
  1394. ObjectMeta: metav1.ObjectMeta{
  1395. Name: name,
  1396. },
  1397. Spec: v1.ServiceSpec{
  1398. Type: serviceType,
  1399. Ports: []v1.ServicePort{{
  1400. Port: 80,
  1401. TargetPort: intstr.FromInt(80),
  1402. }},
  1403. },
  1404. }
  1405. }
  1406. func newTestConfigMapForQuota(name string) *v1.ConfigMap {
  1407. return &v1.ConfigMap{
  1408. ObjectMeta: metav1.ObjectMeta{
  1409. Name: name,
  1410. },
  1411. Data: map[string]string{
  1412. "a": "b",
  1413. },
  1414. }
  1415. }
  1416. func newTestSecretForQuota(name string) *v1.Secret {
  1417. return &v1.Secret{
  1418. ObjectMeta: metav1.ObjectMeta{
  1419. Name: name,
  1420. },
  1421. Data: map[string][]byte{
  1422. "data-1": []byte("value-1\n"),
  1423. "data-2": []byte("value-2\n"),
  1424. "data-3": []byte("value-3\n"),
  1425. },
  1426. }
  1427. }
  1428. // createResourceQuota in the specified namespace
  1429. func createResourceQuota(c clientset.Interface, namespace string, resourceQuota *v1.ResourceQuota) (*v1.ResourceQuota, error) {
  1430. return c.CoreV1().ResourceQuotas(namespace).Create(context.TODO(), resourceQuota, metav1.CreateOptions{})
  1431. }
  1432. // deleteResourceQuota with the specified name
  1433. func deleteResourceQuota(c clientset.Interface, namespace, name string) error {
  1434. return c.CoreV1().ResourceQuotas(namespace).Delete(context.TODO(), name, nil)
  1435. }
  1436. // countResourceQuota counts the number of ResourceQuota in the specified namespace
  1437. // On contended servers the service account controller can slow down, leading to the count changing during a run.
  1438. // Wait up to 5s for the count to stabilize, assuming that updates come at a consistent rate, and are not held indefinitely.
  1439. func countResourceQuota(c clientset.Interface, namespace string) (int, error) {
  1440. found, unchanged := 0, 0
  1441. return found, wait.Poll(1*time.Second, 30*time.Second, func() (bool, error) {
  1442. resourceQuotas, err := c.CoreV1().ResourceQuotas(namespace).List(context.TODO(), metav1.ListOptions{})
  1443. framework.ExpectNoError(err)
  1444. if len(resourceQuotas.Items) == found {
  1445. // loop until the number of resource quotas has stabilized for 5 seconds
  1446. unchanged++
  1447. return unchanged > 4, nil
  1448. }
  1449. unchanged = 0
  1450. found = len(resourceQuotas.Items)
  1451. return false, nil
  1452. })
  1453. }
  1454. // wait for resource quota status to show the expected used resources value
  1455. func waitForResourceQuota(c clientset.Interface, ns, quotaName string, used v1.ResourceList) error {
  1456. return wait.Poll(framework.Poll, resourceQuotaTimeout, func() (bool, error) {
  1457. resourceQuota, err := c.CoreV1().ResourceQuotas(ns).Get(context.TODO(), quotaName, metav1.GetOptions{})
  1458. if err != nil {
  1459. return false, err
  1460. }
  1461. // used may not yet be calculated
  1462. if resourceQuota.Status.Used == nil {
  1463. return false, nil
  1464. }
  1465. // verify that the quota shows the expected used resource values
  1466. for k, v := range used {
  1467. if actualValue, found := resourceQuota.Status.Used[k]; !found || (actualValue.Cmp(v) != 0) {
  1468. framework.Logf("resource %s, expected %s, actual %s", k, v.String(), actualValue.String())
  1469. return false, nil
  1470. }
  1471. }
  1472. return true, nil
  1473. })
  1474. }
  1475. // updateResourceQuotaUntilUsageAppears updates the resource quota object until the usage is populated
  1476. // for the specific resource name.
  1477. func updateResourceQuotaUntilUsageAppears(c clientset.Interface, ns, quotaName string, resourceName v1.ResourceName) error {
  1478. return wait.Poll(framework.Poll, 1*time.Minute, func() (bool, error) {
  1479. resourceQuota, err := c.CoreV1().ResourceQuotas(ns).Get(context.TODO(), quotaName, metav1.GetOptions{})
  1480. if err != nil {
  1481. return false, err
  1482. }
  1483. // verify that the quota shows the expected used resource values
  1484. _, ok := resourceQuota.Status.Used[resourceName]
  1485. if ok {
  1486. return true, nil
  1487. }
  1488. current := resourceQuota.Spec.Hard[resourceName]
  1489. current.Add(resource.MustParse("1"))
  1490. resourceQuota.Spec.Hard[resourceName] = current
  1491. _, err = c.CoreV1().ResourceQuotas(ns).Update(context.TODO(), resourceQuota, metav1.UpdateOptions{})
  1492. // ignoring conflicts since someone else may already updated it.
  1493. if apierrors.IsConflict(err) {
  1494. return false, nil
  1495. }
  1496. return false, err
  1497. })
  1498. }