Add pkg/fall for Fallthrough (#1355)

* Add pkg/fall for Fallthrough

Move this into it's own package to facilitate tests. Important bug
was fixed: make the names fully qualified.

Add fall package to hosts, reverse, etcd, and fix kubernetes and any
tests. The k8s tests are still as-is, might need a future cleanup.
This commit is contained in:
Miek Gieben
2018-01-07 16:32:59 +00:00
committed by GitHub
parent 84ebbbc722
commit c6febe6250
22 changed files with 217 additions and 110 deletions

View File

@@ -16,12 +16,12 @@ available hosts files that block access to advertising servers.
~~~
hosts [FILE [ZONES...]] {
[INLINE]
fallthrough
fallthrough [ZONES...]
}
~~~
* **FILE** the hosts file to read and parse. If the path is relative the path from the *root*
directive will be prepended to it. Defaults to /etc/hosts if omitted. We scan the file for changes
directive will be prepended to it. Defaults to /etc/hosts if omitted. We scan the file for changes
every 5 seconds.
* **ZONES** zones it should be authoritative for. If empty, the zones from the configuration block
are used.
@@ -29,6 +29,9 @@ hosts [FILE [ZONES...]] {
then all of them will be treated as the additional content for hosts file. The specified hosts
file path will still be read but entries will be overrided.
* `fallthrough` If zone matches and no record can be generated, pass request to the next plugin.
If **[ZONES...]** is omitted, then fallthrough happens for all zones for which the plugin
is authoritative. If specific zones are listed (for example `in-addr.arpa` and `ip6.arpa`), then only
queries for those zones will be subject to fallthrough.
## Examples

View File

@@ -7,6 +7,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
"github.com/coredns/coredns/plugin/pkg/fall"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -16,7 +17,7 @@ type Hosts struct {
Next plugin.Handler
*Hostsfile
Fallthrough bool
Fall *fall.F
}
// ServeDNS implements the plugin.Handle interface.
@@ -52,7 +53,7 @@ func (h Hosts) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
}
if len(answers) == 0 {
if h.Fallthrough {
if h.Fall.Through(qname) {
return plugin.NextOrFailure(h.Name(), h.Next, ctx, w, r)
}
if !h.otherRecordsExist(state.QType(), qname) {

View File

@@ -9,6 +9,7 @@ import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/fall"
"github.com/mholt/caddy"
)
@@ -105,14 +106,10 @@ func hostsParse(c *caddy.Controller) (Hosts, error) {
for c.NextBlock() {
switch c.Val() {
case "fallthrough":
args := c.RemainingArgs()
if len(args) == 0 {
h.Fallthrough = true
continue
}
return h, c.ArgErr()
h.Fall = fall.New()
h.Fall.SetZones(c.RemainingArgs())
default:
if !h.Fallthrough {
if h.Fall.IsNil() {
line := strings.Join(append([]string{c.Val()}, c.RemainingArgs()...), " ")
inline = append(inline, line)
continue

View File

@@ -3,6 +3,8 @@ package hosts
import (
"testing"
"github.com/coredns/coredns/plugin/pkg/fall"
"github.com/mholt/caddy"
)
@@ -12,48 +14,48 @@ func TestHostsParse(t *testing.T) {
shouldErr bool
expectedPath string
expectedOrigins []string
expectedFallthrough bool
expectedFallthrough *fall.F
}{
{
`hosts
`,
false, "/etc/hosts", nil, false,
false, "/etc/hosts", nil, nil,
},
{
`hosts /tmp`,
false, "/tmp", nil, false,
false, "/tmp", nil, nil,
},
{
`hosts /etc/hosts miek.nl.`,
false, "/etc/hosts", []string{"miek.nl."}, false,
false, "/etc/hosts", []string{"miek.nl."}, nil,
},
{
`hosts /etc/hosts miek.nl. pun.gent.`,
false, "/etc/hosts", []string{"miek.nl.", "pun.gent."}, false,
false, "/etc/hosts", []string{"miek.nl.", "pun.gent."}, nil,
},
{
`hosts {
fallthrough
}`,
false, "/etc/hosts", nil, true,
false, "/etc/hosts", nil, fall.Zero(),
},
{
`hosts /tmp {
fallthrough
}`,
false, "/tmp", nil, true,
false, "/tmp", nil, fall.Zero(),
},
{
`hosts /etc/hosts miek.nl. {
fallthrough
}`,
false, "/etc/hosts", []string{"miek.nl."}, true,
false, "/etc/hosts", []string{"miek.nl."}, fall.Zero(),
},
{
`hosts /etc/hosts miek.nl 10.0.0.9/8 {
fallthrough
}`,
false, "/etc/hosts", []string{"miek.nl.", "10.in-addr.arpa."}, true,
false, "/etc/hosts", []string{"miek.nl.", "10.in-addr.arpa."}, fall.Zero(),
},
}
@@ -70,8 +72,8 @@ func TestHostsParse(t *testing.T) {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedPath, h.path)
}
} else {
if h.Fallthrough != test.expectedFallthrough {
t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fallthrough)
if !h.Fall.Equal(test.expectedFallthrough) {
t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fall)
}
if len(h.Origins) != len(test.expectedOrigins) {
t.Fatalf("Test %d expected %v, got %v", i, test.expectedOrigins, h.Origins)
@@ -90,7 +92,7 @@ func TestHostsInlineParse(t *testing.T) {
inputFileRules string
shouldErr bool
expectedbyAddr map[string][]string
expectedFallthrough bool
expectedFallthrough *fall.F
}{
{
`hosts highly_unlikely_to_exist_hosts_file example.org {
@@ -103,28 +105,28 @@ func TestHostsInlineParse(t *testing.T) {
`example.org.`,
},
},
true,
fall.Zero(),
},
{
`hosts highly_unlikely_to_exist_hosts_file example.org {
10.0.0.1 example.org
}`,
10.0.0.1 example.org
}`,
false,
map[string][]string{
`10.0.0.1`: {
`example.org.`,
},
},
false,
nil,
},
{
`hosts highly_unlikely_to_exist_hosts_file example.org {
fallthrough
10.0.0.1 example.org
}`,
fallthrough
10.0.0.1 example.org
}`,
true,
map[string][]string{},
true,
fall.Zero(),
},
}
@@ -137,8 +139,8 @@ func TestHostsInlineParse(t *testing.T) {
} else if err != nil && !test.shouldErr {
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
} else if !test.shouldErr {
if h.Fallthrough != test.expectedFallthrough {
t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fallthrough)
if !h.Fall.Equal(test.expectedFallthrough) {
t.Fatalf("Test %d expected fallthrough of %v, got %v", i, test.expectedFallthrough, h.Fall)
}
for k, expectedVal := range test.expectedbyAddr {
if val, ok := h.hmap.byAddr[k]; !ok {