Commit 53f3f0b6 authored by Miek Gieben's avatar Miek Gieben Committed by GitHub

Remove context.Context from request.Request (#2726)

* Remove context.Context from request.Request

This removes the context from request.Request and makes all the changes
in the code to make it compile again. It's all mechanical. It did
unearth some weirdness in that the context was kept in handler structs
which may cause havoc with concurrently handling of requests.

Fixes #2721
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>

* Make test compile
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>
parent 6492f777
...@@ -41,7 +41,7 @@ type ( ...@@ -41,7 +41,7 @@ type (
// ServeDNS implements the plugin.Handler interface. // ServeDNS implements the plugin.Handler interface.
func (a Auto) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (a Auto) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r, Context: ctx} state := request.Request{W: w, Req: r}
qname := state.Name() qname := state.Name()
// Precheck with the origins, i.e. are we allowed to look here? // Precheck with the origins, i.e. are we allowed to look here?
...@@ -66,7 +66,7 @@ func (a Auto) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i ...@@ -66,7 +66,7 @@ func (a Auto) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i
return xfr.ServeDNS(ctx, w, r) return xfr.ServeDNS(ctx, w, r)
} }
answer, ns, extra, result := z.Lookup(state, qname) answer, ns, extra, result := z.Lookup(ctx, state, qname)
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
......
...@@ -13,18 +13,18 @@ import ( ...@@ -13,18 +13,18 @@ import (
type ServiceBackend interface { type ServiceBackend interface {
// Services communicates with the backend to retrieve the service definitions. Exact indicates // Services communicates with the backend to retrieve the service definitions. Exact indicates
// on exact match should be returned. // on exact match should be returned.
Services(state request.Request, exact bool, opt Options) ([]msg.Service, error) Services(ctx context.Context, state request.Request, exact bool, opt Options) ([]msg.Service, error)
// Reverse communicates with the backend to retrieve service definition based on a IP address // Reverse communicates with the backend to retrieve service definition based on a IP address
// instead of a name. I.e. a reverse DNS lookup. // instead of a name. I.e. a reverse DNS lookup.
Reverse(state request.Request, exact bool, opt Options) ([]msg.Service, error) Reverse(ctx context.Context, state request.Request, exact bool, opt Options) ([]msg.Service, error)
// Lookup is used to find records else where. // Lookup is used to find records else where.
Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error)
// Returns _all_ services that matches a certain name. // Returns _all_ services that matches a certain name.
// Note: it does not implement a specific service. // Note: it does not implement a specific service.
Records(state request.Request, exact bool) ([]msg.Service, error) Records(ctx context.Context, state request.Request, exact bool) ([]msg.Service, error)
// IsNameError return true if err indicated a record not found condition // IsNameError return true if err indicated a record not found condition
IsNameError(err error) bool IsNameError(err error) bool
......
This diff is collapsed.
...@@ -36,14 +36,13 @@ type Etcd struct { ...@@ -36,14 +36,13 @@ type Etcd struct {
PathPrefix string PathPrefix string
Upstream *upstream.Upstream Upstream *upstream.Upstream
Client *etcdcv3.Client Client *etcdcv3.Client
Ctx context.Context
endpoints []string // Stored here as well, to aid in testing. endpoints []string // Stored here as well, to aid in testing.
} }
// Services implements the ServiceBackend interface. // Services implements the ServiceBackend interface.
func (e *Etcd) Services(state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) { func (e *Etcd) Services(ctx context.Context, state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
services, err = e.Records(state, exact) services, err = e.Records(ctx, state, exact)
if err != nil { if err != nil {
return return
} }
...@@ -53,13 +52,13 @@ func (e *Etcd) Services(state request.Request, exact bool, opt plugin.Options) ( ...@@ -53,13 +52,13 @@ func (e *Etcd) Services(state request.Request, exact bool, opt plugin.Options) (
} }
// Reverse implements the ServiceBackend interface. // Reverse implements the ServiceBackend interface.
func (e *Etcd) Reverse(state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) { func (e *Etcd) Reverse(ctx context.Context, state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
return e.Services(state, exact, opt) return e.Services(ctx, state, exact, opt)
} }
// Lookup implements the ServiceBackend interface. // Lookup implements the ServiceBackend interface.
func (e *Etcd) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) { func (e *Etcd) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
return e.Upstream.Lookup(state, name, typ) return e.Upstream.Lookup(ctx, state, name, typ)
} }
// IsNameError implements the ServiceBackend interface. // IsNameError implements the ServiceBackend interface.
...@@ -69,11 +68,11 @@ func (e *Etcd) IsNameError(err error) bool { ...@@ -69,11 +68,11 @@ func (e *Etcd) IsNameError(err error) bool {
// Records looks up records in etcd. If exact is true, it will lookup just this // 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. // name. This is used when find matches when completing SRV lookups for instance.
func (e *Etcd) Records(state request.Request, exact bool) ([]msg.Service, error) { func (e *Etcd) Records(ctx context.Context, state request.Request, exact bool) ([]msg.Service, error) {
name := state.Name() name := state.Name()
path, star := msg.PathWithWildcard(name, e.PathPrefix) path, star := msg.PathWithWildcard(name, e.PathPrefix)
r, err := e.get(path, !exact) r, err := e.get(ctx, path, !exact)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -81,8 +80,8 @@ func (e *Etcd) Records(state request.Request, exact bool) ([]msg.Service, error) ...@@ -81,8 +80,8 @@ func (e *Etcd) Records(state request.Request, exact bool) ([]msg.Service, error)
return e.loopNodes(r.Kvs, segments, star, state.QType()) return e.loopNodes(r.Kvs, segments, star, state.QType())
} }
func (e *Etcd) get(path string, recursive bool) (*etcdcv3.GetResponse, error) { func (e *Etcd) get(ctx context.Context, path string, recursive bool) (*etcdcv3.GetResponse, error) {
ctx, cancel := context.WithTimeout(e.Ctx, etcdTimeout) ctx, cancel := context.WithTimeout(ctx, etcdTimeout)
defer cancel() defer cancel()
if recursive == true { if recursive == true {
if !strings.HasSuffix(path, "/") { if !strings.HasSuffix(path, "/") {
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
// ServeDNS implements the plugin.Handler interface. // ServeDNS implements the plugin.Handler interface.
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) {
opt := plugin.Options{} opt := plugin.Options{}
state := request.Request{W: w, Req: r, Context: ctx} state := request.Request{W: w, Req: r}
zone := plugin.Zones(e.Zones).Matches(state.Name()) zone := plugin.Zones(e.Zones).Matches(state.Name())
if zone == "" { if zone == "" {
...@@ -26,44 +26,44 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ( ...@@ -26,44 +26,44 @@ func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
switch state.QType() { switch state.QType() {
case dns.TypeA: case dns.TypeA:
records, err = plugin.A(e, zone, state, nil, opt) records, err = plugin.A(ctx, e, zone, state, nil, opt)
case dns.TypeAAAA: case dns.TypeAAAA:
records, err = plugin.AAAA(e, zone, state, nil, opt) records, err = plugin.AAAA(ctx, e, zone, state, nil, opt)
case dns.TypeTXT: case dns.TypeTXT:
records, err = plugin.TXT(e, zone, state, opt) records, err = plugin.TXT(ctx, e, zone, state, opt)
case dns.TypeCNAME: case dns.TypeCNAME:
records, err = plugin.CNAME(e, zone, state, opt) records, err = plugin.CNAME(ctx, e, zone, state, opt)
case dns.TypePTR: case dns.TypePTR:
records, err = plugin.PTR(e, zone, state, opt) records, err = plugin.PTR(ctx, e, zone, state, opt)
case dns.TypeMX: case dns.TypeMX:
records, extra, err = plugin.MX(e, zone, state, opt) records, extra, err = plugin.MX(ctx, e, zone, state, opt)
case dns.TypeSRV: case dns.TypeSRV:
records, extra, err = plugin.SRV(e, zone, state, opt) records, extra, err = plugin.SRV(ctx, e, zone, state, opt)
case dns.TypeSOA: case dns.TypeSOA:
records, err = plugin.SOA(e, zone, state, opt) records, err = plugin.SOA(ctx, e, zone, state, opt)
case dns.TypeNS: case dns.TypeNS:
if state.Name() == zone { if state.Name() == zone {
records, extra, err = plugin.NS(e, zone, state, opt) records, extra, err = plugin.NS(ctx, e, zone, state, opt)
break break
} }
fallthrough fallthrough
default: default:
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN // Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
_, err = plugin.A(e, zone, state, nil, opt) _, err = plugin.A(ctx, e, zone, state, nil, opt)
} }
if err != nil && e.IsNameError(err) { if err != nil && e.IsNameError(err) {
if e.Fall.Through(state.Name()) { if e.Fall.Through(state.Name()) {
return plugin.NextOrFailure(e.Name(), e.Next, ctx, w, r) return plugin.NextOrFailure(e.Name(), e.Next, ctx, w, r)
} }
// Make err nil when returning here, so we don't log spam for NXDOMAIN. // Make err nil when returning here, so we don't log spam for NXDOMAIN.
return plugin.BackendError(e, zone, dns.RcodeNameError, state, nil /* err */, opt) return plugin.BackendError(ctx, e, zone, dns.RcodeNameError, state, nil /* err */, opt)
} }
if err != nil { if err != nil {
return plugin.BackendError(e, zone, dns.RcodeServerFailure, state, err, opt) return plugin.BackendError(ctx, e, zone, dns.RcodeServerFailure, state, err, opt)
} }
if len(records) == 0 { if len(records) == 0 {
return plugin.BackendError(e, zone, dns.RcodeSuccess, state, err, opt) return plugin.BackendError(ctx, e, zone, dns.RcodeSuccess, state, err, opt)
} }
m := new(dns.Msg) m := new(dns.Msg)
......
...@@ -294,7 +294,6 @@ func newEtcdPlugin() *Etcd { ...@@ -294,7 +294,6 @@ func newEtcdPlugin() *Etcd {
return &Etcd{ return &Etcd{
Upstream: upstream.New(), Upstream: upstream.New(),
PathPrefix: "skydns", PathPrefix: "skydns",
Ctx: context.Background(),
Zones: []string{"skydns.test.", "skydns_extra.test.", "skydns_zonea.test.", "skydns_zoneb.test.", "skydns_zonec.test.", "skydns_zoned.test.", "in-addr.arpa."}, Zones: []string{"skydns.test.", "skydns_extra.test.", "skydns_zonea.test.", "skydns_zoneb.test.", "skydns_zonec.test.", "skydns_zoned.test.", "in-addr.arpa."},
Client: client, Client: client,
} }
......
package etcd package etcd
import ( import (
"context"
"crypto/tls" "crypto/tls"
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
...@@ -38,10 +37,7 @@ func setup(c *caddy.Controller) error { ...@@ -38,10 +37,7 @@ func setup(c *caddy.Controller) error {
} }
func etcdParse(c *caddy.Controller) (*Etcd, error) { func etcdParse(c *caddy.Controller) (*Etcd, error) {
etc := Etcd{ etc := Etcd{PathPrefix: "skydns"}
PathPrefix: "skydns",
Ctx: context.Background(),
}
var ( var (
tlsConfig *tls.Config tlsConfig *tls.Config
err error err error
......
...@@ -51,7 +51,7 @@ func (f *Federation) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns. ...@@ -51,7 +51,7 @@ func (f *Federation) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.
return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r) return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
} }
state := request.Request{W: w, Req: r, Context: ctx} state := request.Request{W: w, Req: r}
zone := plugin.Zones(f.zones).Matches(state.Name()) zone := plugin.Zones(f.zones).Matches(state.Name())
if zone == "" { if zone == "" {
...@@ -109,7 +109,7 @@ func (f *Federation) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns. ...@@ -109,7 +109,7 @@ func (f *Federation) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.
m.Answer = []dns.RR{service.NewCNAME(state.QName(), service.Host)} m.Answer = []dns.RR{service.NewCNAME(state.QName(), service.Host)}
if f.Upstream != nil { if f.Upstream != nil {
aRecord, err := f.Upstream.Lookup(state, service.Host, state.QType()) aRecord, err := f.Upstream.Lookup(ctx, state, service.Host, state.QType())
if err == nil && aRecord != nil && len(aRecord.Answer) > 0 { if err == nil && aRecord != nil && len(aRecord.Answer) > 0 {
m.Answer = append(m.Answer, aRecord.Answer...) m.Answer = append(m.Answer, aRecord.Answer...)
} }
......
...@@ -31,7 +31,7 @@ type ( ...@@ -31,7 +31,7 @@ type (
// ServeDNS implements the plugin.Handle interface. // ServeDNS implements the plugin.Handle interface.
func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r, Context: ctx} state := request.Request{W: w, Req: r}
qname := state.Name() qname := state.Name()
// TODO(miek): match the qname better in the map // TODO(miek): match the qname better in the map
...@@ -79,7 +79,7 @@ func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i ...@@ -79,7 +79,7 @@ func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (i
return xfr.ServeDNS(ctx, w, r) return xfr.ServeDNS(ctx, w, r)
} }
answer, ns, extra, result := z.Lookup(state, qname) answer, ns, extra, result := z.Lookup(ctx, state, qname)
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
......
package file package file
import ( import (
"context"
"github.com/coredns/coredns/plugin/file/tree" "github.com/coredns/coredns/plugin/file/tree"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
...@@ -25,7 +27,7 @@ const ( ...@@ -25,7 +27,7 @@ const (
// Lookup looks up qname and qtype in the zone. When do is true DNSSEC records are included. // Lookup looks up qname and qtype in the zone. When do is true DNSSEC records are included.
// Three sets of records are returned, one for the answer, one for authority and one for the additional section. // Three sets of records are returned, one for the answer, one for authority and one for the additional section.
func (z *Zone) Lookup(state request.Request, qname string) ([]dns.RR, []dns.RR, []dns.RR, Result) { func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string) ([]dns.RR, []dns.RR, []dns.RR, Result) {
qtype := state.QType() qtype := state.QType()
do := state.Do() do := state.Do()
...@@ -106,7 +108,7 @@ func (z *Zone) Lookup(state request.Request, qname string) ([]dns.RR, []dns.RR, ...@@ -106,7 +108,7 @@ func (z *Zone) Lookup(state request.Request, qname string) ([]dns.RR, []dns.RR,
// Only one DNAME is allowed per name. We just pick the first one to synthesize from. // Only one DNAME is allowed per name. We just pick the first one to synthesize from.
dname := dnamerrs[0] dname := dnamerrs[0]
if cname := synthesizeCNAME(state.Name(), dname.(*dns.DNAME)); cname != nil { if cname := synthesizeCNAME(state.Name(), dname.(*dns.DNAME)); cname != nil {
answer, ns, extra, rcode := z.additionalProcessing(state, elem, []dns.RR{cname}) answer, ns, extra, rcode := z.additionalProcessing(ctx, state, elem, []dns.RR{cname})
if do { if do {
sigs := elem.Types(dns.TypeRRSIG) sigs := elem.Types(dns.TypeRRSIG)
...@@ -157,7 +159,7 @@ func (z *Zone) Lookup(state request.Request, qname string) ([]dns.RR, []dns.RR, ...@@ -157,7 +159,7 @@ func (z *Zone) Lookup(state request.Request, qname string) ([]dns.RR, []dns.RR,
if found && shot { if found && shot {
if rrs := elem.Types(dns.TypeCNAME); len(rrs) > 0 && qtype != dns.TypeCNAME { if rrs := elem.Types(dns.TypeCNAME); len(rrs) > 0 && qtype != dns.TypeCNAME {
return z.additionalProcessing(state, elem, rrs) return z.additionalProcessing(ctx, state, elem, rrs)
} }
rrs := elem.Types(qtype, qname) rrs := elem.Types(qtype, qname)
...@@ -193,7 +195,7 @@ func (z *Zone) Lookup(state request.Request, qname string) ([]dns.RR, []dns.RR, ...@@ -193,7 +195,7 @@ func (z *Zone) Lookup(state request.Request, qname string) ([]dns.RR, []dns.RR,
auth := z.ns(do) auth := z.ns(do)
if rrs := wildElem.Types(dns.TypeCNAME, qname); len(rrs) > 0 { if rrs := wildElem.Types(dns.TypeCNAME, qname); len(rrs) > 0 {
return z.additionalProcessing(state, wildElem, rrs) return z.additionalProcessing(ctx, state, wildElem, rrs)
} }
rrs := wildElem.Types(qtype, qname) rrs := wildElem.Types(qtype, qname)
...@@ -296,7 +298,7 @@ func (z *Zone) ns(do bool) []dns.RR { ...@@ -296,7 +298,7 @@ func (z *Zone) ns(do bool) []dns.RR {
} }
// aditionalProcessing adds signatures and tries to resolve CNAMEs that point to external names. // aditionalProcessing adds signatures and tries to resolve CNAMEs that point to external names.
func (z *Zone) additionalProcessing(state request.Request, elem *tree.Elem, rrs []dns.RR) ([]dns.RR, []dns.RR, []dns.RR, Result) { func (z *Zone) additionalProcessing(ctx context.Context, state request.Request, elem *tree.Elem, rrs []dns.RR) ([]dns.RR, []dns.RR, []dns.RR, Result) {
qtype := state.QType() qtype := state.QType()
do := state.Do() do := state.Do()
...@@ -312,7 +314,7 @@ func (z *Zone) additionalProcessing(state request.Request, elem *tree.Elem, rrs ...@@ -312,7 +314,7 @@ func (z *Zone) additionalProcessing(state request.Request, elem *tree.Elem, rrs
targetName := rrs[0].(*dns.CNAME).Target targetName := rrs[0].(*dns.CNAME).Target
elem, _ = z.Tree.Search(targetName) elem, _ = z.Tree.Search(targetName)
if elem == nil { if elem == nil {
rrs = append(rrs, z.externalLookup(state, targetName, qtype)...) rrs = append(rrs, z.externalLookup(ctx, state, targetName, qtype)...)
return rrs, z.ns(do), nil, Success return rrs, z.ns(do), nil, Success
} }
...@@ -333,7 +335,7 @@ Redo: ...@@ -333,7 +335,7 @@ Redo:
targetName := cname[0].(*dns.CNAME).Target targetName := cname[0].(*dns.CNAME).Target
elem, _ = z.Tree.Search(targetName) elem, _ = z.Tree.Search(targetName)
if elem == nil { if elem == nil {
rrs = append(rrs, z.externalLookup(state, targetName, qtype)...) rrs = append(rrs, z.externalLookup(ctx, state, targetName, qtype)...)
return rrs, z.ns(do), nil, Success return rrs, z.ns(do), nil, Success
} }
...@@ -371,8 +373,8 @@ func cnameForType(targets []dns.RR, origQtype uint16) []dns.RR { ...@@ -371,8 +373,8 @@ func cnameForType(targets []dns.RR, origQtype uint16) []dns.RR {
return ret return ret
} }
func (z *Zone) externalLookup(state request.Request, target string, qtype uint16) []dns.RR { func (z *Zone) externalLookup(ctx context.Context, state request.Request, target string, qtype uint16) []dns.RR {
m, e := z.Upstream.Lookup(state, target, qtype) m, e := z.Upstream.Lookup(ctx, state, target, qtype)
if e != nil { if e != nil {
return nil return nil
} }
......
package file package file
import ( import (
"context"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
...@@ -33,17 +34,18 @@ func TestZoneReload(t *testing.T) { ...@@ -33,17 +34,18 @@ func TestZoneReload(t *testing.T) {
z.Reload() z.Reload()
time.Sleep(time.Second) time.Sleep(time.Second)
ctx := context.TODO()
r := new(dns.Msg) r := new(dns.Msg)
r.SetQuestion("miek.nl", dns.TypeSOA) r.SetQuestion("miek.nl", dns.TypeSOA)
state := request.Request{W: &test.ResponseWriter{}, Req: r} state := request.Request{W: &test.ResponseWriter{}, Req: r}
if _, _, _, res := z.Lookup(state, "miek.nl."); res != Success { if _, _, _, res := z.Lookup(ctx, state, "miek.nl."); res != Success {
t.Fatalf("Failed to lookup, got %d", res) t.Fatalf("Failed to lookup, got %d", res)
} }
r = new(dns.Msg) r = new(dns.Msg)
r.SetQuestion("miek.nl", dns.TypeNS) r.SetQuestion("miek.nl", dns.TypeNS)
state = request.Request{W: &test.ResponseWriter{}, Req: r} state = request.Request{W: &test.ResponseWriter{}, Req: r}
if _, _, _, res := z.Lookup(state, "miek.nl."); res != Success { if _, _, _, res := z.Lookup(ctx, state, "miek.nl."); res != Success {
t.Fatalf("Failed to lookup, got %d", res) t.Fatalf("Failed to lookup, got %d", res)
} }
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
// ServeDNS implements the plugin.Handler interface. // ServeDNS implements the plugin.Handler interface.
func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r, Context: ctx} state := request.Request{W: w, Req: r}
qname := state.QName() qname := state.QName()
zone := plugin.Zones(k.Zones).Matches(qname) zone := plugin.Zones(k.Zones).Matches(qname)
...@@ -29,24 +29,24 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M ...@@ -29,24 +29,24 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
switch state.QType() { switch state.QType() {
case dns.TypeA: case dns.TypeA:
records, err = plugin.A(&k, zone, state, nil, plugin.Options{}) records, err = plugin.A(ctx, &k, zone, state, nil, plugin.Options{})
case dns.TypeAAAA: case dns.TypeAAAA:
records, err = plugin.AAAA(&k, zone, state, nil, plugin.Options{}) records, err = plugin.AAAA(ctx, &k, zone, state, nil, plugin.Options{})
case dns.TypeTXT: case dns.TypeTXT:
records, err = plugin.TXT(&k, zone, state, plugin.Options{}) records, err = plugin.TXT(ctx, &k, zone, state, plugin.Options{})
case dns.TypeCNAME: case dns.TypeCNAME:
records, err = plugin.CNAME(&k, zone, state, plugin.Options{}) records, err = plugin.CNAME(ctx, &k, zone, state, plugin.Options{})
case dns.TypePTR: case dns.TypePTR:
records, err = plugin.PTR(&k, zone, state, plugin.Options{}) records, err = plugin.PTR(ctx, &k, zone, state, plugin.Options{})
case dns.TypeMX: case dns.TypeMX:
records, extra, err = plugin.MX(&k, zone, state, plugin.Options{}) records, extra, err = plugin.MX(ctx, &k, zone, state, plugin.Options{})
case dns.TypeSRV: case dns.TypeSRV:
records, extra, err = plugin.SRV(&k, zone, state, plugin.Options{}) records, extra, err = plugin.SRV(ctx, &k, zone, state, plugin.Options{})
case dns.TypeSOA: case dns.TypeSOA:
records, err = plugin.SOA(&k, zone, state, plugin.Options{}) records, err = plugin.SOA(ctx, &k, zone, state, plugin.Options{})
case dns.TypeNS: case dns.TypeNS:
if state.Name() == zone { if state.Name() == zone {
records, extra, err = plugin.NS(&k, zone, state, plugin.Options{}) records, extra, err = plugin.NS(ctx, &k, zone, state, plugin.Options{})
break break
} }
fallthrough fallthrough
...@@ -54,7 +54,7 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M ...@@ -54,7 +54,7 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
k.Transfer(ctx, state) k.Transfer(ctx, state)
default: default:
// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN // Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
_, err = plugin.A(&k, zone, state, nil, plugin.Options{}) _, err = plugin.A(ctx, &k, zone, state, nil, plugin.Options{})
} }
if k.IsNameError(err) { if k.IsNameError(err) {
...@@ -63,16 +63,16 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M ...@@ -63,16 +63,16 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
} }
if !k.APIConn.HasSynced() { if !k.APIConn.HasSynced() {
// If we haven't synchronized with the kubernetes cluster, return server failure // If we haven't synchronized with the kubernetes cluster, return server failure
return plugin.BackendError(&k, zone, dns.RcodeServerFailure, state, nil /* err */, plugin.Options{}) return plugin.BackendError(ctx, &k, zone, dns.RcodeServerFailure, state, nil /* err */, plugin.Options{})
} }
return plugin.BackendError(&k, zone, dns.RcodeNameError, state, nil /* err */, plugin.Options{}) return plugin.BackendError(ctx, &k, zone, dns.RcodeNameError, state, nil /* err */, plugin.Options{})
} }
if err != nil { if err != nil {
return dns.RcodeServerFailure, err return dns.RcodeServerFailure, err
} }
if len(records) == 0 { if len(records) == 0 {
return plugin.BackendError(&k, zone, dns.RcodeSuccess, state, nil, plugin.Options{}) return plugin.BackendError(ctx, &k, zone, dns.RcodeSuccess, state, nil, plugin.Options{})
} }
m := new(dns.Msg) m := new(dns.Msg)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
package kubernetes package kubernetes
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"net" "net"
...@@ -86,7 +87,7 @@ var ( ...@@ -86,7 +87,7 @@ var (
) )
// Services implements the ServiceBackend interface. // Services implements the ServiceBackend interface.
func (k *Kubernetes) Services(state request.Request, exact bool, opt plugin.Options) (svcs []msg.Service, err error) { func (k *Kubernetes) Services(ctx context.Context, state request.Request, exact bool, opt plugin.Options) (svcs []msg.Service, err error) {
// We're looking again at types, which we've already done in ServeDNS, but there are some types k8s just can't answer. // We're looking again at types, which we've already done in ServeDNS, but there are some types k8s just can't answer.
switch state.QType() { switch state.QType() {
...@@ -119,7 +120,7 @@ func (k *Kubernetes) Services(state request.Request, exact bool, opt plugin.Opti ...@@ -119,7 +120,7 @@ func (k *Kubernetes) Services(state request.Request, exact bool, opt plugin.Opti
return []msg.Service{svc}, nil return []msg.Service{svc}, nil
} }
s, e := k.Records(state, false) s, e := k.Records(ctx, state, false)
// SRV for external services is not yet implemented, so remove those records. // SRV for external services is not yet implemented, so remove those records.
...@@ -141,8 +142,8 @@ func (k *Kubernetes) Services(state request.Request, exact bool, opt plugin.Opti ...@@ -141,8 +142,8 @@ func (k *Kubernetes) Services(state request.Request, exact bool, opt plugin.Opti
func (k *Kubernetes) primaryZone() string { return k.Zones[k.primaryZoneIndex] } func (k *Kubernetes) primaryZone() string { return k.Zones[k.primaryZoneIndex] }
// Lookup implements the ServiceBackend interface. // Lookup implements the ServiceBackend interface.
func (k *Kubernetes) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) { func (k *Kubernetes) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
return k.Upstream.Lookup(state, name, typ) return k.Upstream.Lookup(ctx, state, name, typ)
} }
// IsNameError implements the ServiceBackend interface. // IsNameError implements the ServiceBackend interface.
...@@ -236,7 +237,7 @@ func (k *Kubernetes) InitKubeCache() (err error) { ...@@ -236,7 +237,7 @@ func (k *Kubernetes) InitKubeCache() (err error) {
} }
// Records looks up services in kubernetes. // Records looks up services in kubernetes.
func (k *Kubernetes) Records(state request.Request, exact bool) ([]msg.Service, error) { func (k *Kubernetes) Records(ctx context.Context, state request.Request, exact bool) ([]msg.Service, error) {
r, e := parseRequest(state) r, e := parseRequest(state)
if e != nil { if e != nil {
return nil, e return nil, e
......
package kubernetes package kubernetes
import ( import (
"context"
"testing" "testing"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
...@@ -281,7 +282,7 @@ func TestServices(t *testing.T) { ...@@ -281,7 +282,7 @@ func TestServices(t *testing.T) {
Req: &dns.Msg{Question: []dns.Question{{Name: test.qname, Qtype: test.qtype}}}, Req: &dns.Msg{Question: []dns.Question{{Name: test.qname, Qtype: test.qtype}}},
Zone: "interwebs.test.", // must match from k.Zones[0] Zone: "interwebs.test.", // must match from k.Zones[0]
} }
svcs, e := k.Services(state, false, plugin.Options{}) svcs, e := k.Services(context.TODO(), state, false, plugin.Options{})
if e != nil { if e != nil {
t.Errorf("Test %d: got error '%v'", i, e) t.Errorf("Test %d: got error '%v'", i, e)
continue continue
......
package kubernetes package kubernetes
import ( import (
"context"
"strings" "strings"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
...@@ -10,11 +11,11 @@ import ( ...@@ -10,11 +11,11 @@ import (
) )
// Reverse implements the ServiceBackend interface. // Reverse implements the ServiceBackend interface.
func (k *Kubernetes) Reverse(state request.Request, exact bool, opt plugin.Options) ([]msg.Service, error) { func (k *Kubernetes) Reverse(ctx context.Context, state request.Request, exact bool, opt plugin.Options) ([]msg.Service, error) {
ip := dnsutil.ExtractAddressFromReverse(state.Name()) ip := dnsutil.ExtractAddressFromReverse(state.Name())
if ip == "" { if ip == "" {
_, e := k.Records(state, exact) _, e := k.Records(ctx, state, exact)
return nil, e return nil, e
} }
......
...@@ -45,7 +45,7 @@ func (k *Kubernetes) Transfer(ctx context.Context, state request.Request) (int, ...@@ -45,7 +45,7 @@ func (k *Kubernetes) Transfer(ctx context.Context, state request.Request) (int,
ch := make(chan *dns.Envelope) ch := make(chan *dns.Envelope)
tr := new(dns.Transfer) tr := new(dns.Transfer)
soa, err := plugin.SOA(k, state.Zone, state, plugin.Options{}) soa, err := plugin.SOA(ctx, k, state.Zone, state, plugin.Options{})
if err != nil { if err != nil {
return dns.RcodeServerFailure, nil return dns.RcodeServerFailure, nil
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
package upstream package upstream
import ( import (
"context"
"fmt" "fmt"
"github.com/miekg/dns" "github.com/miekg/dns"
...@@ -18,8 +19,8 @@ type Upstream struct{} ...@@ -18,8 +19,8 @@ type Upstream struct{}
func New() *Upstream { return &Upstream{} } func New() *Upstream { return &Upstream{} }
// Lookup routes lookups to our selves or forward to a remote. // Lookup routes lookups to our selves or forward to a remote.
func (u *Upstream) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) { func (u *Upstream) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
server, ok := state.Context.Value(dnsserver.Key{}).(*dnsserver.Server) server, ok := ctx.Value(dnsserver.Key{}).(*dnsserver.Server)
if !ok { if !ok {
return nil, fmt.Errorf("no full server is running") return nil, fmt.Errorf("no full server is running")
} }
...@@ -29,7 +30,7 @@ func (u *Upstream) Lookup(state request.Request, name string, typ uint16) (*dns. ...@@ -29,7 +30,7 @@ func (u *Upstream) Lookup(state request.Request, name string, typ uint16) (*dns.
nw := nonwriter.New(state.W) nw := nonwriter.New(state.W)
server.ServeDNS(state.Context, nw, req) server.ServeDNS(ctx, nw, req)
return nw.Msg, nil return nw.Msg, nil
} }
...@@ -117,7 +117,7 @@ func (h *Route53) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg ...@@ -117,7 +117,7 @@ func (h *Route53) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
var result file.Result var result file.Result
for _, hostedZone := range z { for _, hostedZone := range z {
h.zMu.RLock() h.zMu.RLock()
m.Answer, m.Ns, m.Extra, result = hostedZone.z.Lookup(state, qname) m.Answer, m.Ns, m.Extra, result = hostedZone.z.Lookup(ctx, state, qname)
h.zMu.RUnlock() h.zMu.RUnlock()
if len(m.Answer) != 0 { if len(m.Answer) != 0 {
break break
......
...@@ -51,7 +51,7 @@ type templateData struct { ...@@ -51,7 +51,7 @@ type templateData struct {
// ServeDNS implements the plugin.Handler interface. // ServeDNS implements the plugin.Handler interface.
func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r, Context: ctx} state := request.Request{W: w, Req: r}
zone := plugin.Zones(h.Zones).Matches(state.Name()) zone := plugin.Zones(h.Zones).Matches(state.Name())
if zone == "" { if zone == "" {
...@@ -85,7 +85,7 @@ func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ...@@ -85,7 +85,7 @@ func (h Handler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
} }
msg.Answer = append(msg.Answer, rr) msg.Answer = append(msg.Answer, rr)
if template.upstream != nil && (state.QType() == dns.TypeA || state.QType() == dns.TypeAAAA) && rr.Header().Rrtype == dns.TypeCNAME { if template.upstream != nil && (state.QType() == dns.TypeA || state.QType() == dns.TypeAAAA) && rr.Header().Rrtype == dns.TypeCNAME {
up, _ := template.upstream.Lookup(state, rr.(*dns.CNAME).Target, state.QType()) up, _ := template.upstream.Lookup(ctx, state, rr.(*dns.CNAME).Target, state.QType())
msg.Answer = append(msg.Answer, up.Answer...) msg.Answer = append(msg.Answer, up.Answer...)
} }
} }
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
package request package request
import ( import (
"context"
"net" "net"
"strings" "strings"
...@@ -19,12 +18,9 @@ type Request struct { ...@@ -19,12 +18,9 @@ type Request struct {
// Optional lowercased zone of this query. // Optional lowercased zone of this query.
Zone string Zone string
Context context.Context
// Cache size after first call to Size or Do. // Cache size after first call to Size or Do.
size int size int
do *bool // nil: nothing, otherwise *do value do *bool // nil: nothing, otherwise *do value
// TODO(miek): opt record itself as well?
// Caches // Caches
name string // lowercase qname. name string // lowercase qname.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment