aboutsummaryrefslogtreecommitdiff
path: root/middleware/autopath
diff options
context:
space:
mode:
Diffstat (limited to 'middleware/autopath')
-rw-r--r--middleware/autopath/README.md1
-rw-r--r--middleware/autopath/autopath.go36
-rw-r--r--middleware/autopath/autopath_test.go2
-rw-r--r--middleware/autopath/setup.go9
-rw-r--r--middleware/autopath/setup_test.go1
5 files changed, 27 insertions, 22 deletions
diff --git a/middleware/autopath/README.md b/middleware/autopath/README.md
index 37ed789c6..4f0507891 100644
--- a/middleware/autopath/README.md
+++ b/middleware/autopath/README.md
@@ -22,6 +22,7 @@ autopath [ZONE..] RESOLV-CONF
Currently the following set of middleware has implemented *autopath*:
* *kubernetes*
+* *erratic*
## Examples
diff --git a/middleware/autopath/autopath.go b/middleware/autopath/autopath.go
index 2c581eadd..d3a8c84d2 100644
--- a/middleware/autopath/autopath.go
+++ b/middleware/autopath/autopath.go
@@ -33,7 +33,7 @@ func (m Middleware ) AutoPath(state request.Request) []string {
*/
import (
- "errors"
+ "log"
"github.com/coredns/coredns/middleware"
"github.com/coredns/coredns/middleware/pkg/dnsutil"
@@ -61,35 +61,33 @@ type AutoPath struct {
// ServeDNS implements the middleware.Handle interface.
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"))
- }
zone := middleware.Zones(a.Zones).Matches(state.Name())
if zone == "" {
return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
}
- // Check if autopath should be done, searchFunc takes precedence over the local configured
- // search path.
+ // 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 = a.searchFunc(state)
- if len(searchpath) == 0 {
- return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
- }
}
- match := a.FirstInSearchPath(state.Name())
- if !match {
+ if len(searchpath) == 0 {
+ log.Printf("[WARNING] No search path available for autopath")
+ return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
+ }
+
+ if !firstInSearchPath(state.Name(), searchpath) {
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]) // TODO(miek): we loose the original case of the query here.
+ base, err := dnsutil.TrimZone(state.QName(), searchpath[0]) // TODO(miek): we loose the original case of the query here.
if err != nil {
return dns.RcodeServerFailure, err
}
@@ -140,16 +138,16 @@ func (a *AutoPath) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
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] {
+// Name implements the Handler interface.
+func (a *AutoPath) Name() string { return "autopath" }
+
+// firstInSearchPath checks if name is equal to are a sibling of the first element in the search path.
+func firstInSearchPath(name string, searchpath []string) bool {
+ if name == searchpath[0] {
return true
}
- if dns.IsSubDomain(a.search[0], name) {
+ if dns.IsSubDomain(searchpath[0], name) {
return true
}
return false
}
-
-// Name implements the Handler interface.
-func (a *AutoPath) Name() string { return "autopath" }
diff --git a/middleware/autopath/autopath_test.go b/middleware/autopath/autopath_test.go
index ca1290e26..b99744ab8 100644
--- a/middleware/autopath/autopath_test.go
+++ b/middleware/autopath/autopath_test.go
@@ -158,7 +158,7 @@ func TestInSearchPath(t *testing.T) {
{"a.b.svc.cluster.local.", false},
}
for i, tc := range tests {
- got := a.FirstInSearchPath(tc.qname)
+ got := firstInSearchPath(tc.qname, a.search)
if got != tc.b {
t.Errorf("Test %d, got %v, expected %v", i, got, tc.b)
}
diff --git a/middleware/autopath/setup.go b/middleware/autopath/setup.go
index 1855dd440..f9b12d98e 100644
--- a/middleware/autopath/setup.go
+++ b/middleware/autopath/setup.go
@@ -5,6 +5,7 @@ import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/middleware"
+ "github.com/coredns/coredns/middleware/erratic"
"github.com/coredns/coredns/middleware/kubernetes"
"github.com/mholt/caddy"
@@ -32,8 +33,11 @@ func setup(c *caddy.Controller) error {
if m == nil {
return nil
}
- if k, ok := m.(kubernetes.Kubernetes); ok {
- ap.searchFunc = k.AutoPath
+ if x, ok := m.(kubernetes.Kubernetes); ok {
+ ap.searchFunc = x.AutoPath
+ }
+ if x, ok := m.(*erratic.Erratic); ok {
+ ap.searchFunc = x.AutoPath
}
return nil
})
@@ -50,6 +54,7 @@ func setup(c *caddy.Controller) error {
// need to register themselves with dnsserver.RegisterHandler.
var allowedMiddleware = map[string]bool{
"@kubernetes": true,
+ "@erratic": true,
}
func autoPathParse(c *caddy.Controller) (*AutoPath, string, error) {
diff --git a/middleware/autopath/setup_test.go b/middleware/autopath/setup_test.go
index b31110ddb..0f086c5bb 100644
--- a/middleware/autopath/setup_test.go
+++ b/middleware/autopath/setup_test.go
@@ -33,6 +33,7 @@ func TestSetupAutoPath(t *testing.T) {
{`autopath ` + resolv, false, "", "", []string{"bar.com.", "baz.com.", ""}, ""},
// negative
{`autopath kubernetes`, true, "", "", nil, "open kubernetes: no such file or directory"},
+ {`autopath`, true, "", "", nil, "no resolv-conf"},
}
for i, test := range tests {