* reload: use OnRestart Close the listener on OnRestart for health and metrics so the default setup function can setup the listener when the plugin is "starting up". Lightly test with some SIGUSR1-ing. Also checked the reload plugin with this, seems fine: .com.:1043 .:1043 2018/04/20 15:01:25 [INFO] CoreDNS-1.1.1 2018/04/20 15:01:25 [INFO] linux/amd64, go1.10, CoreDNS-1.1.1 linux/amd64, go1.10, 2018/04/20 15:01:25 [INFO] Running configuration MD5 = aa8b3f03946fb60546ca1f725d482714 2018/04/20 15:02:01 [INFO] Reloading 2018/04/20 15:02:01 [INFO] Running configuration MD5 = b34a96d99e01db4015a892212560155f 2018/04/20 15:02:01 [INFO] Reloading complete ^C2018/04/20 15:02:06 [INFO] SIGINT: Shutting down With this corefile: .com { proxy . 127.0.0.1:53 prometheus :9054 whoami reload } . { proxy . 127.0.0.1:53 prometheus :9054 whoami reload } The prometheus port was 9053, changed that to 54 so reload would pick it up. From a cursory look it seems this also fixes: Fixes #1604 #1618 #1686 #1492 * At least make it test * Use onfinalshutdown * reload: add reload test This test #1604 adn right now fails. * Address review comments * Add bug section explaining things a bit * compile tests * Fix tests * fixes * slightly less crazy * try to make prometheus setup less confusing * Use ephermal port for test * Don't use the listener * These are shared between goroutines, just use the boolean in the main structure. * Fix text in the reload README, * Set addr to TODO once stopping it * Morph fturb's comment into test, to test reload and scrape health and metric endpoint
105 lines
1.9 KiB
Go
105 lines
1.9 KiB
Go
package health
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
"github.com/coredns/coredns/plugin"
|
|
"github.com/coredns/coredns/plugin/metrics"
|
|
|
|
"github.com/mholt/caddy"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterPlugin("health", caddy.Plugin{
|
|
ServerType: "dns",
|
|
Action: setup,
|
|
})
|
|
}
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
addr, lame, err := healthParse(c)
|
|
if err != nil {
|
|
return plugin.Error("health", err)
|
|
}
|
|
|
|
h := newHealth(addr)
|
|
h.lameduck = lame
|
|
|
|
c.OnStartup(func() error {
|
|
plugins := dnsserver.GetConfig(c).Handlers()
|
|
for _, p := range plugins {
|
|
if x, ok := p.(Healther); ok {
|
|
h.h = append(h.h, x)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
c.OnStartup(func() error {
|
|
// Poll all middleware every second.
|
|
h.poll()
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-time.After(1 * time.Second):
|
|
h.poll()
|
|
case <-h.pollstop:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
return nil
|
|
})
|
|
|
|
c.OnStartup(func() error {
|
|
once.Do(func() { metrics.MustRegister(c, HealthDuration) })
|
|
return nil
|
|
})
|
|
|
|
c.OnStartup(h.OnStartup)
|
|
c.OnRestart(h.OnRestart)
|
|
c.OnFinalShutdown(h.OnFinalShutdown)
|
|
|
|
// Don't do AddPlugin, as health is not *really* a plugin just a separate webserver running.
|
|
return nil
|
|
}
|
|
|
|
func healthParse(c *caddy.Controller) (string, time.Duration, error) {
|
|
addr := ""
|
|
dur := time.Duration(0)
|
|
for c.Next() {
|
|
args := c.RemainingArgs()
|
|
|
|
switch len(args) {
|
|
case 0:
|
|
case 1:
|
|
addr = args[0]
|
|
if _, _, e := net.SplitHostPort(addr); e != nil {
|
|
return "", 0, e
|
|
}
|
|
default:
|
|
return "", 0, c.ArgErr()
|
|
}
|
|
|
|
for c.NextBlock() {
|
|
switch c.Val() {
|
|
case "lameduck":
|
|
args := c.RemainingArgs()
|
|
if len(args) != 1 {
|
|
return "", 0, c.ArgErr()
|
|
}
|
|
l, err := time.ParseDuration(args[0])
|
|
if err != nil {
|
|
return "", 0, fmt.Errorf("unable to parse lameduck duration value: '%v' : %v", args[0], err)
|
|
}
|
|
dur = l
|
|
default:
|
|
return "", 0, c.ArgErr()
|
|
}
|
|
}
|
|
}
|
|
return addr, dur, nil
|
|
}
|