it compiles

This commit is contained in:
Miek Gieben 2016-03-22 11:04:56 +00:00
parent 1a7f0deadd
commit ae841ae342
5 changed files with 47 additions and 46 deletions

View file

@ -58,6 +58,7 @@ var directiveOrder = []directive{
{"log", setup.Log}, {"log", setup.Log},
{"errors", setup.Errors}, {"errors", setup.Errors},
{"proxy", setup.Proxy}, {"proxy", setup.Proxy},
{"etcd", setup.Etcd},
} }
// RegisterDirective adds the given directive to caddy's list of directives. // RegisterDirective adds the given directive to caddy's list of directives.

View file

@ -19,13 +19,13 @@ If you want to `round robin` A and AAAA responses look at the `round_robin` midd
~~~ ~~~
etcd { etcd {
path /skydns path /skydns
endpoint address... endpoint endpoint...
stubzones stubzones
} }
~~~ ~~~
* `path` /skydns * `path` /skydns
* `endpoint` address... * `endpoint` endpoints...
* `stubzones` * `stubzones`
## Examples ## Examples

View file

@ -8,63 +8,61 @@ import (
func (e Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (e Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
println("ETCD MIDDLEWARE HIT") println("ETCD MIDDLEWARE HIT")
state := middleware.State{W: w, Req: r} state := middleware.State{W: w, Req: r}
zone := middleware.Zones(e.Zones).Matches(state.Name())
if zone == "" {
return e.Next.ServeDNS(ctx, w, r)
}
m := state.AnswerMessage() m := state.AnswerMessage()
m.Authoritative = true m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
m.RecursionAvailable = true
m.Compress = true
// TODO(miek): get current zone when serving multiple
zone := "."
var (
records, extra []dns.RR
err error
)
switch state.Type() { switch state.Type() {
case "A": case "A":
records, err := e.A(zone, state, nil) records, err = e.A(zone, state, nil)
case "AAAA": case "AAAA":
records, err := e.AAAA(zone, state, nil) records, err = e.AAAA(zone, state, nil)
fallthrough
case "TXT": case "TXT":
records, err := e.TXT(zone, state) records, err = e.TXT(zone, state)
fallthrough
case "CNAME": case "CNAME":
records, err := e.CNAME(zone, state) records, err = e.CNAME(zone, state)
fallthrough
case "MX": case "MX":
records, extra, err := e.MX(zone, state) records, extra, err = e.MX(zone, state)
fallthrough
case "SRV": case "SRV":
records, extra, err := e.SRV(zone, state) records, extra, err = e.SRV(zone, state)
if isEtcdNameError(err) {
NameError(zone, state)
return dns.RcodeNameError, nil
}
if err != nil {
// TODO(miek): err or nil in this case?
return dns.RcodeServerFailure, err
}
if len(records) > 0 {
m.Answer = append(m.Answer, records...)
}
if len(extra) > 0 {
m.Extra = append(m.Extra, extra...)
}
default: default:
// rwrite and return
// Nodata response // Nodata response
// also catch other types, so that they return NODATA // also catch other types, so that they return NODATA
return 0, nil
} }
return e.Next.ServeDNS(ctx, w, r) if isEtcdNameError(err) {
NameError(zone, state)
return dns.RcodeNameError, nil
}
if err != nil {
// TODO(miek): err or nil in this case?
return dns.RcodeServerFailure, err
}
if len(records) > 0 {
m.Answer = append(m.Answer, records...)
}
if len(extra) > 0 {
m.Extra = append(m.Extra, extra...)
}
state.W.WriteMsg(m)
return 0, nil
} }
// NameError writes a name error to the client. // NameError writes a name error to the client.
func NameError(zone string, state middleware.State) { func NameError(zone string, state middleware.State) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetRcode(state.Req, dns.RcodeNameError) m.SetRcode(state.Req, dns.RcodeNameError)
m.Ns = []dns.RR{SOA(zone)}
m.Ns = []dns.RR{NewSOA()}
m.Ns[0].Header().Ttl = minTtl
state.W.WriteMsg(m) state.W.WriteMsg(m)
} }

View file

