From 305411b59baa2cdcce69d8532aee906d80b79d78 Mon Sep 17 00:00:00 2001 From: derekwaynecarr Date: Fri, 22 Jul 2016 11:50:29 -0400 Subject: [PATCH] Fix bug in node port counting in quota not counting multi-node ports --- pkg/quota/evaluator/core/services.go | 3 ++- pkg/quota/evaluator/core/services_test.go | 24 +++++++++++++++++++ .../admission/resourcequota/admission_test.go | 5 +++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pkg/quota/evaluator/core/services.go b/pkg/quota/evaluator/core/services.go index 9e3de1e0f7b28..60fda367b753a 100644 --- a/pkg/quota/evaluator/core/services.go +++ b/pkg/quota/evaluator/core/services.go @@ -57,7 +57,8 @@ func ServiceUsageFunc(object runtime.Object) api.ResourceList { result[api.ResourceServices] = resource.MustParse("1") switch service.Spec.Type { case api.ServiceTypeNodePort: - result[api.ResourceServicesNodePorts] = resource.MustParse("1") + value := resource.NewQuantity(int64(len(service.Spec.Ports)), resource.DecimalSI) + result[api.ResourceServicesNodePorts] = *value case api.ServiceTypeLoadBalancer: result[api.ResourceServicesLoadBalancers] = resource.MustParse("1") } diff --git a/pkg/quota/evaluator/core/services_test.go b/pkg/quota/evaluator/core/services_test.go index fdd07abdae99e..100f6605b42fb 100644 --- a/pkg/quota/evaluator/core/services_test.go +++ b/pkg/quota/evaluator/core/services_test.go @@ -71,6 +71,11 @@ func TestServiceEvaluatorUsage(t *testing.T) { service: &api.Service{ Spec: api.ServiceSpec{ Type: api.ServiceTypeNodePort, + Ports: []api.ServicePort{ + { + Port: 27443, + }, + }, }, }, usage: api.ResourceList{ @@ -78,6 +83,25 @@ func TestServiceEvaluatorUsage(t *testing.T) { api.ResourceServicesNodePorts: resource.MustParse("1"), }, }, + "multi-nodeports": { + service: &api.Service{ + Spec: api.ServiceSpec{ + Type: api.ServiceTypeNodePort, + Ports: []api.ServicePort{ + { + Port: 27443, + }, + { + Port: 27444, + }, + }, + }, + }, + usage: api.ResourceList{ + api.ResourceServices: resource.MustParse("1"), + api.ResourceServicesNodePorts: resource.MustParse("2"), + }, + }, } for testName, testCase := range testCases { actual := evaluator.Usage(testCase.service) diff --git a/plugin/pkg/admission/resourcequota/admission_test.go b/plugin/pkg/admission/resourcequota/admission_test.go index 87c244c0d2bfb..4a44d28ae7b32 100644 --- a/plugin/pkg/admission/resourcequota/admission_test.go +++ b/plugin/pkg/admission/resourcequota/admission_test.go @@ -292,7 +292,10 @@ func TestAdmitHandlesOldObjects(t *testing.T) { } newService := &api.Service{ ObjectMeta: api.ObjectMeta{Name: "service", Namespace: "test"}, - Spec: api.ServiceSpec{Type: api.ServiceTypeNodePort}, + Spec: api.ServiceSpec{ + Type: api.ServiceTypeNodePort, + Ports: []api.ServicePort{{Port: 1234}}, + }, } err := handler.Admit(admission.NewAttributesRecord(newService, oldService, api.Kind("Service").WithVersion("version"), newService.Namespace, newService.Name, api.Resource("services").WithVersion("version"), "", admission.Update, nil)) if err != nil {