2016-04-08 20:32:08 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-04-08 20:32:08 +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.
|
|
|
|
*/
|
|
|
|
|
2016-06-08 18:44:10 +00:00
|
|
|
package quota
|
2016-04-08 20:32:08 +00:00
|
|
|
|
|
|
|
import (
|
2018-08-02 12:11:59 +00:00
|
|
|
"context"
|
2016-05-03 18:23:37 +00:00
|
|
|
"fmt"
|
2016-04-08 20:32:08 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-25 13:13:07 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-19 14:50:16 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2018-05-01 14:54:37 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2017-02-22 14:57:20 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2017-06-23 20:56:37 +00:00
|
|
|
"k8s.io/client-go/informers"
|
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
2017-01-19 18:27:59 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
2017-01-30 18:39:54 +00:00
|
|
|
"k8s.io/client-go/tools/record"
|
2018-08-02 12:11:59 +00:00
|
|
|
watchtools "k8s.io/client-go/tools/watch"
|
2016-04-08 20:32:08 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
|
|
|
replicationcontroller "k8s.io/kubernetes/pkg/controller/replication"
|
|
|
|
resourcequotacontroller "k8s.io/kubernetes/pkg/controller/resourcequota"
|
2018-08-27 13:50:30 +00:00
|
|
|
"k8s.io/kubernetes/pkg/quota/v1/generic"
|
|
|
|
quotainstall "k8s.io/kubernetes/pkg/quota/v1/install"
|
2016-04-08 20:32:08 +00:00
|
|
|
"k8s.io/kubernetes/plugin/pkg/admission/resourcequota"
|
2017-02-18 17:10:22 +00:00
|
|
|
resourcequotaapi "k8s.io/kubernetes/plugin/pkg/admission/resourcequota/apis/resourcequota"
|
2016-04-08 20:32:08 +00:00
|
|
|
"k8s.io/kubernetes/test/integration/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 1.2 code gets:
|
|
|
|
// quota_test.go:95: Took 4.218619579s to scale up without quota
|
|
|
|
// quota_test.go:199: unexpected error: timed out waiting for the condition, ended with 342 pods (1 minute)
|
|
|
|
// 1.3+ code gets:
|
|
|
|
// quota_test.go:100: Took 4.196205966s to scale up without quota
|
|
|
|
// quota_test.go:115: Took 12.021640372s to scale up with quota
|
|
|
|
func TestQuota(t *testing.T) {
|
|
|
|
// Set up a master
|
2016-10-03 15:31:00 +00:00
|
|
|
h := &framework.MasterHolder{Initialized: make(chan struct{})}
|
2016-04-08 20:32:08 +00:00
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2016-10-03 15:31:00 +00:00
|
|
|
<-h.Initialized
|
2016-09-30 16:16:32 +00:00
|
|
|
h.M.GenericAPIServer.Handler.ServeHTTP(w, req)
|
2016-04-08 20:32:08 +00:00
|
|
|
}))
|
2016-10-03 15:31:00 +00:00
|
|
|
|
2016-05-10 20:00:24 +00:00
|
|
|
admissionCh := make(chan struct{})
|
2018-05-01 14:54:37 +00:00
|
|
|
clientset := clientset.NewForConfigOrDie(&restclient.Config{QPS: -1, Host: s.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
2017-02-18 17:10:22 +00:00
|
|
|
config := &resourcequotaapi.Configuration{}
|
2017-05-18 18:00:02 +00:00
|
|
|
admission, err := resourcequota.NewResourceQuota(config, 5, admissionCh)
|
2016-04-08 20:32:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2018-08-27 13:50:30 +00:00
|
|
|
admission.SetExternalKubeClientSet(clientset)
|
|
|
|
internalInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
|
|
|
|
admission.SetExternalKubeInformerFactory(internalInformers)
|
2017-10-27 15:08:30 +00:00
|
|
|
qca := quotainstall.NewQuotaConfigurationForAdmission()
|
2017-10-30 13:20:40 +00:00
|
|
|
admission.SetQuotaConfiguration(qca)
|
2016-05-10 20:00:24 +00:00
|
|
|
defer close(admissionCh)
|
2016-04-08 20:32:08 +00:00
|
|
|
|
|
|
|
masterConfig := framework.NewIntegrationTestMasterConfig()
|
2016-09-28 18:52:28 +00:00
|
|
|
masterConfig.GenericConfig.AdmissionControl = admission
|
2017-05-08 15:05:28 +00:00
|
|
|
_, _, closeFn := framework.RunAMasterUsingServer(masterConfig, s, h)
|
|
|
|
defer closeFn()
|
2016-04-08 20:32:08 +00:00
|
|
|
|
2016-07-05 09:26:12 +00:00
|
|
|
ns := framework.CreateTestingNamespace("quotaed", s, t)
|
|
|
|
defer framework.DeleteTestingNamespace(ns, s, t)
|
|
|
|
ns2 := framework.CreateTestingNamespace("non-quotaed", s, t)
|
|
|
|
defer framework.DeleteTestingNamespace(ns2, s, t)
|
|
|
|
|
2016-05-03 18:23:37 +00:00
|
|
|
controllerCh := make(chan struct{})
|
|
|
|
defer close(controllerCh)
|
|
|
|
|
2017-02-08 21:18:21 +00:00
|
|
|
informers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
|
2017-02-06 18:35:50 +00:00
|
|
|
rm := replicationcontroller.NewReplicationManager(
|
|
|
|
informers.Core().V1().Pods(),
|
|
|
|
informers.Core().V1().ReplicationControllers(),
|
|
|
|
clientset,
|
|
|
|
replicationcontroller.BurstReplicas,
|
|
|
|
)
|
2017-01-02 16:35:12 +00:00
|
|
|
rm.SetEventRecorder(&record.FakeRecorder{})
|
|
|
|
go rm.Run(3, controllerCh)
|
2016-04-08 20:32:08 +00:00
|
|
|
|
2017-10-27 15:08:30 +00:00
|
|
|
discoveryFunc := clientset.Discovery().ServerPreferredNamespacedResources
|
|
|
|
listerFuncForResource := generic.ListerFuncForResourceFunc(informers.ForResource)
|
|
|
|
qc := quotainstall.NewQuotaConfigurationForControllers(listerFuncForResource)
|
|
|
|
informersStarted := make(chan struct{})
|
2016-04-08 20:32:08 +00:00
|
|
|
resourceQuotaControllerOptions := &resourcequotacontroller.ResourceQuotaControllerOptions{
|
2017-07-19 19:36:45 +00:00
|
|
|
QuotaClient: clientset.Core(),
|
2017-02-10 16:25:54 +00:00
|
|
|
ResourceQuotaInformer: informers.Core().V1().ResourceQuotas(),
|
2016-04-08 20:32:08 +00:00
|
|
|
ResyncPeriod: controller.NoResyncPeriodFunc,
|
2017-10-27 15:08:30 +00:00
|
|
|
InformerFactory: informers,
|
2016-04-08 20:32:08 +00:00
|
|
|
ReplenishmentResyncPeriod: controller.NoResyncPeriodFunc,
|
2017-10-27 15:08:30 +00:00
|
|
|
DiscoveryFunc: discoveryFunc,
|
|
|
|
IgnoredResourcesFunc: qc.IgnoredResources,
|
|
|
|
InformersStarted: informersStarted,
|
|
|
|
Registry: generic.NewRegistry(qc.Evaluators()),
|
|
|
|
}
|
|
|
|
resourceQuotaController, err := resourcequotacontroller.NewResourceQuotaController(resourceQuotaControllerOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected err: %v", err)
|
2016-04-08 20:32:08 +00:00
|
|
|
}
|
2017-10-27 15:08:30 +00:00
|
|
|
go resourceQuotaController.Run(2, controllerCh)
|
|
|
|
|
|
|
|
// Periodically the quota controller to detect new resource types
|
|
|
|
go resourceQuotaController.Sync(discoveryFunc, 30*time.Second, controllerCh)
|
|
|
|
|
2017-02-15 19:00:29 +00:00
|
|
|
internalInformers.Start(controllerCh)
|
2017-02-10 16:25:54 +00:00
|
|
|
informers.Start(controllerCh)
|
2017-10-27 15:08:30 +00:00
|
|
|
close(informersStarted)
|
2016-04-08 20:32:08 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
2016-07-05 09:26:12 +00:00
|
|
|
scale(t, ns2.Name, clientset)
|
2016-04-08 20:32:08 +00:00
|
|
|
endTime := time.Now()
|
|
|
|
t.Logf("Took %v to scale up without quota", endTime.Sub(startTime))
|
|
|
|
|
2016-11-18 20:55:32 +00:00
|
|
|
quota := &v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-07-05 09:26:12 +00:00
|
|
|
Name: "quota",
|
|
|
|
Namespace: ns.Name,
|
|
|
|
},
|
2016-11-18 20:55:32 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourcePods: resource.MustParse("1000"),
|
2016-04-08 20:32:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
waitForQuota(t, quota, clientset)
|
|
|
|
|
|
|
|
startTime = time.Now()
|
|
|
|
scale(t, "quotaed", clientset)
|
|
|
|
endTime = time.Now()
|
|
|
|
t.Logf("Took %v to scale up with quota", endTime.Sub(startTime))
|
|
|
|
}
|
2016-05-10 20:00:24 +00:00
|
|
|
|
2016-11-18 20:55:32 +00:00
|
|
|
func waitForQuota(t *testing.T, quota *v1.ResourceQuota, clientset *clientset.Clientset) {
|
2017-01-22 03:36:02 +00:00
|
|
|
w, err := clientset.Core().ResourceQuotas(quota.Namespace).Watch(metav1.SingleObject(metav1.ObjectMeta{Name: quota.Name}))
|
2016-04-08 20:32:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-07-05 09:26:12 +00:00
|
|
|
if _, err := clientset.Core().ResourceQuotas(quota.Namespace).Create(quota); err != nil {
|
2016-04-08 20:32:08 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-02 12:11:59 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
_, err = watchtools.UntilWithoutRetry(ctx, w, func(event watch.Event) (bool, error) {
|
2016-04-08 20:32:08 +00:00
|
|
|
switch event.Type {
|
|
|
|
case watch.Modified:
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
switch cast := event.Object.(type) {
|
2016-11-18 20:55:32 +00:00
|
|
|
case *v1.ResourceQuota:
|
2016-04-08 20:32:08 +00:00
|
|
|
if len(cast.Status.Hard) > 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func scale(t *testing.T, namespace string, clientset *clientset.Clientset) {
|
2016-11-18 20:55:32 +00:00
|
|
|
target := int32(100)
|
|
|
|
rc := &v1.ReplicationController{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-04-08 20:32:08 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: namespace,
|
|
|
|
},
|
2016-11-18 20:55:32 +00:00
|
|
|
Spec: v1.ReplicationControllerSpec{
|
|
|
|
Replicas: &target,
|
2016-04-08 20:32:08 +00:00
|
|
|
Selector: map[string]string{"foo": "bar"},
|
2016-11-18 20:55:32 +00:00
|
|
|
Template: &v1.PodTemplateSpec{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-04-08 20:32:08 +00:00
|
|
|
Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:55:32 +00:00
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-04-08 20:32:08 +00:00
|
|
|
{
|
|
|
|
Name: "container",
|
|
|
|
Image: "busybox",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-22 03:36:02 +00:00
|
|
|
w, err := clientset.Core().ReplicationControllers(namespace).Watch(metav1.SingleObject(metav1.ObjectMeta{Name: rc.Name}))
|
2016-04-08 20:32:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := clientset.Core().ReplicationControllers(namespace).Create(rc); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-02 12:11:59 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
_, err = watchtools.UntilWithoutRetry(ctx, w, func(event watch.Event) (bool, error) {
|
2016-04-08 20:32:08 +00:00
|
|
|
switch event.Type {
|
|
|
|
case watch.Modified:
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch cast := event.Object.(type) {
|
2016-11-18 20:55:32 +00:00
|
|
|
case *v1.ReplicationController:
|
2016-05-03 18:23:37 +00:00
|
|
|
fmt.Printf("Found %v of %v replicas\n", int(cast.Status.Replicas), target)
|
2016-11-18 20:55:32 +00:00
|
|
|
if cast.Status.Replicas == target {
|
2016-04-08 20:32:08 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-01-22 03:36:02 +00:00
|
|
|
pods, _ := clientset.Core().Pods(namespace).List(metav1.ListOptions{LabelSelector: labels.Everything().String(), FieldSelector: fields.Everything().String()})
|
2016-04-08 20:32:08 +00:00
|
|
|
t.Fatalf("unexpected error: %v, ended with %v pods", err, len(pods.Items))
|
|
|
|
}
|
|
|
|
}
|
2017-02-18 17:10:22 +00:00
|
|
|
|
|
|
|
func TestQuotaLimitedResourceDenial(t *testing.T) {
|
|
|
|
// Set up a master
|
|
|
|
h := &framework.MasterHolder{Initialized: make(chan struct{})}
|
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
<-h.Initialized
|
|
|
|
h.M.GenericAPIServer.Handler.ServeHTTP(w, req)
|
|
|
|
}))
|
|
|
|
|
|
|
|
admissionCh := make(chan struct{})
|
2018-05-01 14:54:37 +00:00
|
|
|
clientset := clientset.NewForConfigOrDie(&restclient.Config{QPS: -1, Host: s.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
|
2017-02-18 17:10:22 +00:00
|
|
|
|
|
|
|
// stop creation of a pod resource unless there is a quota
|
|
|
|
config := &resourcequotaapi.Configuration{
|
|
|
|
LimitedResources: []resourcequotaapi.LimitedResource{
|
|
|
|
{
|
|
|
|
Resource: "pods",
|
|
|
|
MatchContains: []string{"pods"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-10-27 15:08:30 +00:00
|
|
|
qca := quotainstall.NewQuotaConfigurationForAdmission()
|
2017-05-18 18:00:02 +00:00
|
|
|
admission, err := resourcequota.NewResourceQuota(config, 5, admissionCh)
|
2017-02-18 17:10:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2018-08-27 13:50:30 +00:00
|
|
|
admission.SetExternalKubeClientSet(clientset)
|
|
|
|
externalInformers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
|
|
|
|
admission.SetExternalKubeInformerFactory(externalInformers)
|
2017-10-30 13:20:40 +00:00
|
|
|
admission.SetQuotaConfiguration(qca)
|
2017-02-18 17:10:22 +00:00
|
|
|
defer close(admissionCh)
|
|
|
|
|
|
|
|
masterConfig := framework.NewIntegrationTestMasterConfig()
|
|
|
|
masterConfig.GenericConfig.AdmissionControl = admission
|
2017-05-08 15:05:28 +00:00
|
|
|
_, _, closeFn := framework.RunAMasterUsingServer(masterConfig, s, h)
|
|
|
|
defer closeFn()
|
2017-02-18 17:10:22 +00:00
|
|
|
|
|
|
|
ns := framework.CreateTestingNamespace("quota", s, t)
|
|
|
|
defer framework.DeleteTestingNamespace(ns, s, t)
|
|
|
|
|
|
|
|
controllerCh := make(chan struct{})
|
|
|
|
defer close(controllerCh)
|
|
|
|
|
|
|
|
informers := informers.NewSharedInformerFactory(clientset, controller.NoResyncPeriodFunc())
|
|
|
|
rm := replicationcontroller.NewReplicationManager(
|
|
|
|
informers.Core().V1().Pods(),
|
|
|
|
informers.Core().V1().ReplicationControllers(),
|
|
|
|
clientset,
|
|
|
|
replicationcontroller.BurstReplicas,
|
|
|
|
)
|
|
|
|
rm.SetEventRecorder(&record.FakeRecorder{})
|
|
|
|
go rm.Run(3, controllerCh)
|
|
|
|
|
2017-10-27 15:08:30 +00:00
|
|
|
discoveryFunc := clientset.Discovery().ServerPreferredNamespacedResources
|
|
|
|
listerFuncForResource := generic.ListerFuncForResourceFunc(informers.ForResource)
|
|
|
|
qc := quotainstall.NewQuotaConfigurationForControllers(listerFuncForResource)
|
|
|
|
informersStarted := make(chan struct{})
|
2017-02-18 17:10:22 +00:00
|
|
|
resourceQuotaControllerOptions := &resourcequotacontroller.ResourceQuotaControllerOptions{
|
2017-07-19 19:36:45 +00:00
|
|
|
QuotaClient: clientset.Core(),
|
2017-02-18 17:10:22 +00:00
|
|
|
ResourceQuotaInformer: informers.Core().V1().ResourceQuotas(),
|
|
|
|
ResyncPeriod: controller.NoResyncPeriodFunc,
|
2017-10-27 15:08:30 +00:00
|
|
|
InformerFactory: informers,
|
2017-02-18 17:10:22 +00:00
|
|
|
ReplenishmentResyncPeriod: controller.NoResyncPeriodFunc,
|
2017-10-27 15:08:30 +00:00
|
|
|
DiscoveryFunc: discoveryFunc,
|
|
|
|
IgnoredResourcesFunc: qc.IgnoredResources,
|
|
|
|
InformersStarted: informersStarted,
|
|
|
|
Registry: generic.NewRegistry(qc.Evaluators()),
|
|
|
|
}
|
|
|
|
resourceQuotaController, err := resourcequotacontroller.NewResourceQuotaController(resourceQuotaControllerOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected err: %v", err)
|
2017-02-18 17:10:22 +00:00
|
|
|
}
|
2017-10-27 15:08:30 +00:00
|
|
|
go resourceQuotaController.Run(2, controllerCh)
|
|
|
|
|
|
|
|
// Periodically the quota controller to detect new resource types
|
|
|
|
go resourceQuotaController.Sync(discoveryFunc, 30*time.Second, controllerCh)
|
|
|
|
|
2018-08-27 13:50:30 +00:00
|
|
|
externalInformers.Start(controllerCh)
|
2017-02-18 17:10:22 +00:00
|
|
|
informers.Start(controllerCh)
|
2017-10-27 15:08:30 +00:00
|
|
|
close(informersStarted)
|
2017-02-18 17:10:22 +00:00
|
|
|
|
|
|
|
// try to create a pod
|
|
|
|
pod := &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: ns.Name,
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "container",
|
|
|
|
Image: "busybox",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if _, err := clientset.Core().Pods(ns.Name).Create(pod); err == nil {
|
|
|
|
t.Fatalf("expected error for insufficient quota")
|
|
|
|
}
|
|
|
|
|
|
|
|
// now create a covering quota
|
2017-10-27 15:08:30 +00:00
|
|
|
// note: limited resource does a matchContains, so we now have "pods" matching "pods" and "count/pods"
|
2017-02-18 17:10:22 +00:00
|
|
|
quota := &v1.ResourceQuota{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "quota",
|
|
|
|
Namespace: ns.Name,
|
|
|
|
},
|
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
2017-10-27 15:08:30 +00:00
|
|
|
v1.ResourcePods: resource.MustParse("1000"),
|
|
|
|
v1.ResourceName("count/pods"): resource.MustParse("1000"),
|
2017-02-18 17:10:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
waitForQuota(t, quota, clientset)
|
|
|
|
|
2017-02-22 14:57:20 +00:00
|
|
|
// attempt to create a new pod once the quota is propagated
|
|
|
|
err = wait.PollImmediate(5*time.Second, time.Minute, func() (bool, error) {
|
|
|
|
// retry until we succeed (to allow time for all changes to propagate)
|
|
|
|
if _, err := clientset.Core().Pods(ns.Name).Create(pod); err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-02-18 17:10:22 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|