diff options
-rw-r--r-- | plugin/reload/README.md | 5 | ||||
-rw-r--r-- | plugin/reload/reload.go | 25 |
2 files changed, 26 insertions, 4 deletions
diff --git a/plugin/reload/README.md b/plugin/reload/README.md index c1f5d206b..89a49f14a 100644 --- a/plugin/reload/README.md +++ b/plugin/reload/README.md @@ -89,8 +89,9 @@ closed in step 1; so the health endpoint is broken. The same can hopen in the pr In general be careful with assigning new port and expecting reload to work fully. -Also any `import` statement is not discovered by this plugin. This means if any of these imported files -changes the *reload* plugin is ignorant of that fact. +In CoreDNS v1.6.0 and earlier any `import` statements are not discovered by this plugin. +This means if any of these imported files changes the *reload* plugin is ignorant of that fact. +CoreDNS v1.7.0 and later does parse the Corefile and supports detecting changes in imported files. ## Metrics diff --git a/plugin/reload/reload.go b/plugin/reload/reload.go index f6e676d88..59e040479 100644 --- a/plugin/reload/reload.go +++ b/plugin/reload/reload.go @@ -1,11 +1,14 @@ package reload import ( + "bytes" "crypto/md5" + "encoding/json" "sync" "time" "github.com/caddyserver/caddy" + "github.com/caddyserver/caddy/caddyfile" ) // reload periodically checks if the Corefile has changed, and reloads if so @@ -46,6 +49,14 @@ func (r *reload) interval() time.Duration { return r.dur } +func parse(corefile caddy.Input) ([]byte, error) { + serverBlocks, err := caddyfile.Parse(corefile.Path(), bytes.NewReader(corefile.Body()), nil) + if err != nil { + return nil, err + } + return json.Marshal(serverBlocks) +} + func hook(event caddy.EventName, info interface{}) error { if event != caddy.InstanceStartupEvent { return nil @@ -60,7 +71,12 @@ func hook(event caddy.EventName, info interface{}) error { // this should be an instance. ok to panic if not instance := info.(*caddy.Instance) - md5sum := md5.Sum(instance.Caddyfile().Body()) + parsedCorefile, err := parse(instance.Caddyfile()) + if err != nil { + return err + } + + md5sum := md5.Sum(parsedCorefile) log.Infof("Running configuration MD5 = %x\n", md5sum) go func() { @@ -73,7 +89,12 @@ func hook(event caddy.EventName, info interface{}) error { if err != nil { continue } - s := md5.Sum(corefile.Body()) + parsedCorefile, err := parse(corefile) + if err != nil { + log.Warningf("Corefile parse failed: %s", err) + continue + } + s := md5.Sum(parsedCorefile) if s != md5sum { // Let not try to restart with the same file, even though it is wrong. md5sum = s |