parent
c3705ec68c
commit
b46b9880bd
20 changed files with 597 additions and 642 deletions
|
@ -21,6 +21,7 @@ var directives = []string{
|
|||
"prometheus",
|
||||
"errors",
|
||||
"log",
|
||||
"autopath",
|
||||
"dnstap",
|
||||
"chaos",
|
||||
"cache",
|
||||
|
|
|
@ -5,6 +5,7 @@ package core
|
|||
import (
|
||||
// Include all middleware.
|
||||
_ "github.com/coredns/coredns/middleware/auto"
|
||||
_ "github.com/coredns/coredns/middleware/autopath"
|
||||
_ "github.com/coredns/coredns/middleware/bind"
|
||||
_ "github.com/coredns/coredns/middleware/cache"
|
||||
_ "github.com/coredns/coredns/middleware/chaos"
|
||||
|
|
|
@ -29,21 +29,22 @@
|
|||
70:prometheus:metrics
|
||||
80:errors:errors
|
||||
90:log:log
|
||||
95:dnstap:dnstap
|
||||
100:chaos:chaos
|
||||
110:cache:cache
|
||||
120:rewrite:rewrite
|
||||
130:loadbalance:loadbalance
|
||||
140:dnssec:dnssec
|
||||
150:reverse:reverse
|
||||
160:hosts:hosts
|
||||
170:kubernetes:kubernetes
|
||||
180:file:file
|
||||
190:auto:auto
|
||||
200:secondary:secondary
|
||||
210:etcd:etcd
|
||||
220:proxy:proxy
|
||||
230:erratic:erratic
|
||||
240:whoami:whoami
|
||||
100:autopath:autopath
|
||||
110:dnstap:dnstap
|
||||
120:chaos:chaos
|
||||
130:cache:cache
|
||||
140:rewrite:rewrite
|
||||
150:loadbalance:loadbalance
|
||||
160:dnssec:dnssec
|
||||
170:reverse:reverse
|
||||
180:hosts:hosts
|
||||
190:kubernetes:kubernetes
|
||||
200:file:file
|
||||
210:auto:auto
|
||||
220:secondary:secondary
|
||||
230:etcd:etcd
|
||||
240:proxy:proxy
|
||||
250:erratic:erratic
|
||||
260:whoami:whoami
|
||||
500:startup:github.com/mholt/caddy/startupshutdown
|
||||
510:shutdown:github.com/mholt/caddy/startupshutdown
|
||||
|
|
39
middleware/autopath/README.md
Normal file
39
middleware/autopath/README.md
Normal file
|
@ -0,0 +1,39 @@
|
|||
# autopath
|
||||
|
||||
The *autopath* middleware allows CoreDNS to perform server side search path completion.
|
||||
If it sees a query that matches the first element of the configured search path, *autopath* will
|
||||
follow the chain of search path elements and returns the first reply that is not NXDOMAIN.
|
||||
On any failures the original reply is returned.
|
||||
|
||||
Because *autopath* returns a reply for a name that wasn't the original question it will add a CNAME
|
||||
that points from the original name (with the search path element in it) to the name of this answer.
|
||||
|
||||
## Syntax
|
||||
|
||||
~~~
|
||||
autopath [ZONE..] RESOLV-CONF
|
||||
~~~
|
||||
|
||||
* **ZONES** zones *autopath* should be authoritative for.
|
||||
* **RESOLV-CONF** points to a `resolv.conf` like file or uses a special syntax to point to another
|
||||
middleware. For instance `@kubernetes`, will call out to the kubernetes middleware (for each
|
||||
query) to retrieve the search list it should use.
|
||||
|
||||
Currently the following set of middleware has implemented *autopath*:
|
||||
|
||||
* *kubernetes*
|
||||
|
||||
## Examples
|
||||
|
||||
~~~
|
||||
autopath my-resolv.conf
|
||||
~~~
|
||||
|
||||
Use `my-resolv.conf` as the file to get the search path from. This file only needs so have one line:
|
||||
`search domain1 domain2 ...`
|
||||
|
||||
~~~
|
||||
autopath @kubernetes
|
||||
~~~
|
||||
|
||||
Use the search path dynamically retrieved from the kubernetes middleware.
|
142
middleware/autopath/autopath.go
Normal file
142
middleware/autopath/autopath.go
Normal file
|
@ -0,0 +1,142 @@
|
|||
package autopath
|
||||
|
||||
/*
|
||||
Autopath is a hack; it shortcuts the client's search path resolution by performing
|
||||
these lookups on the server...
|
||||
|
||||
The server has a copy (via AutoPathFunc) of the client's search path and on
|
||||
receiving a query it first establish if the suffix matches the FIRST configured
|
||||
element. If no match can be found the query will be forwarded up the middleware
|
||||
chain without interference (iff 'fallthrough' has been set).
|
||||
|
||||
If the query is deemed to fall in the search path the server will perform the
|
||||
queries with each element of the search path appended in sequence until a
|
||||
non-NXDOMAIN answer has been found. That reply will then be returned to the
|
||||
client - with some CNAME hackery to let the client accept the reply.
|
||||
|
||||
If all queries return NXDOMAIN we return the original as-is and let the client
|
||||
continue searching. The client will go to the next element in the search path,
|
||||
but we won’t do any more autopathing. It means that in the failure case, you do
|
||||
more work, since the server looks it up, then the client still needs to go
|
||||
through the search path.
|
||||
|
||||
It is assume the search path ordering is identical between server and client.
|
||||
|
||||
Midldeware implementing autopath, must have a function called `AutoPath` of type
|
||||
AutoPathFunc. Note the searchpath must be ending with the empty string.
|
||||
|
||||
I.e:
|
||||
|
||||
func (m Middleware ) AutoPath(state request.Request) ([]string, error) {
|
||||
return []string{"first", "second", "last", ""}, nil
|
||||
}
|
||||
*/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// AutoPathFunc defines the function middleware should implement to return a search
|
||||
// path to the autopath middleware. The last element of the slice must be the empty string.
|
||||
// If AutoPathFunc returns a non-nil error no autopathing is performed.
|
||||
type AutoPathFunc func(request.Request) ([]string, error)
|
||||
|
||||
// Autopath perform autopath: service side search path completion.
|
||||
type AutoPath struct {
|
||||
Next middleware.Handler
|
||||
Zones []string
|
||||
|
||||
// Search always includes "" as the last element, so we try the base query with out any search paths added as well.
|
||||
search []string
|
||||
searchFunc AutoPathFunc
|
||||
}
|
||||
|
||||
func (a *AutoPath) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
state := request.Request{W: w, Req: r}
|
||||
if state.QClass() != dns.ClassINET {
|
||||
return dns.RcodeServerFailure, middleware.Error(a.Name(), errors.New("can only deal with ClassINET"))
|
||||
}
|
||||
|
||||
// Check if autopath should be done, searchFunc takes precedence over the local configured
|
||||
// search path.
|
||||
var err error
|
||||
searchpath := a.search
|
||||
if a.searchFunc != nil {
|
||||
searchpath, err = a.searchFunc(state)
|
||||
if err != nil {
|
||||
return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
match := a.FirstInSearchPath(state.Name())
|
||||
if !match {
|
||||
return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
|
||||
}
|
||||
|
||||
origQName := state.QName()
|
||||
|
||||
// Establish base name of the query. I.e what was originally asked.
|
||||
base, err := dnsutil.TrimZone(state.QName(), a.search[0]) // TOD(miek): we loose the original case of the query here.
|
||||
if err != nil {
|
||||
return dns.RcodeServerFailure, err
|
||||
}
|
||||
|
||||
firstReply := new(dns.Msg)
|
||||
firstRcode := 0
|
||||
var firstErr error
|
||||
// Walk the search path and see if we can get a non-nxdomain - if they all fail we return the first
|
||||
// query we've done and return that as-is. This means the client will do the search path walk again...
|
||||
for i, s := range searchpath {
|
||||
|
||||
newQName := base + "." + s
|
||||
r.Question[0].Name = newQName
|
||||
nw := NewNonWriter(w)
|
||||
|
||||
rcode, err := middleware.NextOrFailure(a.Name(), a.Next, ctx, nw, r)
|
||||
if i == 0 {
|
||||
firstReply = nw.Msg
|
||||
firstRcode = rcode
|
||||
firstErr = err
|
||||
}
|
||||
|
||||
if !middleware.ClientWrite(rcode) {
|
||||
continue
|
||||
}
|
||||
|
||||
if nw.Msg.Rcode == dns.RcodeNameError {
|
||||
continue
|
||||
}
|
||||
|
||||
msg := nw.Msg
|
||||
cnamer(msg, origQName)
|
||||
|
||||
// Write whatever non-nxdomain answer we've found.
|
||||
w.WriteMsg(msg)
|
||||
return rcode, err
|
||||
|
||||
}
|
||||
if middleware.ClientWrite(firstRcode) {
|
||||
w.WriteMsg(firstReply)
|
||||
}
|
||||
return firstRcode, firstErr
|
||||
}
|
||||
|
||||
// FirstInSearchPath checks if name is equal to are a sibling of the first element in the search path.
|
||||
func (a *AutoPath) FirstInSearchPath(name string) bool {
|
||||
if name == a.search[0] {
|
||||
return true
|
||||
}
|
||||
if dns.IsSubDomain(a.search[0], name) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (a AutoPath) Name() string { return "autopath" }
|
165
middleware/autopath/autopath_test.go
Normal file
165
middleware/autopath/autopath_test.go
Normal file
|
@ -0,0 +1,165 @@
|
|||
package autopath
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/pkg/dnsrecorder"
|
||||
"github.com/coredns/coredns/middleware/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var autopathTestCases = []test.Case{
|
||||
{
|
||||
// search path expansion.
|
||||
Qname: "b.example.org.", Qtype: dns.TypeA,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("b.example.org. 3600 IN CNAME b.com."),
|
||||
test.A("b.com." + defaultA),
|
||||
},
|
||||
},
|
||||
{
|
||||
// No search path expansion
|
||||
Qname: "a.example.com.", Qtype: dns.TypeA,
|
||||
Answer: []dns.RR{
|
||||
test.A("a.example.com." + defaultA),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func newTestAutoPath() *AutoPath {
|
||||
ap := new(AutoPath)
|
||||
ap.Next = nextHandler(map[string]int{
|
||||
"b.example.org.": dns.RcodeNameError,
|
||||
"b.com.": dns.RcodeSuccess,
|
||||
"a.example.com.": dns.RcodeSuccess,
|
||||
})
|
||||
|
||||
ap.search = []string{"example.org.", "example.com.", "com.", ""}
|
||||
return ap
|
||||
}
|
||||
|
||||
func TestAutoPath(t *testing.T) {
|
||||
ap := newTestAutoPath()
|
||||
ctx := context.TODO()
|
||||
|
||||
for _, tc := range autopathTestCases {
|
||||
m := tc.Msg()
|
||||
|
||||
rec := dnsrecorder.New(&test.ResponseWriter{})
|
||||
_, err := ap.ServeDNS(ctx, rec, m)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
resp := rec.Msg
|
||||
|
||||
if !test.Header(t, tc, resp) {
|
||||
t.Logf("%v\n", resp)
|
||||
continue
|
||||
}
|
||||
if !test.Section(t, tc, test.Answer, resp.Answer) {
|
||||
t.Logf("%v\n", resp)
|
||||
}
|
||||
if !test.Section(t, tc, test.Ns, resp.Ns) {
|
||||
t.Logf("%v\n", resp)
|
||||
}
|
||||
if !test.Section(t, tc, test.Extra, resp.Extra) {
|
||||
t.Logf("%v\n", resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var autopathNoAnswerTestCases = []test.Case{
|
||||
{
|
||||
// search path expansion, no answer
|
||||
Qname: "c.example.org.", Qtype: dns.TypeA,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("b.example.org. 3600 IN CNAME b.com."),
|
||||
test.A("b.com." + defaultA),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestAutoPathNoAnswer(t *testing.T) {
|
||||
ap := newTestAutoPath()
|
||||
ctx := context.TODO()
|
||||
|
||||
for _, tc := range autopathNoAnswerTestCases {
|
||||
m := tc.Msg()
|
||||
|
||||
rec := dnsrecorder.New(&test.ResponseWriter{})
|
||||
rcode, err := ap.ServeDNS(ctx, rec, m)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v\n", err)
|
||||
continue
|
||||
}
|
||||
if middleware.ClientWrite(rcode) {
|
||||
t.Fatalf("expected no client write, got one for rcode %d", rcode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// nextHandler returns a Handler that returns an answer for the question in the
|
||||
// request per the domain->answer map. On success an RR will be returned: "qname 3600 IN A 127.0.0.53"
|
||||
func nextHandler(mm map[string]int) test.Handler {
|
||||
return test.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
rcode, ok := mm[r.Question[0].Name]
|
||||
if !ok {
|
||||
return dns.RcodeServerFailure, nil
|
||||
}
|
||||
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
|
||||
switch rcode {
|
||||
case dns.RcodeNameError:
|
||||
m.Rcode = rcode
|
||||
m.Ns = []dns.RR{soa}
|
||||
w.WriteMsg(m)
|
||||
return m.Rcode, nil
|
||||
|
||||
case dns.RcodeSuccess:
|
||||
m.Rcode = rcode
|
||||
a, _ := dns.NewRR(r.Question[0].Name + defaultA)
|
||||
m.Answer = []dns.RR{a}
|
||||
|
||||
w.WriteMsg(m)
|
||||
return m.Rcode, nil
|
||||
default:
|
||||
panic("nextHandler: unhandled rcode")
|
||||
}
|
||||
|
||||
return dns.RcodeServerFailure, nil
|
||||
})
|
||||
}
|
||||
|
||||
const defaultA = " 3600 IN A 127.0.0.53"
|
||||
|
||||
var soa = func() dns.RR {
|
||||
s, _ := dns.NewRR("example.org. 1800 IN SOA example.org. example.org. 1502165581 14400 3600 604800 14400")
|
||||
return s
|
||||
}()
|
||||
|
||||
func TestInSearchPath(t *testing.T) {
|
||||
a := AutoPath{search: []string{"default.svc.cluster.local.", "svc.cluster.local.", "cluster.local."}}
|
||||
|
||||
tests := []struct {
|
||||
qname string
|
||||
b bool
|
||||
}{
|
||||
{"google.com", false},
|
||||
{"default.svc.cluster.local.", true},
|
||||
{"a.default.svc.cluster.local.", true},
|
||||
{"a.b.svc.cluster.local.", false},
|
||||
}
|
||||
for i, tc := range tests {
|
||||
got := a.FirstInSearchPath(tc.qname)
|
||||
if got != tc.b {
|
||||
t.Errorf("Test %d, got %d, expected %d", i, got, tc.b)
|
||||
}
|
||||
}
|
||||
}
|
25
middleware/autopath/cname.go
Normal file
25
middleware/autopath/cname.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package autopath
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// cnamer will prefix the answer section with a cname that points from original qname to the
|
||||
// name of the first RR. It will also update the question section and put original in there.
|
||||
func cnamer(m *dns.Msg, original string) {
|
||||
for _, a := range m.Answer {
|
||||
if strings.EqualFold(original, a.Header().Name) {
|
||||
continue
|
||||
}
|
||||
m.Answer = append(m.Answer, nil)
|
||||
copy(m.Answer[1:], m.Answer)
|
||||
m.Answer[0] = &dns.CNAME{
|
||||
Hdr: dns.RR_Header{Name: original, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: a.Header().Ttl},
|
||||
Target: a.Header().Name,
|
||||
}
|
||||
break
|
||||
}
|
||||
m.Question[0].Name = original
|
||||
}
|
22
middleware/autopath/nonwriter.go
Normal file
22
middleware/autopath/nonwriter.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package autopath
|
||||
|
||||
import (
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// NonWriter is a type of ResponseWriter that captures the message, but never writes to the client.
|
||||
type NonWriter struct {
|
||||
dns.ResponseWriter
|
||||
Msg *dns.Msg
|
||||
}
|
||||
|
||||
// NewNonWriter makes and returns a new NonWriter.
|
||||
func NewNonWriter(w dns.ResponseWriter) *NonWriter { return &NonWriter{ResponseWriter: w} }
|
||||
|
||||
// WriteMsg records the message, but doesn't write it itself.
|
||||
func (r *NonWriter) WriteMsg(res *dns.Msg) error {
|
||||
r.Msg = res
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *NonWriter) Write(buf []byte) (int, error) { return len(buf), nil }
|
87
middleware/autopath/setup.go
Normal file
87
middleware/autopath/setup.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package autopath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/middleware"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("autopath", caddy.Plugin{
|
||||
ServerType: "dns",
|
||||
Action: setup,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
ap, mw, err := autoPathParse(c)
|
||||
if err != nil {
|
||||
return middleware.Error("autopath", err)
|
||||
}
|
||||
|
||||
c.OnStartup(func() error {
|
||||
// So we know for sure the mw is initialized.
|
||||
m := dnsserver.GetMiddleware(c, mw)
|
||||
switch mw {
|
||||
case "kubernetes":
|
||||
m = m
|
||||
//if k, ok := m.(kubernetes.Kubernetes); ok {
|
||||
//&ap.searchFunc = k.AutoPath
|
||||
//}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
|
||||
ap.Next = next
|
||||
return ap
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var allowedMiddleware = map[string]bool{
|
||||
"@kubernetes": true,
|
||||
}
|
||||
|
||||
func autoPathParse(c *caddy.Controller) (*AutoPath, string, error) {
|
||||
ap := new(AutoPath)
|
||||
mw := ""
|
||||
|
||||
for c.Next() {
|
||||
zoneAndresolv := c.RemainingArgs()
|
||||
if len(zoneAndresolv) < 1 {
|
||||
return nil, "", fmt.Errorf("no resolv-conf specified")
|
||||
}
|
||||
resolv := zoneAndresolv[len(zoneAndresolv)-1]
|
||||
if resolv[0] == '@' {
|
||||
_, ok := allowedMiddleware[resolv]
|
||||
if ok {
|
||||
mw = resolv[1:]
|
||||
}
|
||||
} else {
|
||||
// assume file on disk
|
||||
rc, err := dns.ClientConfigFromFile(resolv)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to parse %q: %v", resolv, err)
|
||||
}
|
||||
ap.search = rc.Search
|
||||
middleware.Zones(ap.search).Normalize()
|
||||
ap.search = append(ap.search, "") // sentinal value as demanded.
|
||||
}
|
||||
ap.Zones = zoneAndresolv[:len(zoneAndresolv)-1]
|
||||
if len(ap.Zones) == 0 {
|
||||
ap.Zones = make([]string, len(c.ServerBlockKeys))
|
||||
copy(ap.Zones, c.ServerBlockKeys)
|
||||
}
|
||||
for i, str := range ap.Zones {
|
||||
ap.Zones[i] = middleware.Host(str).Normalize()
|
||||
}
|
||||
}
|
||||
return ap, mw, nil
|
||||
}
|
74
middleware/autopath/setup_test.go
Normal file
74
middleware/autopath/setup_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package autopath
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/coredns/middleware/test"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func TestSetupAutoPath(t *testing.T) {
|
||||
resolv, rm, err := test.TempFile(os.TempDir(), resolvConf)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create resolv.conf test file: %s", resolvConf, err)
|
||||
}
|
||||
defer rm()
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
shouldErr bool
|
||||
expectedMw string // expected middleware.
|
||||
expectedSearch []string // expected search path
|
||||
expectedErrContent string // substring from the expected error. Empty for positive cases.
|
||||
}{
|
||||
// positive
|
||||
{
|
||||
`autopath @kubernetes`, false, "kubernetes", nil, "",
|
||||
},
|
||||
{
|
||||
`autopath ` + resolv, false, "", []string{"bar.com.", "baz.com.", ""}, "",
|
||||
},
|
||||
// negative
|
||||
{
|
||||
`autopath kubernetes`, true, "", nil, "open kubernetes: no such file or directory",
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
c := caddy.NewTestController("dns", test.input)
|
||||
ap, mw, err := autoPathParse(c)
|
||||
|
||||
if test.shouldErr && err == nil {
|
||||
t.Errorf("Test %d: Expected error but found %s for input %s", i, err, test.input)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if !test.shouldErr {
|
||||
t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err)
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), test.expectedErrContent) {
|
||||
t.Errorf("Test %d: Expected error to contain: %v, found error: %v, input: %s", i, test.expectedErrContent, err, test.input)
|
||||
}
|
||||
}
|
||||
|
||||
if !test.shouldErr && mw != test.expectedMw {
|
||||
t.Errorf("Test %d, Middleware not correctly set for input %s. Expected: %s, actual: %s", i, test.input, test.expectedMw, mw)
|
||||
}
|
||||
if !test.shouldErr && ap.search != nil {
|
||||
if !reflect.DeepEqual(test.expectedSearch, ap.search) {
|
||||
t.Errorf("Test %d, wrong searchpath for input %s. Expected: '%v', actual: '%v'", i, test.input, test.expectedSearch, ap.search)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const resolvConf = `nameserver 1.2.3.4
|
||||
domain foo.com
|
||||
search bar.com baz.com
|
||||
options ndots:5
|
||||
`
|
|
@ -11,7 +11,7 @@ to deploy CoreDNS in Kubernetes](https://github.com/coredns/deployment/tree/mast
|
|||
## Syntax
|
||||
|
||||
```
|
||||
kubernetes ZONE [ZONE...] [{
|
||||
kubernetes ZONE [ZONE...] [
|
||||
resyncperiod DURATION
|
||||
endpoint URL
|
||||
tls CERT KEY CACERT]
|
||||
|
@ -20,9 +20,8 @@ kubernetes ZONE [ZONE...] [{
|
|||
pods POD-MODE]
|
||||
upstream ADDRESS [ADDRESS...]
|
||||
federation NAME DOMAIN
|
||||
autopath [NDOTS [RESPONSE [RESOLV-CONF]]
|
||||
fallthrough
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
* `resyncperiod` **DURATION**
|
||||
|
@ -135,62 +134,6 @@ specified).
|
|||
}
|
||||
```
|
||||
|
||||
* `autopath` **[NDOTS [RESPONSE [RESOLV-CONF]]**
|
||||
|
||||
Enables server side search path lookups for pods. When enabled, the kubernetes middleware will identify search path queries from pods and perform the remaining lookups in the path on the pod's behalf. The search path used mimics the resolv.conf search path deployed to pods by the "cluster-first" dns-policy. E.g.
|
||||
|
||||
```
|
||||
search ns1.svc.cluster.local svc.cluster.local cluster.local foo.com
|
||||
```
|
||||
|
||||
If no domains in the path produce an answer, a lookup on the bare question will be attempted.
|
||||
|
||||
A successful response will contain a question section with the original question, and an answer section containing the record for the question that actually had an answer. This means that the question and answer will not match. To avoid potential client confusion, a dynamically generated CNAME entry is added to join the two. For example:
|
||||
|
||||
```
|
||||
# host -v -t a google.com
|
||||
Trying "google.com.default.svc.cluster.local"
|
||||
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50957
|
||||
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
|
||||
|
||||
;; QUESTION SECTION:
|
||||
;google.com.default.svc.cluster.local. IN A
|
||||
|
||||
;; ANSWER SECTION:
|
||||
google.com.default.svc.cluster.local. 175 IN CNAME google.com.
|
||||
google.com. 175 IN A 216.58.194.206
|
||||
```
|
||||
|
||||
**Fully Qualified Queries:** There is a known limitation of the autopath feature involving fully qualified queries. When the kubernetes middleware receives a query from a client, it cannot tell the difference between a query that was fully qualified by the user, and one that was expanded by the first search path on the client side.
|
||||
|
||||
This means that the kubernetes middleware with autopath enabled will perform a server-side path search for the query `google.com.default.svc.cluster.local.` as if the client had queried just `google.com`. In other words, a query for `google.com.default.svc.cluster.local.` will produce the IP address for `google.com` as seen below.
|
||||
|
||||
```
|
||||
# host -t a google.com
|
||||
google.com has address 216.58.194.206
|
||||
google.com.default.svc.cluster.local is an alias for google.com.
|
||||
|
||||
# host -t a google.com.default.svc.cluster.local.
|
||||
google.com has address 216.58.194.206
|
||||
google.com.default.svc.cluster.local is an alias for google.com.
|
||||
```
|
||||
|
||||
**NDOTS** (default: `0`) This provides an adjustable threshold to prevent server side lookups from triggering. If the number of dots before the first search domain is less than this number, then the search path will not executed on the server side. When autopath is enabled with default settings, the search path is always conducted when the query is in the first search domain `<pod-namespace>.svc.<zone>.`.
|
||||
|
||||
**RESPONSE** (default: `NOERROR`) This option causes the kubernetes middleware to return the given response instead of NXDOMAIN when the all searches in the path produce no results. Valid values: `NXDOMAIN`, `SERVFAIL` or `NOERROR`. Setting this to `SERVFAIL` or `NOERROR` should prevent the client from fruitlessly continuing the client side searches in the path after the server already checked them.
|
||||
|
||||
**RESOLV-CONF** (default: `/etc/resolv.conf`) If specified, the kubernetes middleware uses this file to get the host's search domains. The kubernetes middleware performs a lookup on these domains if the in-cluster search domains in the path fail to produce an answer. If not specified, the values will be read from the local resolv.conf file (i.e the resolv.conf file in the pod containing CoreDNS). In practice, this option should only need to be used if running CoreDNS outside of the cluster and the search path in /etc/resolv.conf does not match the cluster's "default" dns-policiy.
|
||||
|
||||
Enabling autopath requires more memory, since it needs to maintain a watch on all pods. If autopath and `pods verified` mode are both enabled, they will share the same watch. Enabling both options should have an equivalent memory impact of just one.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
kubernetes cluster.local. {
|
||||
autopath 0 NXDOMAIN /etc/resolv.conf
|
||||
}
|
||||
```
|
||||
|
||||
* `fallthrough`
|
||||
|
||||
If a query for a record in the cluster zone results in NXDOMAIN, normally that is what the response will be. However, if you specify this option, the query will instead be passed on down the middleware chain, which can include another middleware to handle the query.
|
||||
|
@ -204,13 +147,12 @@ specified).
|
|||
|
||||
**Example 2:** Handle all queries in the `cluster.local` zone. Connect to Kubernetes in-cluster.
|
||||
Handle all `PTR` requests for `10.0.0.0/16` . Verify the existence of pods when answering pod
|
||||
requests. Resolve upstream records against `10.102.3.10`. Enable the autopath feature.
|
||||
requests. Resolve upstream records against `10.102.3.10`.
|
||||
|
||||
10.0.0.0/16 cluster.local {
|
||||
kubernetes {
|
||||
pods verified
|
||||
upstream 10.102.3.10:53
|
||||
autopath
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -232,14 +174,13 @@ specified).
|
|||
}
|
||||
|
||||
**Out-Of-Cluster Example:** Handle all queries in the `cluster.local` zone. Connect to Kubernetes from outside the cluster.
|
||||
Verify the existence of pods when answering pod requests. Resolve upstream records against `10.102.3.10`. Enable the autopath feature, using the `cluster.conf` file instead of `/etc/resolv.conf`.
|
||||
Verify the existence of pods when answering pod requests. Resolve upstream records against `10.102.3.10`.
|
||||
|
||||
kubernetes cluster.local {
|
||||
endpoint https://k8s-endpoint:8443
|
||||
tls cert key cacert
|
||||
pods verified
|
||||
upstream 10.102.3.10:53
|
||||
autopath 0 NOERROR cluster.conf
|
||||
}
|
||||
|
||||
|
||||
|
|
18
middleware/kubernetes/autopath.go
Normal file
18
middleware/kubernetes/autopath.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package kubernetes
|
||||
|
||||
import "k8s.io/client-go/1.5/pkg/api"
|
||||
|
||||
// TODO(miek): rename and put in autopath.go file. This will be for the
|
||||
// external middleware autopath to use. Mostly to get the namespace:
|
||||
//name, path, ok := autopath.SplitSearch(zone, state.QName(), p.Namespace)
|
||||
func (k *Kubernetes) findPodWithIP(ip string) (p *api.Pod) {
|
||||
objList := k.APIConn.PodIndex(ip)
|
||||
for _, o := range objList {
|
||||
p, ok := o.(*api.Pod)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return p
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -1,75 +0,0 @@
|
|||
package autopath
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// Writer implements a ResponseWriter that also does the following:
|
||||
// * reverts question section of a packet to its original state.
|
||||
// This is done to avoid the 'Question section mismatch:' error in client.
|
||||
// * Defers write to the client if the response code is NXDOMAIN. This is needed
|
||||
// to enable further search path probing if a search was not successful.
|
||||
// * Allow forced write to client regardless of response code. This is needed to
|
||||
// write the packet to the client if the final search in the path fails to
|
||||
// produce results.
|
||||
// * Overwrites response code with Writer.Rcode if the response code
|
||||
// is NXDOMAIN (NameError). This is needed to support the AutoPath.OnNXDOMAIN
|
||||
// function, which returns a NOERROR to client instead of NXDOMAIN if the final
|
||||
// search in the path fails to produce results.
|
||||
type Writer struct {
|
||||
dns.ResponseWriter
|
||||
original dns.Question
|
||||
Rcode int
|
||||
}
|
||||
|
||||
// AutoPath enables server side search path lookups for pods.
|
||||
type AutoPath struct {
|
||||
NDots int
|
||||
ResolvConfFile string
|
||||
HostSearchPath []string
|
||||
OnNXDOMAIN int
|
||||
}
|
||||
|
||||
// NewWriter returns a pointer to a new Writer
|
||||
func NewWriter(w dns.ResponseWriter, r *dns.Msg) *Writer {
|
||||
return &Writer{
|
||||
ResponseWriter: w,
|
||||
original: r.Question[0],
|
||||
Rcode: dns.RcodeSuccess,
|
||||
}
|
||||
}
|
||||
|
||||
// WriteMsg writes to client, unless response will be NXDOMAIN.
|
||||
func (apw *Writer) WriteMsg(res *dns.Msg) error {
|
||||
if res.Rcode == dns.RcodeNameError {
|
||||
res.Rcode = apw.Rcode
|
||||
}
|
||||
|
||||
for _, a := range res.Answer {
|
||||
if apw.original.Name == a.Header().Name {
|
||||
continue
|
||||
}
|
||||
res.Answer = append(res.Answer, nil)
|
||||
copy(res.Answer[1:], res.Answer)
|
||||
res.Answer[0] = CNAME(apw.original.Name, a.Header().Name, a.Header().Ttl)
|
||||
}
|
||||
res.Question[0] = apw.original
|
||||
|
||||
return apw.ResponseWriter.WriteMsg(res)
|
||||
}
|
||||
|
||||
// Write is a wrapper that records the size of the message that gets written.
|
||||
func (apw *Writer) Write(buf []byte) (int, error) {
|
||||
n, err := apw.ResponseWriter.Write(buf)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func SplitSearch(zone, question, namespace string) (name, search string, ok bool) {
|
||||
search = strings.Join([]string{namespace, "svc", zone}, ".")
|
||||
if dns.IsSubDomain(search, question) {
|
||||
return question[:len(question)-len(search)-1], search, true
|
||||
}
|
||||
return "", "", false
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package autopath
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSplitSearchPath(t *testing.T) {
|
||||
type testCase struct {
|
||||
question string
|
||||
namespace string
|
||||
expectedName string
|
||||
expectedSearch string
|
||||
expectedOk bool
|
||||
}
|
||||
tests := []testCase{
|
||||
{question: "test.blah.com", namespace: "ns1", expectedName: "", expectedSearch: "", expectedOk: false},
|
||||
{question: "foo.com.ns2.svc.interwebs.nets", namespace: "ns1", expectedName: "", expectedSearch: "", expectedOk: false},
|
||||
{question: "foo.com.svc.interwebs.nets", namespace: "ns1", expectedName: "", expectedSearch: "", expectedOk: false},
|
||||
{question: "foo.com.ns1.svc.interwebs.nets", namespace: "ns1", expectedName: "foo.com", expectedSearch: "ns1.svc.interwebs.nets", expectedOk: true},
|
||||
}
|
||||
zone := "interwebs.nets"
|
||||
for _, c := range tests {
|
||||
name, search, ok := SplitSearch(zone, c.question, c.namespace)
|
||||
if c.expectedName != name || c.expectedSearch != search || c.expectedOk != ok {
|
||||
t.Errorf("Case %v: Expected name'%v', search:'%v', ok:'%v'. Got name:'%v', search:'%v', ok:'%v'.", c.question, c.expectedName, c.expectedSearch, c.expectedOk, name, search, ok)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package autopath
|
||||
|
||||
import "github.com/miekg/dns"
|
||||
|
||||
// CNAME returns a new CNAME formed from name target and ttl.
|
||||
func CNAME(name string, target string, ttl uint32) *dns.CNAME {
|
||||
return &dns.CNAME{
|
||||
Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: ttl},
|
||||
Target: dns.Fqdn(target)}
|
||||
}
|
|
@ -1,176 +0,0 @@
|
|||
package kubernetes
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var autopathCases = map[string](*test.Case){
|
||||
"A Autopath Service (Second Search)": {
|
||||
Qname: "svc1.testns.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("svc1.testns.podns.svc.cluster.local. 0 IN CNAME svc1.testns.svc.cluster.local."),
|
||||
test.A("svc1.testns.svc.cluster.local. 0 IN A 10.0.0.1"),
|
||||
},
|
||||
},
|
||||
"A Autopath Service (Third Search)": {
|
||||
Qname: "svc1.testns.svc.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("svc1.testns.svc.podns.svc.cluster.local. 0 IN CNAME svc1.testns.svc.cluster.local."),
|
||||
test.A("svc1.testns.svc.cluster.local. 0 IN A 10.0.0.1"),
|
||||
},
|
||||
},
|
||||
"A Autopath Next Middleware (Host Domain Search)": {
|
||||
Qname: "test1.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("test1.podns.svc.cluster.local. 0 IN CNAME test1.hostdom.test."),
|
||||
test.A("test1.hostdom.test. 0 IN A 11.22.33.44"),
|
||||
},
|
||||
},
|
||||
"A Autopath Service (Bare Search)": {
|
||||
Qname: "svc1.testns.svc.cluster.local.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("svc1.testns.svc.cluster.local.podns.svc.cluster.local. 0 IN CNAME svc1.testns.svc.cluster.local."),
|
||||
test.A("svc1.testns.svc.cluster.local. 0 IN A 10.0.0.1"),
|
||||
},
|
||||
},
|
||||
"A Autopath Next Middleware (Bare Search)": {
|
||||
Qname: "test2.interwebs.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("test2.interwebs.podns.svc.cluster.local. 0 IN CNAME test2.interwebs."),
|
||||
test.A("test2.interwebs. 0 IN A 55.66.77.88"),
|
||||
},
|
||||
},
|
||||
"AAAA Autopath Next Middleware (Bare Search)": {
|
||||
Qname: "test2.interwebs.podns.svc.cluster.local.", Qtype: dns.TypeAAAA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("test2.interwebs.podns.svc.cluster.local. 0 IN CNAME test2.interwebs."),
|
||||
test.AAAA("test2.interwebs. 0 IN AAAA 5555:6666:7777::8888"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var autopathBareSearch = map[string](*test.Case){
|
||||
"A Autopath Next Middleware (Bare Search) Non-existing OnNXDOMAIN default": {
|
||||
Qname: "nothere.interwebs.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{},
|
||||
},
|
||||
}
|
||||
|
||||
var autopathBareSearchExpectNameErr = map[string](*test.Case){
|
||||
"A Autopath Next Middleware (Bare Search) Non-existing OnNXDOMAIN disabled": {
|
||||
Qname: "nothere.interwebs.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeNameError,
|
||||
Answer: []dns.RR{},
|
||||
},
|
||||
}
|
||||
|
||||
var autopath2NDotsCases = map[string](*test.Case){
|
||||
"A Service (0 Dots)": {
|
||||
Qname: "foo.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeNameError,
|
||||
Answer: []dns.RR{},
|
||||
Ns: []dns.RR{
|
||||
test.SOA("cluster.local. 300 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 60"),
|
||||
},
|
||||
},
|
||||
"A Service (1 Dots)": {
|
||||
Qname: "foo.foo.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeNameError,
|
||||
Answer: []dns.RR{},
|
||||
Ns: []dns.RR{
|
||||
test.SOA("cluster.local. 300 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 60"),
|
||||
},
|
||||
},
|
||||
"A Service (2 Dots)": {
|
||||
Qname: "foo.foo.foo.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.A("foo.foo.foo.hostdom.test. 0 IN A 11.22.33.44"),
|
||||
test.CNAME("foo.foo.foo.podns.svc.cluster.local. 0 IN CNAME foo.foo.foo.hostdom.test."),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Disabled because broken.
|
||||
func testServeDNSAutoPath(t *testing.T) {
|
||||
|
||||
k := Kubernetes{}
|
||||
k.Zones = []string{"cluster.local."}
|
||||
_, cidr, _ := net.ParseCIDR("10.0.0.0/8")
|
||||
|
||||
k.ReverseCidrs = []net.IPNet{*cidr}
|
||||
k.Federations = []Federation{{name: "fed", zone: "federal.test."}}
|
||||
k.APIConn = &APIConnServeTest{}
|
||||
k.autoPath = new(autopath.AutoPath)
|
||||
k.autoPath.HostSearchPath = []string{"hostdom.test"}
|
||||
k.interfaceAddrsFunc = localPodIP
|
||||
k.Next = nextHandler(nextMap)
|
||||
|
||||
ctx := context.TODO()
|
||||
runServeDNSTests(ctx, t, autopathCases, k)
|
||||
runServeDNSTests(ctx, t, autopathBareSearch, k)
|
||||
|
||||
// Set ndots to 2 for the ndots test cases
|
||||
k.autoPath.NDots = 2
|
||||
runServeDNSTests(ctx, t, autopath2NDotsCases, k)
|
||||
k.autoPath.NDots = defautNdots
|
||||
// Disable the NXDOMAIN override (enabled by default)
|
||||
k.autoPath.OnNXDOMAIN = dns.RcodeNameError
|
||||
runServeDNSTests(ctx, t, autopathCases, k)
|
||||
runServeDNSTests(ctx, t, autopathBareSearchExpectNameErr, k)
|
||||
}
|
||||
|
||||
var nextMap = map[dns.Question]dns.Msg{
|
||||
{Name: "test1.hostdom.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("test1.hostdom.test. 0 IN A 11.22.33.44")},
|
||||
},
|
||||
{Name: "test2.interwebs.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("test2.interwebs. 0 IN A 55.66.77.88")},
|
||||
},
|
||||
{Name: "test2.interwebs.", Qtype: dns.TypeAAAA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.AAAA("test2.interwebs. 0 IN AAAA 5555:6666:7777::8888")},
|
||||
},
|
||||
{Name: "foo.hostdom.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("foo.hostdom.test. 0 IN A 11.22.33.44")},
|
||||
},
|
||||
{Name: "foo.foo.hostdom.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("foo.foo.hostdom.test. 0 IN A 11.22.33.44")},
|
||||
},
|
||||
{Name: "foo.foo.foo.hostdom.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("foo.foo.foo.hostdom.test. 0 IN A 11.22.33.44")},
|
||||
},
|
||||
}
|
||||
|
||||
// nextHandler returns a Handler that returns an answer for the question in the
|
||||
// request per the question->answer map qMap.
|
||||
func nextHandler(mm map[dns.Question]dns.Msg) test.Handler {
|
||||
return test.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
|
||||
msg, ok := mm[r.Question[0]]
|
||||
if !ok {
|
||||
r.Rcode = dns.RcodeNameError
|
||||
w.WriteMsg(m)
|
||||
return r.Rcode, nil
|
||||
}
|
||||
r.Rcode = dns.RcodeSuccess
|
||||
m.Answer = append(m.Answer, msg.Answer...)
|
||||
w.WriteMsg(m)
|
||||
return r.Rcode, nil
|
||||
})
|
||||
}
|
|
@ -2,10 +2,8 @@ package kubernetes
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
|
@ -40,70 +38,9 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
|
|||
zone = state.Name()
|
||||
}
|
||||
|
||||
// TODO(miek): place contents of route-request back here.
|
||||
records, extra, _, err := k.routeRequest(zone, state)
|
||||
|
||||
// Check for Autopath search eligibility
|
||||
if (k.autoPath != nil) && k.IsNameError(err) && (state.QType() == dns.TypeA || state.QType() == dns.TypeAAAA) {
|
||||
p := k.findPodWithIP(state.IP())
|
||||
|
||||
for p != nil {
|
||||
name, path, ok := autopath.SplitSearch(zone, state.QName(), p.Namespace)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if (dns.CountLabel(name) - 1) < k.autoPath.NDots {
|
||||
break
|
||||
}
|
||||
origQName := state.QName()
|
||||
// Search "svc.cluster.local." and "cluster.local."
|
||||
for i := 0; i < 2; i++ {
|
||||
path = strings.Join(dns.SplitDomainName(path)[1:], ".")
|
||||
newstate := state.NewWithQuestion(strings.Join([]string{name, path}, "."), state.QType())
|
||||
records, extra, _, err = k.routeRequest(zone, newstate)
|
||||
if !k.IsNameError(err) && len(records) > 0 {
|
||||
records = append(records, nil)
|
||||
copy(records[1:], records)
|
||||
records[0] = autopath.CNAME(origQName, records[0].Header().Name, records[0].Header().Ttl)
|
||||
break
|
||||
}
|
||||
}
|
||||
if !k.IsNameError(err) {
|
||||
break
|
||||
}
|
||||
// Try host search path (if set) in the next middleware
|
||||
apw := autopath.NewWriter(w, r)
|
||||
for _, hostsearch := range k.autoPath.HostSearchPath {
|
||||
newstate := state.NewWithQuestion(strings.Join([]string{name, hostsearch}, "."), state.QType())
|
||||
rcode, nextErr := middleware.NextOrFailure(k.Name(), k.Next, ctx, apw, newstate.Req)
|
||||
|
||||
if middleware.ClientWrite(rcode) || rcode == dns.RcodeNameError {
|
||||
return rcode, nextErr
|
||||
}
|
||||
}
|
||||
// Search . in this middleware
|
||||
newstate := state.NewWithQuestion(strings.Join([]string{name, "."}, ""), state.QType())
|
||||
records, extra, _, err = k.routeRequest(zone, newstate)
|
||||
if !k.IsNameError(err) && len(records) > 0 {
|
||||
records = append(records, nil)
|
||||
copy(records[1:], records)
|
||||
records[0] = autopath.CNAME(origQName, records[0].Header().Name, records[0].Header().Ttl)
|
||||
break
|
||||
}
|
||||
// Search . in the next middleware
|
||||
apw.Rcode = k.autoPath.OnNXDOMAIN
|
||||
newstate = state.NewWithQuestion(strings.Join([]string{name, "."}, ""), state.QType())
|
||||
r = newstate.Req
|
||||
rcode, nextErr := middleware.NextOrFailure(k.Name(), k.Next, ctx, apw, r)
|
||||
if !(middleware.ClientWrite(rcode) || rcode == dns.RcodeNameError) && nextErr == nil {
|
||||
r = dnsutil.Dedup(r)
|
||||
state.SizeAndDo(r)
|
||||
r, _ = state.Scrub(r)
|
||||
apw.WriteMsg(r)
|
||||
}
|
||||
return rcode, nextErr
|
||||
}
|
||||
}
|
||||
|
||||
if k.IsNameError(err) {
|
||||
if k.Fallthrough {
|
||||
return middleware.NextOrFailure(k.Name(), k.Next, ctx, w, r)
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/etcd/msg"
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/pkg/dnsutil"
|
||||
dnsstrings "github.com/coredns/coredns/middleware/pkg/strings"
|
||||
"github.com/coredns/coredns/middleware/proxy"
|
||||
|
@ -47,7 +46,6 @@ type Kubernetes struct {
|
|||
ReverseCidrs []net.IPNet
|
||||
Fallthrough bool
|
||||
|
||||
autoPath *autopath.AutoPath
|
||||
interfaceAddrsFunc func() net.IP
|
||||
}
|
||||
|
||||
|
@ -252,7 +250,7 @@ func (k *Kubernetes) InitKubeCache() (err error) {
|
|||
}
|
||||
|
||||
opts := dnsControlOpts{
|
||||
initPodCache: (k.PodMode == PodModeVerified || (k.autoPath != nil)),
|
||||
initPodCache: k.PodMode == PodModeVerified,
|
||||
}
|
||||
k.APIConn = newdnsController(kubeClient, k.ResyncPeriod, k.Selector, opts)
|
||||
|
||||
|
@ -460,21 +458,6 @@ func (k *Kubernetes) getRecordsForK8sItems(services []service, pods []pod, r rec
|
|||
return records
|
||||
}
|
||||
|
||||
func (k *Kubernetes) findPodWithIP(ip string) (p *api.Pod) {
|
||||
if k.autoPath == nil {
|
||||
return nil
|
||||
}
|
||||
objList := k.APIConn.PodIndex(ip)
|
||||
for _, o := range objList {
|
||||
p, ok := o.(*api.Pod)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return p
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *Kubernetes) findPods(namespace, podname string) (pods []pod, err error) {
|
||||
if k.PodMode == PodModeDisabled {
|
||||
return pods, errPodsDisabled
|
||||
|
|
|
@ -5,13 +5,11 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/middleware/proxy"
|
||||
"github.com/miekg/dns"
|
||||
|
@ -196,47 +194,6 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) {
|
|||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("incorrect number of arguments for federation, got %v, expected 2", len(args))
|
||||
case "autopath": // name zone
|
||||
args := c.RemainingArgs()
|
||||
k8s.autoPath = &autopath.AutoPath{
|
||||
NDots: defautNdots,
|
||||
HostSearchPath: []string{},
|
||||
ResolvConfFile: defaultResolvConfFile,
|
||||
OnNXDOMAIN: defaultOnNXDOMAIN,
|
||||
}
|
||||
if len(args) > 3 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for autopath, got %v, expected at most 3", len(args))
|
||||
|
||||
}
|
||||
if len(args) > 0 {
|
||||
ndots, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid NDOTS argument for autopath, got '%v', expected an integer", ndots)
|
||||
}
|
||||
k8s.autoPath.NDots = ndots
|
||||
}
|
||||
if len(args) > 1 {
|
||||
switch args[1] {
|
||||
case dns.RcodeToString[dns.RcodeNameError]:
|
||||
k8s.autoPath.OnNXDOMAIN = dns.RcodeNameError
|
||||
case dns.RcodeToString[dns.RcodeSuccess]:
|
||||
k8s.autoPath.OnNXDOMAIN = dns.RcodeSuccess
|
||||
case dns.RcodeToString[dns.RcodeServerFailure]:
|
||||
k8s.autoPath.OnNXDOMAIN = dns.RcodeServerFailure
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid RESPONSE argument for autopath, got '%v', expected SERVFAIL, NOERROR, or NXDOMAIN", args[1])
|
||||
}
|
||||
}
|
||||
if len(args) > 2 {
|
||||
k8s.autoPath.ResolvConfFile = args[2]
|
||||
}
|
||||
rc, err := dns.ClientConfigFromFile(k8s.autoPath.ResolvConfFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error when parsing %v: %v", k8s.autoPath.ResolvConfFile, err)
|
||||
}
|
||||
k8s.autoPath.HostSearchPath = rc.Search
|
||||
middleware.Zones(k8s.autoPath.HostSearchPath).Normalize()
|
||||
continue
|
||||
}
|
||||
}
|
||||
return k8s, nil
|
||||
|
|
|
@ -2,17 +2,11 @@ package kubernetes
|
|||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/test"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/miekg/dns"
|
||||
unversionedapi "k8s.io/client-go/1.5/pkg/api/unversioned"
|
||||
)
|
||||
|
||||
|
@ -22,13 +16,6 @@ func parseCidr(cidr string) net.IPNet {
|
|||
}
|
||||
|
||||
func TestKubernetesParse(t *testing.T) {
|
||||
f, rm, err := test.TempFile(os.TempDir(), testResolveConf)
|
||||
autoPathResolvConfFile := f
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create resolv.conf TempFile: %s", err)
|
||||
}
|
||||
defer rm()
|
||||
|
||||
tests := []struct {
|
||||
description string // Human-facing description of test case
|
||||
input string // Corefile data as string
|
||||
|
@ -43,7 +30,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
expectedFallthrough bool
|
||||
expectedUpstreams []string
|
||||
expectedFederations []Federation
|
||||
expectedAutoPath *autopath.AutoPath
|
||||
}{
|
||||
// positive
|
||||
{
|
||||
|
@ -60,7 +46,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"kubernetes keyword with multiple zones",
|
||||
|
@ -76,7 +61,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"kubernetes keyword with zone and empty braces",
|
||||
|
@ -93,7 +77,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"endpoint keyword with url",
|
||||
|
@ -111,7 +94,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"namespaces keyword with one namespace",
|
||||
|
@ -129,7 +111,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"namespaces keyword with multiple namespaces",
|
||||
|
@ -147,7 +128,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resync period in seconds",
|
||||
|
@ -165,7 +145,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resync period in minutes",
|
||||
|
@ -183,7 +162,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"basic label selector",
|
||||
|
@ -201,7 +179,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"multi-label selector",
|
||||
|
@ -219,7 +196,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"fully specified valid config",
|
||||
|
@ -241,7 +217,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
true,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// negative
|
||||
{
|
||||
|
@ -258,7 +233,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"kubernetes keyword without a zone",
|
||||
|
@ -274,7 +248,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"endpoint keyword without an endpoint value",
|
||||
|
@ -292,7 +265,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"namespace keyword without a namespace value",
|
||||
|
@ -310,7 +282,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resyncperiod keyword without a duration value",
|
||||
|
@ -328,7 +299,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resync period no units",
|
||||
|
@ -346,7 +316,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resync period invalid",
|
||||
|
@ -364,7 +333,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"labels with no selector value",
|
||||
|
@ -382,7 +350,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"labels with invalid selector value",
|
||||
|
@ -400,7 +367,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// pods disabled
|
||||
{
|
||||
|
@ -419,7 +385,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// pods insecure
|
||||
{
|
||||
|
@ -438,7 +403,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// pods verified
|
||||
{
|
||||
|
@ -457,7 +421,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// pods invalid
|
||||
{
|
||||
|
@ -476,7 +439,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// cidrs ok
|
||||
{
|
||||
|
@ -495,7 +457,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// cidrs ok
|
||||
{
|
||||
|
@ -514,7 +475,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// fallthrough invalid
|
||||
{
|
||||
|
@ -533,7 +493,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// Valid upstream
|
||||
{
|
||||
|
@ -552,7 +511,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
[]string{"13.14.15.16:53"},
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// Invalid upstream
|
||||
{
|
||||
|
@ -571,7 +529,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// Valid federations
|
||||
{
|
||||
|
@ -594,7 +551,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
{name: "foo", zone: "bar.crawl.com"},
|
||||
{name: "fed", zone: "era.tion.com"},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
// Invalid federations
|
||||
{
|
||||
|
@ -613,103 +569,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// autopath
|
||||
{
|
||||
"valid autopath",
|
||||
`kubernetes coredns.local {
|
||||
autopath 1 NXDOMAIN ` + autoPathResolvConfFile + `
|
||||
}`,
|
||||
false,
|
||||
"",
|
||||
1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
&autopath.AutoPath{
|
||||
NDots: 1,
|
||||
HostSearchPath: []string{"bar.com.", "baz.com."},
|
||||
ResolvConfFile: autoPathResolvConfFile,
|
||||
OnNXDOMAIN: dns.RcodeNameError,
|
||||
},
|
||||
},
|
||||
{
|
||||
"invalid autopath RESPONSE",
|
||||
`kubernetes coredns.local {
|
||||
autopath 0 CRY
|
||||
}`,
|
||||
true,
|
||||
"invalid RESPONSE argument for autopath",
|
||||
-1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"invalid autopath NDOTS",
|
||||
`kubernetes coredns.local {
|
||||
autopath polka
|
||||
}`,
|
||||
true,
|
||||
"invalid NDOTS argument for autopath",
|
||||
-1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"invalid autopath RESOLV-CONF",
|
||||
`kubernetes coredns.local {
|
||||
autopath 1 NOERROR /wrong/path/to/resolv.conf
|
||||
}`,
|
||||
true,
|
||||
"error when parsing",
|
||||
-1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"invalid autopath invalid option",
|
||||
`kubernetes coredns.local {
|
||||
autopath 1 SERVFAIL ` + autoPathResolvConfFile + ` foo
|
||||
}`,
|
||||
true,
|
||||
"incorrect number of arguments",
|
||||
-1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -810,15 +669,5 @@ func TestKubernetesParse(t *testing.T) {
|
|||
|
||||
}
|
||||
}
|
||||
// autopath
|
||||
if !reflect.DeepEqual(test.expectedAutoPath, k8sController.autoPath) {
|
||||
t.Errorf("Test %d: Expected kubernetes controller to be initialized with autopath '%v'. Instead found autopath '%v' for input '%s'", i, test.expectedAutoPath, k8sController.autoPath, test.input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const testResolveConf = `nameserver 1.2.3.4
|
||||
domain foo.com
|
||||
search bar.com baz.com
|
||||
options ndots:5
|
||||
`
|
||||
|
|
Loading…
Add table
Reference in a new issue