2016-02-22 16:16:21 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2015 The Kubernetes Authors .
2016-02-22 16:16:21 +00:00
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 .
* /
2018-11-16 18:59:00 +00:00
package apimachinery
2016-02-22 16:16:21 +00:00
import (
2016-02-29 23:52:35 +00:00
"fmt"
2019-01-28 23:50:16 +00:00
"strconv"
2016-02-22 16:16:21 +00:00
"time"
2018-12-19 16:18:53 +00:00
appsv1 "k8s.io/api/apps/v1"
2017-06-22 18:24:23 +00:00
"k8s.io/api/core/v1"
2019-02-20 22:19:17 +00:00
schedulingv1 "k8s.io/api/scheduling/v1"
2017-08-29 01:29:40 +00:00
"k8s.io/apimachinery/pkg/api/errors"
2017-01-25 13:13:07 +00:00
"k8s.io/apimachinery/pkg/api/resource"
2017-01-11 14:09:48 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-01-27 20:42:17 +00:00
"k8s.io/apimachinery/pkg/util/intstr"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/util/wait"
2017-06-23 20:56:37 +00:00
clientset "k8s.io/client-go/kubernetes"
2018-08-27 13:50:30 +00:00
"k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
2016-04-07 17:21:31 +00:00
"k8s.io/kubernetes/test/e2e/framework"
2018-01-09 06:42:02 +00:00
imageutils "k8s.io/kubernetes/test/utils/image"
2016-02-22 16:16:21 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const (
// how long to wait for a resource quota update to occur
resourceQuotaTimeout = 30 * time . Second
2018-11-16 18:59:00 +00:00
podName = "pfpod"
2016-02-22 16:16:21 +00:00
)
2017-03-02 09:23:57 +00:00
var classGold string = "gold"
2018-01-30 01:29:29 +00:00
var extendedResourceName string = "example.com/dongle"
2017-03-02 09:23:57 +00:00
2017-10-20 04:38:55 +00:00
var _ = SIGDescribe ( "ResourceQuota" , func ( ) {
2016-04-07 17:21:31 +00:00
f := framework . NewDefaultFramework ( "resourcequota" )
2016-02-22 16:16:21 +00:00
It ( "should create a ResourceQuota and ensure its status is promptly calculated." , func ( ) {
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-02-22 16:16:21 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should create a ResourceQuota and capture the life of a service." , func ( ) {
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-02-22 16:16:21 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a Service" )
2016-11-18 20:55:17 +00:00
service := newTestServiceForQuota ( "test-service" , v1 . ServiceTypeClusterIP )
2017-10-25 15:54:32 +00:00
service , err = f . ClientSet . CoreV1 ( ) . Services ( f . Namespace . Name ) . Create ( service )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status captures service creation" )
2016-11-18 20:55:17 +00:00
usedResources = v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourceServices ] = resource . MustParse ( "1" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting a Service" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . Services ( f . Namespace . Name ) . Delete ( service . Name , nil )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourceServices ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2016-02-29 22:06:32 +00:00
It ( "should create a ResourceQuota and capture the life of a secret." , func ( ) {
2016-02-29 23:52:35 +00:00
By ( "Discovering how many secrets are in namespace by default" )
2017-01-23 03:30:35 +00:00
found , unchanged := 0 , 0
wait . Poll ( 1 * time . Second , 30 * time . Second , func ( ) ( bool , error ) {
2017-10-25 15:54:32 +00:00
secrets , err := f . ClientSet . CoreV1 ( ) . Secrets ( f . Namespace . Name ) . List ( metav1 . ListOptions { } )
2017-01-23 03:30:35 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
2016-02-29 23:52:35 +00:00
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-02-29 22:06:32 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2016-11-18 20:55:17 +00:00
resourceQuota . Spec . Hard [ v1 . ResourceSecrets ] = resource . MustParse ( hardSecrets )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2016-02-29 22:06:32 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourceSecrets ] = resource . MustParse ( defaultSecrets )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-29 22:06:32 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a Secret" )
secret := newTestSecretForQuota ( "test-secret" )
2017-10-25 15:54:32 +00:00
secret , err = f . ClientSet . CoreV1 ( ) . Secrets ( f . Namespace . Name ) . Create ( secret )
2016-02-29 22:06:32 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status captures secret creation" )
2016-11-18 20:55:17 +00:00
usedResources = v1 . ResourceList { }
usedResources [ v1 . ResourceSecrets ] = resource . MustParse ( hardSecrets )
2016-02-29 22:06:32 +00:00
// we expect there to be two secrets because each namespace will receive
// a service account token secret by default
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-29 22:06:32 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting a secret" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . Secrets ( f . Namespace . Name ) . Delete ( secret . Name , nil )
2016-02-29 22:06:32 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourceSecrets ] = resource . MustParse ( defaultSecrets )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-29 22:06:32 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2016-02-22 16:16:21 +00:00
It ( "should create a ResourceQuota and capture the life of a pod." , func ( ) {
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-02-22 16:16:21 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a Pod that fits quota" )
podName := "test-pod"
2016-11-18 20:55:17 +00:00
requests := v1 . ResourceList { }
2018-01-30 01:29:29 +00:00
limits := v1 . ResourceList { }
2016-11-18 20:55:17 +00:00
requests [ v1 . ResourceCPU ] = resource . MustParse ( "500m" )
requests [ v1 . ResourceMemory ] = resource . MustParse ( "252Mi" )
2018-02-02 20:04:45 +00:00
requests [ v1 . ResourceEphemeralStorage ] = resource . MustParse ( "30Gi" )
2018-01-30 01:29:29 +00:00
requests [ v1 . ResourceName ( extendedResourceName ) ] = resource . MustParse ( "2" )
limits [ v1 . ResourceName ( extendedResourceName ) ] = resource . MustParse ( "2" )
pod := newTestPodForQuota ( f , podName , requests , limits )
2017-10-25 15:54:32 +00:00
pod , err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Create ( pod )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-04-20 03:05:59 +00:00
podToUpdate := pod
2016-02-22 16:16:21 +00:00
By ( "Ensuring ResourceQuota status captures the pod usage" )
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "1" )
usedResources [ v1 . ResourceCPU ] = requests [ v1 . ResourceCPU ]
usedResources [ v1 . ResourceMemory ] = requests [ v1 . ResourceMemory ]
2018-02-02 20:04:45 +00:00
usedResources [ v1 . ResourceEphemeralStorage ] = requests [ v1 . ResourceEphemeralStorage ]
2018-01-30 01:29:29 +00:00
usedResources [ v1 . ResourceName ( v1 . DefaultResourceRequestsPrefix + extendedResourceName ) ] = requests [ v1 . ResourceName ( extendedResourceName ) ]
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Not allowing a pod to be created that exceeds remaining quota" )
2016-11-18 20:55:17 +00:00
requests = v1 . ResourceList { }
requests [ v1 . ResourceCPU ] = resource . MustParse ( "600m" )
requests [ v1 . ResourceMemory ] = resource . MustParse ( "100Mi" )
pod = newTestPodForQuota ( f , "fail-pod" , requests , v1 . ResourceList { } )
2017-10-25 15:54:32 +00:00
pod , err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Create ( pod )
2016-02-22 16:16:21 +00:00
Expect ( err ) . To ( HaveOccurred ( ) )
2018-01-30 01:29:29 +00:00
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" )
2018-02-02 20:04:45 +00:00
requests [ v1 . ResourceEphemeralStorage ] = resource . MustParse ( "30Gi" )
2018-01-30 01:29:29 +00:00
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 )
Expect ( err ) . To ( HaveOccurred ( ) )
2016-04-20 03:05:59 +00:00
By ( "Ensuring a pod cannot update its resource requirements" )
// a pod cannot dynamically update its resource requirements.
2016-11-18 20:55:17 +00:00
requests = v1 . ResourceList { }
requests [ v1 . ResourceCPU ] = resource . MustParse ( "100m" )
requests [ v1 . ResourceMemory ] = resource . MustParse ( "100Mi" )
2018-02-02 20:04:45 +00:00
requests [ v1 . ResourceEphemeralStorage ] = resource . MustParse ( "10Gi" )
2016-04-20 03:05:59 +00:00
podToUpdate . Spec . Containers [ 0 ] . Resources . Requests = requests
2017-10-25 15:54:32 +00:00
_ , err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Update ( podToUpdate )
2016-04-20 03:05:59 +00:00
Expect ( err ) . To ( HaveOccurred ( ) )
By ( "Ensuring attempts to update pod resource requirements did not change quota usage" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-04-20 03:05:59 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-02-22 16:16:21 +00:00
By ( "Deleting the pod" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( podName , metav1 . NewDeleteOptions ( 0 ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released the pod usage" )
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "0" )
usedResources [ v1 . ResourceCPU ] = resource . MustParse ( "0" )
usedResources [ v1 . ResourceMemory ] = resource . MustParse ( "0" )
2018-02-02 20:04:45 +00:00
usedResources [ v1 . ResourceEphemeralStorage ] = resource . MustParse ( "0" )
2018-01-30 01:29:29 +00:00
usedResources [ v1 . ResourceName ( v1 . DefaultResourceRequestsPrefix + extendedResourceName ) ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2016-02-29 17:02:05 +00:00
It ( "should create a ResourceQuota and capture the life of a configMap." , func ( ) {
2018-10-18 06:41:53 +00:00
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 { } )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-02-29 17:02:05 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2016-02-29 17:02:05 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2018-10-18 06:41:53 +00:00
usedResources [ v1 . ResourceConfigMaps ] = resource . MustParse ( defaultConfigMaps )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-29 17:02:05 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a ConfigMap" )
configMap := newTestConfigMapForQuota ( "test-configmap" )
2017-10-25 15:54:32 +00:00
configMap , err = f . ClientSet . CoreV1 ( ) . ConfigMaps ( f . Namespace . Name ) . Create ( configMap )
2016-02-29 17:02:05 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status captures configMap creation" )
2016-11-18 20:55:17 +00:00
usedResources = v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2018-10-18 06:41:53 +00:00
// 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 )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-29 17:02:05 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting a ConfigMap" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . ConfigMaps ( f . Namespace . Name ) . Delete ( configMap . Name , nil )
2016-02-29 17:02:05 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released usage" )
2018-10-18 06:41:53 +00:00
usedResources [ v1 . ResourceConfigMaps ] = resource . MustParse ( defaultConfigMaps )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-02-29 17:02:05 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2016-03-04 17:06:57 +00:00
It ( "should create a ResourceQuota and capture the life of a replication controller." , func ( ) {
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-03-04 17:06:57 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourceReplicationControllers ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a ReplicationController" )
replicationController := newTestReplicationControllerForQuota ( "test-rc" , "nginx" , 0 )
2017-10-25 15:54:32 +00:00
replicationController , err = f . ClientSet . CoreV1 ( ) . ReplicationControllers ( f . Namespace . Name ) . Create ( replicationController )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status captures replication controller creation" )
2016-11-18 20:55:17 +00:00
usedResources = v1 . ResourceList { }
usedResources [ v1 . ResourceReplicationControllers ] = resource . MustParse ( "1" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting a ReplicationController" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . ReplicationControllers ( f . Namespace . Name ) . Delete ( replicationController . Name , nil )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourceReplicationControllers ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2017-10-27 15:08:30 +00:00
It ( "should create a ResourceQuota and capture the life of a replica set." , func ( ) {
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2017-10-27 15:08:30 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2017-10-27 15:08:30 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2018-12-19 16:18:53 +00:00
usedResources [ v1 . ResourceName ( "count/replicasets.apps" ) ] = resource . MustParse ( "0" )
2017-10-27 15:08:30 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a ReplicaSet" )
replicaSet := newTestReplicaSetForQuota ( "test-rs" , "nginx" , 0 )
2018-12-19 16:18:53 +00:00
replicaSet , err = f . ClientSet . AppsV1 ( ) . ReplicaSets ( f . Namespace . Name ) . Create ( replicaSet )
2017-10-27 15:08:30 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status captures replicaset creation" )
usedResources = v1 . ResourceList { }
2018-12-19 16:18:53 +00:00
usedResources [ v1 . ResourceName ( "count/replicasets.apps" ) ] = resource . MustParse ( "1" )
2017-10-27 15:08:30 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting a ReplicaSet" )
2018-12-19 16:18:53 +00:00
err = f . ClientSet . AppsV1 ( ) . ReplicaSets ( f . Namespace . Name ) . Delete ( replicaSet . Name , nil )
2017-10-27 15:08:30 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released usage" )
2018-12-19 16:18:53 +00:00
usedResources [ v1 . ResourceName ( "count/replicasets.apps" ) ] = resource . MustParse ( "0" )
2017-10-27 15:08:30 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2017-07-12 01:49:25 +00:00
It ( "should create a ResourceQuota and capture the life of a persistent volume claim. [sig-storage]" , func ( ) {
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-03-04 17:06:57 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePersistentVolumeClaims ] = resource . MustParse ( "0" )
usedResources [ v1 . ResourceRequestsStorage ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a PersistentVolumeClaim" )
pvc := newTestPersistentVolumeClaimForQuota ( "test-claim" )
2017-10-25 15:54:32 +00:00
pvc , err = f . ClientSet . CoreV1 ( ) . PersistentVolumeClaims ( f . Namespace . Name ) . Create ( pvc )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-12-07 20:44:16 +00:00
By ( "Ensuring resource quota status captures persistent volume claim creation" )
2016-11-18 20:55:17 +00:00
usedResources = v1 . ResourceList { }
usedResources [ v1 . ResourcePersistentVolumeClaims ] = resource . MustParse ( "1" )
usedResources [ v1 . ResourceRequestsStorage ] = resource . MustParse ( "1Gi" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting a PersistentVolumeClaim" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . PersistentVolumeClaims ( f . Namespace . Name ) . Delete ( pvc . Name , nil )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePersistentVolumeClaims ] = resource . MustParse ( "0" )
usedResources [ v1 . ResourceRequestsStorage ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
2016-03-04 17:06:57 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2017-07-12 01:49:25 +00:00
It ( "should create a ResourceQuota and capture the life of a persistent volume claim with a storage class. [sig-storage]" , func ( ) {
2019-01-28 23:50:16 +00:00
By ( "Counting existing ResourceQuota" )
c , err := countResourceQuota ( f . ClientSet , f . Namespace . Name )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2016-12-07 20:44:16 +00:00
By ( "Creating a ResourceQuota" )
quotaName := "test-quota"
resourceQuota := newTestResourceQuota ( quotaName )
2019-01-28 23:50:16 +00:00
resourceQuota , err = createResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuota )
2016-12-07 20:44:16 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status is calculated" )
usedResources := v1 . ResourceList { }
2019-01-28 23:50:16 +00:00
usedResources [ v1 . ResourceQuotas ] = resource . MustParse ( strconv . Itoa ( c + 1 ) )
2016-12-07 20:44:16 +00:00
usedResources [ v1 . ResourcePersistentVolumeClaims ] = resource . MustParse ( "0" )
usedResources [ v1 . ResourceRequestsStorage ] = resource . MustParse ( "0" )
2017-03-02 09:23:57 +00:00
usedResources [ core . V1ResourceByStorageClass ( classGold , v1 . ResourcePersistentVolumeClaims ) ] = resource . MustParse ( "0" )
usedResources [ core . V1ResourceByStorageClass ( classGold , v1 . ResourceRequestsStorage ) ] = resource . MustParse ( "0" )
2016-12-07 20:44:16 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a PersistentVolumeClaim with storage class" )
pvc := newTestPersistentVolumeClaimForQuota ( "test-claim" )
2017-03-02 09:23:57 +00:00
pvc . Spec . StorageClassName = & classGold
2017-10-25 15:54:32 +00:00
pvc , err = f . ClientSet . CoreV1 ( ) . PersistentVolumeClaims ( f . Namespace . Name ) . Create ( pvc )
2016-12-07 20:44:16 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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" )
2017-03-02 09:23:57 +00:00
usedResources [ core . V1ResourceByStorageClass ( classGold , v1 . ResourcePersistentVolumeClaims ) ] = resource . MustParse ( "1" )
usedResources [ core . V1ResourceByStorageClass ( classGold , v1 . ResourceRequestsStorage ) ] = resource . MustParse ( "1Gi" )
2016-12-07 20:44:16 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting a PersistentVolumeClaim" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . PersistentVolumeClaims ( f . Namespace . Name ) . Delete ( pvc . Name , nil )
2016-12-07 20:44:16 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released usage" )
usedResources [ v1 . ResourcePersistentVolumeClaims ] = resource . MustParse ( "0" )
usedResources [ v1 . ResourceRequestsStorage ] = resource . MustParse ( "0" )
2017-03-02 09:23:57 +00:00
usedResources [ core . V1ResourceByStorageClass ( classGold , v1 . ResourcePersistentVolumeClaims ) ] = resource . MustParse ( "0" )
usedResources [ core . V1ResourceByStorageClass ( classGold , v1 . ResourceRequestsStorage ) ] = resource . MustParse ( "0" )
2016-12-07 20:44:16 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , quotaName , usedResources )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2016-02-22 16:16:21 +00:00
It ( "should verify ResourceQuota with terminating scopes." , func ( ) {
By ( "Creating a ResourceQuota with terminating scope" )
quotaTerminatingName := "quota-terminating"
2016-11-18 20:55:17 +00:00
resourceQuotaTerminating , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScope ( quotaTerminatingName , v1 . ResourceQuotaScopeTerminating ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring ResourceQuota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaTerminating . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a ResourceQuota with not terminating scope" )
quotaNotTerminatingName := "quota-not-terminating"
2016-11-18 20:55:17 +00:00
resourceQuotaNotTerminating , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScope ( quotaNotTerminatingName , v1 . ResourceQuotaScopeNotTerminating ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring ResourceQuota status is calculated" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotTerminating . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a long running pod" )
podName := "test-pod"
2016-11-18 20:55:17 +00:00
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" )
2016-05-26 16:16:43 +00:00
pod := newTestPodForQuota ( f , podName , requests , limits )
2017-10-25 15:54:32 +00:00
pod , err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Create ( pod )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota with not terminating scope captures the pod usage" )
2016-11-18 20:55:17 +00:00
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 ]
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotTerminating . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota with terminating scope ignored the pod usage" )
2016-11-18 20:55:17 +00:00
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" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaTerminating . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( podName , metav1 . NewDeleteOptions ( 0 ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released the pod usage" )
2016-11-18 20:55:17 +00:00
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" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotTerminating . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a terminating pod" )
podName = "terminating-pod"
2016-05-26 16:16:43 +00:00
pod = newTestPodForQuota ( f , podName , requests , limits )
2016-02-22 16:16:21 +00:00
activeDeadlineSeconds := int64 ( 3600 )
pod . Spec . ActiveDeadlineSeconds = & activeDeadlineSeconds
2017-10-25 15:54:32 +00:00
pod , err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Create ( pod )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota with terminating scope captures the pod usage" )
2016-11-18 20:55:17 +00:00
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 ]
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaTerminating . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota with not terminating scope ignored the pod usage" )
2016-11-18 20:55:17 +00:00
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" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotTerminating . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( podName , metav1 . NewDeleteOptions ( 0 ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released the pod usage" )
2016-11-18 20:55:17 +00:00
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" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaTerminating . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should verify ResourceQuota with best effort scope." , func ( ) {
By ( "Creating a ResourceQuota with best effort scope" )
2016-11-18 20:55:17 +00:00
resourceQuotaBestEffort , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScope ( "quota-besteffort" , v1 . ResourceQuotaScopeBestEffort ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring ResourceQuota status is calculated" )
2016-11-18 20:55:17 +00:00
usedResources := v1 . ResourceList { }
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaBestEffort . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a ResourceQuota with not best effort scope" )
2016-11-18 20:55:17 +00:00
resourceQuotaNotBestEffort , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScope ( "quota-not-besteffort" , v1 . ResourceQuotaScopeNotBestEffort ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring ResourceQuota status is calculated" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotBestEffort . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a best-effort pod" )
2016-11-18 20:55:17 +00:00
pod := newTestPodForQuota ( f , podName , v1 . ResourceList { } , v1 . ResourceList { } )
2017-10-25 15:54:32 +00:00
pod , err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Create ( pod )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota with best effort scope captures the pod usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "1" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaBestEffort . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota with not best effort ignored the pod usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotBestEffort . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released the pod usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaBestEffort . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a not best-effort pod" )
2016-11-18 20:55:17 +00:00
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" )
2016-05-26 16:16:43 +00:00
pod = newTestPodForQuota ( f , "burstable-pod" , requests , limits )
2017-10-25 15:54:32 +00:00
pod , err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Create ( pod )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota with not best effort scope captures the pod usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "1" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotBestEffort . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota with best effort scope ignored the pod usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaBestEffort . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
2017-10-25 15:54:32 +00:00
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring resource quota status released the pod usage" )
2016-11-18 20:55:17 +00:00
usedResources [ v1 . ResourcePods ] = resource . MustParse ( "0" )
2016-10-18 13:00:38 +00:00
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotBestEffort . Name , usedResources )
2016-02-22 16:16:21 +00:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2019-02-26 06:26:52 +00:00
It ( "Should be able to update and delete ResourceQuota." , func ( ) {
client := f . ClientSet
ns := f . Namespace . Name
2018-06-02 00:23:37 +00:00
2019-02-26 06:26:52 +00:00
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Getting a ResourceQuota" )
resourceQuotaResult , err := client . CoreV1 ( ) . ResourceQuotas ( ns ) . Get ( quotaName , metav1 . GetOptions { } )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( resourceQuotaResult . Spec . Hard [ v1 . ResourceCPU ] ) . To ( Equal ( resource . MustParse ( "1" ) ) )
Expect ( resourceQuotaResult . Spec . Hard [ v1 . ResourceMemory ] ) . To ( Equal ( resource . MustParse ( "500Mi" ) ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( resourceQuotaResult . Spec . Hard [ v1 . ResourceCPU ] ) . To ( Equal ( resource . MustParse ( "2" ) ) )
Expect ( resourceQuotaResult . Spec . Hard [ v1 . ResourceMemory ] ) . To ( Equal ( resource . MustParse ( "1Gi" ) ) )
By ( "Verifying a ResourceQuota was modified" )
resourceQuotaResult , err = client . CoreV1 ( ) . ResourceQuotas ( ns ) . Get ( quotaName , metav1 . GetOptions { } )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( resourceQuotaResult . Spec . Hard [ v1 . ResourceCPU ] ) . To ( Equal ( resource . MustParse ( "2" ) ) )
Expect ( resourceQuotaResult . Spec . Hard [ v1 . ResourceMemory ] ) . To ( Equal ( resource . MustParse ( "1Gi" ) ) )
By ( "Deleting a ResourceQuota" )
err = deleteResourceQuota ( client , ns , quotaName )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Verifying the deleted ResourceQuota" )
_ , err = client . CoreV1 ( ) . ResourceQuotas ( ns ) . Get ( quotaName , metav1 . GetOptions { } )
Expect ( errors . IsNotFound ( err ) ) . To ( Equal ( true ) )
} )
2018-06-02 00:23:37 +00:00
} )
2018-08-02 12:20:59 +00:00
var _ = SIGDescribe ( "ResourceQuota [Feature:ScopeSelectors]" , func ( ) {
f := framework . NewDefaultFramework ( "scope-selectors" )
It ( "should verify ResourceQuota with best effort scope using scope-selectors." , func ( ) {
By ( "Creating a ResourceQuota with best effort scope" )
resourceQuotaBestEffort , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeSelector ( "quota-besteffort" , v1 . ResourceQuotaScopeBestEffort ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a ResourceQuota with not best effort scope" )
resourceQuotaNotBestEffort , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeSelector ( "quota-not-besteffort" , v1 . ResourceQuotaScopeNotBestEffort ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring ResourceQuota status is calculated" )
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotBestEffort . Name , usedResources )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should verify ResourceQuota with terminating scopes through scope selectors." , func ( ) {
By ( "Creating a ResourceQuota with terminating scope" )
quotaTerminatingName := "quota-terminating"
resourceQuotaTerminating , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeSelector ( quotaTerminatingName , v1 . ResourceQuotaScopeTerminating ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Creating a ResourceQuota with not terminating scope" )
quotaNotTerminatingName := "quota-not-terminating"
resourceQuotaNotTerminating , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeSelector ( quotaNotTerminatingName , v1 . ResourceQuotaScopeNotTerminating ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Ensuring ResourceQuota status is calculated" )
err = waitForResourceQuota ( f . ClientSet , f . Namespace . Name , resourceQuotaNotTerminating . Name , usedResources )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( podName , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( podName , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
} )
2018-06-02 00:23:37 +00:00
var _ = SIGDescribe ( "ResourceQuota [Feature:PodPriority]" , func ( ) {
f := framework . NewDefaultFramework ( "resourcequota-priorityclass" )
It ( "should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with same priority class." , func ( ) {
2019-02-20 22:19:17 +00:00
_ , err := f . ClientSet . SchedulingV1 ( ) . PriorityClasses ( ) . Create ( & schedulingv1 . PriorityClass { ObjectMeta : metav1 . ObjectMeta { Name : "pclass1" } , Value : int32 ( 1000 ) } )
2018-06-02 00:23:37 +00:00
Expect ( err == nil || errors . IsAlreadyExists ( err ) ) . To ( Equal ( true ) )
hard := v1 . ResourceList { }
hard [ v1 . ResourcePods ] = resource . MustParse ( "1" )
By ( "Creating a ResourceQuota with priority class scope" )
resourceQuotaPriorityClass , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeForPriorityClass ( "quota-priorityclass" , hard , v1 . ScopeSelectorOpIn , [ ] string { "pclass1" } ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should verify ResourceQuota's priority class scope (quota set to pod count: 1) against 2 pods with same priority class." , func ( ) {
2019-02-20 22:19:17 +00:00
_ , err := f . ClientSet . SchedulingV1 ( ) . PriorityClasses ( ) . Create ( & schedulingv1 . PriorityClass { ObjectMeta : metav1 . ObjectMeta { Name : "pclass2" } , Value : int32 ( 1000 ) } )
2018-06-02 00:23:37 +00:00
Expect ( err == nil || errors . IsAlreadyExists ( err ) ) . To ( Equal ( true ) )
hard := v1 . ResourceList { }
hard [ v1 . ResourcePods ] = resource . MustParse ( "1" )
By ( "Creating a ResourceQuota with priority class scope" )
resourceQuotaPriorityClass , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeForPriorityClass ( "quota-priorityclass" , hard , v1 . ScopeSelectorOpIn , [ ] string { "pclass2" } ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . To ( HaveOccurred ( ) )
By ( "Deleting first pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should verify ResourceQuota's priority class scope (quota set to pod count: 1) against 2 pods with different priority class." , func ( ) {
2019-02-20 22:19:17 +00:00
_ , err := f . ClientSet . SchedulingV1 ( ) . PriorityClasses ( ) . Create ( & schedulingv1 . PriorityClass { ObjectMeta : metav1 . ObjectMeta { Name : "pclass3" } , Value : int32 ( 1000 ) } )
2018-06-02 00:23:37 +00:00
Expect ( err == nil || errors . IsAlreadyExists ( err ) ) . To ( Equal ( true ) )
hard := v1 . ResourceList { }
hard [ v1 . ResourcePods ] = resource . MustParse ( "1" )
By ( "Creating a ResourceQuota with priority class scope" )
resourceQuotaPriorityClass , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeForPriorityClass ( "quota-priorityclass" , hard , v1 . ScopeSelectorOpIn , [ ] string { "pclass4" } ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting both pods" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod2 . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should verify ResourceQuota's multiple priority class scope (quota set to pod count: 2) against 2 pods with same priority classes." , func ( ) {
2019-02-20 22:19:17 +00:00
_ , err := f . ClientSet . SchedulingV1 ( ) . PriorityClasses ( ) . Create ( & schedulingv1 . PriorityClass { ObjectMeta : metav1 . ObjectMeta { Name : "pclass5" } , Value : int32 ( 1000 ) } )
2018-06-02 00:23:37 +00:00
Expect ( err == nil || errors . IsAlreadyExists ( err ) ) . To ( Equal ( true ) )
2019-02-20 22:19:17 +00:00
_ , err = f . ClientSet . SchedulingV1 ( ) . PriorityClasses ( ) . Create ( & schedulingv1 . PriorityClass { ObjectMeta : metav1 . ObjectMeta { Name : "pclass6" } , Value : int32 ( 1000 ) } )
2018-06-02 00:23:37 +00:00
Expect ( err == nil || errors . IsAlreadyExists ( err ) ) . To ( Equal ( true ) )
hard := v1 . ResourceList { }
hard [ v1 . ResourcePods ] = resource . MustParse ( "2" )
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" } ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting both pods" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod2 . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with different priority class (ScopeSelectorOpNotIn)." , func ( ) {
2019-02-20 22:19:17 +00:00
_ , err := f . ClientSet . SchedulingV1 ( ) . PriorityClasses ( ) . Create ( & schedulingv1 . PriorityClass { ObjectMeta : metav1 . ObjectMeta { Name : "pclass7" } , Value : int32 ( 1000 ) } )
2018-06-02 00:23:37 +00:00
Expect ( err == nil || errors . IsAlreadyExists ( err ) ) . To ( Equal ( true ) )
hard := v1 . ResourceList { }
hard [ v1 . ResourcePods ] = resource . MustParse ( "1" )
By ( "Creating a ResourceQuota with priority class scope" )
resourceQuotaPriorityClass , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeForPriorityClass ( "quota-priorityclass" , hard , v1 . ScopeSelectorOpNotIn , [ ] string { "pclass7" } ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should verify ResourceQuota's priority class scope (quota set to pod count: 1) against a pod with different priority class (ScopeSelectorOpExists)." , func ( ) {
2019-02-20 22:19:17 +00:00
_ , err := f . ClientSet . SchedulingV1 ( ) . PriorityClasses ( ) . Create ( & schedulingv1 . PriorityClass { ObjectMeta : metav1 . ObjectMeta { Name : "pclass8" } , Value : int32 ( 1000 ) } )
2018-06-02 00:23:37 +00:00
Expect ( err == nil || errors . IsAlreadyExists ( err ) ) . To ( Equal ( true ) )
hard := v1 . ResourceList { }
hard [ v1 . ResourcePods ] = resource . MustParse ( "1" )
By ( "Creating a ResourceQuota with priority class scope" )
resourceQuotaPriorityClass , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeForPriorityClass ( "quota-priorityclass" , hard , v1 . ScopeSelectorOpExists , [ ] string { } ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
It ( "should verify ResourceQuota's priority class scope (cpu, memory quota set) against a pod with same priority class." , func ( ) {
2019-02-20 22:19:17 +00:00
_ , err := f . ClientSet . SchedulingV1 ( ) . PriorityClasses ( ) . Create ( & schedulingv1 . PriorityClass { ObjectMeta : metav1 . ObjectMeta { Name : "pclass9" } , Value : int32 ( 1000 ) } )
2018-06-02 00:23:37 +00:00
Expect ( err == nil || errors . IsAlreadyExists ( err ) ) . To ( 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" )
By ( "Creating a ResourceQuota with priority class scope" )
resourceQuotaPriorityClass , err := createResourceQuota ( f . ClientSet , f . Namespace . Name , newTestResourceQuotaWithScopeForPriorityClass ( "quota-priorityclass" , hard , v1 . ScopeSelectorOpIn , [ ] string { "pclass9" } ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Deleting the pod" )
err = f . ClientSet . CoreV1 ( ) . Pods ( f . Namespace . Name ) . Delete ( pod . Name , metav1 . NewDeleteOptions ( 0 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2016-02-22 16:16:21 +00:00
} )
2018-08-02 12:20:59 +00:00
// 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 } ,
} ,
} ,
} ,
}
}
2016-02-22 16:16:21 +00:00
// newTestResourceQuotaWithScope returns a quota that enforces default constraints for testing with scopes
2016-11-18 20:55:17 +00:00
func newTestResourceQuotaWithScope ( name string , scope v1 . ResourceQuotaScope ) * v1 . ResourceQuota {
hard := v1 . ResourceList { }
hard [ v1 . ResourcePods ] = resource . MustParse ( "5" )
2016-02-22 16:16:21 +00:00
switch scope {
2016-11-18 20:55:17 +00:00
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" )
2016-02-22 16:16:21 +00:00
}
2016-11-18 20:55:17 +00:00
return & v1 . ResourceQuota {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : name } ,
2016-11-18 20:55:17 +00:00
Spec : v1 . ResourceQuotaSpec { Hard : hard , Scopes : [ ] v1 . ResourceQuotaScope { scope } } ,
2016-02-22 16:16:21 +00:00
}
}
2018-06-02 00:23:37 +00:00
// 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 ,
} ,
} ,
} ,
} ,
}
}
2018-02-02 20:04:45 +00:00
// newTestResourceQuotaForEphemeralStorage returns a quota that enforces default constraints for testing feature LocalStorageCapacityIsolation
2017-09-15 04:59:02 +00:00
func newTestResourceQuotaForEphemeralStorage ( name string ) * v1 . ResourceQuota {
hard := v1 . ResourceList { }
hard [ v1 . ResourceEphemeralStorage ] = resource . MustParse ( "500Mi" )
2018-02-02 20:04:45 +00:00
hard [ v1 . ResourceQuotas ] = resource . MustParse ( "1" )
2017-09-15 04:59:02 +00:00
return & v1 . ResourceQuota {
ObjectMeta : metav1 . ObjectMeta { Name : name } ,
Spec : v1 . ResourceQuotaSpec { Hard : hard } ,
}
}
2016-02-22 16:16:21 +00:00
// newTestResourceQuota returns a quota that enforces default constraints for testing
2016-11-18 20:55:17 +00:00
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" )
2018-02-02 20:04:45 +00:00
hard [ v1 . ResourceEphemeralStorage ] = resource . MustParse ( "50Gi" )
2017-03-02 09:23:57 +00:00
hard [ core . V1ResourceByStorageClass ( classGold , v1 . ResourcePersistentVolumeClaims ) ] = resource . MustParse ( "10" )
hard [ core . V1ResourceByStorageClass ( classGold , v1 . ResourceRequestsStorage ) ] = resource . MustParse ( "10Gi" )
2017-10-27 15:08:30 +00:00
// test quota on discovered resource type
2018-12-19 16:18:53 +00:00
hard [ v1 . ResourceName ( "count/replicasets.apps" ) ] = resource . MustParse ( "5" )
2018-01-30 01:29:29 +00:00
// test quota on extended resource
hard [ v1 . ResourceName ( v1 . DefaultResourceRequestsPrefix + extendedResourceName ) ] = resource . MustParse ( "3" )
2016-11-18 20:55:17 +00:00
return & v1 . ResourceQuota {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta { Name : name } ,
2016-11-18 20:55:17 +00:00
Spec : v1 . ResourceQuotaSpec { Hard : hard } ,
2016-02-22 16:16:21 +00:00
}
}
// newTestPodForQuota returns a pod that has the specified requests and limits
2016-11-18 20:55:17 +00:00
func newTestPodForQuota ( f * framework . Framework , name string , requests v1 . ResourceList , limits v1 . ResourceList ) * v1 . Pod {
return & v1 . Pod {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-02-22 16:16:21 +00:00
Name : name ,
} ,
2016-11-18 20:55:17 +00:00
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
2016-02-22 16:16:21 +00:00
{
2016-05-26 16:16:43 +00:00
Name : "pause" ,
2018-01-09 06:42:02 +00:00
Image : imageutils . GetPauseImageName ( ) ,
2016-11-18 20:55:17 +00:00
Resources : v1 . ResourceRequirements {
2016-02-22 16:16:21 +00:00
Requests : requests ,
Limits : limits ,
} ,
} ,
} ,
} ,
}
}
2018-06-02 00:23:37 +00:00
// 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 ,
} ,
}
}
2016-03-04 17:06:57 +00:00
// newTestPersistentVolumeClaimForQuota returns a simple persistent volume claim
2016-11-18 20:55:17 +00:00
func newTestPersistentVolumeClaimForQuota ( name string ) * v1 . PersistentVolumeClaim {
return & v1 . PersistentVolumeClaim {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-03-04 17:06:57 +00:00
Name : name ,
} ,
2016-11-18 20:55:17 +00:00
Spec : v1 . PersistentVolumeClaimSpec {
AccessModes : [ ] v1 . PersistentVolumeAccessMode {
v1 . ReadWriteOnce ,
v1 . ReadOnlyMany ,
v1 . ReadWriteMany ,
2016-03-04 17:06:57 +00:00
} ,
2016-11-18 20:55:17 +00:00
Resources : v1 . ResourceRequirements {
Requests : v1 . ResourceList {
v1 . ResourceName ( v1 . ResourceStorage ) : resource . MustParse ( "1Gi" ) ,
2016-03-04 17:06:57 +00:00
} ,
} ,
} ,
}
}
// newTestReplicationControllerForQuota returns a simple replication controller
2016-11-18 20:55:17 +00:00
func newTestReplicationControllerForQuota ( name , image string , replicas int32 ) * v1 . ReplicationController {
return & v1 . ReplicationController {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-03-04 17:06:57 +00:00
Name : name ,
} ,
2016-11-18 20:55:17 +00:00
Spec : v1 . ReplicationControllerSpec {
Replicas : func ( i int32 ) * int32 { return & i } ( replicas ) ,
2016-03-04 17:06:57 +00:00
Selector : map [ string ] string {
"name" : name ,
} ,
2016-11-18 20:55:17 +00:00
Template : & v1 . PodTemplateSpec {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-03-04 17:06:57 +00:00
Labels : map [ string ] string { "name" : name } ,
} ,
2016-11-18 20:55:17 +00:00
Spec : v1 . PodSpec {
Containers : [ ] v1 . Container {
2016-03-04 17:06:57 +00:00
{
Name : name ,
Image : image ,
} ,
} ,
} ,
} ,
} ,
}
}
2017-10-27 15:08:30 +00:00
// newTestReplicaSetForQuota returns a simple replica set
2018-12-19 16:18:53 +00:00
func newTestReplicaSetForQuota ( name , image string , replicas int32 ) * appsv1 . ReplicaSet {
2017-10-27 15:08:30 +00:00
zero := int64 ( 0 )
2018-12-19 16:18:53 +00:00
return & appsv1 . ReplicaSet {
2017-10-27 15:08:30 +00:00
ObjectMeta : metav1 . ObjectMeta {
Name : name ,
} ,
2018-12-19 16:18:53 +00:00
Spec : appsv1 . ReplicaSetSpec {
2017-10-27 15:08:30 +00:00
Replicas : & replicas ,
2018-12-19 16:18:53 +00:00
Selector : & metav1 . LabelSelector { MatchLabels : map [ string ] string { "name" : name } } ,
2017-10-27 15:08:30 +00:00
Template : v1 . PodTemplateSpec {
ObjectMeta : metav1 . ObjectMeta {
Labels : map [ string ] string { "name" : name } ,
} ,
Spec : v1 . PodSpec {
TerminationGracePeriodSeconds : & zero ,
Containers : [ ] v1 . Container {
{
Name : name ,
Image : image ,
} ,
} ,
} ,
} ,
} ,
}
}
2016-02-22 16:16:21 +00:00
// newTestServiceForQuota returns a simple service
2016-11-18 20:55:17 +00:00
func newTestServiceForQuota ( name string , serviceType v1 . ServiceType ) * v1 . Service {
return & v1 . Service {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-02-22 16:16:21 +00:00
Name : name ,
} ,
2016-11-18 20:55:17 +00:00
Spec : v1 . ServiceSpec {
2016-02-18 15:54:24 +00:00
Type : serviceType ,
2016-11-18 20:55:17 +00:00
Ports : [ ] v1 . ServicePort { {
2016-02-22 16:16:21 +00:00
Port : 80 ,
TargetPort : intstr . FromInt ( 80 ) ,
} } ,
} ,
}
}
2016-11-18 20:55:17 +00:00
func newTestConfigMapForQuota ( name string ) * v1 . ConfigMap {
return & v1 . ConfigMap {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-02-29 17:02:05 +00:00
Name : name ,
} ,
Data : map [ string ] string {
"a" : "b" ,
} ,
}
}
2016-11-18 20:55:17 +00:00
func newTestSecretForQuota ( name string ) * v1 . Secret {
return & v1 . Secret {
2017-01-17 03:38:19 +00:00
ObjectMeta : metav1 . ObjectMeta {
2016-02-29 22:06:32 +00:00
Name : name ,
} ,
Data : map [ string ] [ ] byte {
"data-1" : [ ] byte ( "value-1\n" ) ,
"data-2" : [ ] byte ( "value-2\n" ) ,
"data-3" : [ ] byte ( "value-3\n" ) ,
} ,
}
}
2016-02-22 16:16:21 +00:00
// createResourceQuota in the specified namespace
2016-11-18 20:55:17 +00:00
func createResourceQuota ( c clientset . Interface , namespace string , resourceQuota * v1 . ResourceQuota ) ( * v1 . ResourceQuota , error ) {
2017-10-25 15:54:32 +00:00
return c . CoreV1 ( ) . ResourceQuotas ( namespace ) . Create ( resourceQuota )
2016-02-22 16:16:21 +00:00
}
// deleteResourceQuota with the specified name
2016-10-18 13:00:38 +00:00
func deleteResourceQuota ( c clientset . Interface , namespace , name string ) error {
2017-10-25 15:54:32 +00:00
return c . CoreV1 ( ) . ResourceQuotas ( namespace ) . Delete ( name , nil )
2016-02-22 16:16:21 +00:00
}
2019-01-28 23:50:16 +00:00
// 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 { } )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
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
} )
}
2016-02-22 16:16:21 +00:00
// wait for resource quota status to show the expected used resources value
2016-11-18 20:55:17 +00:00
func waitForResourceQuota ( c clientset . Interface , ns , quotaName string , used v1 . ResourceList ) error {
2016-04-07 17:21:31 +00:00
return wait . Poll ( framework . Poll , resourceQuotaTimeout , func ( ) ( bool , error ) {
2017-10-25 15:54:32 +00:00
resourceQuota , err := c . CoreV1 ( ) . ResourceQuotas ( ns ) . Get ( quotaName , metav1 . GetOptions { } )
2016-02-22 16:16:21 +00:00
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 ) {
2016-04-07 17:21:31 +00:00
framework . Logf ( "resource %s, expected %s, actual %s" , k , v . String ( ) , actualValue . String ( ) )
2016-02-22 16:16:21 +00:00
return false , nil
}
}
return true , nil
} )
}