plugin/reload: Graceful reload of imported files (#3068)

Automatically submitted.
This commit is contained in:
Erik Wilson
2019-07-30 15:44:16 -07:00
committed by corbot[bot]
parent 4fda9535d2
commit 367d285765
2 changed files with 26 additions and 4 deletions

View File

@@ -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

View File

@@ -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