mirror of
https://github.com/coredns/coredns.git
synced 2025-12-15 06:45:11 -05:00
ServiceBackend interface (#369)
* Add ServiceBackend interface This adds a ServiceBackend interface that is shared between etcd/etcd3 (later) and kubernetes, leading to a massive reduction in code. When returning the specific records from their backend. Fixes #273
This commit is contained in:
@@ -39,7 +39,7 @@ etcd [ZONES...] {
|
||||
pointing to external names. If you want CoreDNS to act as a proxy for clients, you'll need to add
|
||||
the proxy middleware.
|
||||
* `tls` followed the cert, key and the CA's cert filenames.
|
||||
* `debug` allow debug queries. Prefix the name with `o-o.debug.` to retrieve extra information in the
|
||||
* `debug` allows for debug queries. Prefix the name with `o-o.debug.` to retrieve extra information in the
|
||||
additional section of the reply in the form of TXT records.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/miekg/coredns/middleware/etcd/msg"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
import "strings"
|
||||
|
||||
const debugName = "o-o.debug."
|
||||
|
||||
@@ -24,34 +18,3 @@ func isDebug(name string) string {
|
||||
}
|
||||
return name[len(debugName):]
|
||||
}
|
||||
|
||||
// servicesToTxt puts debug in TXT RRs.
|
||||
func servicesToTxt(debug []msg.Service) []dns.RR {
|
||||
if debug == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
rr := make([]dns.RR, len(debug))
|
||||
for i, d := range debug {
|
||||
rr[i] = d.RR()
|
||||
}
|
||||
return rr
|
||||
}
|
||||
|
||||
func errorToTxt(err error) dns.RR {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
msg := err.Error()
|
||||
if len(msg) > 255 {
|
||||
msg = msg[:255]
|
||||
}
|
||||
t := new(dns.TXT)
|
||||
t.Hdr.Class = dns.ClassCHAOS
|
||||
t.Hdr.Ttl = 0
|
||||
t.Hdr.Rrtype = dns.TypeTXT
|
||||
t.Hdr.Name = "."
|
||||
|
||||
t.Txt = []string{msg}
|
||||
return t
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestIsDebug(t *testing.T) {
|
||||
|
||||
func TestDebugLookup(t *testing.T) {
|
||||
etc := newEtcdMiddleware()
|
||||
etc.Debug = true
|
||||
etc.Debugging = true
|
||||
|
||||
for _, serv := range servicesDebug {
|
||||
set(t, etc, serv.Key, 0, serv)
|
||||
|
||||
@@ -11,8 +11,10 @@ import (
|
||||
"github.com/miekg/coredns/middleware/etcd/msg"
|
||||
"github.com/miekg/coredns/middleware/pkg/singleflight"
|
||||
"github.com/miekg/coredns/middleware/proxy"
|
||||
"github.com/miekg/coredns/request"
|
||||
|
||||
etcdc "github.com/coreos/etcd/client"
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
@@ -26,17 +28,47 @@ type Etcd struct {
|
||||
Ctx context.Context
|
||||
Inflight *singleflight.Group
|
||||
Stubmap *map[string]proxy.Proxy // list of proxies for stub resolving.
|
||||
Debug bool // Do we allow debug queries.
|
||||
Debugging bool // Do we allow debug queries.
|
||||
|
||||
endpoints []string // Stored here as well, to aid in testing.
|
||||
}
|
||||
|
||||
// Records looks up records in etcd. If exact is true, it will lookup just
|
||||
// this name. This is used when find matches when completing SRV lookups
|
||||
// for instance.
|
||||
// Services implements the ServiceBackend interface.
|
||||
func (e *Etcd) Services(state request.Request, exact bool, opt middleware.Options) (services, debug []msg.Service, err error) {
|
||||
services, err = e.Records(state.Name(), exact)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if opt.Debug != "" {
|
||||
debug = services
|
||||
}
|
||||
services = msg.Group(services)
|
||||
return
|
||||
}
|
||||
|
||||
// Lookup implements the ServiceBackend interface.
|
||||
func (e *Etcd) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
|
||||
return e.Proxy.Lookup(state, name, typ)
|
||||
}
|
||||
|
||||
// IsNameError implements the ServiceBackend interface.
|
||||
func (e *Etcd) IsNameError(err error) bool {
|
||||
if ee, ok := err.(etcdc.Error); ok && ee.Code == etcdc.ErrorCodeKeyNotFound {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Debug implements the ServiceBackend interface.
|
||||
func (e *Etcd) Debug() string {
|
||||
return e.PathPrefix
|
||||
}
|
||||
|
||||
// Records looks up records in etcd. If exact is true, it will lookup just this
|
||||
// name. This is used when find matches when completing SRV lookups for instance.
|
||||
func (e *Etcd) Records(name string, exact bool) ([]msg.Service, error) {
|
||||
path, star := msg.PathWithWildcard(name, e.PathPrefix)
|
||||
r, err := e.Get(path, true)
|
||||
r, err := e.get(path, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -51,8 +83,8 @@ func (e *Etcd) Records(name string, exact bool) ([]msg.Service, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Get is a wrapper for client.Get that uses SingleInflight to suppress multiple outstanding queries.
|
||||
func (e *Etcd) Get(path string, recursive bool) (*etcdc.Response, error) {
|
||||
// get is a wrapper for client.Get that uses SingleInflight to suppress multiple outstanding queries.
|
||||
func (e *Etcd) get(path string, recursive bool) (*etcdc.Response, error) {
|
||||
resp, err := e.Inflight.Do(path, func() (interface{}, error) {
|
||||
ctx, cancel := context.WithTimeout(e.Ctx, etcdTimeout)
|
||||
defer cancel()
|
||||
@@ -76,7 +108,7 @@ func (e *Etcd) Get(path string, recursive bool) (*etcdc.Response, error) {
|
||||
|
||||
// loopNodes recursively loops through the nodes and returns all the values. The nodes' keyname
|
||||
// will be match against any wildcards when star is true.
|
||||
func (e Etcd) loopNodes(ns []*etcdc.Node, nameParts []string, star bool, bx map[msg.Service]bool) (sx []msg.Service, err error) {
|
||||
func (e *Etcd) loopNodes(ns []*etcdc.Node, nameParts []string, star bool, bx map[msg.Service]bool) (sx []msg.Service, err error) {
|
||||
if bx == nil {
|
||||
bx = make(map[msg.Service]bool)
|
||||
}
|
||||
@@ -145,18 +177,8 @@ func (e *Etcd) TTL(node *etcdc.Node, serv *msg.Service) uint32 {
|
||||
return serv.TTL
|
||||
}
|
||||
|
||||
// etcNameError checks if the error is ErrorCodeKeyNotFound from etcd.
|
||||
func isEtcdNameError(err error) bool {
|
||||
if e, ok := err.(etcdc.Error); ok && e.Code == etcdc.ErrorCodeKeyNotFound {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const (
|
||||
priority = 10 // default priority when nothing is set
|
||||
ttl = 300 // default ttl when nothing is set
|
||||
minTTL = 60
|
||||
hostmaster = "hostmaster"
|
||||
etcdTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
@@ -14,13 +14,13 @@ import (
|
||||
|
||||
// ServeDNS implements the middleware.Handler interface.
|
||||
func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
opt := Options{}
|
||||
opt := middleware.Options{}
|
||||
state := request.Request{W: w, Req: r}
|
||||
if state.QClass() != dns.ClassINET {
|
||||
return dns.RcodeServerFailure, fmt.Errorf("can only deal with ClassINET")
|
||||
}
|
||||
name := state.Name()
|
||||
if e.Debug {
|
||||
if e.Debugging {
|
||||
if debug := isDebug(name); debug != "" {
|
||||
opt.Debug = r.Question[0].Name
|
||||
state.Clear()
|
||||
@@ -58,30 +58,30 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
|
||||
)
|
||||
switch state.Type() {
|
||||
case "A":
|
||||
records, debug, err = e.A(zone, state, nil, opt)
|
||||
records, debug, err = middleware.A(e, zone, state, nil, opt)
|
||||
case "AAAA":
|
||||
records, debug, err = e.AAAA(zone, state, nil, opt)
|
||||
records, debug, err = middleware.AAAA(e, zone, state, nil, opt)
|
||||
case "TXT":
|
||||
records, debug, err = e.TXT(zone, state, opt)
|
||||
records, debug, err = middleware.TXT(e, zone, state, opt)
|
||||
case "CNAME":
|
||||
records, debug, err = e.CNAME(zone, state, opt)
|
||||
records, debug, err = middleware.CNAME(e, zone, state, opt)
|
||||
case "PTR":
|
||||
records, debug, err = e.PTR(zone, state, opt)
|
||||
records, debug, err = middleware.PTR(e, zone, state, opt)
|
||||
case "MX":
|
||||
records, extra, debug, err = e.MX(zone, state, opt)
|
||||
records, extra, debug, err = middleware.MX(e, zone, state, opt)
|
||||
case "SRV":
|
||||
records, extra, debug, err = e.SRV(zone, state, opt)
|
||||
records, extra, debug, err = middleware.SRV(e, zone, state, opt)
|
||||
case "SOA":
|
||||
records, debug, err = e.SOA(zone, state, opt)
|
||||
records, debug, err = middleware.SOA(e, zone, state, opt)
|
||||
case "NS":
|
||||
if state.Name() == zone {
|
||||
records, extra, debug, err = e.NS(zone, state, opt)
|
||||
records, extra, debug, err = middleware.NS(e, zone, state, opt)
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
default:
|
||||
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
|
||||
_, debug, err = e.A(zone, state, nil, opt)
|
||||
_, debug, err = middleware.A(e, zone, state, nil, opt)
|
||||
}
|
||||
|
||||
if opt.Debug != "" {
|
||||
@@ -90,15 +90,15 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
|
||||
state.Req.Question[0].Name = opt.Debug
|
||||
}
|
||||
|
||||
if isEtcdNameError(err) {
|
||||
return e.Err(zone, dns.RcodeNameError, state, debug, err, opt)
|
||||
if e.IsNameError(err) {
|
||||
return middleware.BackendError(e, zone, dns.RcodeNameError, state, debug, err, opt)
|
||||
}
|
||||
if err != nil {
|
||||
return e.Err(zone, dns.RcodeServerFailure, state, debug, err, opt)
|
||||
return middleware.BackendError(e, zone, dns.RcodeServerFailure, state, debug, err, opt)
|
||||
}
|
||||
|
||||
if len(records) == 0 {
|
||||
return e.Err(zone, dns.RcodeSuccess, state, debug, err, opt)
|
||||
return middleware.BackendError(e, zone, dns.RcodeSuccess, state, debug, err, opt)
|
||||
}
|
||||
|
||||
m := new(dns.Msg)
|
||||
@@ -107,7 +107,7 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
|
||||
m.Answer = append(m.Answer, records...)
|
||||
m.Extra = append(m.Extra, extra...)
|
||||
if opt.Debug != "" {
|
||||
m.Extra = append(m.Extra, servicesToTxt(debug)...)
|
||||
m.Extra = append(m.Extra, middleware.ServicesToTxt(debug)...)
|
||||
}
|
||||
|
||||
m = dnsutil.Dedup(m)
|
||||
@@ -119,22 +119,3 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
|
||||
|
||||
// Name implements the Handler interface.
|
||||
func (e *Etcd) Name() string { return "etcd" }
|
||||
|
||||
// Err write an error response to the client.
|
||||
func (e *Etcd) Err(zone string, rcode int, state request.Request, debug []msg.Service, err error, opt Options) (int, error) {
|
||||
m := new(dns.Msg)
|
||||
m.SetRcode(state.Req, rcode)
|
||||
m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
|
||||
m.Ns, _, _ = e.SOA(zone, state, opt)
|
||||
if opt.Debug != "" {
|
||||
m.Extra = servicesToTxt(debug)
|
||||
txt := errorToTxt(err)
|
||||
if txt != nil {
|
||||
m.Extra = append(m.Extra, errorToTxt(err))
|
||||
}
|
||||
}
|
||||
state.SizeAndDo(m)
|
||||
state.W.WriteMsg(m)
|
||||
// Return success as the rcode to signal we have written to the client.
|
||||
return dns.RcodeSuccess, nil
|
||||
}
|
||||
|
||||
@@ -1,414 +0,0 @@
|
||||
package etcd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/middleware/etcd/msg"
|
||||
"github.com/miekg/coredns/middleware/pkg/dnsutil"
|
||||
"github.com/miekg/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// Options are extra options that can be specified for a lookup.
|
||||
type Options struct {
|
||||
Debug string // This is a debug query. A query prefixed with debug.o-o
|
||||
}
|
||||
|
||||
func (e Etcd) records(state request.Request, exact bool, opt Options) (services, debug []msg.Service, err error) {
|
||||
services, err = e.Records(state.Name(), exact)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if opt.Debug != "" {
|
||||
debug = services
|
||||
}
|
||||
services = msg.Group(services)
|
||||
return
|
||||
}
|
||||
|
||||
// A returns A records from etcd or an error.
|
||||
func (e Etcd) A(zone string, state request.Request, previousRecords []dns.RR, opt Options) (records []dns.RR, debug []msg.Service, err error) {
|
||||
services, debug, err := e.records(state, false, opt)
|
||||
if err != nil {
|
||||
return nil, debug, err
|
||||
}
|
||||
|
||||
for _, serv := range services {
|
||||
ip := net.ParseIP(serv.Host)
|
||||
switch {
|
||||
case ip == nil:
|
||||
// TODO(miek): lowercasing? Should lowercase in everything see #85
|
||||
if middleware.Name(state.Name()).Matches(dns.Fqdn(serv.Host)) {
|
||||
// x CNAME x is a direct loop, don't add those
|
||||
continue
|
||||
}
|
||||
|
||||
newRecord := serv.NewCNAME(state.QName(), serv.Host)
|
||||
if len(previousRecords) > 7 {
|
||||
// don't add it, and just continue
|
||||
continue
|
||||
}
|
||||
if dnsutil.DuplicateCNAME(newRecord, previousRecords) {
|
||||
continue
|
||||
}
|
||||
|
||||
state1 := state.NewWithQuestion(serv.Host, state.QType())
|
||||
nextRecords, nextDebug, err := e.A(zone, state1, append(previousRecords, newRecord), opt)
|
||||
|
||||
if err == nil {
|
||||
// Not only have we found something we should add the CNAME and the IP addresses.
|
||||
if len(nextRecords) > 0 {
|
||||
records = append(records, newRecord)
|
||||
records = append(records, nextRecords...)
|
||||
debug = append(debug, nextDebug...)
|
||||
}
|
||||
continue
|
||||
}
|
||||
// This means we can not complete the CNAME, try to look else where.
|
||||
target := newRecord.Target
|
||||
if dns.IsSubDomain(zone, target) {
|
||||
// We should already have found it
|
||||
continue
|
||||
}
|
||||
m1, e1 := e.Proxy.Lookup(state, target, state.QType())
|
||||
if e1 != nil {
|
||||
debugMsg := msg.Service{Key: msg.Path(target, e.PathPrefix), Host: target, Text: " IN " + state.Type() + ": " + e1.Error()}
|
||||
debug = append(debug, debugMsg)
|
||||
continue
|
||||
}
|
||||
// Len(m1.Answer) > 0 here is well?
|
||||
records = append(records, newRecord)
|
||||
records = append(records, m1.Answer...)
|
||||
continue
|
||||
case ip.To4() != nil:
|
||||
records = append(records, serv.NewA(state.QName(), ip.To4()))
|
||||
case ip.To4() == nil:
|
||||
// nodata?
|
||||
}
|
||||
}
|
||||
return records, debug, nil
|
||||
}
|
||||
|
||||
// AAAA returns AAAA records from etcd or an error.
|
||||
func (e Etcd) AAAA(zone string, state request.Request, previousRecords []dns.RR, opt Options) (records []dns.RR, debug []msg.Service, err error) {
|
||||
services, debug, err := e.records(state, false, opt)
|
||||
if err != nil {
|
||||
return nil, debug, err
|
||||
}
|
||||
|
||||
for _, serv := range services {
|
||||
ip := net.ParseIP(serv.Host)
|
||||
switch {
|
||||
case ip == nil:
|
||||
// Try to resolve as CNAME if it's not an IP, but only if we don't create loops.
|
||||
if middleware.Name(state.Name()).Matches(dns.Fqdn(serv.Host)) {
|
||||
// x CNAME x is a direct loop, don't add those
|
||||
continue
|
||||
}
|
||||
|
||||
newRecord := serv.NewCNAME(state.QName(), serv.Host)
|
||||
if len(previousRecords) > 7 {
|
||||
// don't add it, and just continue
|
||||
continue
|
||||
}
|
||||
if dnsutil.DuplicateCNAME(newRecord, previousRecords) {
|
||||
continue
|
||||
}
|
||||
|
||||
state1 := state.NewWithQuestion(serv.Host, state.QType())
|
||||
nextRecords, nextDebug, err := e.AAAA(zone, state1, append(previousRecords, newRecord), opt)
|
||||
|
||||
if err == nil {
|
||||
// Not only have we found something we should add the CNAME and the IP addresses.
|
||||
if len(nextRecords) > 0 {
|
||||
records = append(records, newRecord)
|
||||
records = append(records, nextRecords...)
|
||||
debug = append(debug, nextDebug...)
|
||||
}
|
||||
continue
|
||||
}
|
||||
// This means we can not complete the CNAME, try to look else where.
|
||||
target := newRecord.Target
|
||||
if dns.IsSubDomain(zone, target) {
|
||||
// We should already have found it
|
||||
continue
|
||||
}
|
||||
m1, e1 := e.Proxy.Lookup(state, target, state.QType())
|
||||
if e1 != nil {
|
||||
debugMsg := msg.Service{Key: msg.Path(target, e.PathPrefix), Host: target, Text: " IN " + state.Type() + ": " + e1.Error()}
|
||||
debug = append(debug, debugMsg)
|
||||
continue
|
||||
}
|
||||
// Len(m1.Answer) > 0 here is well?
|
||||
records = append(records, newRecord)
|
||||
records = append(records, m1.Answer...)
|
||||
continue
|
||||
// both here again
|
||||
case ip.To4() != nil:
|
||||
// nada?
|
||||
case ip.To4() == nil:
|
||||
records = append(records, serv.NewAAAA(state.QName(), ip.To16()))
|
||||
}
|
||||
}
|
||||
return records, debug, nil
|
||||
}
|
||||
|
||||
// SRV returns SRV records from etcd.
|
||||
// If the Target is not a name but an IP address, a name is created on the fly.
|
||||
func (e Etcd) SRV(zone string, state request.Request, opt Options) (records, extra []dns.RR, debug []msg.Service, err error) {
|
||||
services, debug, err := e.records(state, false, opt)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
// Looping twice to get the right weight vs priority
|
||||
w := make(map[int]int)
|
||||
for _, serv := range services {
|
||||
weight := 100
|
||||
if serv.Weight != 0 {
|
||||
weight = serv.Weight
|
||||
}
|
||||
if _, ok := w[serv.Priority]; !ok {
|
||||
w[serv.Priority] = weight
|
||||
continue
|
||||
}
|
||||
w[serv.Priority] += weight
|
||||
}
|
||||
lookup := make(map[string]bool)
|
||||
for _, serv := range services {
|
||||
w1 := 100.0 / float64(w[serv.Priority])
|
||||
if serv.Weight == 0 {
|
||||
w1 *= 100
|
||||
} else {
|
||||
w1 *= float64(serv.Weight)
|
||||
}
|
||||
weight := uint16(math.Floor(w1))
|
||||
ip := net.ParseIP(serv.Host)
|
||||
switch {
|
||||
case ip == nil:
|
||||
srv := serv.NewSRV(state.QName(), weight)
|
||||
records = append(records, srv)
|
||||
|
||||
if _, ok := lookup[srv.Target]; ok {
|
||||
break
|
||||
}
|
||||
|
||||
lookup[srv.Target] = true
|
||||
|
||||
if !dns.IsSubDomain(zone, srv.Target) {
|
||||
m1, e1 := e.Proxy.Lookup(state, srv.Target, dns.TypeA)
|
||||
if e1 == nil {
|
||||
extra = append(extra, m1.Answer...)
|
||||
} else {
|
||||
debugMsg := msg.Service{Key: msg.Path(srv.Target, e.PathPrefix), Host: srv.Target, Text: " IN A: " + e1.Error()}
|
||||
debug = append(debug, debugMsg)
|
||||
}
|
||||
|
||||
m1, e1 = e.Proxy.Lookup(state, srv.Target, dns.TypeAAAA)
|
||||
if e1 == nil {
|
||||
// If we have seen CNAME's we *assume* that they are already added.
|
||||
for _, a := range m1.Answer {
|
||||
if _, ok := a.(*dns.CNAME); !ok {
|
||||
extra = append(extra, a)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debugMsg := msg.Service{Key: msg.Path(srv.Target, e.PathPrefix), Host: srv.Target, Text: " IN AAAA: " + e1.Error()}
|
||||
debug = append(debug, debugMsg)
|
||||
}
|
||||
break
|
||||
}
|
||||
// Internal name, we should have some info on them, either v4 or v6
|
||||
// Clients expect a complete answer, because we are a recursor in their view.
|
||||
state1 := state.NewWithQuestion(srv.Target, dns.TypeA)
|
||||
addr, debugAddr, e1 := e.A(zone, state1, nil, opt)
|
||||
if e1 == nil {
|
||||
extra = append(extra, addr...)
|
||||
debug = append(debug, debugAddr...)
|
||||
}
|
||||
// e.AAA(zone, state1, nil) as well...?
|
||||
case ip.To4() != nil:
|
||||
serv.Host = msg.Domain(serv.Key)
|
||||
srv := serv.NewSRV(state.QName(), weight)
|
||||
|
||||
records = append(records, srv)
|
||||
extra = append(extra, serv.NewA(srv.Target, ip.To4()))
|
||||
case ip.To4() == nil:
|
||||
serv.Host = msg.Domain(serv.Key)
|
||||
srv := serv.NewSRV(state.QName(), weight)
|
||||
|
||||
records = append(records, srv)
|
||||
extra = append(extra, serv.NewAAAA(srv.Target, ip.To16()))
|
||||
}
|
||||
}
|
||||
return records, extra, debug, nil
|
||||
}
|
||||
|
||||
// MX returns MX records from etcd.
|
||||
// If the Target is not a name but an IP address, a name is created on the fly.
|
||||
func (e Etcd) MX(zone string, state request.Request, opt Options) (records, extra []dns.RR, debug []msg.Service, err error) {
|
||||
services, debug, err := e.records(state, false, opt)
|
||||
if err != nil {
|
||||
return nil, nil, debug, err
|
||||
}
|
||||
|
||||
lookup := make(map[string]bool)
|
||||
for _, serv := range services {
|
||||
if !serv.Mail {
|
||||
continue
|
||||
}
|
||||
ip := net.ParseIP(serv.Host)
|
||||
switch {
|
||||
case ip == nil:
|
||||
mx := serv.NewMX(state.QName())
|
||||
records = append(records, mx)
|
||||
if _, ok := lookup[mx.Mx]; ok {
|
||||
break
|
||||
}
|
||||
|
||||
lookup[mx.Mx] = true
|
||||
|
||||
if !dns.IsSubDomain(zone, mx.Mx) {
|
||||
m1, e1 := e.Proxy.Lookup(state, mx.Mx, dns.TypeA)
|
||||
if e1 == nil {
|
||||
extra = append(extra, m1.Answer...)
|
||||
} else {
|
||||
debugMsg := msg.Service{Key: msg.Path(mx.Mx, e.PathPrefix), Host: mx.Mx, Text: " IN A: " + e1.Error()}
|
||||
debug = append(debug, debugMsg)
|
||||
}
|
||||
m1, e1 = e.Proxy.Lookup(state, mx.Mx, dns.TypeAAAA)
|
||||
if e1 == nil {
|
||||
// If we have seen CNAME's we *assume* that they are already added.
|
||||
for _, a := range m1.Answer {
|
||||
if _, ok := a.(*dns.CNAME); !ok {
|
||||
extra = append(extra, a)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debugMsg := msg.Service{Key: msg.Path(mx.Mx, e.PathPrefix), Host: mx.Mx, Text: " IN AAAA: " + e1.Error()}
|
||||
debug = append(debug, debugMsg)
|
||||
}
|
||||
break
|
||||
}
|
||||
// Internal name
|
||||
state1 := state.NewWithQuestion(mx.Mx, dns.TypeA)
|
||||
addr, debugAddr, e1 := e.A(zone, state1, nil, opt)
|
||||
if e1 == nil {
|
||||
extra = append(extra, addr...)
|
||||
debug = append(debug, debugAddr...)
|
||||
}
|
||||
// e.AAAA as well
|
||||
case ip.To4() != nil:
|
||||
serv.Host = msg.Domain(serv.Key)
|
||||
records = append(records, serv.NewMX(state.QName()))
|
||||
extra = append(extra, serv.NewA(serv.Host, ip.To4()))
|
||||
case ip.To4() == nil:
|
||||
serv.Host = msg.Domain(serv.Key)
|
||||
records = append(records, serv.NewMX(state.QName()))
|
||||
extra = append(extra, serv.NewAAAA(serv.Host, ip.To16()))
|
||||
}
|
||||
}
|
||||
return records, extra, debug, nil
|
||||
}
|
||||
|
||||
// CNAME returns CNAME records from etcd or an error.
|
||||
func (e Etcd) CNAME(zone string, state request.Request, opt Options) (records []dns.RR, debug []msg.Service, err error) {
|
||||
services, debug, err := e.records(state, true, opt)
|
||||
if err != nil {
|
||||
return nil, debug, err
|
||||
}
|
||||
|
||||
if len(services) > 0 {
|
||||
serv := services[0]
|
||||
if ip := net.ParseIP(serv.Host); ip == nil {
|
||||
records = append(records, serv.NewCNAME(state.QName(), serv.Host))
|
||||
}
|
||||
}
|
||||
return records, debug, nil
|
||||
}
|
||||
|
||||
// PTR returns the PTR records, only services that have a domain name as host are included.
|
||||
func (e Etcd) PTR(zone string, state request.Request, opt Options) (records []dns.RR, debug []msg.Service, err error) {
|
||||
services, debug, err := e.records(state, true, opt)
|
||||
if err != nil {
|
||||
return nil, debug, err
|
||||
}
|
||||
|
||||
for _, serv := range services {
|
||||
if ip := net.ParseIP(serv.Host); ip == nil {
|
||||
records = append(records, serv.NewPTR(state.QName(), serv.Host))
|
||||
}
|
||||
}
|
||||
return records, debug, nil
|
||||
}
|
||||
|
||||
// TXT returns TXT records from etcd or an error.
|
||||
func (e Etcd) TXT(zone string, state request.Request, opt Options) (records []dns.RR, debug []msg.Service, err error) {
|
||||
services, debug, err := e.records(state, false, opt)
|
||||
if err != nil {
|
||||
return nil, debug, err
|
||||
}
|
||||
|
||||
for _, serv := range services {
|
||||
if serv.Text == "" {
|
||||
continue
|
||||
}
|
||||
records = append(records, serv.NewTXT(state.QName()))
|
||||
}
|
||||
return records, debug, nil
|
||||
}
|
||||
|
||||
// NS returns NS records from etcd or an error.
|
||||
func (e Etcd) NS(zone string, state request.Request, opt Options) (records, extra []dns.RR, debug []msg.Service, err error) {
|
||||
// NS record for this zone live in a special place, ns.dns.<zone>. Fake our lookup.
|
||||
// only a tad bit fishy...
|
||||
old := state.QName()
|
||||
|
||||
state.Clear()
|
||||
state.Req.Question[0].Name = "ns.dns." + zone
|
||||
services, debug, err := e.records(state, false, opt)
|
||||
if err != nil {
|
||||
return nil, nil, debug, err
|
||||
}
|
||||
// ... and reset
|
||||
state.Req.Question[0].Name = old
|
||||
|
||||
for _, serv := range services {
|
||||
ip := net.ParseIP(serv.Host)
|
||||
switch {
|
||||
case ip == nil:
|
||||
return nil, nil, debug, fmt.Errorf("NS record must be an IP address: %s", serv.Host)
|
||||
case ip.To4() != nil:
|
||||
serv.Host = msg.Domain(serv.Key)
|
||||
records = append(records, serv.NewNS(state.QName()))
|
||||
extra = append(extra, serv.NewA(serv.Host, ip.To4()))
|
||||
case ip.To4() == nil:
|
||||
serv.Host = msg.Domain(serv.Key)
|
||||
records = append(records, serv.NewNS(state.QName()))
|
||||
extra = append(extra, serv.NewAAAA(serv.Host, ip.To16()))
|
||||
}
|
||||
}
|
||||
return records, extra, debug, nil
|
||||
}
|
||||
|
||||
// SOA returns a SOA record from etcd.
|
||||
func (e Etcd) SOA(zone string, state request.Request, opt Options) ([]dns.RR, []msg.Service, error) {
|
||||
header := dns.RR_Header{Name: zone, Rrtype: dns.TypeSOA, Ttl: 300, Class: dns.ClassINET}
|
||||
|
||||
soa := &dns.SOA{Hdr: header,
|
||||
Mbox: hostmaster + "." + zone,
|
||||
Ns: "ns.dns." + zone,
|
||||
Serial: uint32(time.Now().Unix()),
|
||||
Refresh: 7200,
|
||||
Retry: 1800,
|
||||
Expire: 86400,
|
||||
Minttl: minTTL,
|
||||
}
|
||||
// TODO(miek): fake some msg.Service here when returning.
|
||||
return []dns.RR{soa}, nil, nil
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
func TestProxyLookupFailDebug(t *testing.T) {
|
||||
etc := newEtcdMiddleware()
|
||||
etc.Proxy = proxy.New([]string{"127.0.0.1:154"})
|
||||
etc.Debug = true
|
||||
etc.Debugging = true
|
||||
|
||||
for _, serv := range servicesProxy {
|
||||
set(t, etc, serv.Key, 0, serv)
|
||||
|
||||
@@ -30,6 +30,7 @@ func setup(c *caddy.Controller) error {
|
||||
if err != nil {
|
||||
return middleware.Error("etcd", err)
|
||||
}
|
||||
|
||||
if stubzones {
|
||||
c.OnStartup(func() error {
|
||||
e.UpdateStubZones()
|
||||
@@ -55,7 +56,6 @@ func etcdParse(c *caddy.Controller) (*Etcd, bool, error) {
|
||||
Stubmap: &stub,
|
||||
}
|
||||
var (
|
||||
client etcdc.KeysAPI
|
||||
tlsCertFile = ""
|
||||
tlsKeyFile = ""
|
||||
tlsCAcertFile = ""
|
||||
@@ -64,7 +64,6 @@ func etcdParse(c *caddy.Controller) (*Etcd, bool, error) {
|
||||
)
|
||||
for c.Next() {
|
||||
if c.Val() == "etcd" {
|
||||
etc.Client = client
|
||||
etc.Zones = c.RemainingArgs()
|
||||
if len(etc.Zones) == 0 {
|
||||
etc.Zones = make([]string, len(c.ServerBlockKeys))
|
||||
@@ -77,7 +76,7 @@ func etcdParse(c *caddy.Controller) (*Etcd, bool, error) {
|
||||
case "stubzones":
|
||||
stubzones = true
|
||||
case "debug":
|
||||
etc.Debug = true
|
||||
etc.Debugging = true
|
||||
case "path":
|
||||
if !c.NextArg() {
|
||||
return &Etcd{}, false, c.ArgErr()
|
||||
@@ -117,7 +116,7 @@ func etcdParse(c *caddy.Controller) (*Etcd, bool, error) {
|
||||
case "stubzones":
|
||||
stubzones = true
|
||||
case "debug":
|
||||
etc.Debug = true
|
||||
etc.Debugging = true
|
||||
case "path":
|
||||
if !c.NextArg() {
|
||||
return &Etcd{}, false, c.ArgErr()
|
||||
@@ -161,6 +160,7 @@ func etcdParse(c *caddy.Controller) (*Etcd, bool, error) {
|
||||
}
|
||||
etc.Client = client
|
||||
etc.endpoints = endpoints
|
||||
|
||||
return &etc, stubzones, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,17 +28,16 @@ func init() {
|
||||
func newEtcdMiddleware() *Etcd {
|
||||
ctxt, _ = context.WithTimeout(context.Background(), etcdTimeout)
|
||||
|
||||
etcdCfg := etcdc.Config{
|
||||
Endpoints: []string{"http://localhost:2379"},
|
||||
}
|
||||
cli, _ := etcdc.New(etcdCfg)
|
||||
endpoints := []string{"http://localhost:2379"}
|
||||
client, _ := newEtcdClient(endpoints, "", "", "")
|
||||
|
||||
return &Etcd{
|
||||
Proxy: proxy.New([]string{"8.8.8.8:53"}),
|
||||
PathPrefix: "skydns",
|
||||
Ctx: context.Background(),
|
||||
Inflight: &singleflight.Group{},
|
||||
Zones: []string{"skydns.test.", "skydns_extra.test.", "in-addr.arpa."},
|
||||
Client: etcdc.NewKeysAPI(cli),
|
||||
Client: client,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user