12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628 |
- /*
- Copyright 2015 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package apimachinery
- import (
- "fmt"
- "strconv"
- "time"
- appsv1 "k8s.io/api/apps/v1"
- "k8s.io/api/core/v1"
- schedulingv1 "k8s.io/api/scheduling/v1"
- "k8s.io/apimachinery/pkg/api/errors"
- "k8s.io/apimachinery/pkg/api/resource"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
- "k8s.io/apimachinery/pkg/util/intstr"
- "k8s.io/apimachinery/pkg/util/wait"
- clientset "k8s.io/client-go/kubernetes"
- "k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
- "k8s.io/kubernetes/test/e2e/framework"
- e2elog "k8s.io/kubernetes/test/e2e/framework/log"
- "k8s.io/kubernetes/test/utils/crd"
- imageutils "k8s.io/kubernetes/test/utils/image"
- "github.com/onsi/ginkgo"
- "github.com/onsi/gomega"
- )
- const (
- // how long to wait for a resource quota update to occur
- resourceQuotaTimeout = 30 * time.Second
- podName = "pfpod"
- )
- var classGold = "gold"
- var extendedResourceName = "example.com/dongle"
- var _ = SIGDescribe("ResourceQuota", func() {
- f := framework.NewDefaultFramework("resourcequota")
- ginkgo.It("should create a ResourceQuota and ensure its status is promptly calculated.", func() {
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a service.", func() {
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a Service")
- service := newTestServiceForQuota("test-service", v1.ServiceTypeClusterIP)
- service, err = f.ClientSet.CoreV1().Services(f.Namespace.Name).Create(service)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status captures service creation")
- usedResources = v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourceServices] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting a Service")
- err = f.ClientSet.CoreV1().Services(f.Namespace.Name).Delete(service.Name, nil)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released usage")
- usedResources[v1.ResourceServices] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a secret.", func() {
- ginkgo.By("Discovering how many secrets are in namespace by default")
- found, unchanged := 0, 0
- wait.Poll(1*time.Second, 30*time.Second, func() (bool, error) {
- secrets, err := f.ClientSet.CoreV1().Secrets(f.Namespace.Name).List(metav1.ListOptions{})
- framework.ExpectNoError(err)
- if len(secrets.Items) == found {
- // loop until the number of secrets has stabilized for 5 seconds
- unchanged++
- return unchanged > 4, nil
- }
- unchanged = 0
- found = len(secrets.Items)
- return false, nil
- })
- defaultSecrets := fmt.Sprintf("%d", found)
- hardSecrets := fmt.Sprintf("%d", found+1)
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota.Spec.Hard[v1.ResourceSecrets] = resource.MustParse(hardSecrets)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourceSecrets] = resource.MustParse(defaultSecrets)
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a Secret")
- secret := newTestSecretForQuota("test-secret")
- secret, err = f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Create(secret)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status captures secret creation")
- usedResources = v1.ResourceList{}
- usedResources[v1.ResourceSecrets] = resource.MustParse(hardSecrets)
- // we expect there to be two secrets because each namespace will receive
- // a service account token secret by default
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting a secret")
- err = f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Delete(secret.Name, nil)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released usage")
- usedResources[v1.ResourceSecrets] = resource.MustParse(defaultSecrets)
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a pod.", func() {
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a Pod that fits quota")
- podName := "test-pod"
- requests := v1.ResourceList{}
- limits := v1.ResourceList{}
- requests[v1.ResourceCPU] = resource.MustParse("500m")
- requests[v1.ResourceMemory] = resource.MustParse("252Mi")
- requests[v1.ResourceEphemeralStorage] = resource.MustParse("30Gi")
- requests[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
- limits[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
- pod := newTestPodForQuota(f, podName, requests, limits)
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- podToUpdate := pod
- ginkgo.By("Ensuring ResourceQuota status captures the pod usage")
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- usedResources[v1.ResourceCPU] = requests[v1.ResourceCPU]
- usedResources[v1.ResourceMemory] = requests[v1.ResourceMemory]
- usedResources[v1.ResourceEphemeralStorage] = requests[v1.ResourceEphemeralStorage]
- usedResources[v1.ResourceName(v1.DefaultResourceRequestsPrefix+extendedResourceName)] = requests[v1.ResourceName(extendedResourceName)]
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Not allowing a pod to be created that exceeds remaining quota")
- requests = v1.ResourceList{}
- requests[v1.ResourceCPU] = resource.MustParse("600m")
- requests[v1.ResourceMemory] = resource.MustParse("100Mi")
- pod = newTestPodForQuota(f, "fail-pod", requests, v1.ResourceList{})
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectError(err)
- ginkgo.By("Not allowing a pod to be created that exceeds remaining quota(validation on extended resources)")
- requests = v1.ResourceList{}
- limits = v1.ResourceList{}
- requests[v1.ResourceCPU] = resource.MustParse("500m")
- requests[v1.ResourceMemory] = resource.MustParse("100Mi")
- requests[v1.ResourceEphemeralStorage] = resource.MustParse("30Gi")
- requests[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
- limits[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
- pod = newTestPodForQuota(f, "fail-pod-for-extended-resource", requests, limits)
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectError(err)
- ginkgo.By("Ensuring a pod cannot update its resource requirements")
- // a pod cannot dynamically update its resource requirements.
- requests = v1.ResourceList{}
- requests[v1.ResourceCPU] = resource.MustParse("100m")
- requests[v1.ResourceMemory] = resource.MustParse("100Mi")
- requests[v1.ResourceEphemeralStorage] = resource.MustParse("10Gi")
- podToUpdate.Spec.Containers[0].Resources.Requests = requests
- _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Update(podToUpdate)
- framework.ExpectError(err)
- ginkgo.By("Ensuring attempts to update pod resource requirements did not change quota usage")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(podName, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceCPU] = resource.MustParse("0")
- usedResources[v1.ResourceMemory] = resource.MustParse("0")
- usedResources[v1.ResourceEphemeralStorage] = resource.MustParse("0")
- usedResources[v1.ResourceName(v1.DefaultResourceRequestsPrefix+extendedResourceName)] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a configMap.", func() {
- found, unchanged := 0, 0
- wait.Poll(1*time.Second, 30*time.Second, func() (bool, error) {
- configmaps, err := f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).List(metav1.ListOptions{})
- framework.ExpectNoError(err)
- if len(configmaps.Items) == found {
- // loop until the number of configmaps has stabilized for 5 seconds
- unchanged++
- return unchanged > 4, nil
- }
- unchanged = 0
- found = len(configmaps.Items)
- return false, nil
- })
- defaultConfigMaps := fmt.Sprintf("%d", found)
- hardConfigMaps := fmt.Sprintf("%d", found+1)
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourceConfigMaps] = resource.MustParse(defaultConfigMaps)
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ConfigMap")
- configMap := newTestConfigMapForQuota("test-configmap")
- configMap, err = f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Create(configMap)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status captures configMap creation")
- usedResources = v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- // we expect there to be two configmaps because each namespace will receive
- // a ca.crt configmap by default.
- // ref:https://github.com/kubernetes/kubernetes/pull/68812
- usedResources[v1.ResourceConfigMaps] = resource.MustParse(hardConfigMaps)
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting a ConfigMap")
- err = f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Delete(configMap.Name, nil)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released usage")
- usedResources[v1.ResourceConfigMaps] = resource.MustParse(defaultConfigMaps)
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a replication controller.", func() {
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourceReplicationControllers] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ReplicationController")
- replicationController := newTestReplicationControllerForQuota("test-rc", "nginx", 0)
- replicationController, err = f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Create(replicationController)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status captures replication controller creation")
- usedResources = v1.ResourceList{}
- usedResources[v1.ResourceReplicationControllers] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting a ReplicationController")
- err = f.ClientSet.CoreV1().ReplicationControllers(f.Namespace.Name).Delete(replicationController.Name, nil)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released usage")
- usedResources[v1.ResourceReplicationControllers] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a replica set.", func() {
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourceName("count/replicasets.apps")] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ReplicaSet")
- replicaSet := newTestReplicaSetForQuota("test-rs", "nginx", 0)
- replicaSet, err = f.ClientSet.AppsV1().ReplicaSets(f.Namespace.Name).Create(replicaSet)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status captures replicaset creation")
- usedResources = v1.ResourceList{}
- usedResources[v1.ResourceName("count/replicasets.apps")] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting a ReplicaSet")
- err = f.ClientSet.AppsV1().ReplicaSets(f.Namespace.Name).Delete(replicaSet.Name, nil)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released usage")
- usedResources[v1.ResourceName("count/replicasets.apps")] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a persistent volume claim. [sig-storage]", func() {
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsStorage] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a PersistentVolumeClaim")
- pvc := newTestPersistentVolumeClaimForQuota("test-claim")
- pvc, err = f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Create(pvc)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status captures persistent volume claim creation")
- usedResources = v1.ResourceList{}
- usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("1")
- usedResources[v1.ResourceRequestsStorage] = resource.MustParse("1Gi")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting a PersistentVolumeClaim")
- err = f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Delete(pvc.Name, nil)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released usage")
- usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsStorage] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a persistent volume claim with a storage class. [sig-storage]", func() {
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := newTestResourceQuota(quotaName)
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsStorage] = resource.MustParse("0")
- usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourcePersistentVolumeClaims)] = resource.MustParse("0")
- usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourceRequestsStorage)] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a PersistentVolumeClaim with storage class")
- pvc := newTestPersistentVolumeClaimForQuota("test-claim")
- pvc.Spec.StorageClassName = &classGold
- pvc, err = f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Create(pvc)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status captures persistent volume claim creation")
- usedResources = v1.ResourceList{}
- usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("1")
- usedResources[v1.ResourceRequestsStorage] = resource.MustParse("1Gi")
- usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourcePersistentVolumeClaims)] = resource.MustParse("1")
- usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourceRequestsStorage)] = resource.MustParse("1Gi")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting a PersistentVolumeClaim")
- err = f.ClientSet.CoreV1().PersistentVolumeClaims(f.Namespace.Name).Delete(pvc.Name, nil)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released usage")
- usedResources[v1.ResourcePersistentVolumeClaims] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsStorage] = resource.MustParse("0")
- usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourcePersistentVolumeClaims)] = resource.MustParse("0")
- usedResources[core.V1ResourceByStorageClass(classGold, v1.ResourceRequestsStorage)] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should create a ResourceQuota and capture the life of a custom resource.", func() {
- ginkgo.By("Creating a Custom Resource Definition")
- testcrd, err := crd.CreateTestCRD(f)
- framework.ExpectNoError(err)
- defer testcrd.CleanUp()
- countResourceName := "count/" + testcrd.Crd.Spec.Names.Plural + "." + testcrd.Crd.Spec.Group
- // resourcequota controller needs to take 30 seconds at most to detect the new custom resource.
- // in order to make sure the resourcequota controller knows this resource, we create one test
- // resourcequota object, and triggering updates on it until the status is updated.
- quotaName := "quota-for-" + testcrd.Crd.Spec.Names.Plural
- resourceQuota, err := createResourceQuota(f.ClientSet, f.Namespace.Name, &v1.ResourceQuota{
- ObjectMeta: metav1.ObjectMeta{Name: quotaName},
- Spec: v1.ResourceQuotaSpec{
- Hard: v1.ResourceList{
- v1.ResourceName(countResourceName): resource.MustParse("0"),
- },
- },
- })
- err = updateResourceQuotaUntilUsageAppears(f.ClientSet, f.Namespace.Name, quotaName, v1.ResourceName(countResourceName))
- framework.ExpectNoError(err)
- err = f.ClientSet.CoreV1().ResourceQuotas(f.Namespace.Name).Delete(quotaName, nil)
- framework.ExpectNoError(err)
- ginkgo.By("Counting existing ResourceQuota")
- c, err := countResourceQuota(f.ClientSet, f.Namespace.Name)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota")
- quotaName = "test-quota"
- resourceQuota = newTestResourceQuota(quotaName)
- resourceQuota.Spec.Hard[v1.ResourceName(countResourceName)] = resource.MustParse("1")
- resourceQuota, err = createResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourceQuotas] = resource.MustParse(strconv.Itoa(c + 1))
- usedResources[v1.ResourceName(countResourceName)] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a custom resource")
- resourceClient := testcrd.DynamicClients["v1"]
- testcr, err := instantiateCustomResource(&unstructured.Unstructured{
- Object: map[string]interface{}{
- "apiVersion": testcrd.Crd.Spec.Group + "/" + testcrd.Crd.Spec.Versions[0].Name,
- "kind": testcrd.Crd.Spec.Names.Kind,
- "metadata": map[string]interface{}{
- "name": "test-cr-1",
- },
- },
- }, resourceClient, testcrd.Crd)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status captures custom resource creation")
- usedResources = v1.ResourceList{}
- usedResources[v1.ResourceName(countResourceName)] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a second custom resource")
- _, err = instantiateCustomResource(&unstructured.Unstructured{
- Object: map[string]interface{}{
- "apiVersion": testcrd.Crd.Spec.Group + "/" + testcrd.Crd.Spec.Versions[0].Name,
- "kind": testcrd.Crd.Spec.Names.Kind,
- "metadata": map[string]interface{}{
- "name": "test-cr-2",
- },
- },
- }, resourceClient, testcrd.Crd)
- // since we only give one quota, this creation should fail.
- framework.ExpectError(err)
- ginkgo.By("Deleting a custom resource")
- err = deleteCustomResource(resourceClient, testcr.GetName())
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released usage")
- usedResources[v1.ResourceName(countResourceName)] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, quotaName, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota with terminating scopes.", func() {
- ginkgo.By("Creating a ResourceQuota with terminating scope")
- quotaTerminatingName := "quota-terminating"
- resourceQuotaTerminating, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScope(quotaTerminatingName, v1.ResourceQuotaScopeTerminating))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota with not terminating scope")
- quotaNotTerminatingName := "quota-not-terminating"
- resourceQuotaNotTerminating, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScope(quotaNotTerminatingName, v1.ResourceQuotaScopeNotTerminating))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a long running pod")
- podName := "test-pod"
- requests := v1.ResourceList{}
- requests[v1.ResourceCPU] = resource.MustParse("500m")
- requests[v1.ResourceMemory] = resource.MustParse("200Mi")
- limits := v1.ResourceList{}
- limits[v1.ResourceCPU] = resource.MustParse("1")
- limits[v1.ResourceMemory] = resource.MustParse("400Mi")
- pod := newTestPodForQuota(f, podName, requests, limits)
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with not terminating scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- usedResources[v1.ResourceRequestsCPU] = requests[v1.ResourceCPU]
- usedResources[v1.ResourceRequestsMemory] = requests[v1.ResourceMemory]
- usedResources[v1.ResourceLimitsCPU] = limits[v1.ResourceCPU]
- usedResources[v1.ResourceLimitsMemory] = limits[v1.ResourceMemory]
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with terminating scope ignored the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(podName, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a terminating pod")
- podName = "terminating-pod"
- pod = newTestPodForQuota(f, podName, requests, limits)
- activeDeadlineSeconds := int64(3600)
- pod.Spec.ActiveDeadlineSeconds = &activeDeadlineSeconds
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with terminating scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- usedResources[v1.ResourceRequestsCPU] = requests[v1.ResourceCPU]
- usedResources[v1.ResourceRequestsMemory] = requests[v1.ResourceMemory]
- usedResources[v1.ResourceLimitsCPU] = limits[v1.ResourceCPU]
- usedResources[v1.ResourceLimitsMemory] = limits[v1.ResourceMemory]
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with not terminating scope ignored the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(podName, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota with best effort scope.", func() {
- ginkgo.By("Creating a ResourceQuota with best effort scope")
- resourceQuotaBestEffort, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScope("quota-besteffort", v1.ResourceQuotaScopeBestEffort))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota with not best effort scope")
- resourceQuotaNotBestEffort, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScope("quota-not-besteffort", v1.ResourceQuotaScopeNotBestEffort))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a best-effort pod")
- pod := newTestPodForQuota(f, podName, v1.ResourceList{}, v1.ResourceList{})
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with best effort scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with not best effort ignored the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a not best-effort pod")
- requests := v1.ResourceList{}
- requests[v1.ResourceCPU] = resource.MustParse("500m")
- requests[v1.ResourceMemory] = resource.MustParse("200Mi")
- limits := v1.ResourceList{}
- limits[v1.ResourceCPU] = resource.MustParse("1")
- limits[v1.ResourceMemory] = resource.MustParse("400Mi")
- pod = newTestPodForQuota(f, "burstable-pod", requests, limits)
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with not best effort scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with best effort scope ignored the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("Should be able to update and delete ResourceQuota.", func() {
- client := f.ClientSet
- ns := f.Namespace.Name
- ginkgo.By("Creating a ResourceQuota")
- quotaName := "test-quota"
- resourceQuota := &v1.ResourceQuota{
- Spec: v1.ResourceQuotaSpec{
- Hard: v1.ResourceList{},
- },
- }
- resourceQuota.ObjectMeta.Name = quotaName
- resourceQuota.Spec.Hard[v1.ResourceCPU] = resource.MustParse("1")
- resourceQuota.Spec.Hard[v1.ResourceMemory] = resource.MustParse("500Mi")
- _, err := createResourceQuota(client, ns, resourceQuota)
- framework.ExpectNoError(err)
- ginkgo.By("Getting a ResourceQuota")
- resourceQuotaResult, err := client.CoreV1().ResourceQuotas(ns).Get(quotaName, metav1.GetOptions{})
- framework.ExpectNoError(err)
- gomega.Expect(resourceQuotaResult.Spec.Hard[v1.ResourceCPU]).To(gomega.Equal(resource.MustParse("1")))
- gomega.Expect(resourceQuotaResult.Spec.Hard[v1.ResourceMemory]).To(gomega.Equal(resource.MustParse("500Mi")))
- ginkgo.By("Updating a ResourceQuota")
- resourceQuota.Spec.Hard[v1.ResourceCPU] = resource.MustParse("2")
- resourceQuota.Spec.Hard[v1.ResourceMemory] = resource.MustParse("1Gi")
- resourceQuotaResult, err = client.CoreV1().ResourceQuotas(ns).Update(resourceQuota)
- framework.ExpectNoError(err)
- gomega.Expect(resourceQuotaResult.Spec.Hard[v1.ResourceCPU]).To(gomega.Equal(resource.MustParse("2")))
- gomega.Expect(resourceQuotaResult.Spec.Hard[v1.ResourceMemory]).To(gomega.Equal(resource.MustParse("1Gi")))
- ginkgo.By("Verifying a ResourceQuota was modified")
- resourceQuotaResult, err = client.CoreV1().ResourceQuotas(ns).Get(quotaName, metav1.GetOptions{})
- framework.ExpectNoError(err)
- gomega.Expect(resourceQuotaResult.Spec.Hard[v1.ResourceCPU]).To(gomega.Equal(resource.MustParse("2")))
- gomega.Expect(resourceQuotaResult.Spec.Hard[v1.ResourceMemory]).To(gomega.Equal(resource.MustParse("1Gi")))
- ginkgo.By("Deleting a ResourceQuota")
- err = deleteResourceQuota(client, ns, quotaName)
- framework.ExpectNoError(err)
- ginkgo.By("Verifying the deleted ResourceQuota")
- _, err = client.CoreV1().ResourceQuotas(ns).Get(quotaName, metav1.GetOptions{})
- gomega.Expect(errors.IsNotFound(err)).To(gomega.Equal(true))
- })
- })
- var _ = SIGDescribe("ResourceQuota [Feature:ScopeSelectors]", func() {
- f := framework.NewDefaultFramework("scope-selectors")
- ginkgo.It("should verify ResourceQuota with best effort scope using scope-selectors.", func() {
- ginkgo.By("Creating a ResourceQuota with best effort scope")
- resourceQuotaBestEffort, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeSelector("quota-besteffort", v1.ResourceQuotaScopeBestEffort))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota with not best effort scope")
- resourceQuotaNotBestEffort, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeSelector("quota-not-besteffort", v1.ResourceQuotaScopeNotBestEffort))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a best-effort pod")
- pod := newTestPodForQuota(f, podName, v1.ResourceList{}, v1.ResourceList{})
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with best effort scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with not best effort ignored the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a not best-effort pod")
- requests := v1.ResourceList{}
- requests[v1.ResourceCPU] = resource.MustParse("500m")
- requests[v1.ResourceMemory] = resource.MustParse("200Mi")
- limits := v1.ResourceList{}
- limits[v1.ResourceCPU] = resource.MustParse("1")
- limits[v1.ResourceMemory] = resource.MustParse("400Mi")
- pod = newTestPodForQuota(f, "burstable-pod", requests, limits)
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with not best effort scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with best effort scope ignored the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotBestEffort.Name, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota with terminating scopes through scope selectors.", func() {
- ginkgo.By("Creating a ResourceQuota with terminating scope")
- quotaTerminatingName := "quota-terminating"
- resourceQuotaTerminating, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeSelector(quotaTerminatingName, v1.ResourceQuotaScopeTerminating))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a ResourceQuota with not terminating scope")
- quotaNotTerminatingName := "quota-not-terminating"
- resourceQuotaNotTerminating, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeSelector(quotaNotTerminatingName, v1.ResourceQuotaScopeNotTerminating))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a long running pod")
- podName := "test-pod"
- requests := v1.ResourceList{}
- requests[v1.ResourceCPU] = resource.MustParse("500m")
- requests[v1.ResourceMemory] = resource.MustParse("200Mi")
- limits := v1.ResourceList{}
- limits[v1.ResourceCPU] = resource.MustParse("1")
- limits[v1.ResourceMemory] = resource.MustParse("400Mi")
- pod := newTestPodForQuota(f, podName, requests, limits)
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with not terminating scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- usedResources[v1.ResourceRequestsCPU] = requests[v1.ResourceCPU]
- usedResources[v1.ResourceRequestsMemory] = requests[v1.ResourceMemory]
- usedResources[v1.ResourceLimitsCPU] = limits[v1.ResourceCPU]
- usedResources[v1.ResourceLimitsMemory] = limits[v1.ResourceMemory]
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with terminating scope ignored the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(podName, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a terminating pod")
- podName = "terminating-pod"
- pod = newTestPodForQuota(f, podName, requests, limits)
- activeDeadlineSeconds := int64(3600)
- pod.Spec.ActiveDeadlineSeconds = &activeDeadlineSeconds
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with terminating scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- usedResources[v1.ResourceRequestsCPU] = requests[v1.ResourceCPU]
- usedResources[v1.ResourceRequestsMemory] = requests[v1.ResourceMemory]
- usedResources[v1.ResourceLimitsCPU] = limits[v1.ResourceCPU]
- usedResources[v1.ResourceLimitsMemory] = limits[v1.ResourceMemory]
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with not terminating scope ignored the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaNotTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(podName, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaTerminating.Name, usedResources)
- framework.ExpectNoError(err)
- })
- })
- var _ = SIGDescribe("ResourceQuota [Feature:PodPriority]", func() {
- f := framework.NewDefaultFramework("resourcequota-priorityclass")
- ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with same priority class.", func() {
- _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(&schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass1"}, Value: int32(1000)})
- gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.Equal(true))
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("1")
- ginkgo.By("Creating a ResourceQuota with priority class scope")
- resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass1"}))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a pod with priority class")
- podName := "testpod-pclass1"
- pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass1")
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against 2 pods with same priority class.", func() {
- _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(&schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass2"}, Value: int32(1000)})
- gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.Equal(true))
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("1")
- ginkgo.By("Creating a ResourceQuota with priority class scope")
- resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass2"}))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating first pod with priority class should pass")
- podName := "testpod-pclass2-1"
- pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass2")
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating 2nd pod with priority class should fail")
- podName2 := "testpod-pclass2-2"
- pod2 := newTestPodForQuotaWithPriority(f, podName2, v1.ResourceList{}, v1.ResourceList{}, "pclass2")
- pod2, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod2)
- framework.ExpectError(err)
- ginkgo.By("Deleting first pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against 2 pods with different priority class.", func() {
- _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(&schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass3"}, Value: int32(1000)})
- gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.Equal(true))
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("1")
- ginkgo.By("Creating a ResourceQuota with priority class scope")
- resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass4"}))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a pod with priority class with pclass3")
- podName := "testpod-pclass3-1"
- pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass3")
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class scope remains same")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a 2nd pod with priority class pclass3")
- podName2 := "testpod-pclass2-2"
- pod2 := newTestPodForQuotaWithPriority(f, podName2, v1.ResourceList{}, v1.ResourceList{}, "pclass3")
- pod2, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod2)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class scope remains same")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting both pods")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod2.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota's multiple priority class scope (quota set to pod count: 2) against 2 pods with same priority classes.", func() {
- _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(&schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass5"}, Value: int32(1000)})
- gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.Equal(true))
- _, err = f.ClientSet.SchedulingV1().PriorityClasses().Create(&schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass6"}, Value: int32(1000)})
- gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.Equal(true))
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("2")
- ginkgo.By("Creating a ResourceQuota with priority class scope")
- resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass5", "pclass6"}))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a pod with priority class pclass5")
- podName := "testpod-pclass5"
- pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass5")
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class is updated with the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating 2nd pod with priority class pclass6")
- podName2 := "testpod-pclass6"
- pod2 := newTestPodForQuotaWithPriority(f, podName2, v1.ResourceList{}, v1.ResourceList{}, "pclass6")
- pod2, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod2)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class scope is updated with the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("2")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting both pods")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod2.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with different priority class (ScopeSelectorOpNotIn).", func() {
- _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(&schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass7"}, Value: int32(1000)})
- gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.Equal(true))
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("1")
- ginkgo.By("Creating a ResourceQuota with priority class scope")
- resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpNotIn, []string{"pclass7"}))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a pod with priority class pclass7")
- podName := "testpod-pclass7"
- pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass7")
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class is not used")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with different priority class (ScopeSelectorOpExists).", func() {
- _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(&schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass8"}, Value: int32(1000)})
- gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.Equal(true))
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("1")
- ginkgo.By("Creating a ResourceQuota with priority class scope")
- resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpExists, []string{}))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a pod with priority class pclass8")
- podName := "testpod-pclass8"
- pod := newTestPodForQuotaWithPriority(f, podName, v1.ResourceList{}, v1.ResourceList{}, "pclass8")
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class is updated with the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- })
- ginkgo.It("should verify ResourceQuota's priority class scope (cpu, memory quota set) against a pod with same priority class.", func() {
- _, err := f.ClientSet.SchedulingV1().PriorityClasses().Create(&schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "pclass9"}, Value: int32(1000)})
- gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.Equal(true))
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("1")
- hard[v1.ResourceRequestsCPU] = resource.MustParse("1")
- hard[v1.ResourceRequestsMemory] = resource.MustParse("1Gi")
- hard[v1.ResourceLimitsCPU] = resource.MustParse("3")
- hard[v1.ResourceLimitsMemory] = resource.MustParse("3Gi")
- ginkgo.By("Creating a ResourceQuota with priority class scope")
- resourceQuotaPriorityClass, err := createResourceQuota(f.ClientSet, f.Namespace.Name, newTestResourceQuotaWithScopeForPriorityClass("quota-priorityclass", hard, v1.ScopeSelectorOpIn, []string{"pclass9"}))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring ResourceQuota status is calculated")
- usedResources := v1.ResourceList{}
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0Gi")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0Gi")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Creating a pod with priority class")
- podName := "testpod-pclass9"
- request := v1.ResourceList{}
- request[v1.ResourceCPU] = resource.MustParse("1")
- request[v1.ResourceMemory] = resource.MustParse("1Gi")
- limit := v1.ResourceList{}
- limit[v1.ResourceCPU] = resource.MustParse("2")
- limit[v1.ResourceMemory] = resource.MustParse("2Gi")
- pod := newTestPodForQuotaWithPriority(f, podName, request, limit, "pclass9")
- pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota with priority class scope captures the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("1")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("1")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("1Gi")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("2")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("2Gi")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- ginkgo.By("Deleting the pod")
- err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(pod.Name, metav1.NewDeleteOptions(0))
- framework.ExpectNoError(err)
- ginkgo.By("Ensuring resource quota status released the pod usage")
- usedResources[v1.ResourcePods] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceRequestsMemory] = resource.MustParse("0Gi")
- usedResources[v1.ResourceLimitsCPU] = resource.MustParse("0")
- usedResources[v1.ResourceLimitsMemory] = resource.MustParse("0Gi")
- err = waitForResourceQuota(f.ClientSet, f.Namespace.Name, resourceQuotaPriorityClass.Name, usedResources)
- framework.ExpectNoError(err)
- })
- })
- // newTestResourceQuotaWithScopeSelector returns a quota that enforces default constraints for testing with scopeSelectors
- func newTestResourceQuotaWithScopeSelector(name string, scope v1.ResourceQuotaScope) *v1.ResourceQuota {
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("5")
- switch scope {
- case v1.ResourceQuotaScopeTerminating, v1.ResourceQuotaScopeNotTerminating:
- hard[v1.ResourceRequestsCPU] = resource.MustParse("1")
- hard[v1.ResourceRequestsMemory] = resource.MustParse("500Mi")
- hard[v1.ResourceLimitsCPU] = resource.MustParse("2")
- hard[v1.ResourceLimitsMemory] = resource.MustParse("1Gi")
- }
- return &v1.ResourceQuota{
- ObjectMeta: metav1.ObjectMeta{Name: name},
- Spec: v1.ResourceQuotaSpec{Hard: hard,
- ScopeSelector: &v1.ScopeSelector{
- MatchExpressions: []v1.ScopedResourceSelectorRequirement{
- {
- ScopeName: scope,
- Operator: v1.ScopeSelectorOpExists},
- },
- },
- },
- }
- }
- // newTestResourceQuotaWithScope returns a quota that enforces default constraints for testing with scopes
- func newTestResourceQuotaWithScope(name string, scope v1.ResourceQuotaScope) *v1.ResourceQuota {
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("5")
- switch scope {
- case v1.ResourceQuotaScopeTerminating, v1.ResourceQuotaScopeNotTerminating:
- hard[v1.ResourceRequestsCPU] = resource.MustParse("1")
- hard[v1.ResourceRequestsMemory] = resource.MustParse("500Mi")
- hard[v1.ResourceLimitsCPU] = resource.MustParse("2")
- hard[v1.ResourceLimitsMemory] = resource.MustParse("1Gi")
- }
- return &v1.ResourceQuota{
- ObjectMeta: metav1.ObjectMeta{Name: name},
- Spec: v1.ResourceQuotaSpec{Hard: hard, Scopes: []v1.ResourceQuotaScope{scope}},
- }
- }
- // newTestResourceQuotaWithScopeForPriorityClass returns a quota
- // that enforces default constraints for testing with ResourceQuotaScopePriorityClass scope
- func newTestResourceQuotaWithScopeForPriorityClass(name string, hard v1.ResourceList, op v1.ScopeSelectorOperator, values []string) *v1.ResourceQuota {
- return &v1.ResourceQuota{
- ObjectMeta: metav1.ObjectMeta{Name: name},
- Spec: v1.ResourceQuotaSpec{Hard: hard,
- ScopeSelector: &v1.ScopeSelector{
- MatchExpressions: []v1.ScopedResourceSelectorRequirement{
- {
- ScopeName: v1.ResourceQuotaScopePriorityClass,
- Operator: op,
- Values: values,
- },
- },
- },
- },
- }
- }
- // newTestResourceQuota returns a quota that enforces default constraints for testing
- func newTestResourceQuota(name string) *v1.ResourceQuota {
- hard := v1.ResourceList{}
- hard[v1.ResourcePods] = resource.MustParse("5")
- hard[v1.ResourceServices] = resource.MustParse("10")
- hard[v1.ResourceServicesNodePorts] = resource.MustParse("1")
- hard[v1.ResourceServicesLoadBalancers] = resource.MustParse("1")
- hard[v1.ResourceReplicationControllers] = resource.MustParse("10")
- hard[v1.ResourceQuotas] = resource.MustParse("1")
- hard[v1.ResourceCPU] = resource.MustParse("1")
- hard[v1.ResourceMemory] = resource.MustParse("500Mi")
- hard[v1.ResourceConfigMaps] = resource.MustParse("2")
- hard[v1.ResourceSecrets] = resource.MustParse("10")
- hard[v1.ResourcePersistentVolumeClaims] = resource.MustParse("10")
- hard[v1.ResourceRequestsStorage] = resource.MustParse("10Gi")
- hard[v1.ResourceEphemeralStorage] = resource.MustParse("50Gi")
- hard[core.V1ResourceByStorageClass(classGold, v1.ResourcePersistentVolumeClaims)] = resource.MustParse("10")
- hard[core.V1ResourceByStorageClass(classGold, v1.ResourceRequestsStorage)] = resource.MustParse("10Gi")
- // test quota on discovered resource type
- hard[v1.ResourceName("count/replicasets.apps")] = resource.MustParse("5")
- // test quota on extended resource
- hard[v1.ResourceName(v1.DefaultResourceRequestsPrefix+extendedResourceName)] = resource.MustParse("3")
- return &v1.ResourceQuota{
- ObjectMeta: metav1.ObjectMeta{Name: name},
- Spec: v1.ResourceQuotaSpec{Hard: hard},
- }
- }
- // newTestPodForQuota returns a pod that has the specified requests and limits
- func newTestPodForQuota(f *framework.Framework, name string, requests v1.ResourceList, limits v1.ResourceList) *v1.Pod {
- return &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "pause",
- Image: imageutils.GetPauseImageName(),
- Resources: v1.ResourceRequirements{
- Requests: requests,
- Limits: limits,
- },
- },
- },
- },
- }
- }
- // newTestPodForQuotaWithPriority returns a pod that has the specified requests, limits and priority class
- func newTestPodForQuotaWithPriority(f *framework.Framework, name string, requests v1.ResourceList, limits v1.ResourceList, pclass string) *v1.Pod {
- return &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: "pause",
- Image: imageutils.GetPauseImageName(),
- Resources: v1.ResourceRequirements{
- Requests: requests,
- Limits: limits,
- },
- },
- },
- PriorityClassName: pclass,
- },
- }
- }
- // newTestPersistentVolumeClaimForQuota returns a simple persistent volume claim
- func newTestPersistentVolumeClaimForQuota(name string) *v1.PersistentVolumeClaim {
- return &v1.PersistentVolumeClaim{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- Spec: v1.PersistentVolumeClaimSpec{
- AccessModes: []v1.PersistentVolumeAccessMode{
- v1.ReadWriteOnce,
- v1.ReadOnlyMany,
- v1.ReadWriteMany,
- },
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName(v1.ResourceStorage): resource.MustParse("1Gi"),
- },
- },
- },
- }
- }
- // newTestReplicationControllerForQuota returns a simple replication controller
- func newTestReplicationControllerForQuota(name, image string, replicas int32) *v1.ReplicationController {
- return &v1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- Spec: v1.ReplicationControllerSpec{
- Replicas: func(i int32) *int32 { return &i }(replicas),
- Selector: map[string]string{
- "name": name,
- },
- Template: &v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"name": name},
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Name: name,
- Image: image,
- },
- },
- },
- },
- },
- }
- }
- // newTestReplicaSetForQuota returns a simple replica set
- func newTestReplicaSetForQuota(name, image string, replicas int32) *appsv1.ReplicaSet {
- zero := int64(0)
- return &appsv1.ReplicaSet{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- Spec: appsv1.ReplicaSetSpec{
- Replicas: &replicas,
- Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"name": name}},
- Template: v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{"name": name},
- },
- Spec: v1.PodSpec{
- TerminationGracePeriodSeconds: &zero,
- Containers: []v1.Container{
- {
- Name: name,
- Image: image,
- },
- },
- },
- },
- },
- }
- }
- // newTestServiceForQuota returns a simple service
- func newTestServiceForQuota(name string, serviceType v1.ServiceType) *v1.Service {
- return &v1.Service{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- Spec: v1.ServiceSpec{
- Type: serviceType,
- Ports: []v1.ServicePort{{
- Port: 80,
- TargetPort: intstr.FromInt(80),
- }},
- },
- }
- }
- func newTestConfigMapForQuota(name string) *v1.ConfigMap {
- return &v1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- Data: map[string]string{
- "a": "b",
- },
- }
- }
- func newTestSecretForQuota(name string) *v1.Secret {
- return &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- Data: map[string][]byte{
- "data-1": []byte("value-1\n"),
- "data-2": []byte("value-2\n"),
- "data-3": []byte("value-3\n"),
- },
- }
- }
- // createResourceQuota in the specified namespace
- func createResourceQuota(c clientset.Interface, namespace string, resourceQuota *v1.ResourceQuota) (*v1.ResourceQuota, error) {
- return c.CoreV1().ResourceQuotas(namespace).Create(resourceQuota)
- }
- // deleteResourceQuota with the specified name
- func deleteResourceQuota(c clientset.Interface, namespace, name string) error {
- return c.CoreV1().ResourceQuotas(namespace).Delete(name, nil)
- }
- // countResourceQuota counts the number of ResourceQuota in the specified namespace
- func countResourceQuota(c clientset.Interface, namespace string) (int, error) {
- found, unchanged := 0, 0
- return found, wait.Poll(1*time.Second, 30*time.Second, func() (bool, error) {
- resourceQuotas, err := c.CoreV1().ResourceQuotas(namespace).List(metav1.ListOptions{})
- framework.ExpectNoError(err)
- if len(resourceQuotas.Items) == found {
- // loop until the number of resource quotas has stabilized for 5 seconds
- unchanged++
- return unchanged > 4, nil
- }
- unchanged = 0
- found = len(resourceQuotas.Items)
- return false, nil
- })
- }
- // wait for resource quota status to show the expected used resources value
- func waitForResourceQuota(c clientset.Interface, ns, quotaName string, used v1.ResourceList) error {
- return wait.Poll(framework.Poll, resourceQuotaTimeout, func() (bool, error) {
- resourceQuota, err := c.CoreV1().ResourceQuotas(ns).Get(quotaName, metav1.GetOptions{})
- if err != nil {
- return false, err
- }
- // used may not yet be calculated
- if resourceQuota.Status.Used == nil {
- return false, nil
- }
- // verify that the quota shows the expected used resource values
- for k, v := range used {
- if actualValue, found := resourceQuota.Status.Used[k]; !found || (actualValue.Cmp(v) != 0) {
- e2elog.Logf("resource %s, expected %s, actual %s", k, v.String(), actualValue.String())
- return false, nil
- }
- }
- return true, nil
- })
- }
- // updateResourceQuotaUntilUsageAppears updates the resource quota object until the usage is populated
- // for the specific resource name.
- func updateResourceQuotaUntilUsageAppears(c clientset.Interface, ns, quotaName string, resourceName v1.ResourceName) error {
- return wait.Poll(framework.Poll, 1*time.Minute, func() (bool, error) {
- resourceQuota, err := c.CoreV1().ResourceQuotas(ns).Get(quotaName, metav1.GetOptions{})
- if err != nil {
- return false, err
- }
- // verify that the quota shows the expected used resource values
- _, ok := resourceQuota.Status.Used[resourceName]
- if ok {
- return true, nil
- }
- current := resourceQuota.Spec.Hard[resourceName]
- current.Add(resource.MustParse("1"))
- resourceQuota.Spec.Hard[resourceName] = current
- _, err = c.CoreV1().ResourceQuotas(ns).Update(resourceQuota)
- // ignoring conflicts since someone else may already updated it.
- if errors.IsConflict(err) {
- return false, nil
- }
- return false, err
- })
- }
|