mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 10:13:14 -05:00
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:
@@ -1,60 +0,0 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/test"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/mholt/caddy/caddyfile"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func TestUnhealthy(t *testing.T) {
|
||||
// High HC interval, we want to test the HC after failed queries.
|
||||
config := "proxy . %s {\n health_check /healthcheck:%s 10s \nfail_timeout 100ms\n}"
|
||||
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body.Close()
|
||||
w.Write([]byte("OK"))
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
port := backend.URL[17:] // Remove all crap up to the port
|
||||
back := backend.URL[7:] // Remove http://
|
||||
|
||||
c := caddyfile.NewDispenser("testfile", strings.NewReader(fmt.Sprintf(config, back, port)))
|
||||
upstreams, err := NewStaticUpstreams(&c)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error. Got: %s", err)
|
||||
}
|
||||
p := &Proxy{Upstreams: &upstreams}
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion("example.org.", dns.TypeA)
|
||||
state := request.Request{W: &test.ResponseWriter{}, Req: m}
|
||||
|
||||
// Should all fail.
|
||||
for j := 0; j < failureCheck; j++ {
|
||||
if _, err := p.Forward(state); err == nil {
|
||||
t.Errorf("Expected error. Got: nil")
|
||||
}
|
||||
}
|
||||
|
||||
fails := atomic.LoadInt32(&upstreams[0].(*staticUpstream).Hosts[0].Fails)
|
||||
if fails != 3 {
|
||||
t.Errorf("Expected %d fails, got %d", 3, fails)
|
||||
}
|
||||
// HC should be kicked off, and reset the counter to 0
|
||||
i := 0
|
||||
for fails != 0 {
|
||||
fails = atomic.LoadInt32(&upstreams[0].(*staticUpstream).Hosts[0].Fails)
|
||||
time.Sleep(100 * time.Microsecond)
|
||||
i++
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
package proxy
|
||||
|
||||
// functions other plugin might want to use to do lookup in the same style as the proxy.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/pkg/healthcheck"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// NewLookup create a new proxy with the hosts in host and a Random policy.
|
||||
func NewLookup(hosts []string) Proxy { return NewLookupWithOption(hosts, Options{}) }
|
||||
|
||||
// NewLookupWithOption process creates a simple round robin forward with potentially forced proto for upstream.
|
||||
func NewLookupWithOption(hosts []string, opts Options) Proxy {
|
||||
p := Proxy{Next: nil}
|
||||
|
||||
// TODO(miek): this needs to be unified with upstream.go's NewStaticUpstreams, caddy uses NewHost
|
||||
// we should copy/make something similar.
|
||||
upstream := &staticUpstream{
|
||||
from: ".",
|
||||
HealthCheck: healthcheck.HealthCheck{
|
||||
FailTimeout: 5 * time.Second,
|
||||
MaxFails: 3,
|
||||
},
|
||||
ex: newDNSExWithOption(opts),
|
||||
}
|
||||
upstream.Hosts = make([]*healthcheck.UpstreamHost, len(hosts))
|
||||
|
||||
for i, host := range hosts {
|
||||
uh := &healthcheck.UpstreamHost{
|
||||
Name: host,
|
||||
FailTimeout: upstream.FailTimeout,
|
||||
CheckDown: checkDownFunc(upstream),
|
||||
}
|
||||
|
||||
upstream.Hosts[i] = uh
|
||||
}
|
||||
p.Upstreams = &[]Upstream{upstream}
|
||||
return p
|
||||
}
|
||||
|
||||
// Lookup will use name and type to forge a new message and will send that upstream. It will
|
||||
// set any EDNS0 options correctly so that downstream will be able to process the reply.
|
||||
func (p Proxy) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
|
||||
req := new(dns.Msg)
|
||||
req.SetQuestion(name, typ)
|
||||
state.SizeAndDo(req)
|
||||
|
||||
state2 := request.Request{W: state.W, Req: req}
|
||||
|
||||
return p.lookup(state2)
|
||||
}
|
||||
|
||||
// Forward forward the request in state as-is. Unlike Lookup that adds EDNS0 suffix to the message.
|
||||
func (p Proxy) Forward(state request.Request) (*dns.Msg, error) {
|
||||
return p.lookup(state)
|
||||
}
|
||||
|
||||
func (p Proxy) lookup(state request.Request) (*dns.Msg, error) {
|
||||
upstream := p.match(state)
|
||||
if upstream == nil {
|
||||
return nil, errInvalidDomain
|
||||
}
|
||||
for {
|
||||
start := time.Now()
|
||||
var reply *dns.Msg
|
||||
var backendErr error
|
||||
|
||||
// Since Select() should give us "up" hosts, keep retrying
|
||||
// hosts until timeout (or until we get a nil host).
|
||||
for time.Since(start) < tryDuration {
|
||||
host := upstream.Select()
|
||||
if host == nil {
|
||||
return nil, fmt.Errorf("%s: %s", errUnreachable, "no upstream host")
|
||||
}
|
||||
|
||||
// duplicated from proxy.go, but with a twist, we don't write the
|
||||
// reply back to the client, we return it and there is no monitoring to update here.
|
||||
|
||||
atomic.AddInt64(&host.Conns, 1)
|
||||
|
||||
reply, backendErr = upstream.Exchanger().Exchange(context.TODO(), host.Name, state)
|
||||
|
||||
atomic.AddInt64(&host.Conns, -1)
|
||||
|
||||
if backendErr == nil {
|
||||
|
||||
if !state.Match(reply) {
|
||||
return state.ErrorMessage(dns.RcodeFormatError), nil
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
|
||||
if oe, ok := backendErr.(*net.OpError); ok {
|
||||
if oe.Timeout() { // see proxy.go for docs.
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
timeout := host.FailTimeout
|
||||
if timeout == 0 {
|
||||
timeout = defaultFailTimeout
|
||||
}
|
||||
|
||||
atomic.AddInt32(&host.Fails, 1)
|
||||
fails := atomic.LoadInt32(&host.Fails)
|
||||
|
||||
go func(host *healthcheck.UpstreamHost, timeout time.Duration) {
|
||||
time.Sleep(timeout)
|
||||
atomic.AddInt32(&host.Fails, -1)
|
||||
if fails%failureCheck == 0 { // Kick off healthcheck on eveyry third failure.
|
||||
host.HealthCheckURL()
|
||||
}
|
||||
}(host, timeout)
|
||||
}
|
||||
return nil, fmt.Errorf("%s: %s", errUnreachable, backendErr)
|
||||
}
|
||||
}
|
||||
@@ -9,12 +9,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
||||
"github.com/coredns/coredns/plugin/test"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/mholt/caddy/caddyfile"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func TestStop(t *testing.T) {
|
||||
@@ -75,25 +70,3 @@ func TestStop(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyRefused(t *testing.T) {
|
||||
s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
|
||||
ret := new(dns.Msg)
|
||||
ret.SetReply(r)
|
||||
ret.Rcode = dns.RcodeRefused
|
||||
w.WriteMsg(ret)
|
||||
})
|
||||
defer s.Close()
|
||||
|
||||
p := NewLookup([]string{s.Addr})
|
||||
|
||||
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
|
||||
state.Req.SetQuestion("example.org.", dns.TypeA)
|
||||
resp, err := p.Forward(state)
|
||||
if err != nil {
|
||||
t.Fatal("Expected to receive reply, but didn't")
|
||||
}
|
||||
if resp.Rcode != dns.RcodeRefused {
|
||||
t.Errorf("Expected rcode to be %d, got %d", dns.RcodeRefused, resp.Rcode)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user