From 241e3dbcb7286d39bcd8257af9fef83327080b07 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 11 Aug 2017 16:21:07 +0100 Subject: [PATCH] mw/k8s: cleanups (#893) * mw/k8s: cleanups Remove some constants that aren't used any more. Make PrimaryZone private because it doesn't need to be exported. Remove test that did not cover corner case as expressed in setup.go * cleanup this as well --- middleware/kubernetes/kubernetes.go | 12 ++++++------ middleware/kubernetes/kubernetes_test.go | 10 ---------- middleware/kubernetes/reverse_test.go | 4 ++-- middleware/kubernetes/setup.go | 12 ++++-------- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/middleware/kubernetes/kubernetes.go b/middleware/kubernetes/kubernetes.go index be6fde424..c2e7d8faa 100644 --- a/middleware/kubernetes/kubernetes.go +++ b/middleware/kubernetes/kubernetes.go @@ -32,7 +32,6 @@ import ( type Kubernetes struct { Next middleware.Handler Zones []string - primaryZone int Proxy proxy.Proxy // Proxy for looking up names during the resolution process APIServerList []string APIProxy *apiProxy @@ -49,6 +48,7 @@ type Kubernetes struct { ReverseCidrs []net.IPNet Fallthrough bool + primaryZoneIndex int interfaceAddrsFunc func() net.IP autoPathSearch []string // Local search path from /etc/resolv.conf. Needed for autopath. } @@ -154,9 +154,9 @@ func (k *Kubernetes) Services(state request.Request, exact bool, opt middleware. return nil, nil, nil } -// PrimaryZone will return the first non-reverse zone being handled by this middleware -func (k *Kubernetes) PrimaryZone() string { - return k.Zones[k.primaryZone] +// primaryZone will return the first non-reverse zone being handled by this middleware +func (k *Kubernetes) primaryZone() string { + return k.Zones[k.primaryZoneIndex] } // Lookup implements the ServiceBackend interface. @@ -538,7 +538,7 @@ func (k *Kubernetes) getServiceRecordForIP(ip, name string) []msg.Service { continue } if service.Spec.ClusterIP == ip { - domain := strings.Join([]string{service.Name, service.Namespace, Svc, k.PrimaryZone()}, ".") + domain := strings.Join([]string{service.Name, service.Namespace, Svc, k.primaryZone()}, ".") return []msg.Service{{Host: domain}} } } @@ -551,7 +551,7 @@ func (k *Kubernetes) getServiceRecordForIP(ip, name string) []msg.Service { for _, eps := range ep.Subsets { for _, addr := range eps.Addresses { if addr.IP == ip { - domain := strings.Join([]string{endpointHostname(addr), ep.ObjectMeta.Name, ep.ObjectMeta.Namespace, Svc, k.PrimaryZone()}, ".") + domain := strings.Join([]string{endpointHostname(addr), ep.ObjectMeta.Name, ep.ObjectMeta.Namespace, Svc, k.primaryZone()}, ".") return []msg.Service{{Host: domain}} } } diff --git a/middleware/kubernetes/kubernetes_test.go b/middleware/kubernetes/kubernetes_test.go index 8037c0987..d9a731aef 100644 --- a/middleware/kubernetes/kubernetes_test.go +++ b/middleware/kubernetes/kubernetes_test.go @@ -10,15 +10,6 @@ import ( "k8s.io/client-go/1.5/pkg/api" ) -func TestPrimaryZone(t *testing.T) { - k := Kubernetes{Zones: []string{"inter.webs.test", "inter.nets.test"}} - expected := "inter.webs.test" - result := k.PrimaryZone() - if result != expected { - t.Errorf("Expected result '%v'. Instead got result '%v'.", expected, result) - } -} - func TestWildcard(t *testing.T) { var tests = []struct { s string @@ -104,7 +95,6 @@ func (APIConnServiceTest) ServiceList() []*api.Service { }, } return svcs - } func (APIConnServiceTest) EndpointsList() api.EndpointsList { diff --git a/middleware/kubernetes/reverse_test.go b/middleware/kubernetes/reverse_test.go index 4e145992e..75bc7da72 100644 --- a/middleware/kubernetes/reverse_test.go +++ b/middleware/kubernetes/reverse_test.go @@ -14,13 +14,13 @@ func TestIsRequestInReverseRange(t *testing.T) { }{ {"1.2.3.0/24", "4.3.2.1.in-addr.arpa.", true}, {"1.2.3.0/24", "5.3.2.1.in-addr.arpa.", true}, + {"5.6.0.0/16", "5.4.6.5.in-addr.arpa.", true}, {"1.2.3.0/24", "5.4.2.1.in-addr.arpa.", false}, {"5.6.0.0/16", "5.4.2.1.in-addr.arpa.", false}, - {"5.6.0.0/16", "5.4.6.5.in-addr.arpa.", true}, {"5.6.0.0/16", "5.6.0.1.in-addr.arpa.", false}, } - k := Kubernetes{Zones: []string{"inter.webs.test"}} + k := Kubernetes{} for _, test := range tests { _, cidr, _ := net.ParseCIDR(test.cidr) diff --git a/middleware/kubernetes/setup.go b/middleware/kubernetes/setup.go index 8b367e1a2..071db851a 100644 --- a/middleware/kubernetes/setup.go +++ b/middleware/kubernetes/setup.go @@ -87,16 +87,16 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) { } } - k8s.primaryZone = -1 + k8s.primaryZoneIndex = -1 for i, z := range k8s.Zones { if strings.HasSuffix(z, "in-addr.arpa.") || strings.HasSuffix(z, "ip6.arpa.") { continue } - k8s.primaryZone = i + k8s.primaryZoneIndex = i break } - if k8s.primaryZone == -1 { + if k8s.primaryZoneIndex == -1 { return nil, errors.New("non-reverse zone name must be given for Kubernetes") } @@ -222,8 +222,4 @@ func searchFromResolvConf() []string { return rc.Search } -const ( - defaultResyncPeriod = 5 * time.Minute - defautNdots = 0 - defaultOnNXDOMAIN = dns.RcodeSuccess -) +const defaultResyncPeriod = 5 * time.Minute