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

@@ -7,10 +7,6 @@ import (
"testing"
"time"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -34,10 +30,9 @@ func TestAuto(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "www.example.org.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("www.example.org.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}
@@ -52,7 +47,7 @@ func TestAuto(t *testing.T) {
time.Sleep(1500 * time.Millisecond) // wait for it to be picked up
resp, err = p.Lookup(state, "www.example.org.", dns.TypeA)
resp, err = dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}
@@ -64,7 +59,7 @@ func TestAuto(t *testing.T) {
os.Remove(filepath.Join(tmpdir, "db.example.org"))
time.Sleep(1100 * time.Millisecond) // wait for it to be picked up
resp, err = p.Lookup(state, "www.example.org.", dns.TypeA)
resp, err = dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}
@@ -99,10 +94,9 @@ func TestAutoNonExistentZone(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "example.org.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}
@@ -145,12 +139,9 @@ func TestAutoAXFR(t *testing.T) {
time.Sleep(1100 * time.Millisecond) // wait for it to be picked up
p := proxy.NewLookup([]string{udp})
m := new(dns.Msg)
m.SetAxfr("example.org.")
state := request.Request{W: &test.ResponseWriter{}, Req: m}
resp, err := p.Lookup(state, "example.org.", dns.TypeAXFR)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}

View File

@@ -3,9 +3,7 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -40,21 +38,20 @@ func TestLookupCache(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
t.Run("Long TTL", func(t *testing.T) {
testCase(t, state, p, "example.org.", 2, 10)
testCase(t, "example.org.", udp, 2, 10)
})
t.Run("Short TTL", func(t *testing.T) {
testCase(t, state, p, "short.example.org.", 1, 5)
testCase(t, "short.example.org.", udp, 1, 5)
})
}
func testCase(t *testing.T, state request.Request, p proxy.Proxy, name string, expectAnsLen int, expectTTL uint32) {
resp, err := p.Lookup(state, name, dns.TypeA)
func testCase(t *testing.T, name, addr string, expectAnsLen int, expectTTL uint32) {
m := new(dns.Msg)
m.SetQuestion(name, dns.TypeA)
resp, err := dns.Exchange(m, addr)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}

View File

@@ -3,9 +3,7 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
mtest "github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -47,11 +45,10 @@ func TestLookupDS(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &mtest.ResponseWriter{}, Req: new(dns.Msg)}
for _, tc := range dsTestCases {
resp, err := p.Lookup(state, tc.Qname, tc.Qtype)
m := new(dns.Msg)
m.SetQuestion(tc.Qname, tc.Qtype)
resp, err := dns.Exchange(m, udp)
if err != nil || resp == nil {
t.Fatalf("Expected to receive reply, but didn't for %s %d", tc.Qname, tc.Qtype)
}

View File

@@ -7,9 +7,6 @@ import (
"testing"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -38,16 +35,15 @@ func TestEtcdCache(t *testing.T) {
defer delete(ctx, t, etc, serv.Key)
}
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "b.example.skydns.test.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("b.example.skydns.test.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Errorf("Expected to receive reply, but didn't: %s", err)
}
checkResponse(t, resp)
resp, err = p.Lookup(state, "b.example.skydns.test.", dns.TypeA)
resp, err = dns.Exchange(m, udp)
if err != nil {
t.Errorf("Expected to receive reply, but didn't: %s", err)
}

View File

@@ -10,9 +10,6 @@ import (
"github.com/coredns/coredns/plugin/etcd"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
etcdcv3 "github.com/coreos/etcd/clientv3"
"github.com/miekg/dns"
@@ -38,7 +35,7 @@ func TestEtcdStubAndProxyLookup(t *testing.T) {
stubzones
path /skydns
endpoint http://localhost:2379
upstream 8.8.8.8:53 8.8.4.4:53
upstream
fallthrough
}
proxy . 8.8.8.8:53
@@ -58,9 +55,9 @@ func TestEtcdStubAndProxyLookup(t *testing.T) {
defer delete(ctx, t, etc, serv.Key)
}
p := proxy.NewLookup([]string{udp}) // use udp port from the server
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "example.com.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %v", err)
}

View File

@@ -11,6 +11,5 @@ short.example.org. 1 IN A 127.0.0.3
*.w.example.org. IN TXT "Wildcard"
a.b.c.w.example.org. IN TXT "Not a wildcard"
cname.example.org. IN CNAME www.example.net.
service.example.org. IN SRV 8080 10 10 example.org.
`

View File

@@ -3,10 +3,6 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -30,10 +26,9 @@ func TestZoneExternalCNAMELookupWithoutProxy(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "cname.example.org.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("cname.example.org.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}
@@ -52,11 +47,12 @@ func TestZoneExternalCNAMELookupWithProxy(t *testing.T) {
}
defer rm()
// Corefile with for example without proxy section.
corefile := `example.org:0 {
file ` + name + ` {
upstream 8.8.8.8
// Corefile with for example proxy section.
corefile := `.:0 {
file ` + name + ` example.org {
upstream
}
proxy . 8.8.8.8 8.8.4.4
}
`
i, udp, _, err := CoreDNSServerAndPorts(corefile)
@@ -65,10 +61,9 @@ func TestZoneExternalCNAMELookupWithProxy(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "cname.example.org.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("cname.example.org.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}

View File

@@ -6,9 +6,6 @@ import (
"time"
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -39,10 +36,9 @@ example.net:0 {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "example.org.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}
@@ -55,7 +51,7 @@ example.net:0 {
time.Sleep(2 * time.Second) // reload time
resp, err = p.Lookup(state, "example.org.", dns.TypeA)
resp, err = dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}

View File

@@ -3,10 +3,6 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -30,10 +26,9 @@ func TestZoneSRVAdditional(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "service.example.org.", dns.TypeSRV)
m := new(dns.Msg)
m.SetQuestion("service.example.org.", dns.TypeSRV)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}

View File

@@ -3,10 +3,6 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -24,10 +20,9 @@ func TestHostsInlineLookup(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "example.org.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}

View File

@@ -3,36 +3,9 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
func TestProxyErratic(t *testing.T) {
corefile := `example.org:0 {
erratic {
drop 2
}
}
`
backend, udp, _, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer backend.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
// We do one lookup that should not time out.
// After this the backend is marked unhealthy anyway. So basically this
// tests that it times out.
p.Lookup(state, "example.org.", dns.TypeA)
}
func TestProxyThreeWay(t *testing.T) {
// Run 3 CoreDNS server, 2 upstream ones and a proxy. 1 Upstream is unhealthy after 1 query, but after
// that we should still be able to send to the other one.

View File

@@ -1,86 +0,0 @@
package test
import (
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
func TestProxyWithHTTPCheckOK(t *testing.T) {
healthCheckServer := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
io.WriteString(w, "OK\n")
}))
defer healthCheckServer.Close()
healthCheckURL, err := url.Parse(healthCheckServer.URL)
if err != nil {
t.Fatal(err)
}
// TODO: use URL.Port() (Go 1.8+) once we've deprecated Go 1.7 support
var healthCheckPort string
if _, healthCheckPort, err = net.SplitHostPort(healthCheckURL.Host); err != nil {
healthCheckPort = "80"
}
name, rm, err := test.TempFile(".", exampleOrg)
if err != nil {
t.Fatalf("Failed to create zone: %s", err)
}
defer rm()
// We have to bind to 127.0.0.1 because the server started by
// httptest.NewServer does, and the IP addresses of the backend
// DNS and HTTP servers must match.
authoritativeCorefile := `example.org:0 {
bind 127.0.0.1
file ` + name + `
}
`
authoritativeInstance, authoritativeAddr, _, err := CoreDNSServerAndPorts(authoritativeCorefile)
if err != nil {
t.Fatalf("Could not get CoreDNS authoritative instance: %s", err)
}
defer authoritativeInstance.Stop()
proxyCorefile := `example.org:0 {
proxy . ` + authoritativeAddr + ` {
health_check /health:` + healthCheckPort + ` 1s
}
}
`
proxyInstance, proxyAddr, _, err := CoreDNSServerAndPorts(proxyCorefile)
if err != nil {
t.Fatalf("Could not get CoreDNS proxy instance: %s", err)
}
defer proxyInstance.Stop()
p := proxy.NewLookup([]string{proxyAddr})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "example.org.", dns.TypeA)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}
// expect answer section with A record in it
if len(resp.Answer) == 0 {
t.Fatalf("Expected to at least one RR in the answer section, got none: %s", resp)
}
if resp.Answer[0].Header().Rrtype != dns.TypeA {
t.Errorf("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype)
}
if resp.Answer[0].(*dns.A).A.String() != "127.0.0.1" {
t.Errorf("Expected 127.0.0.1, got: %s", resp.Answer[0].(*dns.A).A.String())
}
}

View File

@@ -3,9 +3,7 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -29,46 +27,9 @@ func TestLookupProxy(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "example.org.", dns.TypeA)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}
// expect answer section with A record in it
if len(resp.Answer) == 0 {
t.Fatalf("Expected to at least one RR in the answer section, got none: %s", resp)
}
if resp.Answer[0].Header().Rrtype != dns.TypeA {
t.Errorf("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype)
}
if resp.Answer[0].(*dns.A).A.String() != "127.0.0.1" {
t.Errorf("Expected 127.0.0.1, got: %s", resp.Answer[0].(*dns.A).A.String())
}
}
func TestLookupDnsWithForcedTcp(t *testing.T) {
t.Parallel()
name, rm, err := test.TempFile(".", exampleOrg)
if err != nil {
t.Fatalf("Failed to create zone: %s", err)
}
defer rm()
corefile := `example.org:0 {
file ` + name + `
}
`
i, _, tcp, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer i.Stop()
p := proxy.NewLookupWithOption([]string{tcp}, proxy.Options{ForceTCP: true})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "example.org.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}
@@ -108,13 +69,12 @@ func BenchmarkProxyLookup(b *testing.B) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeA)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := p.Lookup(state, "example.org.", dns.TypeA)
if err != nil {
if _, err := dns.Exchange(m, udp); err != nil {
b.Fatal("Expected to receive reply, but didn't")
}
}

View File

@@ -3,10 +3,6 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -26,9 +22,9 @@ func TestReverseCorefile(t *testing.T) {
t.Fatalf("Could not get UDP listening port")
}
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "17.0.0.10.in-addr.arpa.", dns.TypePTR)
m := new(dns.Msg)
m.SetQuestion("17.0.0.10.in-addr.arpa.", dns.TypePTR)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}

View File

@@ -3,9 +3,7 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -25,10 +23,9 @@ func TestEmptySecondaryZone(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
resp, err := p.Lookup(state, "www.example.org.", dns.TypeA)
m := new(dns.Msg)
m.SetQuestion("www.example.org.", dns.TypeA)
resp, err := dns.Exchange(m, udp)
if err != nil {
t.Fatal("Expected to receive reply, but didn't")
}

View File

@@ -3,9 +3,7 @@ package test
import (
"testing"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -29,11 +27,10 @@ func TestLookupWildcard(t *testing.T) {
}
defer i.Stop()
p := proxy.NewLookup([]string{udp})
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
for _, lookup := range []string{"a.w.example.org.", "a.a.w.example.org."} {
resp, err := p.Lookup(state, lookup, dns.TypeTXT)
m := new(dns.Msg)
m.SetQuestion(lookup, dns.TypeTXT)
resp, err := dns.Exchange(m, udp)
if err != nil || resp == nil {
t.Fatalf("Expected to receive reply, but didn't for %s", lookup)
}
@@ -64,7 +61,9 @@ func TestLookupWildcard(t *testing.T) {
}
for _, lookup := range []string{"w.example.org.", "a.w.example.org.", "a.a.w.example.org."} {
resp, err := p.Lookup(state, lookup, dns.TypeSRV)
m := new(dns.Msg)
m.SetQuestion(lookup, dns.TypeSRV)
resp, err := dns.Exchange(m, udp)
if err != nil || resp == nil {
t.Fatal("Expected to receive reply, but didn't", lookup)
}