k8s/autopath: Add CNAMES (#771)

* Add unit tests & cnames

* more progress

* fix

* next mw dependent unit tests

* add tests for OnNXDOMAIN

* Add AAAA and ndots unit tests; fix request.NewWithQuestion

* Correct default value in README

* add CNAMEs to readme

* review

* fix autopath examples

* fix and test CNAME response order
This commit is contained in:
Chris O'Haver 2017-07-11 18:05:32 -04:00 committed by John Belamaric
parent 0049230a93
commit 8495e48297
7 changed files with 586 additions and 28 deletions

View file

@ -6,7 +6,6 @@ import (
"github.com/coredns/coredns/middleware"
"github.com/coredns/coredns/middleware/pkg/dnsutil"
"github.com/coredns/coredns/middleware/rewrite"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
@ -23,7 +22,6 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
// Check that query matches one of the zones served by this middleware,
// otherwise delegate to the next in the pipeline.
zone := middleware.Zones(k.Zones).Matches(state.Name())
@ -40,10 +38,10 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
// Set the zone to this specific request.
zone = state.Name()
}
records, extra, _, err := k.routeRequest(zone, state)
if k.AutoPath.Enabled && k.IsNameError(err) {
// Check for Autopath search eligibility
if k.AutoPath.Enabled && k.IsNameError(err) && (state.QType() == dns.TypeA || state.QType() == dns.TypeAAAA) {
p := k.findPodWithIP(state.IP())
for p != nil {
name, path, ok := splitSearch(zone, state.QName(), p.Namespace)
@ -53,38 +51,50 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
if (dns.CountLabel(name) - 1) < k.AutoPath.NDots {
break
}
// Search "svc.cluster.local" and "cluster.local"
origQName := state.QName()
// Search "svc.cluster.local." and "cluster.local."
for i := 0; i < 2; i++ {
path = strings.Join(dns.SplitDomainName(path)[1:], ".")
state = state.NewWithQuestion(strings.Join([]string{name, path}, "."), state.QType())
records, extra, _, err = k.routeRequest(zone, state)
newstate := state.NewWithQuestion(strings.Join([]string{name, path}, "."), state.QType())
records, extra, _, err = k.routeRequest(zone, newstate)
if !k.IsNameError(err) {
records = append(records, nil)
copy(records[1:], records)
records[0] = newCNAME(origQName, records[0].Header().Name, records[0].Header().Ttl)
break
}
}
if !k.IsNameError(err) {
break
}
// Fallthrough with the host search path (if set)
wr := rewrite.NewResponseReverter(w, r)
// Try host search path (if set) in the next middleware
apw := NewAutoPathWriter(w, r)
for _, hostsearch := range k.AutoPath.HostSearchPath {
r = state.NewWithQuestion(strings.Join([]string{name, hostsearch}, "."), state.QType()).Req
rcode, nextErr := middleware.NextOrFailure(k.Name(), k.Next, ctx, wr, r)
if rcode == dns.RcodeSuccess {
newstate := state.NewWithQuestion(strings.Join([]string{name, hostsearch}, "."), state.QType())
rcode, nextErr := middleware.NextOrFailure(k.Name(), k.Next, ctx, apw, newstate.Req)
if apw.Sent {
return rcode, nextErr
}
}
// Search . in this middleware
state = state.NewWithQuestion(strings.Join([]string{name, "."}, ""), state.QType())
records, extra, _, err = k.routeRequest(zone, state)
newstate := state.NewWithQuestion(strings.Join([]string{name, "."}, ""), state.QType())
records, extra, _, err = k.routeRequest(zone, newstate)
if !k.IsNameError(err) {
records = append(records, nil)
copy(records[1:], records)
records[0] = newCNAME(origQName, records[0].Header().Name, records[0].Header().Ttl)
break
}
// Search . in the next middleware
r = state.Req
rcode, nextErr := middleware.NextOrFailure(k.Name(), k.Next, ctx, wr, r)
if rcode == dns.RcodeNameError {
rcode = k.AutoPath.OnNXDOMAIN
apw.Rcode = k.AutoPath.OnNXDOMAIN
newstate = state.NewWithQuestion(strings.Join([]string{name, "."}, ""), state.QType())
r = newstate.Req
rcode, nextErr := middleware.NextOrFailure(k.Name(), k.Next, ctx, apw, r)
if !apw.Sent && nextErr == nil {
r = dnsutil.Dedup(r)
state.SizeAndDo(r)
r, _ = state.Scrub(r)
apw.ForceWriteMsg(r)
}
return rcode, nextErr
}
@ -115,6 +125,11 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
return dns.RcodeSuccess, nil
}
func newCNAME(name string, target string, ttl uint32) *dns.CNAME {
// TODO factor this out and put in dnsutil
return &dns.CNAME{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: ttl}, Target: dns.Fqdn(target)}
}
func (k *Kubernetes) routeRequest(zone string, state request.Request) (records []dns.RR, extra []dns.RR, debug []dns.RR, err error) {
switch state.Type() {
case "A":