Default to upstream to self (#2436)

* Default to upstream to self

This is a backwards incompatible change.

This is a massive (cleanup) PR where we default to resolving external
names by the coredns process itself, instead of directly forwarding them
to some upstream.

This ignores any arguments `upstream` may have had and makes it depend
on proxy/forward configuration in the Corefile. This allows resolved
upstream names to be cached and we have better healthchecking of the
upstreams. It also means there is only one way to resolve names, by
either using the proxy or forward plugin.

The proxy/forward lookup.go functions have been removed. This also
lessen the dependency on proxy, meaning deprecating proxy will become
easier. Some tests have been removed as well, or moved to the top-level
test directory as they now require a full coredns process instead of
just the plugin.

For the etcd plugin, the entire StubZone resolving is *dropped*! This
was a hacky (but working) solution to say the least. If someone cares
deeply it can be brought back (maybe)?

The pkg/upstream is now very small and almost does nothing. Also the
New() function was changed to return a pointer to upstream.Upstream. It
also returns only one parameter, so any stragglers using it will
encounter a compile error.

All documentation has been adapted. This affected the following plugins:
* etcd
* file
* auto
* secondary
* federation
* template
* route53

A followup PR will make any upstream directives with arguments an error,
right now they are ignored.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Fix etcd build - probably still fails unit test

Signed-off-by: Miek Gieben <miek@miek.nl>

* Slightly smarter lookup check in upstream

Signed-off-by: Miek Gieben <miek@miek.nl>

* Compilez

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2019-01-13 16:54:49 +00:00
committed by GitHub
parent 6b56a9c921
commit 9c16ed1d14
55 changed files with 184 additions and 1349 deletions

View File

@@ -1,58 +1,35 @@
// Package upstream abstracts a upstream lookups so that plugins
// can handle them in an unified way.
// Package upstream abstracts a upstream lookups so that plugins can handle them in an unified way.
package upstream
import (
"fmt"
"github.com/miekg/dns"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin/pkg/nonwriter"
"github.com/coredns/coredns/plugin/pkg/parse"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/request"
)
// Upstream is used to resolve CNAME targets
type Upstream struct {
self bool
Forward *proxy.Proxy
}
// Upstream is used to resolve CNAME or other external targets via CoreDNS itself.
type Upstream struct{}
// New creates a new Upstream for given destination(s). If dests is empty it default to upstreaming to
// the coredns process.
func New(dests []string) (Upstream, error) {
u := Upstream{}
if len(dests) == 0 {
u.self = true
return u, nil
}
u.self = false
ups, err := parse.HostPortOrFile(dests...)
if err != nil {
return u, err
}
p := proxy.NewLookup(ups)
u.Forward = &p
return u, nil
}
// New creates a new Upstream to resolve names using the the coredns process.
func New() *Upstream { return &Upstream{} }
// 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) {
if u.self {
req := new(dns.Msg)
req.SetQuestion(name, typ)
nw := nonwriter.New(state.W)
server := state.Context.Value(dnsserver.Key{}).(*dnsserver.Server)
server.ServeDNS(state.Context, nw, req)
return nw.Msg, nil
func (u *Upstream) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
server, ok := state.Context.Value(dnsserver.Key{}).(*dnsserver.Server)
if !ok {
return nil, fmt.Errorf("no full server is running")
}
if u.Forward != nil {
return u.Forward.Lookup(state, name, typ)
}
req := new(dns.Msg)
req.SetQuestion(name, typ)
return nil, nil
nw := nonwriter.New(state.W)
server.ServeDNS(state.Context, nw, req)
return nw.Msg, nil
}