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

@@ -15,7 +15,7 @@ response. This is only done for "address" records (PTR, A and AAAA).
reverse NETWORK... {
hostname TEMPLATE
[ttl TTL]
[fallthrough]
[fallthrough [ZONES...]]
[wildcard]
~~~
@@ -23,6 +23,9 @@ reverse NETWORK... {
* `hostname` injects the IP and zone to a template for the hostname. Defaults to "ip-{IP}.{zone[1]}". See below for template.
* `ttl` defaults to 60
* `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.
* `wildcard` allows matches to catch all subdomains as well.
### Template Syntax

View File

@@ -5,6 +5,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"
@@ -13,9 +14,10 @@ import (
// Reverse provides dynamic reverse DNS and the related forward RR.
type Reverse struct {
Next plugin.Handler
Networks networks
Fallthrough bool
Next plugin.Handler
Networks networks
Fall *fall.F
}
// ServeDNS implements the plugin.Handler interface.
@@ -97,7 +99,7 @@ func (re Reverse) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
return dns.RcodeSuccess, nil
}
if re.Fallthrough {
if re.Fall.Through(state.Name()) {
return plugin.NextOrFailure(re.Name(), re.Next, ctx, w, r)
}
return dns.RcodeServerFailure, nil

View File

@@ -24,7 +24,6 @@ func TestReverse(t *testing.T) {
Template: "ip-{ip}.example.org.",
RegexMatchIP: regexIP4,
}},
Fallthrough: false,
}
tests := []struct {

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"
)
@@ -21,19 +22,19 @@ func init() {
}
func setupReverse(c *caddy.Controller) error {
networks, fallThrough, err := reverseParse(c)
networks, fall, err := reverseParse(c)
if err != nil {
return plugin.Error("reverse", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Reverse{Next: next, Networks: networks, Fallthrough: fallThrough}
return Reverse{Next: next, Networks: networks, Fall: fall}
})
return nil
}
func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
func reverseParse(c *caddy.Controller) (nets networks, f *fall.F, err error) {
zones := make([]string, len(c.ServerBlockKeys))
wildcard := false
@@ -52,12 +53,12 @@ func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
}
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, false, c.Errf("network needs to be CIDR formatted: %q\n", cidr)
return nil, f, c.Errf("network needs to be CIDR formatted: %q\n", cidr)
}
cidrs = append(cidrs, ipnet)
}
if len(cidrs) == 0 {
return nil, false, c.ArgErr()
return nil, f, c.ArgErr()
}
// set defaults
@@ -69,27 +70,28 @@ func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
switch c.Val() {
case "hostname":
if !c.NextArg() {
return nil, false, c.ArgErr()
return nil, f, c.ArgErr()
}
template = c.Val()
case "ttl":
if !c.NextArg() {
return nil, false, c.ArgErr()
return nil, f, c.ArgErr()
}
ttl, err = strconv.Atoi(c.Val())
if err != nil {
return nil, false, err
return nil, f, err
}
case "wildcard":
wildcard = true
case "fallthrough":
fall = true
f = fall.New()
f.SetZones(c.RemainingArgs())
default:
return nil, false, c.ArgErr()
return nil, f, c.ArgErr()
}
}
@@ -107,7 +109,7 @@ func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
// extract zone from template
templateZone := strings.SplitAfterN(template, ".", 2)
if len(templateZone) != 2 || templateZone[1] == "" {
return nil, false, c.Errf("cannot find domain in template '%v'", template)
return nil, f, c.Errf("cannot find domain in template '%v'", template)
}
// Create for each configured network in this stanza
@@ -128,7 +130,7 @@ func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
regexIP,
1) + "$")
if err != nil {
return nil, false, err
return nil, f, err
}
nets = append(nets, network{
@@ -143,5 +145,5 @@ func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
// sort by cidr
sort.Sort(nets)
return nets, fall, nil
return nets, f, nil
}