@ -10,7 +10,8 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
// need current zone argument. // TODO(miek): factor out common code a bit
func (e Etcd) A(zone string, state middleware.State, previousRecords []dns.RR) (records []dns.RR, err error) { func (e Etcd) A(zone string, state middleware.State, previousRecords []dns.RR) (records []dns.RR, err error) {
services, err := e.Records(state.Name(), false) services, err := e.Records(state.Name(), false)
if err != nil { if err != nil {
@ -24,13 +25,13 @@ func (e Etcd) A(zone string, state middleware.State, previousRecords []dns.RR) (
switch { switch {
case ip == nil: case ip == nil:
// Try to resolve as CNAME if it's not an IP, but only if we don't create loops. // Try to resolve as CNAME if it's not an IP, but only if we don't create loops.
// TODO(miek): lowercasing, use Match in middleware/ // TODO(miek): lowercasing, use Match in middleware?
if state.Name() == dns.Fqdn(serv.Host) { if state.Name() == dns.Fqdn(serv.Host) {
// x CNAME x is a direct loop, don't add those // x CNAME x is a direct loop, don't add those
continue continue
} }
newRecord := serv.NewCNAME(state.QName(), dns.Fqdn(serv.Host)) newRecord := serv.NewCNAME(state.QName(), serv.Host)
if len(previousRecords) > 7 { if len(previousRecords) > 7 {
// don't add it, and just continue // don't add it, and just continue
continue continue
@ -93,7 +94,7 @@ func (e Etcd) AAAA(zone string, state middleware.State, previousRecords []dns.RR
continue continue
} }
newRecord := serv.NewCNAME(state.QName(), dns.Fqdn(serv.Host)) newRecord := serv.NewCNAME(state.QName(), serv.Host)
if len(previousRecords) > 7 { if len(previousRecords) > 7 {
// don't add it, and just continue // don't add it, and just continue
continue continue
@ -297,7 +298,7 @@ func (e Etcd) CNAME(zone string, state middleware.State) (records []dns.RR, err
if len(services) > 0 { if len(services) > 0 {
serv := services[0] serv := services[0]
if ip := net.ParseIP(serv.Host); ip == nil { if ip := net.ParseIP(serv.Host); ip == nil {
records = append(records, serv.NewCNAME(state.QName(), dns.Fqdn(serv.Host))) records = append(records, serv.NewCNAME(state.QName(), serv.Host))
} }
} }
return records, nil return records, nil
@ -320,7 +321,8 @@ func (e Etcd) TXT(zone string, state middleware.State) (records []dns.RR, err er
return records, nil return records, nil
} }
func (e Etcd) SOA(zone string, state middleware.State) *dns.SOA { // synthesis a SOA Record.
func SOA(zone string) *dns.SOA {
return nil return nil
} }

View file

@ -40,7 +40,7 @@ func (s *Service) NewSRV(name string, weight uint16) *dns.SRV {
host := targetStrip(dns.Fqdn(s.Host), s.TargetStrip) host := targetStrip(dns.Fqdn(s.Host), s.TargetStrip)
return &dns.SRV{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeSRV, Class: dns.ClassINET, Ttl: s.Ttl}, return &dns.SRV{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeSRV, Class: dns.ClassINET, Ttl: s.Ttl},
Priority: uint16(s.Priority), Weight: weight, Port: uint16(s.Port), Target: host} Priority: uint16(s.Priority), Weight: weight, Port: uint16(s.Port), Target: dns.Fqdn(host)}
} }
// NewMX returns a new MX record based on the Service. // NewMX returns a new MX record based on the Service.
@ -63,7 +63,7 @@ func (s *Service) NewAAAA(name string, ip net.IP) *dns.AAAA {
// NewCNAME returns a new CNAME record based on the Service. // NewCNAME returns a new CNAME record based on the Service.
func (s *Service) NewCNAME(name string, target string) *dns.CNAME { func (s *Service) NewCNAME(name string, target string) *dns.CNAME {
return &dns.CNAME{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: s.Ttl}, Target: target} return &dns.CNAME{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: s.Ttl}, Target: dns.Fqdn(target)}
} }
// NewTXT returns a new TXT record based on the Service. // NewTXT returns a new TXT record based on the Service.