mirror of
https://github.com/coredns/coredns.git
synced 2026-03-11 00:03:10 -04:00
* feat(secondary): Send NOTIFY messages after zone transfer - Modified TransferIn() method to accept a transfer.Transfer parameter - Added NOTIFY message sending after successful zone transfer in secondary plugin - Updated Update() method to pass the transfer handler through the zone update cycle - Added comprehensive tests for the secondary notify functionality Closes #5669 Signed-off-by: liucongran <liucongran327@gmail.com> * fix(secondary): Fix TransferIn method call in test Update test to pass nil parameter to TransferIn method after signature change Signed-off-by: liucongran <liucongran327@gmail.com> * refactor(secondary): Clean up imports and add helper methods - Reorder imports for consistency - Add hasSOA() and getSOA() helper methods to Zone - Remove unnecessary blank lines in tests Signed-off-by: liucongran <liucongran327@gmail.com> * fix(test): Fix variable declaration in secondary test Change corefile variable assignment to use short declaration syntax (:=) to fix compilation error. Signed-off-by: liucongran <liucongran327@gmail.com> * refactor(secondary): Use getSOA helper method in shouldTransfer Replace direct SOA access with getSOA() helper method for consistency. Signed-off-by: liucongran <liucongran327@gmail.com> --------- Signed-off-by: liucongran <liucongran327@gmail.com> Co-authored-by: liucongran <liucongran@cestc.cn>
158 lines
3.3 KiB
Go
158 lines
3.3 KiB
Go
package file
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/coredns/caddy"
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
"github.com/coredns/coredns/plugin"
|
|
"github.com/coredns/coredns/plugin/pkg/fall"
|
|
"github.com/coredns/coredns/plugin/pkg/upstream"
|
|
"github.com/coredns/coredns/plugin/transfer"
|
|
)
|
|
|
|
func init() { plugin.Register("file", setup) }
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
zones, fall, err := fileParse(c)
|
|
if err != nil {
|
|
return plugin.Error("file", err)
|
|
}
|
|
|
|
f := File{Zones: zones, Fall: fall}
|
|
// get the transfer plugin, so we can send notifies and send notifies on startup as well.
|
|
c.OnStartup(func() error {
|
|
t := dnsserver.GetConfig(c).Handler("transfer")
|
|
if t == nil {
|
|
return nil
|
|
}
|
|
f.Xfer = t.(*transfer.Transfer) // if found this must be OK.
|
|
go func() {
|
|
for _, n := range zones.Names {
|
|
f.Xfer.Notify(n)
|
|
}
|
|
}()
|
|
return nil
|
|
})
|
|
|
|
c.OnRestartFailed(func() error {
|
|
t := dnsserver.GetConfig(c).Handler("transfer")
|
|
if t == nil {
|
|
return nil
|
|
}
|
|
go func() {
|
|
for _, n := range zones.Names {
|
|
f.Xfer.Notify(n)
|
|
}
|
|
}()
|
|
return nil
|
|
})
|
|
|
|
for _, n := range zones.Names {
|
|
z := zones.Z[n]
|
|
c.OnShutdown(z.OnShutdown)
|
|
c.OnStartup(func() error {
|
|
z.StartupOnce.Do(func() { z.Reload(f.Xfer) })
|
|
return nil
|
|
})
|
|
}
|
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
|
f.Next = next
|
|
return f
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func fileParse(c *caddy.Controller) (Zones, fall.F, error) {
|
|
z := make(map[string]*Zone)
|
|
names := []string{}
|
|
fall := fall.F{}
|
|
|
|
config := dnsserver.GetConfig(c)
|
|
|
|
var openErr error
|
|
reload := 1 * time.Minute
|
|
|
|
for c.Next() {
|
|
// file db.file [zones...]
|
|
if !c.NextArg() {
|
|
return Zones{}, fall, c.ArgErr()
|
|
}
|
|
fileName := c.Val()
|
|
|
|
origins := plugin.OriginsFromArgsOrServerBlock(c.RemainingArgs(), c.ServerBlockKeys)
|
|
if !filepath.IsAbs(fileName) && config.Root != "" {
|
|
fileName = filepath.Join(config.Root, fileName)
|
|
}
|
|
|
|
reader, err := os.Open(filepath.Clean(fileName))
|
|
if err != nil {
|
|
openErr = err
|
|
}
|
|
|
|
err = func() error {
|
|
defer reader.Close()
|
|
|
|
for i := range origins {
|
|
z[origins[i]] = NewZone(origins[i], fileName)
|
|
if openErr == nil {
|
|
reader.Seek(0, 0)
|
|
zone, err := Parse(reader, origins[i], fileName, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
z[origins[i]] = zone
|
|
}
|
|
names = append(names, origins[i])
|
|
}
|
|
return nil
|
|
}()
|
|
|
|
if err != nil {
|
|
return Zones{}, fall, err
|
|
}
|
|
|
|
for c.NextBlock() {
|
|
switch c.Val() {
|
|
case "fallthrough":
|
|
fall.SetZonesFromArgs(c.RemainingArgs())
|
|
case "reload":
|
|
t := c.RemainingArgs()
|
|
if len(t) < 1 {
|
|
return Zones{}, fall, errors.New("reload duration value is expected")
|
|
}
|
|
d, err := time.ParseDuration(t[0])
|
|
if err != nil {
|
|
return Zones{}, fall, plugin.Error("file", err)
|
|
}
|
|
reload = d
|
|
case "upstream":
|
|
// remove soon
|
|
c.RemainingArgs()
|
|
|
|
default:
|
|
return Zones{}, fall, c.Errf("unknown property '%s'", c.Val())
|
|
}
|
|
}
|
|
|
|
for i := range origins {
|
|
z[origins[i]].ReloadInterval = reload
|
|
z[origins[i]].Upstream = upstream.New()
|
|
}
|
|
}
|
|
|
|
if openErr != nil {
|
|
if reload == 0 {
|
|
// reload hasn't been set make this a fatal error
|
|
return Zones{}, fall, plugin.Error("file", openErr)
|
|
}
|
|
log.Warningf("Failed to open %q: trying again in %s", openErr, reload)
|
|
}
|
|
return Zones{Z: z, Names: names}, fall, nil
|
|
}
|