mirror of
https://github.com/coredns/coredns.git
synced 2026-05-25 19:30:23 -04:00
feat(secondary): add fallthrough support (#8041)
This commit is contained in:
@@ -27,6 +27,7 @@ A working syntax would be:
|
||||
~~~
|
||||
secondary [zones...] {
|
||||
transfer from ADDRESS [ADDRESS...]
|
||||
fallthrough [ZONES...]
|
||||
}
|
||||
~~~
|
||||
|
||||
@@ -34,6 +35,11 @@ secondary [zones...] {
|
||||
times; if one does not work, another will be tried. Transferring this zone outwards again can be
|
||||
done by enabling the *transfer* plugin.
|
||||
|
||||
* `fallthrough` If a query for a record in the zone results in NXDOMAIN, the query will be passed
|
||||
to the next plugin in the chain. If **[ZONES...]** are listed, then only queries for those zones
|
||||
will be subject to fallthrough. This can be useful in split DNS setups where the secondary zone
|
||||
contains only partial records.
|
||||
|
||||
When a zone is due to be refreshed (refresh timer fires) a random jitter of 5 seconds is applied,
|
||||
before fetching. In the case of retry this will be 2 seconds. If there are any errors during the
|
||||
transfer in, the transfer fails; this will be logged.
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/file"
|
||||
"github.com/coredns/coredns/plugin/pkg/fall"
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
"github.com/coredns/coredns/plugin/pkg/parse"
|
||||
"github.com/coredns/coredns/plugin/pkg/upstream"
|
||||
@@ -18,12 +19,12 @@ var log = clog.NewWithPlugin("secondary")
|
||||
func init() { plugin.Register("secondary", setup) }
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
zones, err := secondaryParse(c)
|
||||
zones, fall, err := secondaryParse(c)
|
||||
if err != nil {
|
||||
return plugin.Error("secondary", err)
|
||||
}
|
||||
|
||||
s := &Secondary{file.File{Zones: zones}}
|
||||
s := &Secondary{file.File{Zones: zones, Fall: fall}}
|
||||
var x *transfer.Transfer
|
||||
c.OnStartup(func() error {
|
||||
t := dnsserver.GetConfig(c).Handler("transfer")
|
||||
@@ -84,9 +85,10 @@ func setup(c *caddy.Controller) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func secondaryParse(c *caddy.Controller) (file.Zones, error) {
|
||||
func secondaryParse(c *caddy.Controller) (file.Zones, fall.F, error) {
|
||||
z := make(map[string]*file.Zone)
|
||||
names := []string{}
|
||||
fall := fall.F{}
|
||||
for c.Next() {
|
||||
if c.Val() == "secondary" {
|
||||
// secondary [origin]
|
||||
@@ -105,11 +107,13 @@ func secondaryParse(c *caddy.Controller) (file.Zones, error) {
|
||||
var err error
|
||||
f, err = parse.TransferIn(c)
|
||||
if err != nil {
|
||||
return file.Zones{}, err
|
||||
return file.Zones{}, fall, err
|
||||
}
|
||||
hasTransfer = true
|
||||
case "fallthrough":
|
||||
fall.SetZonesFromArgs(c.RemainingArgs())
|
||||
default:
|
||||
return file.Zones{}, c.Errf("unknown property '%s'", c.Val())
|
||||
return file.Zones{}, fall, c.Errf("unknown property '%s'", c.Val())
|
||||
}
|
||||
|
||||
for _, origin := range origins {
|
||||
@@ -120,9 +124,9 @@ func secondaryParse(c *caddy.Controller) (file.Zones, error) {
|
||||
}
|
||||
}
|
||||
if !hasTransfer {
|
||||
return file.Zones{}, c.Err("secondary zones require a transfer from property")
|
||||
return file.Zones{}, fall, c.Err("secondary zones require a transfer from property")
|
||||
}
|
||||
}
|
||||
}
|
||||
return file.Zones{Z: z, Names: names}, nil
|
||||
return file.Zones{Z: z, Names: names}, fall, nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/caddy"
|
||||
"github.com/coredns/coredns/plugin/pkg/fall"
|
||||
)
|
||||
|
||||
func TestSecondaryParse(t *testing.T) {
|
||||
@@ -12,6 +13,7 @@ func TestSecondaryParse(t *testing.T) {
|
||||
shouldErr bool
|
||||
transferFrom string
|
||||
zones []string
|
||||
fall fall.F
|
||||
}{
|
||||
{
|
||||
`secondary {
|
||||
@@ -20,6 +22,7 @@ func TestSecondaryParse(t *testing.T) {
|
||||
false,
|
||||
"127.0.0.1:53",
|
||||
nil,
|
||||
fall.F{},
|
||||
},
|
||||
{
|
||||
`secondary example.org {
|
||||
@@ -28,12 +31,14 @@ func TestSecondaryParse(t *testing.T) {
|
||||
false,
|
||||
"127.0.0.1:53",
|
||||
[]string{"example.org."},
|
||||
fall.F{},
|
||||
},
|
||||
{
|
||||
`secondary`,
|
||||
true,
|
||||
"",
|
||||
nil,
|
||||
fall.F{},
|
||||
},
|
||||
{
|
||||
`secondary example.org {
|
||||
@@ -42,12 +47,35 @@ func TestSecondaryParse(t *testing.T) {
|
||||
true,
|
||||
"",
|
||||
nil,
|
||||
fall.F{},
|
||||
},
|
||||
// fallthrough: bare (all zones)
|
||||
{
|
||||
`secondary {
|
||||
transfer from 127.0.0.1
|
||||
fallthrough
|
||||
}`,
|
||||
false,
|
||||
"127.0.0.1:53",
|
||||
nil,
|
||||
fall.Root,
|
||||
},
|
||||
// fallthrough: specific zone
|
||||
{
|
||||
`secondary example.org {
|
||||
transfer from 127.0.0.1
|
||||
fallthrough example.org
|
||||
}`,
|
||||
false,
|
||||
"127.0.0.1:53",
|
||||
[]string{"example.org."},
|
||||
fall.F{Zones: []string{"example.org."}},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
c := caddy.NewTestController("dns", test.inputFileRules)
|
||||
s, err := secondaryParse(c)
|
||||
s, f, err := secondaryParse(c)
|
||||
|
||||
if err == nil && test.shouldErr {
|
||||
t.Fatalf("Test %d expected errors, but got no error", i)
|
||||
@@ -67,5 +95,9 @@ func TestSecondaryParse(t *testing.T) {
|
||||
t.Fatalf("Test %d transform from names don't match expected %q, but got %q", i, test.transferFrom, x)
|
||||
}
|
||||
}
|
||||
|
||||
if !f.Equal(test.fall) {
|
||||
t.Fatalf("Test %d fallthrough not equal: expected %v, got %v", i, test.fall, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user