diff --git a/middleware/kubernetes/kubernetes.go b/middleware/kubernetes/kubernetes.go index 2ec90b4d2..1b07d30d5 100644 --- a/middleware/kubernetes/kubernetes.go +++ b/middleware/kubernetes/kubernetes.go @@ -11,7 +11,6 @@ import ( "github.com/coredns/coredns/middleware" "github.com/coredns/coredns/middleware/etcd/msg" - "github.com/coredns/coredns/middleware/pkg/dnsutil" dnsstrings "github.com/coredns/coredns/middleware/pkg/strings" "github.com/coredns/coredns/middleware/proxy" "github.com/coredns/coredns/request" @@ -159,28 +158,6 @@ func (k *Kubernetes) PrimaryZone() string { return k.Zones[k.primaryZone] } -// Reverse implements the ServiceBackend interface. -func (k *Kubernetes) Reverse(state request.Request, exact bool, opt middleware.Options) ([]msg.Service, []msg.Service, error) { - - ip := dnsutil.ExtractAddressFromReverse(state.Name()) - if ip == "" { - return nil, nil, nil - } - - records := k.getServiceRecordForIP(ip, state.Name()) - return records, nil, nil -} - -func (k *Kubernetes) isRequestInReverseRange(name string) bool { - ip := dnsutil.ExtractAddressFromReverse(name) - for _, c := range k.ReverseCidrs { - if c.Contains(net.ParseIP(ip)) { - return true - } - } - return false -} - // Lookup implements the ServiceBackend interface. func (k *Kubernetes) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) { return k.Proxy.Lookup(state, name, typ) diff --git a/middleware/kubernetes/kubernetes_test.go b/middleware/kubernetes/kubernetes_test.go index d31e4b9e6..a19cb16f6 100644 --- a/middleware/kubernetes/kubernetes_test.go +++ b/middleware/kubernetes/kubernetes_test.go @@ -2,7 +2,6 @@ package kubernetes import ( "errors" - "net" "reflect" "testing" @@ -33,33 +32,6 @@ func TestPrimaryZone(t *testing.T) { } } -func TestIsRequestInReverseRange(t *testing.T) { - - tests := []struct { - cidr string - name string - expected bool - }{ - {"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}, - {"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"}} - - for _, test := range tests { - _, cidr, _ := net.ParseCIDR(test.cidr) - k.ReverseCidrs = []net.IPNet{*cidr} - result := k.isRequestInReverseRange(test.name) - if result != test.expected { - t.Errorf("Expected '%v' for '%v' in %v.", test.expected, test.name, test.cidr) - } - } -} - func TestIsNameError(t *testing.T) { k := Kubernetes{Zones: []string{"inter.webs.test"}} if !k.IsNameError(errNoItems) { @@ -77,7 +49,7 @@ func TestIsNameError(t *testing.T) { } } -func TestSymbolContainsWildcard(t *testing.T) { +func TestWildcard(t *testing.T) { var tests = []struct { s string expected bool diff --git a/middleware/kubernetes/reverse.go b/middleware/kubernetes/reverse.go new file mode 100644 index 000000000..3edb026f8 --- /dev/null +++ b/middleware/kubernetes/reverse.go @@ -0,0 +1,32 @@ +package kubernetes + +import ( + "net" + + "github.com/coredns/coredns/middleware" + "github.com/coredns/coredns/middleware/etcd/msg" + "github.com/coredns/coredns/middleware/pkg/dnsutil" + "github.com/coredns/coredns/request" +) + +// Reverse implements the ServiceBackend interface. +func (k *Kubernetes) Reverse(state request.Request, exact bool, opt middleware.Options) ([]msg.Service, []msg.Service, error) { + + ip := dnsutil.ExtractAddressFromReverse(state.Name()) + if ip == "" { + return nil, nil, nil + } + + records := k.getServiceRecordForIP(ip, state.Name()) + return records, nil, nil +} + +func (k *Kubernetes) isRequestInReverseRange(name string) bool { + ip := dnsutil.ExtractAddressFromReverse(name) + for _, c := range k.ReverseCidrs { + if c.Contains(net.ParseIP(ip)) { + return true + } + } + return false +} diff --git a/middleware/kubernetes/reverse_test.go b/middleware/kubernetes/reverse_test.go new file mode 100644 index 000000000..4e145992e --- /dev/null +++ b/middleware/kubernetes/reverse_test.go @@ -0,0 +1,33 @@ +package kubernetes + +import ( + "net" + "testing" +) + +func TestIsRequestInReverseRange(t *testing.T) { + + tests := []struct { + cidr string + name string + expected bool + }{ + {"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}, + {"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"}} + + for _, test := range tests { + _, cidr, _ := net.ParseCIDR(test.cidr) + k.ReverseCidrs = []net.IPNet{*cidr} + result := k.isRequestInReverseRange(test.name) + if result != test.expected { + t.Errorf("Expected '%v' for '%v' in %v.", test.expected, test.name, test.cidr) + } + } +}