From 9f5ed978b98e784cb94468677b90efcbe56ff34b Mon Sep 17 00:00:00 2001 From: chentao1596 Date: Fri, 24 Mar 2017 10:17:39 +0800 Subject: [PATCH 1/2] use interface instead of implementation --- core/pkg/ingress/controller/controller.go | 2 +- core/pkg/ingress/controller/named_port.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/pkg/ingress/controller/controller.go b/core/pkg/ingress/controller/controller.go index 036e38d501..a2ca86bd18 100644 --- a/core/pkg/ingress/controller/controller.go +++ b/core/pkg/ingress/controller/controller.go @@ -111,7 +111,7 @@ type GenericController struct { // Configuration contains all the settings required by an Ingress controller type Configuration struct { - Client *clientset.Clientset + Client clientset.Interface ResyncPeriod time.Duration DefaultService string diff --git a/core/pkg/ingress/controller/named_port.go b/core/pkg/ingress/controller/named_port.go index abc1e6ef2a..da28df1359 100644 --- a/core/pkg/ingress/controller/named_port.go +++ b/core/pkg/ingress/controller/named_port.go @@ -38,7 +38,7 @@ import ( func (ic *GenericController) checkSvcForUpdate(svc *api.Service) error { // get the pods associated with the service // TODO: switch this to a watch - pods, err := ic.cfg.Client.Pods(svc.Namespace).List(api.ListOptions{ + pods, err := ic.cfg.Client.Core().Pods(svc.Namespace).List(api.ListOptions{ LabelSelector: labels.Set(svc.Spec.Selector).AsSelector(), }) @@ -82,7 +82,7 @@ func (ic *GenericController) checkSvcForUpdate(svc *api.Service) error { if len(namedPorts) > 0 && !reflect.DeepEqual(curNamedPort, namedPorts) { data, _ := json.Marshal(namedPorts) - newSvc, err := ic.cfg.Client.Services(svc.Namespace).Get(svc.Name) + newSvc, err := ic.cfg.Client.Core().Services(svc.Namespace).Get(svc.Name) if err != nil { return fmt.Errorf("error getting service %v/%v: %v", svc.Namespace, svc.Name, err) } @@ -93,7 +93,7 @@ func (ic *GenericController) checkSvcForUpdate(svc *api.Service) error { newSvc.ObjectMeta.Annotations[service.NamedPortAnnotation] = string(data) glog.Infof("updating service %v with new named port mappings", svc.Name) - _, err = ic.cfg.Client.Services(svc.Namespace).Update(newSvc) + _, err = ic.cfg.Client.Core().Services(svc.Namespace).Update(newSvc) if err != nil { return fmt.Errorf("error syncing service %v/%v: %v", svc.Namespace, svc.Name, err) } From 740f7caeb838f3f8e532a2fa4d7dfdb02f2719b2 Mon Sep 17 00:00:00 2001 From: chentao1596 Date: Fri, 24 Mar 2017 11:02:10 +0800 Subject: [PATCH 2/2] add unit test case for named_port --- .../pkg/ingress/controller/named_port_test.go | 191 ++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 core/pkg/ingress/controller/named_port_test.go diff --git a/core/pkg/ingress/controller/named_port_test.go b/core/pkg/ingress/controller/named_port_test.go new file mode 100644 index 0000000000..c92ad9806e --- /dev/null +++ b/core/pkg/ingress/controller/named_port_test.go @@ -0,0 +1,191 @@ +/* +Copyright 2017 The Kubernetes Authors. + +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 controller + +import ( + "reflect" + "testing" + + "k8s.io/ingress/core/pkg/ingress/annotations/service" + "k8s.io/kubernetes/pkg/api" + testclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" + "k8s.io/kubernetes/pkg/util/intstr" +) + +func buildSimpleClientSet() *testclient.Clientset { + return testclient.NewSimpleClientset( + &api.PodList{Items: []api.Pod{ + { + ObjectMeta: api.ObjectMeta{ + Name: "foo1", + Namespace: api.NamespaceDefault, + Labels: map[string]string{ + "lable_sig": "foo_pod", + }, + }, + Spec: api.PodSpec{ + NodeName: "foo_node_1", + Containers: []api.Container{ + { + Ports: []api.ContainerPort{ + { + Name: "foo1_named_port_c1", + Protocol: api.ProtocolTCP, + ContainerPort: 80, + }, + }, + }, + }, + }, + }, + { + ObjectMeta: api.ObjectMeta{ + Name: "foo1", + Namespace: api.NamespaceSystem, + Labels: map[string]string{ + "lable_sig": "foo_pod", + }, + }, + }, + }}, + &api.ServiceList{Items: []api.Service{ + { + ObjectMeta: api.ObjectMeta{ + Namespace: api.NamespaceDefault, + Name: "named_port_test_service", + }, + }, + }}, + ) +} + +func buildGenericController() *GenericController { + return &GenericController{ + cfg: &Configuration{ + Client: buildSimpleClientSet(), + }, + } +} + +func buildService() *api.Service { + return &api.Service{ + ObjectMeta: api.ObjectMeta{ + Namespace: api.NamespaceSystem, + Name: "named_port_test_service", + }, + + Spec: api.ServiceSpec{ + ClusterIP: "10.10.10.10", + }, + } +} + +func TestCheckSvcForUpdate(t *testing.T) { + foos := []struct { + n string + ns string + sps []api.ServicePort + sl map[string]string + er string + }{ + { + "pods_have_not_been_found_in_this_namespace", + api.NamespaceSystem, + []api.ServicePort{ + {Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("foo1_named_port_c1")}, + {Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)}, + {Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")}, + }, + map[string]string{ + "lable_sig": "foo_pod", + }, + "", + }, + { + "ports_have_not_been_found_in_this_pod", + api.NamespaceDefault, + []api.ServicePort{ + {Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("foo1_named_port_cXX")}, + {Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)}, + {Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")}, + }, + map[string]string{ + "lable_sig": "foo_pod", + }, + "", + }, + + { + "ports_fixed", + api.NamespaceDefault, + []api.ServicePort{ + {Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(80)}, + {Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)}, + {Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")}, + }, + map[string]string{ + "lable_sig": "foo_pod", + }, + "", + }, + { + "nil_selector", + api.NamespaceDefault, + []api.ServicePort{ + {Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("foo1_named_port_c1")}, + {Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)}, + {Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")}, + }, + nil, + "{\"foo1_named_port_c1\":\"80\"}", + }, + { + "normal_update", + api.NamespaceDefault, + []api.ServicePort{ + {Name: "foo_port_1", Port: 8080, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("foo1_named_port_c1")}, + {Name: "foo_port_2", Port: 8181, Protocol: api.ProtocolTCP, TargetPort: intstr.FromInt(81)}, + {Name: "foo_port_3", Port: 8282, Protocol: api.ProtocolTCP, TargetPort: intstr.FromString("")}, + }, + map[string]string{ + "lable_sig": "foo_pod", + }, + "{\"foo1_named_port_c1\":\"80\"}", + }, + } + + for _, foo := range foos { + t.Run(foo.n, func(t *testing.T) { + gc := buildGenericController() + s := buildService() + s.SetNamespace(foo.ns) + s.Spec.Ports = foo.sps + s.Spec.Selector = foo.sl + + err := gc.checkSvcForUpdate(s) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + rs, _ := gc.cfg.Client.Core().Services(api.NamespaceDefault).Get("named_port_test_service") + rr := rs.ObjectMeta.Annotations[service.NamedPortAnnotation] + if !reflect.DeepEqual(rr, foo.er) { + t.Errorf("Returned %s, but expected %s for %s", rr, foo.er, foo.n) + } + }) + } +}