aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorGravatar Erik Wilson <Erik.E.Wilson@gmail.com> 2019-07-30 15:44:16 -0700
committerGravatar corbot[bot] <39114087+corbot[bot]@users.noreply.github.com> 2019-07-30 22:44:16 +0000
commit367d285765fef0f030be1b59e5d6bf5d5a008e45 (patch)
treec2efff2dba7e793fc7d8f8de66be0e5ddb4cfb7c /plugin
parent4fda9535d2f446de9dbaf8b397b0d871bdc882e5 (diff)
downloadcoredns-367d285765fef0f030be1b59e5d6bf5d5a008e45.tar.gz
coredns-367d285765fef0f030be1b59e5d6bf5d5a008e45.tar.zst
coredns-367d285765fef0f030be1b59e5d6bf5d5a008e45.zip
plugin/reload: Graceful reload of imported files (#3068)
Automatically submitted.
Diffstat (limited to 'plugin')
-rw-r--r--plugin/reload/README.md5
-rw-r--r--plugin/reload/reload.go25
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