Path stuff copied over and tests added

This commit is contained in:
Miek Gieben
2016-03-21 07:20:22 +00:00
parent b6341e8b63
commit ecc5f28671
4 changed files with 73 additions and 54 deletions

View File

@@ -15,31 +15,33 @@ import (
type ( type (
Etcd struct { Etcd struct {
Next middleware.Handler Next middleware.Handler
Zones []string Zones []string
client etcdc.KeysAPI client etcdc.KeysAPI
ctx context.Context ctx context.Context
inflight *singleflight.Group inflight *singleflight.Group
PathPrefix string
} }
) )
func NewEtcd(client etcdc.KeysAPI, next middleware.Handler, zones []string) Etcd { func NewEtcd(client etcdc.KeysAPI, next middleware.Handler, zones []string) Etcd {
return Etcd{ return Etcd{
Next: next, Next: next,
Zones: zones, Zones: zones,
client: client, client: client,
ctx: context.Background(), ctx: context.Background(),
inflight: &singleflight.Group{}, inflight: &singleflight.Group{},
PathPrefix: "skydns", // TODO(miek): configurable
} }
} }
func (g Etcd) Records(name string, exact bool) ([]msg.Service, error) { func (g Etcd) Records(name string, exact bool) ([]msg.Service, error) {
path, star := msg.PathWithWildcard(name) path, star := g.PathWithWildcard(name)
r, err := g.Get(path, true) r, err := g.Get(path, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
segments := strings.Split(msg.Path(name), "/") segments := strings.Split(g.Path(name), "/")
switch { switch {
case exact && r.Node.Dir: case exact && r.Node.Dir:
return nil, nil return nil, nil

View File

@@ -2,15 +2,11 @@ package msg
import ( import (
"net" "net"
"path"
"strings" "strings"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
// PathPrefix is the prefix used to store CoreDNS data in etcd.
var PathPrefix string = "skydns"
// This *is* the rdata from a SRV record, but with a twist. // This *is* the rdata from a SRV record, but with a twist.
// Host (Target in SRV) must be a domain name, but if it looks like an IP // Host (Target in SRV) must be a domain name, but if it looks like an IP
// address (4/6), we will treat it like an IP address. // address (4/6), we will treat it like an IP address.
@@ -85,44 +81,6 @@ func (s *Service) NewPTR(name string, ttl uint32) *dns.PTR {
return &dns.PTR{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: ttl}, Ptr: dns.Fqdn(s.Host)} return &dns.PTR{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: ttl}, Ptr: dns.Fqdn(s.Host)}
} }
// As Path, but if a name contains wildcards (* or any), the name will be
// chopped of before the (first) wildcard, and we do a highler evel search and
// later find the matching names. So service.*.skydns.local, will look for all
// services under skydns.local and will later check for names that match
// service.*.skydns.local. If a wildcard is found the returned bool is true.
func PathWithWildcard(s string) (string, bool) {
l := dns.SplitDomainName(s)
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i]
}
for i, k := range l {
if k == "*" || k == "any" {
return path.Join(append([]string{"/" + PathPrefix + "/"}, l[:i]...)...), true
}
}
return path.Join(append([]string{"/" + PathPrefix + "/"}, l...)...), false
}
// Path converts a domainname to an etcd path. If s looks like service.staging.skydns.local.,
// the resulting key will be /skydns/local/skydns/staging/service .
func Path(s string) string {
l := dns.SplitDomainName(s)
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i]
}
return path.Join(append([]string{"/" + PathPrefix + "/"}, l...)...)
}
// Domain is the opposite of Path.
func Domain(s string) string {
l := strings.Split(s, "/")
// start with 1, to strip /skydns
for i, j := 1, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i]
}
return dns.Fqdn(strings.Join(l[1:len(l)-1], "."))
}
// Group checks the services in sx, it looks for a Group attribute on the shortest // Group checks the services in sx, it looks for a Group attribute on the shortest
// keys. If there are multiple shortest keys *and* the group attribute disagrees (and // keys. If there are multiple shortest keys *and* the group attribute disagrees (and
// is not empty), we don't consider it a group. // is not empty), we don't consider it a group.

46
middleware/etcd/path.go Normal file
View File

@@ -0,0 +1,46 @@
package etcd
import (
"path"
"strings"
"github.com/miekg/dns"
)
// Path converts a domainname to an etcd path. If s looks like service.staging.skydns.local.,
// the resulting key will be /skydns/local/skydns/staging/service .
func (e Etcd) Path(s string) string {
l := dns.SplitDomainName(s)
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i]
}
return path.Join(append([]string{"/" + e.PathPrefix + "/"}, l...)...)
}
// Domain is the opposite of Path.
func (e Etcd) Domain(s string) string {
l := strings.Split(s, "/")
// start with 1, to strip /skydns
for i, j := 1, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i]
}
return dns.Fqdn(strings.Join(l[1:len(l)-1], "."))
}
// As Path, but if a name contains wildcards (* or any), the name will be
// chopped of before the (first) wildcard, and we do a highler evel search and
// later find the matching names. So service.*.skydns.local, will look for all
// services under skydns.local and will later check for names that match
// service.*.skydns.local. If a wildcard is found the returned bool is true.
func (e Etcd) PathWithWildcard(s string) (string, bool) {
l := dns.SplitDomainName(s)
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
l[i], l[j] = l[j], l[i]
}
for i, k := range l {
if k == "*" || k == "any" {
return path.Join(append([]string{"/" + e.PathPrefix + "/"}, l[:i]...)...), true
}
}
return path.Join(append([]string{"/" + e.PathPrefix + "/"}, l...)...), false
}

View File

@@ -0,0 +1,13 @@
package etcd
import "testing"
func TestPath(t *testing.T) {
for path := range []string{"mydns", "skydns"} {
e := Etcd{PathPrefix: "mydns"}
result := e.Path("service.staging.skydns.local.")
if result != "/"+path+"/"+"/local/skydns/staging/service" {
t.Errorf("Failure to get domain's path with prefix: %s", path)
}
}
}