2015-02-24 16:17:41 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-02-24 16:17:41 +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.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 03:58:57 +00:00
|
|
|
package resourcequota
|
2015-02-24 16:17:41 +00:00
|
|
|
|
|
|
|
import (
|
2016-02-22 16:15:09 +00:00
|
|
|
"strings"
|
2015-02-24 16:17:41 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-01-25 20:07:10 +00:00
|
|
|
core "k8s.io/client-go/testing"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
2016-11-18 20:50:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
2016-12-14 01:18:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
|
2015-11-11 21:19:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
2016-07-18 19:48:37 +00:00
|
|
|
"k8s.io/kubernetes/pkg/quota/generic"
|
2016-02-22 16:15:09 +00:00
|
|
|
"k8s.io/kubernetes/pkg/quota/install"
|
2015-02-24 16:17:41 +00:00
|
|
|
)
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
func getResourceList(cpu, memory string) v1.ResourceList {
|
|
|
|
res := v1.ResourceList{}
|
2015-02-24 16:17:41 +00:00
|
|
|
if cpu != "" {
|
2016-11-18 20:50:17 +00:00
|
|
|
res[v1.ResourceCPU] = resource.MustParse(cpu)
|
2015-02-24 16:17:41 +00:00
|
|
|
}
|
|
|
|
if memory != "" {
|
2016-11-18 20:50:17 +00:00
|
|
|
res[v1.ResourceMemory] = resource.MustParse(memory)
|
2015-02-24 16:17:41 +00:00
|
|
|
}
|
2015-08-13 14:19:27 +00:00
|
|
|
return res
|
|
|
|
}
|
2015-02-24 16:17:41 +00:00
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
func getResourceRequirements(requests, limits v1.ResourceList) v1.ResourceRequirements {
|
|
|
|
res := v1.ResourceRequirements{}
|
2015-08-13 14:19:27 +00:00
|
|
|
res.Requests = requests
|
|
|
|
res.Limits = limits
|
2015-02-24 16:17:41 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncResourceQuota(t *testing.T) {
|
2016-11-18 20:50:17 +00:00
|
|
|
podList := v1.PodList{
|
|
|
|
Items: []v1.Pod{
|
2015-02-24 16:17:41 +00:00
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "pod-running", Namespace: "testing"},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.PodStatus{Phase: v1.PodRunning},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Volumes: []v1.Volume{{Name: "vol"}},
|
|
|
|
Containers: []v1.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements(getResourceList("100m", "1Gi"), getResourceList("", ""))}},
|
2015-02-24 16:17:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "pod-running-2", Namespace: "testing"},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.PodStatus{Phase: v1.PodRunning},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Volumes: []v1.Volume{{Name: "vol"}},
|
|
|
|
Containers: []v1.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements(getResourceList("100m", "1Gi"), getResourceList("", ""))}},
|
2015-02-24 16:17:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "pod-failed", Namespace: "testing"},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.PodStatus{Phase: v1.PodFailed},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Volumes: []v1.Volume{{Name: "vol"}},
|
|
|
|
Containers: []v1.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements(getResourceList("100m", "1Gi"), getResourceList("", ""))}},
|
2015-02-24 16:17:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
resourceQuota := v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "quota", Namespace: "testing"},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("3"),
|
|
|
|
v1.ResourceMemory: resource.MustParse("100Gi"),
|
|
|
|
v1.ResourcePods: resource.MustParse("5"),
|
2015-02-24 16:17:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
expectedUsage := v1.ResourceQuota{
|
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("3"),
|
|
|
|
v1.ResourceMemory: resource.MustParse("100Gi"),
|
|
|
|
v1.ResourcePods: resource.MustParse("5"),
|
2015-02-24 16:17:41 +00:00
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Used: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("200m"),
|
|
|
|
v1.ResourceMemory: resource.MustParse("2Gi"),
|
|
|
|
v1.ResourcePods: resource.MustParse("2"),
|
2015-02-24 16:17:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-22 16:15:09 +00:00
|
|
|
kubeClient := fake.NewSimpleClientset(&podList, &resourceQuota)
|
|
|
|
resourceQuotaControllerOptions := &ResourceQuotaControllerOptions{
|
|
|
|
KubeClient: kubeClient,
|
|
|
|
ResyncPeriod: controller.NoResyncPeriodFunc,
|
2016-10-14 17:44:20 +00:00
|
|
|
Registry: install.NewRegistry(kubeClient, nil),
|
2016-11-21 02:55:31 +00:00
|
|
|
GroupKindsToReplenish: []schema.GroupKind{
|
2016-02-22 16:15:09 +00:00
|
|
|
api.Kind("Pod"),
|
|
|
|
api.Kind("Service"),
|
|
|
|
api.Kind("ReplicationController"),
|
|
|
|
api.Kind("PersistentVolumeClaim"),
|
|
|
|
},
|
2016-04-19 14:46:31 +00:00
|
|
|
ControllerFactory: NewReplenishmentControllerFactoryFromClient(kubeClient),
|
2016-03-07 07:20:32 +00:00
|
|
|
ReplenishmentResyncPeriod: controller.NoResyncPeriodFunc,
|
2016-02-22 16:15:09 +00:00
|
|
|
}
|
|
|
|
quotaController := NewResourceQuotaController(resourceQuotaControllerOptions)
|
|
|
|
err := quotaController.syncResourceQuota(resourceQuota)
|
2015-02-24 16:17:41 +00:00
|
|
|
if err != nil {
|
2015-04-06 23:27:53 +00:00
|
|
|
t.Fatalf("Unexpected error %v", err)
|
2015-02-24 16:17:41 +00:00
|
|
|
}
|
2016-02-22 16:15:09 +00:00
|
|
|
expectedActionSet := sets.NewString(
|
|
|
|
strings.Join([]string{"list", "pods", ""}, "-"),
|
|
|
|
strings.Join([]string{"update", "resourcequotas", "status"}, "-"),
|
|
|
|
)
|
|
|
|
actionSet := sets.NewString()
|
|
|
|
for _, action := range kubeClient.Actions() {
|
2016-04-13 22:33:15 +00:00
|
|
|
actionSet.Insert(strings.Join([]string{action.GetVerb(), action.GetResource().Resource, action.GetSubresource()}, "-"))
|
2016-02-22 16:15:09 +00:00
|
|
|
}
|
|
|
|
if !actionSet.HasAll(expectedActionSet.List()...) {
|
|
|
|
t.Errorf("Expected actions:\n%v\n but got:\n%v\nDifference:\n%v", expectedActionSet, actionSet, expectedActionSet.Difference(actionSet))
|
|
|
|
}
|
2015-02-24 16:17:41 +00:00
|
|
|
|
2016-02-22 16:15:09 +00:00
|
|
|
lastActionIndex := len(kubeClient.Actions()) - 1
|
2016-11-18 20:50:17 +00:00
|
|
|
usage := kubeClient.Actions()[lastActionIndex].(core.UpdateAction).GetObject().(*v1.ResourceQuota)
|
2015-02-24 16:17:41 +00:00
|
|
|
|
|
|
|
// ensure hard and used limits are what we expected
|
|
|
|
for k, v := range expectedUsage.Status.Hard {
|
|
|
|
actual := usage.Status.Hard[k]
|
|
|
|
actualValue := actual.String()
|
|
|
|
expectedValue := v.String()
|
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Errorf("Usage Hard: Key: %v, Expected: %v, Actual: %v", k, expectedValue, actualValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range expectedUsage.Status.Used {
|
|
|
|
actual := usage.Status.Used[k]
|
|
|
|
actualValue := actual.String()
|
|
|
|
expectedValue := v.String()
|
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Errorf("Usage Used: Key: %v, Expected: %v, Actual: %v", k, expectedValue, actualValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 21:46:27 +00:00
|
|
|
|
|
|
|
func TestSyncResourceQuotaSpecChange(t *testing.T) {
|
2016-11-18 20:50:17 +00:00
|
|
|
resourceQuota := v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-02 01:47:36 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: "rq",
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2015-04-16 21:46:27 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("3"),
|
2015-04-16 21:46:27 +00:00
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Used: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("0"),
|
2015-04-16 21:46:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
expectedUsage := v1.ResourceQuota{
|
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2015-04-16 21:46:27 +00:00
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Used: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("0"),
|
2015-04-16 21:46:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-22 16:15:09 +00:00
|
|
|
kubeClient := fake.NewSimpleClientset(&resourceQuota)
|
|
|
|
resourceQuotaControllerOptions := &ResourceQuotaControllerOptions{
|
|
|
|
KubeClient: kubeClient,
|
|
|
|
ResyncPeriod: controller.NoResyncPeriodFunc,
|
2016-10-14 17:44:20 +00:00
|
|
|
Registry: install.NewRegistry(kubeClient, nil),
|
2016-11-21 02:55:31 +00:00
|
|
|
GroupKindsToReplenish: []schema.GroupKind{
|
2016-02-22 16:15:09 +00:00
|
|
|
api.Kind("Pod"),
|
|
|
|
api.Kind("Service"),
|
|
|
|
api.Kind("ReplicationController"),
|
|
|
|
api.Kind("PersistentVolumeClaim"),
|
|
|
|
},
|
2016-04-19 14:46:31 +00:00
|
|
|
ControllerFactory: NewReplenishmentControllerFactoryFromClient(kubeClient),
|
2016-03-07 07:20:32 +00:00
|
|
|
ReplenishmentResyncPeriod: controller.NoResyncPeriodFunc,
|
2016-02-22 16:15:09 +00:00
|
|
|
}
|
|
|
|
quotaController := NewResourceQuotaController(resourceQuotaControllerOptions)
|
|
|
|
err := quotaController.syncResourceQuota(resourceQuota)
|
2015-04-16 21:46:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-22 16:15:09 +00:00
|
|
|
expectedActionSet := sets.NewString(
|
|
|
|
strings.Join([]string{"list", "pods", ""}, "-"),
|
|
|
|
strings.Join([]string{"update", "resourcequotas", "status"}, "-"),
|
|
|
|
)
|
|
|
|
actionSet := sets.NewString()
|
|
|
|
for _, action := range kubeClient.Actions() {
|
2016-04-13 22:33:15 +00:00
|
|
|
actionSet.Insert(strings.Join([]string{action.GetVerb(), action.GetResource().Resource, action.GetSubresource()}, "-"))
|
2016-02-22 16:15:09 +00:00
|
|
|
}
|
|
|
|
if !actionSet.HasAll(expectedActionSet.List()...) {
|
|
|
|
t.Errorf("Expected actions:\n%v\n but got:\n%v\nDifference:\n%v", expectedActionSet, actionSet, expectedActionSet.Difference(actionSet))
|
|
|
|
}
|
|
|
|
|
|
|
|
lastActionIndex := len(kubeClient.Actions()) - 1
|
2016-11-18 20:50:17 +00:00
|
|
|
usage := kubeClient.Actions()[lastActionIndex].(core.UpdateAction).GetObject().(*v1.ResourceQuota)
|
2015-04-16 21:46:27 +00:00
|
|
|
|
|
|
|
// ensure hard and used limits are what we expected
|
|
|
|
for k, v := range expectedUsage.Status.Hard {
|
|
|
|
actual := usage.Status.Hard[k]
|
|
|
|
actualValue := actual.String()
|
|
|
|
expectedValue := v.String()
|
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Errorf("Usage Hard: Key: %v, Expected: %v, Actual: %v", k, expectedValue, actualValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range expectedUsage.Status.Used {
|
|
|
|
actual := usage.Status.Used[k]
|
|
|
|
actualValue := actual.String()
|
|
|
|
expectedValue := v.String()
|
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Errorf("Usage Used: Key: %v, Expected: %v, Actual: %v", k, expectedValue, actualValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-08-05 22:10:09 +00:00
|
|
|
func TestSyncResourceQuotaSpecHardChange(t *testing.T) {
|
2016-11-18 20:50:17 +00:00
|
|
|
resourceQuota := v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-08-05 22:10:09 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: "rq",
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2016-08-05 22:10:09 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("3"),
|
|
|
|
v1.ResourceMemory: resource.MustParse("1Gi"),
|
2016-08-05 22:10:09 +00:00
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Used: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("0"),
|
|
|
|
v1.ResourceMemory: resource.MustParse("0"),
|
2016-08-05 22:10:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
expectedUsage := v1.ResourceQuota{
|
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2016-08-05 22:10:09 +00:00
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Used: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("0"),
|
2016-08-05 22:10:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeClient := fake.NewSimpleClientset(&resourceQuota)
|
|
|
|
resourceQuotaControllerOptions := &ResourceQuotaControllerOptions{
|
|
|
|
KubeClient: kubeClient,
|
|
|
|
ResyncPeriod: controller.NoResyncPeriodFunc,
|
2016-10-14 17:44:20 +00:00
|
|
|
Registry: install.NewRegistry(kubeClient, nil),
|
2016-11-21 02:55:31 +00:00
|
|
|
GroupKindsToReplenish: []schema.GroupKind{
|
2016-08-05 22:10:09 +00:00
|
|
|
api.Kind("Pod"),
|
|
|
|
api.Kind("Service"),
|
|
|
|
api.Kind("ReplicationController"),
|
|
|
|
api.Kind("PersistentVolumeClaim"),
|
|
|
|
},
|
|
|
|
ControllerFactory: NewReplenishmentControllerFactoryFromClient(kubeClient),
|
|
|
|
ReplenishmentResyncPeriod: controller.NoResyncPeriodFunc,
|
|
|
|
}
|
|
|
|
quotaController := NewResourceQuotaController(resourceQuotaControllerOptions)
|
|
|
|
err := quotaController.syncResourceQuota(resourceQuota)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedActionSet := sets.NewString(
|
|
|
|
strings.Join([]string{"list", "pods", ""}, "-"),
|
|
|
|
strings.Join([]string{"update", "resourcequotas", "status"}, "-"),
|
|
|
|
)
|
|
|
|
actionSet := sets.NewString()
|
|
|
|
for _, action := range kubeClient.Actions() {
|
|
|
|
actionSet.Insert(strings.Join([]string{action.GetVerb(), action.GetResource().Resource, action.GetSubresource()}, "-"))
|
|
|
|
}
|
|
|
|
if !actionSet.HasAll(expectedActionSet.List()...) {
|
|
|
|
t.Errorf("Expected actions:\n%v\n but got:\n%v\nDifference:\n%v", expectedActionSet, actionSet, expectedActionSet.Difference(actionSet))
|
|
|
|
}
|
|
|
|
|
|
|
|
lastActionIndex := len(kubeClient.Actions()) - 1
|
2016-11-18 20:50:17 +00:00
|
|
|
usage := kubeClient.Actions()[lastActionIndex].(core.UpdateAction).GetObject().(*v1.ResourceQuota)
|
2016-08-05 22:10:09 +00:00
|
|
|
|
|
|
|
// ensure hard and used limits are what we expected
|
|
|
|
for k, v := range expectedUsage.Status.Hard {
|
|
|
|
actual := usage.Status.Hard[k]
|
|
|
|
actualValue := actual.String()
|
|
|
|
expectedValue := v.String()
|
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Errorf("Usage Hard: Key: %v, Expected: %v, Actual: %v", k, expectedValue, actualValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range expectedUsage.Status.Used {
|
|
|
|
actual := usage.Status.Used[k]
|
|
|
|
actualValue := actual.String()
|
|
|
|
expectedValue := v.String()
|
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Errorf("Usage Used: Key: %v, Expected: %v, Actual: %v", k, expectedValue, actualValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure usage hard and used are are synced with spec hard, not have dirty resource
|
|
|
|
for k, v := range usage.Status.Hard {
|
2016-11-18 20:50:17 +00:00
|
|
|
if k == v1.ResourceMemory {
|
2016-08-05 22:10:09 +00:00
|
|
|
t.Errorf("Unexpected Usage Hard: Key: %v, Value: %v", k, v.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range usage.Status.Used {
|
2016-11-18 20:50:17 +00:00
|
|
|
if k == v1.ResourceMemory {
|
2016-08-05 22:10:09 +00:00
|
|
|
t.Errorf("Unexpected Usage Used: Key: %v, Value: %v", k, v.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 21:46:27 +00:00
|
|
|
|
|
|
|
func TestSyncResourceQuotaNoChange(t *testing.T) {
|
2016-11-18 20:50:17 +00:00
|
|
|
resourceQuota := v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-02 01:47:36 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: "rq",
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2015-04-16 21:46:27 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2015-04-16 21:46:27 +00:00
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Used: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("0"),
|
2015-04-16 21:46:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
kubeClient := fake.NewSimpleClientset(&v1.PodList{}, &resourceQuota)
|
2016-02-22 16:15:09 +00:00
|
|
|
resourceQuotaControllerOptions := &ResourceQuotaControllerOptions{
|
|
|
|
KubeClient: kubeClient,
|
|
|
|
ResyncPeriod: controller.NoResyncPeriodFunc,
|
2016-10-14 17:44:20 +00:00
|
|
|
Registry: install.NewRegistry(kubeClient, nil),
|
2016-11-21 02:55:31 +00:00
|
|
|
GroupKindsToReplenish: []schema.GroupKind{
|
2016-02-22 16:15:09 +00:00
|
|
|
api.Kind("Pod"),
|
|
|
|
api.Kind("Service"),
|
|
|
|
api.Kind("ReplicationController"),
|
|
|
|
api.Kind("PersistentVolumeClaim"),
|
|
|
|
},
|
2016-04-19 14:46:31 +00:00
|
|
|
ControllerFactory: NewReplenishmentControllerFactoryFromClient(kubeClient),
|
2016-03-07 07:20:32 +00:00
|
|
|
ReplenishmentResyncPeriod: controller.NoResyncPeriodFunc,
|
2016-02-22 16:15:09 +00:00
|
|
|
}
|
|
|
|
quotaController := NewResourceQuotaController(resourceQuotaControllerOptions)
|
|
|
|
err := quotaController.syncResourceQuota(resourceQuota)
|
2015-04-16 21:46:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %v", err)
|
|
|
|
}
|
2016-02-22 16:15:09 +00:00
|
|
|
expectedActionSet := sets.NewString(
|
|
|
|
strings.Join([]string{"list", "pods", ""}, "-"),
|
|
|
|
)
|
|
|
|
actionSet := sets.NewString()
|
|
|
|
for _, action := range kubeClient.Actions() {
|
2016-04-13 22:33:15 +00:00
|
|
|
actionSet.Insert(strings.Join([]string{action.GetVerb(), action.GetResource().Resource, action.GetSubresource()}, "-"))
|
2015-08-13 14:19:27 +00:00
|
|
|
}
|
2016-02-22 16:15:09 +00:00
|
|
|
if !actionSet.HasAll(expectedActionSet.List()...) {
|
|
|
|
t.Errorf("Expected actions:\n%v\n but got:\n%v\nDifference:\n%v", expectedActionSet, actionSet, expectedActionSet.Difference(actionSet))
|
2015-04-17 20:59:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-18 19:48:37 +00:00
|
|
|
|
|
|
|
func TestAddQuota(t *testing.T) {
|
|
|
|
kubeClient := fake.NewSimpleClientset()
|
|
|
|
resourceQuotaControllerOptions := &ResourceQuotaControllerOptions{
|
|
|
|
KubeClient: kubeClient,
|
|
|
|
ResyncPeriod: controller.NoResyncPeriodFunc,
|
2016-10-14 17:44:20 +00:00
|
|
|
Registry: install.NewRegistry(kubeClient, nil),
|
2016-11-21 02:55:31 +00:00
|
|
|
GroupKindsToReplenish: []schema.GroupKind{
|
2016-07-18 19:48:37 +00:00
|
|
|
api.Kind("Pod"),
|
|
|
|
api.Kind("ReplicationController"),
|
|
|
|
api.Kind("PersistentVolumeClaim"),
|
|
|
|
},
|
|
|
|
ControllerFactory: NewReplenishmentControllerFactoryFromClient(kubeClient),
|
|
|
|
ReplenishmentResyncPeriod: controller.NoResyncPeriodFunc,
|
|
|
|
}
|
|
|
|
quotaController := NewResourceQuotaController(resourceQuotaControllerOptions)
|
|
|
|
|
|
|
|
delete(quotaController.registry.(*generic.GenericRegistry).InternalEvaluators, api.Kind("Service"))
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
quota *v1.ResourceQuota
|
2016-07-18 19:48:37 +00:00
|
|
|
expectedPriority bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no status",
|
|
|
|
expectedPriority: true,
|
2016-11-18 20:50:17 +00:00
|
|
|
quota: &v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-07-18 19:48:37 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: "rq",
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "status, no usage",
|
|
|
|
expectedPriority: true,
|
2016-11-18 20:50:17 +00:00
|
|
|
quota: &v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-07-18 19:48:37 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: "rq",
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "status, mismatch",
|
|
|
|
expectedPriority: true,
|
2016-11-18 20:50:17 +00:00
|
|
|
quota: &v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-07-18 19:48:37 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: "rq",
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("6"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Used: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("0"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "status, missing usage, but don't care",
|
|
|
|
expectedPriority: false,
|
2016-11-18 20:50:17 +00:00
|
|
|
quota: &v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-07-18 19:48:37 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: "rq",
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceServices: resource.MustParse("4"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceServices: resource.MustParse("4"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ready",
|
|
|
|
expectedPriority: false,
|
2016-11-18 20:50:17 +00:00
|
|
|
quota: &v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-07-18 19:48:37 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: "rq",
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Status: v1.ResourceQuotaStatus{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("4"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
2016-11-18 20:50:17 +00:00
|
|
|
Used: v1.ResourceList{
|
|
|
|
v1.ResourceCPU: resource.MustParse("0"),
|
2016-07-18 19:48:37 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
quotaController.addQuota(tc.quota)
|
|
|
|
if tc.expectedPriority {
|
|
|
|
if e, a := 1, quotaController.missingUsageQueue.Len(); e != a {
|
|
|
|
t.Errorf("%s: expected %v, got %v", tc.name, e, a)
|
|
|
|
}
|
|
|
|
if e, a := 0, quotaController.queue.Len(); e != a {
|
|
|
|
t.Errorf("%s: expected %v, got %v", tc.name, e, a)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if e, a := 0, quotaController.missingUsageQueue.Len(); e != a {
|
|
|
|
t.Errorf("%s: expected %v, got %v", tc.name, e, a)
|
|
|
|
}
|
|
|
|
if e, a := 1, quotaController.queue.Len(); e != a {
|
|
|
|
t.Errorf("%s: expected %v, got %v", tc.name, e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for quotaController.missingUsageQueue.Len() > 0 {
|
|
|
|
key, _ := quotaController.missingUsageQueue.Get()
|
|
|
|
quotaController.missingUsageQueue.Done(key)
|
|
|
|
}
|
|
|
|
for quotaController.queue.Len() > 0 {
|
|
|
|
key, _ := quotaController.queue.Get()
|
|
|
|
quotaController.queue.Done(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|