coredns/plugin/transfer/setup.go
Yong Tang 9798dd067f
Cherry-pick: Implement notifies for transfer plugin (#3972) (#4142)
* Implement notifies for transfer plugin (#3972)

* Fix notifies in transfer plugin

Signed-off-by: Miek Gieben <miek@miek.nl>

* Make it compile

Signed-off-by: Miek Gieben <miek@miek.nl>

* Port more plugins

Signed-off-by: Miek Gieben <miek@miek.nl>

* golint

Signed-off-by: Miek Gieben <miek@miek.nl>

* Fix tests

Signed-off-by: Miek Gieben <miek@miek.nl>

* Fix notifies in transfer plugin

Signed-off-by: Miek Gieben <miek@miek.nl>

* Make it compile

Signed-off-by: Miek Gieben <miek@miek.nl>

* Port more plugins

Signed-off-by: Miek Gieben <miek@miek.nl>

* golint

Signed-off-by: Miek Gieben <miek@miek.nl>

* Fix tests

Signed-off-by: Miek Gieben <miek@miek.nl>

* Fix tests

Signed-off-by: Miek Gieben <miek@miek.nl>

* really fix test

Signed-off-by: Miek Gieben <miek@miek.nl>

* Implement ixfr fallback and unify file and auto for transfering

Signed-off-by: Miek Gieben <miek@miek.nl>

* Add transfer tests

copied and modified from #3452

Signed-off-by: Miek Gieben <miek@miek.nl>

* Test correct selection of plugin

Signed-off-by: Miek Gieben <miek@miek.nl>

* add upstream back in

Signed-off-by: Miek Gieben <miek@miek.nl>

* Implement ixfr fallback and unify file and auto for transfering

Signed-off-by: Miek Gieben <miek@miek.nl>

* fix test

Signed-off-by: Miek Gieben <miek@miek.nl>

* properly merge

Signed-off-by: Miek Gieben <miek@miek.nl>

* Remove plugin/kubernetes/setup_transfer_test.go

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Co-authored-by: Miek Gieben <miek@miek.nl>
2020-09-24 20:30:39 +02:00

100 lines
2.1 KiB
Go

package transfer
import (
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/parse"
"github.com/coredns/coredns/plugin/pkg/transport"
)
func init() {
caddy.RegisterPlugin("transfer", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
t, err := parseTransfer(c)
if err != nil {
return plugin.Error("transfer", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
t.Next = next
return t
})
c.OnStartup(func() error {
// find all plugins that implement Transferer and add them to Transferers
plugins := dnsserver.GetConfig(c).Handlers()
for _, pl := range plugins {
tr, ok := pl.(Transferer)
if !ok {
continue
}
t.Transferers = append(t.Transferers, tr)
}
return nil
})
return nil
}
func parseTransfer(c *caddy.Controller) (*Transfer, error) {
t := &Transfer{}
for c.Next() {
x := &xfr{}
zones := c.RemainingArgs()
if len(zones) != 0 {
x.Zones = zones
for i := 0; i < len(x.Zones); i++ {
nzone, err := plugin.Host(x.Zones[i]).MustNormalize()
if err != nil {
return nil, err
}
x.Zones[i] = nzone
}
} else {
x.Zones = make([]string, len(c.ServerBlockKeys))
for i := 0; i < len(c.ServerBlockKeys); i++ {
nzone, err := plugin.Host(c.ServerBlockKeys[i]).MustNormalize()
if err != nil {
return nil, err
}
x.Zones[i] = nzone
}
}
for c.NextBlock() {
switch c.Val() {
case "to":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
}
for _, host := range args {
if host == "*" {
x.to = append(x.to, host)
continue
}
normalized, err := parse.HostPort(host, transport.Port)
if err != nil {
return nil, err
}
x.to = append(x.to, normalized)
}
default:
return nil, plugin.Error("transfer", c.Errf("unknown property %q", c.Val()))
}
}
if len(x.to) == 0 {
return nil, plugin.Error("transfer", c.Err("'to' is required"))
}
t.xfrs = append(t.xfrs, x)
}
return t, nil
}