2015-01-26 04:34:30 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-01-26 04:34:30 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package resourcequota
|
2015-01-26 18:43:55 +00:00
|
|
|
|
|
|
|
import (
|
2015-08-13 14:19:27 +00:00
|
|
|
"strconv"
|
2015-01-26 18:43:55 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/admission"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
2015-11-30 17:02:04 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-09-03 21:40:58 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/cache"
|
2016-02-16 22:16:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
2015-10-10 03:58:57 +00:00
|
|
|
resourcequotacontroller "k8s.io/kubernetes/pkg/controller/resourcequota"
|
2015-11-10 17:01:08 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-01-26 18:43:55 +00:00
|
|
|
)
|
|
|
|
|
2015-08-13 14:19:27 +00:00
|
|
|
func getResourceList(cpu, memory string) api.ResourceList {
|
|
|
|
res := api.ResourceList{}
|
2015-01-25 04:19:36 +00:00
|
|
|
if cpu != "" {
|
2015-08-13 14:19:27 +00:00
|
|
|
res[api.ResourceCPU] = resource.MustParse(cpu)
|
2015-01-25 04:19:36 +00:00
|
|
|
}
|
|
|
|
if memory != "" {
|
2015-08-13 14:19:27 +00:00
|
|
|
res[api.ResourceMemory] = resource.MustParse(memory)
|
2015-01-25 04:19:36 +00:00
|
|
|
}
|
2015-08-13 14:19:27 +00:00
|
|
|
return res
|
|
|
|
}
|
2015-01-25 04:19:36 +00:00
|
|
|
|
2015-08-13 14:19:27 +00:00
|
|
|
func getResourceRequirements(requests, limits api.ResourceList) api.ResourceRequirements {
|
|
|
|
res := api.ResourceRequirements{}
|
|
|
|
res.Requests = requests
|
|
|
|
res.Limits = limits
|
2015-01-25 04:19:36 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-08-13 14:19:27 +00:00
|
|
|
func validPod(name string, numContainers int, resources api.ResourceRequirements) *api.Pod {
|
|
|
|
pod := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: "test"},
|
|
|
|
Spec: api.PodSpec{},
|
|
|
|
}
|
|
|
|
pod.Spec.Containers = make([]api.Container, 0, numContainers)
|
|
|
|
for i := 0; i < numContainers; i++ {
|
|
|
|
pod.Spec.Containers = append(pod.Spec.Containers, api.Container{
|
|
|
|
Image: "foo:V" + strconv.Itoa(i),
|
|
|
|
Resources: resources,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return pod
|
|
|
|
}
|
|
|
|
|
2015-01-26 18:43:55 +00:00
|
|
|
func TestAdmissionIgnoresDelete(t *testing.T) {
|
|
|
|
namespace := "default"
|
2016-02-01 22:30:47 +00:00
|
|
|
handler := createResourceQuota(&fake.Clientset{}, nil)
|
2015-11-30 17:02:04 +00:00
|
|
|
err := handler.Admit(admission.NewAttributesRecord(nil, api.Kind("Pod"), namespace, "name", api.Resource("pods"), "", admission.Delete, nil))
|
2015-01-26 18:43:55 +00:00
|
|
|
if err != nil {
|
2015-05-15 14:48:33 +00:00
|
|
|
t.Errorf("ResourceQuota should admit all deletes: %v", err)
|
2015-01-26 18:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 20:03:40 +00:00
|
|
|
func TestAdmissionIgnoresSubresources(t *testing.T) {
|
|
|
|
indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{"namespace": cache.MetaNamespaceIndexFunc})
|
2016-02-01 22:30:47 +00:00
|
|
|
handler := createResourceQuota(&fake.Clientset{}, indexer)
|
2015-06-18 20:03:40 +00:00
|
|
|
|
|
|
|
quota := &api.ResourceQuota{}
|
|
|
|
quota.Name = "quota"
|
|
|
|
quota.Namespace = "test"
|
|
|
|
quota.Status = api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
quota.Status.Hard[api.ResourceMemory] = resource.MustParse("2Gi")
|
|
|
|
quota.Status.Used[api.ResourceMemory] = resource.MustParse("1Gi")
|
|
|
|
|
|
|
|
indexer.Add(quota)
|
|
|
|
|
2015-08-13 14:19:27 +00:00
|
|
|
newPod := validPod("123", 1, getResourceRequirements(getResourceList("100m", "2Gi"), getResourceList("", "")))
|
2015-11-30 17:02:04 +00:00
|
|
|
err := handler.Admit(admission.NewAttributesRecord(newPod, api.Kind("Pod"), newPod.Namespace, newPod.Name, api.Resource("pods"), "", admission.Create, nil))
|
2015-06-18 20:03:40 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected an error because the pod exceeded allowed quota")
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:02:04 +00:00
|
|
|
err = handler.Admit(admission.NewAttributesRecord(newPod, api.Kind("Pod"), newPod.Namespace, newPod.Name, api.Resource("pods"), "subresource", admission.Create, nil))
|
2015-06-18 20:03:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error because the action went to a subresource: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-13 14:19:27 +00:00
|
|
|
func TestIncrementUsagePodResources(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
testName string
|
|
|
|
existing *api.Pod
|
|
|
|
input *api.Pod
|
|
|
|
resourceName api.ResourceName
|
|
|
|
hard resource.Quantity
|
|
|
|
expectedUsage resource.Quantity
|
|
|
|
expectedError bool
|
2015-01-26 18:43:55 +00:00
|
|
|
}
|
2015-08-13 14:19:27 +00:00
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
testName: "memory-allowed",
|
|
|
|
existing: validPod("a", 1, getResourceRequirements(getResourceList("", "100Mi"), getResourceList("", ""))),
|
|
|
|
input: validPod("b", 1, getResourceRequirements(getResourceList("", "100Mi"), getResourceList("", ""))),
|
|
|
|
resourceName: api.ResourceMemory,
|
|
|
|
hard: resource.MustParse("500Mi"),
|
|
|
|
expectedUsage: resource.MustParse("200Mi"),
|
|
|
|
expectedError: false,
|
2015-01-26 18:43:55 +00:00
|
|
|
},
|
2015-08-13 14:19:27 +00:00
|
|
|
{
|
|
|
|
testName: "memory-not-allowed",
|
|
|
|
existing: validPod("a", 1, getResourceRequirements(getResourceList("", "100Mi"), getResourceList("", ""))),
|
|
|
|
input: validPod("b", 1, getResourceRequirements(getResourceList("", "450Mi"), getResourceList("", ""))),
|
|
|
|
resourceName: api.ResourceMemory,
|
|
|
|
hard: resource.MustParse("500Mi"),
|
|
|
|
expectedError: true,
|
2015-09-18 03:22:47 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "memory-not-allowed-with-different-format",
|
|
|
|
existing: validPod("a", 1, getResourceRequirements(getResourceList("", "100M"), getResourceList("", ""))),
|
|
|
|
input: validPod("b", 1, getResourceRequirements(getResourceList("", "450Mi"), getResourceList("", ""))),
|
|
|
|
resourceName: api.ResourceMemory,
|
|
|
|
hard: resource.MustParse("500Mi"),
|
|
|
|
expectedError: true,
|
2015-08-13 14:19:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "memory-no-request",
|
|
|
|
existing: validPod("a", 1, getResourceRequirements(getResourceList("", "100Mi"), getResourceList("", ""))),
|
|
|
|
input: validPod("b", 1, getResourceRequirements(getResourceList("", ""), getResourceList("", ""))),
|
|
|
|
resourceName: api.ResourceMemory,
|
|
|
|
hard: resource.MustParse("500Mi"),
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "cpu-allowed",
|
|
|
|
existing: validPod("a", 1, getResourceRequirements(getResourceList("1", ""), getResourceList("", ""))),
|
|
|
|
input: validPod("b", 1, getResourceRequirements(getResourceList("1", ""), getResourceList("", ""))),
|
|
|
|
resourceName: api.ResourceCPU,
|
|
|
|
hard: resource.MustParse("2"),
|
|
|
|
expectedUsage: resource.MustParse("2"),
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "cpu-not-allowed",
|
|
|
|
existing: validPod("a", 1, getResourceRequirements(getResourceList("1", ""), getResourceList("", ""))),
|
|
|
|
input: validPod("b", 1, getResourceRequirements(getResourceList("600m", ""), getResourceList("", ""))),
|
|
|
|
resourceName: api.ResourceCPU,
|
|
|
|
hard: resource.MustParse("1500m"),
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testName: "cpu-no-request",
|
|
|
|
existing: validPod("a", 1, getResourceRequirements(getResourceList("1", ""), getResourceList("", ""))),
|
|
|
|
input: validPod("b", 1, getResourceRequirements(getResourceList("", ""), getResourceList("", ""))),
|
|
|
|
resourceName: api.ResourceCPU,
|
|
|
|
hard: resource.MustParse("1500m"),
|
|
|
|
expectedError: true,
|
2015-01-26 18:43:55 +00:00
|
|
|
},
|
|
|
|
}
|
2015-08-13 14:19:27 +00:00
|
|
|
for _, item := range testCases {
|
|
|
|
podList := &api.PodList{Items: []api.Pod{*item.existing}}
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(podList)
|
2015-08-13 14:19:27 +00:00
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
used, err := resourcequotacontroller.PodRequests(item.existing, item.resourceName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %s, unexpected error %v", item.testName, err)
|
|
|
|
}
|
|
|
|
status.Hard[item.resourceName] = item.hard
|
|
|
|
status.Used[item.resourceName] = *used
|
2015-01-26 18:43:55 +00:00
|
|
|
|
2015-11-30 17:02:04 +00:00
|
|
|
dirty, err := IncrementUsage(admission.NewAttributesRecord(item.input, api.Kind("Pod"), item.input.Namespace, item.input.Name, api.Resource("pods"), "", admission.Create, nil), status, client)
|
2015-08-13 14:19:27 +00:00
|
|
|
if err == nil && item.expectedError {
|
|
|
|
t.Errorf("Test %s, expected error", item.testName)
|
|
|
|
}
|
|
|
|
if err != nil && !item.expectedError {
|
|
|
|
t.Errorf("Test %s, unexpected error", err)
|
|
|
|
}
|
|
|
|
if !item.expectedError {
|
|
|
|
if !dirty {
|
|
|
|
t.Errorf("Test %s, expected the quota to be dirty", item.testName)
|
|
|
|
}
|
|
|
|
quantity := status.Used[item.resourceName]
|
|
|
|
if quantity.String() != item.expectedUsage.String() {
|
|
|
|
t.Errorf("Test %s, expected usage %s, actual usage %s", item.testName, item.expectedUsage.String(), quantity.String())
|
|
|
|
}
|
|
|
|
}
|
2015-01-26 18:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 14:19:27 +00:00
|
|
|
func TestIncrementUsagePods(t *testing.T) {
|
|
|
|
pod := validPod("123", 1, getResourceRequirements(getResourceList("100m", "1Gi"), getResourceList("", "")))
|
|
|
|
podList := &api.PodList{Items: []api.Pod{*pod}}
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(podList)
|
2015-01-26 18:43:55 +00:00
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
2015-08-13 14:19:27 +00:00
|
|
|
r := api.ResourcePods
|
|
|
|
status.Hard[r] = resource.MustParse("2")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
2015-11-30 17:02:04 +00:00
|
|
|
dirty, err := IncrementUsage(admission.NewAttributesRecord(&api.Pod{}, api.Kind("Pod"), pod.Namespace, "new-pod", api.Resource("pods"), "", admission.Create, nil), status, client)
|
2015-01-26 18:43:55 +00:00
|
|
|
if err != nil {
|
2015-05-15 14:48:33 +00:00
|
|
|
t.Errorf("Unexpected error: %v", err)
|
2015-01-26 18:43:55 +00:00
|
|
|
}
|
|
|
|
if !dirty {
|
|
|
|
t.Errorf("Expected the status to get incremented, therefore should have been dirty")
|
|
|
|
}
|
|
|
|
quantity := status.Used[r]
|
2015-08-13 14:19:27 +00:00
|
|
|
if quantity.Value() != int64(2) {
|
|
|
|
t.Errorf("Expected new item count to be 2, but was %s", quantity.String())
|
2015-01-26 18:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExceedUsagePods(t *testing.T) {
|
2015-08-13 14:19:27 +00:00
|
|
|
pod := validPod("123", 1, getResourceRequirements(getResourceList("100m", "1Gi"), getResourceList("", "")))
|
|
|
|
podList := &api.PodList{Items: []api.Pod{*pod}}
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(podList)
|
2015-01-26 18:43:55 +00:00
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
r := api.ResourcePods
|
|
|
|
status.Hard[r] = resource.MustParse("1")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
2015-11-30 17:02:04 +00:00
|
|
|
_, err := IncrementUsage(admission.NewAttributesRecord(&api.Pod{}, api.Kind("Pod"), pod.Namespace, "name", api.Resource("pods"), "", admission.Create, nil), status, client)
|
2015-01-26 18:43:55 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error because this would exceed your quota")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUsageServices(t *testing.T) {
|
|
|
|
namespace := "default"
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(&api.ServiceList{
|
2015-04-06 23:27:53 +00:00
|
|
|
Items: []api.Service{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
|
2015-01-26 18:43:55 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-06 23:27:53 +00:00
|
|
|
})
|
2015-01-26 18:43:55 +00:00
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
r := api.ResourceServices
|
|
|
|
status.Hard[r] = resource.MustParse("2")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
2015-11-30 17:02:04 +00:00
|
|
|
dirty, err := IncrementUsage(admission.NewAttributesRecord(&api.Service{}, api.Kind("Service"), namespace, "name", api.Resource("services"), "", admission.Create, nil), status, client)
|
2015-01-26 18:43:55 +00:00
|
|
|
if err != nil {
|
2015-05-15 14:48:33 +00:00
|
|
|
t.Errorf("Unexpected error: %v", err)
|
2015-01-26 18:43:55 +00:00
|
|
|
}
|
|
|
|
if !dirty {
|
|
|
|
t.Errorf("Expected the status to get incremented, therefore should have been dirty")
|
|
|
|
}
|
|
|
|
quantity := status.Used[r]
|
|
|
|
if quantity.Value() != int64(2) {
|
|
|
|
t.Errorf("Expected new item count to be 2, but was %s", quantity.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExceedUsageServices(t *testing.T) {
|
|
|
|
namespace := "default"
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(&api.ServiceList{
|
2015-04-06 23:27:53 +00:00
|
|
|
Items: []api.Service{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
|
2015-01-26 18:43:55 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-06 23:27:53 +00:00
|
|
|
})
|
2015-01-26 18:43:55 +00:00
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
r := api.ResourceServices
|
|
|
|
status.Hard[r] = resource.MustParse("1")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
2015-11-30 17:02:04 +00:00
|
|
|
_, err := IncrementUsage(admission.NewAttributesRecord(&api.Service{}, api.Kind("Service"), namespace, "name", api.Resource("services"), "", admission.Create, nil), status, client)
|
2015-01-26 18:43:55 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error because this would exceed usage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIncrementUsageReplicationControllers(t *testing.T) {
|
|
|
|
namespace := "default"
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(&api.ReplicationControllerList{
|
2015-04-06 23:27:53 +00:00
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
|
2015-01-26 18:43:55 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-06 23:27:53 +00:00
|
|
|
})
|
2015-01-26 18:43:55 +00:00
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
r := api.ResourceReplicationControllers
|
|
|
|
status.Hard[r] = resource.MustParse("2")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
2015-11-30 17:02:04 +00:00
|
|
|
dirty, err := IncrementUsage(admission.NewAttributesRecord(&api.ReplicationController{}, api.Kind("ReplicationController"), namespace, "name", api.Resource("replicationcontrollers"), "", admission.Create, nil), status, client)
|
2015-01-26 18:43:55 +00:00
|
|
|
if err != nil {
|
2015-05-15 14:48:33 +00:00
|
|
|
t.Errorf("Unexpected error: %v", err)
|
2015-01-26 18:43:55 +00:00
|
|
|
}
|
|
|
|
if !dirty {
|
|
|
|
t.Errorf("Expected the status to get incremented, therefore should have been dirty")
|
|
|
|
}
|
|
|
|
quantity := status.Used[r]
|
|
|
|
if quantity.Value() != int64(2) {
|
|
|
|
t.Errorf("Expected new item count to be 2, but was %s", quantity.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExceedUsageReplicationControllers(t *testing.T) {
|
|
|
|
namespace := "default"
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(&api.ReplicationControllerList{
|
2015-04-06 23:27:53 +00:00
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
|
2015-01-26 18:43:55 +00:00
|
|
|
},
|
|
|
|
},
|
2015-04-06 23:27:53 +00:00
|
|
|
})
|
2015-01-26 18:43:55 +00:00
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
r := api.ResourceReplicationControllers
|
|
|
|
status.Hard[r] = resource.MustParse("1")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
2015-11-30 17:02:04 +00:00
|
|
|
_, err := IncrementUsage(admission.NewAttributesRecord(&api.ReplicationController{}, api.Kind("ReplicationController"), namespace, "name", api.Resource("replicationcontrollers"), "", admission.Create, nil), status, client)
|
2015-01-26 18:43:55 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error for exceeding hard limits")
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 21:03:56 +00:00
|
|
|
|
|
|
|
func TestExceedUsageSecrets(t *testing.T) {
|
|
|
|
namespace := "default"
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(&api.SecretList{
|
2015-04-08 21:03:56 +00:00
|
|
|
Items: []api.Secret{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
r := api.ResourceSecrets
|
|
|
|
status.Hard[r] = resource.MustParse("1")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
2015-11-30 17:02:04 +00:00
|
|
|
_, err := IncrementUsage(admission.NewAttributesRecord(&api.Secret{}, api.Kind("Secret"), namespace, "name", api.Resource("secrets"), "", admission.Create, nil), status, client)
|
2015-04-08 21:03:56 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error for exceeding hard limits")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExceedUsagePersistentVolumeClaims(t *testing.T) {
|
|
|
|
namespace := "default"
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset(&api.PersistentVolumeClaimList{
|
2015-04-08 21:03:56 +00:00
|
|
|
Items: []api.PersistentVolumeClaim{
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
|
|
|
r := api.ResourcePersistentVolumeClaims
|
|
|
|
status.Hard[r] = resource.MustParse("1")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
2015-11-30 17:02:04 +00:00
|
|
|
_, err := IncrementUsage(admission.NewAttributesRecord(&api.PersistentVolumeClaim{}, api.Kind("PersistentVolumeClaim"), namespace, "name", api.Resource("persistentvolumeclaims"), "", admission.Create, nil), status, client)
|
2015-04-08 21:03:56 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error for exceeding hard limits")
|
|
|
|
}
|
|
|
|
}
|
2015-11-10 17:01:08 +00:00
|
|
|
|
|
|
|
func TestIncrementUsageOnUpdateIgnoresNonPodResources(t *testing.T) {
|
|
|
|
testCase := []struct {
|
2015-11-30 17:02:04 +00:00
|
|
|
kind unversioned.GroupKind
|
|
|
|
resource unversioned.GroupResource
|
2015-11-10 17:01:08 +00:00
|
|
|
subresource string
|
|
|
|
object runtime.Object
|
|
|
|
}{
|
|
|
|
{
|
2015-11-30 17:02:04 +00:00
|
|
|
kind: api.Kind("Service"),
|
|
|
|
resource: api.Resource("services"),
|
2015-11-10 17:01:08 +00:00
|
|
|
object: &api.Service{},
|
|
|
|
},
|
|
|
|
{
|
2015-11-30 17:02:04 +00:00
|
|
|
kind: api.Kind("ReplicationController"),
|
|
|
|
resource: api.Resource("replicationcontrollers"),
|
2015-11-10 17:01:08 +00:00
|
|
|
object: &api.ReplicationController{},
|
|
|
|
},
|
|
|
|
{
|
2015-11-30 17:02:04 +00:00
|
|
|
kind: api.Kind("ResourceQuota"),
|
|
|
|
resource: api.Resource("resourcequotas"),
|
2015-11-10 17:01:08 +00:00
|
|
|
object: &api.ResourceQuota{},
|
|
|
|
},
|
|
|
|
{
|
2015-11-30 17:02:04 +00:00
|
|
|
kind: api.Kind("Secret"),
|
|
|
|
resource: api.Resource("secrets"),
|
2015-11-10 17:01:08 +00:00
|
|
|
object: &api.Secret{},
|
|
|
|
},
|
|
|
|
{
|
2015-11-30 17:02:04 +00:00
|
|
|
kind: api.Kind("PersistentVolumeClaim"),
|
|
|
|
resource: api.Resource("persistentvolumeclaims"),
|
2015-11-10 17:01:08 +00:00
|
|
|
object: &api.PersistentVolumeClaim{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCase {
|
2016-02-01 22:30:47 +00:00
|
|
|
client := fake.NewSimpleClientset()
|
2015-11-10 17:01:08 +00:00
|
|
|
status := &api.ResourceQuotaStatus{
|
|
|
|
Hard: api.ResourceList{},
|
|
|
|
Used: api.ResourceList{},
|
|
|
|
}
|
2015-11-30 17:02:04 +00:00
|
|
|
r := resourceToResourceName[testCase.resource]
|
2015-11-10 17:01:08 +00:00
|
|
|
status.Hard[r] = resource.MustParse("2")
|
|
|
|
status.Used[r] = resource.MustParse("1")
|
|
|
|
|
|
|
|
attributesRecord := admission.NewAttributesRecord(testCase.object, testCase.kind, "my-ns", "new-thing",
|
|
|
|
testCase.resource, testCase.subresource, admission.Update, nil)
|
|
|
|
dirty, err := IncrementUsage(attributesRecord, status, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Increment usage of resource %v had unexpected error: %v", testCase.resource, err)
|
|
|
|
}
|
|
|
|
if dirty {
|
|
|
|
t.Errorf("Increment usage of resource %v should not result in a dirty quota on update", testCase.resource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|