Every plugin needs to deal with EDNS0 and should call Scrub to make a message fit the client's buffer. Move this functionality into the server and wrapping the ResponseWriter into a ScrubWriter that handles these bits for us. Result: Less code and faster, because multiple chained plugins could all be calling scrub and SizeAndDo - now there is just one place. Most tests in file/* and dnssec/* needed adjusting because in those unit tests you don't see OPT RRs anymore. The DNSSEC signer was also looking at the returned OPT RR to see if it needed to sign - as those are now added by the server (and thus later), this needed to change slightly. Scrub itself still exist (for backward compat reasons), but has been made a noop. Scrub has been renamed to scrub as it should not be used by external plugins. Fixes: #2010 Signed-off-by: Miek Gieben <miek@miek.nl>
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
"github.com/coredns/coredns/request"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// ServeDNS implements the plugin.Handler interface.
|
|
func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
opt := plugin.Options{}
|
|
state := request.Request{W: w, Req: r, Context: ctx}
|
|
|
|
m := new(dns.Msg)
|
|
m.SetReply(r)
|
|
m.Authoritative, m.RecursionAvailable = true, true
|
|
|
|
zone := plugin.Zones(k.Zones).Matches(state.Name())
|
|
if zone == "" {
|
|
return plugin.NextOrFailure(k.Name(), k.Next, ctx, w, r)
|
|
}
|
|
|
|
state.Zone = zone
|
|
|
|
var (
|
|
records []dns.RR
|
|
extra []dns.RR
|
|
err error
|
|
)
|
|
|
|
switch state.QType() {
|
|
case dns.TypeA:
|
|
records, err = plugin.A(&k, zone, state, nil, opt)
|
|
case dns.TypeAAAA:
|
|
records, err = plugin.AAAA(&k, zone, state, nil, opt)
|
|
case dns.TypeTXT:
|
|
records, err = plugin.TXT(&k, zone, state, opt)
|
|
case dns.TypeCNAME:
|
|
records, err = plugin.CNAME(&k, zone, state, opt)
|
|
case dns.TypePTR:
|
|
records, err = plugin.PTR(&k, zone, state, opt)
|
|
case dns.TypeMX:
|
|
records, extra, err = plugin.MX(&k, zone, state, opt)
|
|
case dns.TypeSRV:
|
|
records, extra, err = plugin.SRV(&k, zone, state, opt)
|
|
case dns.TypeSOA:
|
|
records, err = plugin.SOA(&k, zone, state, opt)
|
|
case dns.TypeNS:
|
|
if state.Name() == zone {
|
|
records, extra, err = plugin.NS(&k, zone, state, opt)
|
|
break
|
|
}
|
|
fallthrough
|
|
case dns.TypeAXFR, dns.TypeIXFR:
|
|
k.Transfer(ctx, state)
|
|
default:
|
|
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
|
|
_, err = plugin.A(&k, zone, state, nil, opt)
|
|
}
|
|
|
|
if k.IsNameError(err) {
|
|
if k.Fall.Through(state.Name()) {
|
|
return plugin.NextOrFailure(k.Name(), k.Next, ctx, w, r)
|
|
}
|
|
return plugin.BackendError(&k, zone, dns.RcodeNameError, state, nil /* err */, opt)
|
|
}
|
|
if err != nil {
|
|
return dns.RcodeServerFailure, err
|
|
}
|
|
|
|
if len(records) == 0 {
|
|
return plugin.BackendError(&k, zone, dns.RcodeSuccess, state, nil, opt)
|
|
}
|
|
|
|
m.Answer = append(m.Answer, records...)
|
|
m.Extra = append(m.Extra, extra...)
|
|
|
|
w.WriteMsg(m)
|
|
return dns.RcodeSuccess, nil
|
|
}
|
|
|
|
// Name implements the Handler interface.
|
|
func (k Kubernetes) Name() string { return "kubernetes" }
|