* Add dnsutil.Join

* Create dnsutil.Join

Create Join helper function and move bits in the code over.
This commit is contained in:
Miek Gieben 2017-08-19 15:22:09 +01:00 committed by GitHub
parent 7c343982a6
commit 02955d7594
8 changed files with 52 additions and 14 deletions

View file

@ -4,6 +4,8 @@ import (
"path" "path"
"strings" "strings"
"github.com/coredns/coredns/middleware/pkg/dnsutil"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -24,7 +26,7 @@ func Domain(s string) string {
for i, j := 1, len(l)-1; i < j; i, j = i+1, j-1 { for i, j := 1, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i] l[i], l[j] = l[j], l[i]
} }
return dns.Fqdn(strings.Join(l[1:len(l)-1], ".")) return dnsutil.Join(l[1 : len(l)-1])
} }
// PathWithWildcard ascts as Path, but if a name contains wildcards (* or any), the name will be // PathWithWildcard ascts as Path, but if a name contains wildcards (* or any), the name will be

View file

@ -4,10 +4,10 @@ import (
"log" "log"
"net" "net"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/coredns/coredns/middleware/etcd/msg" "github.com/coredns/coredns/middleware/etcd/msg"
"github.com/coredns/coredns/middleware/pkg/dnsutil"
"github.com/coredns/coredns/middleware/proxy" "github.com/coredns/coredns/middleware/proxy"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
@ -62,7 +62,7 @@ Services:
// Chop of left most label, because that is used as the nameserver place holder // Chop of left most label, because that is used as the nameserver place holder
// and drop the right most labels that belong to zone. // and drop the right most labels that belong to zone.
// We must *also* chop of dns.stub. which means cutting two more labels. // We must *also* chop of dns.stub. which means cutting two more labels.
domain = dns.Fqdn(strings.Join(labels[1:len(labels)-dns.CountLabel(z)-2], ".")) domain = dnsutil.Join(labels[1 : len(labels)-dns.CountLabel(z)-2])
if domain == z { if domain == z {
log.Printf("[WARNING] Skipping nameserver for domain we are authoritative for: %s", domain) log.Printf("[WARNING] Skipping nameserver for domain we are authoritative for: %s", domain)
continue Services continue Services

View file

@ -14,8 +14,6 @@ Federation is only useful in conjunction with the kubernetes middleware, without
package federation package federation
import ( import (
"strings"
"github.com/coredns/coredns/middleware" "github.com/coredns/coredns/middleware"
"github.com/coredns/coredns/middleware/etcd/msg" "github.com/coredns/coredns/middleware/etcd/msg"
"github.com/coredns/coredns/middleware/pkg/dnsutil" "github.com/coredns/coredns/middleware/pkg/dnsutil"
@ -133,7 +131,7 @@ func (f *Federation) isNameFederation(name, zone string) (string, string) {
fed := labels[ll-2] fed := labels[ll-2]
if _, ok := f.f[fed]; ok { if _, ok := f.f[fed]; ok {
without := strings.Join(labels[:ll-2], ".") + "." + labels[ll-1] + "." + zone without := dnsutil.Join(labels[:ll-2]) + labels[ll-1] + "." + zone
return without, fed return without, fed
} }
return "", "" return "", ""

View file

@ -1,7 +1,7 @@
package file package file
import ( import (
"strings" "github.com/coredns/coredns/middleware/pkg/dnsutil"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -14,7 +14,7 @@ func substituteDNAME(qname, owner, target string) string {
labels := dns.SplitDomainName(qname) labels := dns.SplitDomainName(qname)
labels = append(labels[0:len(labels)-dns.CountLabel(owner)], dns.SplitDomainName(target)...) labels = append(labels[0:len(labels)-dns.CountLabel(owner)], dns.SplitDomainName(target)...)
return strings.Join(labels, ".") + "." return dnsutil.Join(labels)
} }
return "" return ""

View file

@ -1,9 +1,8 @@
package kubernetes package kubernetes
import ( import (
"strings"
"github.com/coredns/coredns/middleware/etcd/msg" "github.com/coredns/coredns/middleware/etcd/msg"
"github.com/coredns/coredns/middleware/pkg/dnsutil"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
) )
@ -36,8 +35,8 @@ func (k *Kubernetes) Federations(state request.Request, fname, fzone string) (ms
lr := node.Labels[LabelRegion] lr := node.Labels[LabelRegion]
if r.endpoint == "" { if r.endpoint == "" {
return msg.Service{Host: strings.Join([]string{r.service, r.namespace, fname, r.podOrSvc, lz, lr, fzone}, ".")}, nil return msg.Service{Host: dnsutil.Join([]string{r.service, r.namespace, fname, r.podOrSvc, lz, lr, fzone})}, nil
} }
return msg.Service{Host: strings.Join([]string{r.endpoint, r.service, r.namespace, fname, r.podOrSvc, lz, lr, fzone}, ".")}, nil return msg.Service{Host: dnsutil.Join([]string{r.endpoint, r.service, r.namespace, fname, r.podOrSvc, lz, lr, fzone})}, nil
} }

View file

@ -508,7 +508,7 @@ func (k *Kubernetes) getServiceRecordForIP(ip, name string) []msg.Service {
continue continue
} }
if service.Spec.ClusterIP == ip { if service.Spec.ClusterIP == ip {
domain := strings.Join([]string{service.Name, service.Namespace, Svc, k.primaryZone()}, ".") domain := dnsutil.Join([]string{service.Name, service.Namespace, Svc, k.primaryZone()})
return []msg.Service{{Host: domain}} return []msg.Service{{Host: domain}}
} }
} }
@ -521,7 +521,7 @@ func (k *Kubernetes) getServiceRecordForIP(ip, name string) []msg.Service {
for _, eps := range ep.Subsets { for _, eps := range ep.Subsets {
for _, addr := range eps.Addresses { for _, addr := range eps.Addresses {
if addr.IP == ip { if addr.IP == ip {
domain := strings.Join([]string{endpointHostname(addr), ep.ObjectMeta.Name, ep.ObjectMeta.Namespace, Svc, k.primaryZone()}, ".") domain := dnsutil.Join([]string{endpointHostname(addr), ep.ObjectMeta.Name, ep.ObjectMeta.Namespace, Svc, k.primaryZone()})
return []msg.Service{{Host: domain}} return []msg.Service{{Host: domain}}
} }
} }

View file

@ -0,0 +1,19 @@
package dnsutil
import (
"strings"
"github.com/miekg/dns"
)
// Join joins labels to form a fully qualified domain name. If the last label is
// the root label it is ignored. Not other syntax checks are performed.
func Join(labels []string) string {
ll := len(labels)
if labels[ll-1] == "." {
s := strings.Join(labels[:ll-1], ".")
return dns.Fqdn(s)
}
s := strings.Join(labels, ".")
return dns.Fqdn(s)
}

View file

@ -0,0 +1,20 @@
package dnsutil
import "testing"
func TestJoin(t *testing.T) {
tests := []struct {
in []string
out string
}{
{[]string{"bla", "bliep", "example", "org"}, "bla.bliep.example.org."},
{[]string{"example", "."}, "example."},
{[]string{"."}, "."},
}
for i, tc := range tests {
if x := Join(tc.in); x != tc.out {
t.Errorf("Test %d, expected %s, got %s", i, tc.out, x)
}
}
}