diff options
author | 2017-03-16 14:10:54 -0700 | |
---|---|---|
committer | 2017-03-16 21:10:54 +0000 | |
commit | dfc71df07d7a19cc0227083ce6b04eea60ecd5a0 (patch) | |
tree | d1d22f3c0317f310935abcf56a72c8461af210d3 /middleware | |
parent | 36c743a4d81cac1f8de8454c707c1ac7089bf783 (diff) | |
download | coredns-dfc71df07d7a19cc0227083ce6b04eea60ecd5a0.tar.gz coredns-dfc71df07d7a19cc0227083ce6b04eea60ecd5a0.tar.zst coredns-dfc71df07d7a19cc0227083ce6b04eea60ecd5a0.zip |
middleware/proxy: Allow non-HTTP upstreams to be health checked (#589)
Allow HTTP health check to be performed against a regular DNS upstream server.
TODO: Add tests.
Diffstat (limited to 'middleware')
-rw-r--r-- | middleware/proxy/upstream.go | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/middleware/proxy/upstream.go b/middleware/proxy/upstream.go index 59e1a534f..cabcd9ba8 100644 --- a/middleware/proxy/upstream.go +++ b/middleware/proxy/upstream.go @@ -4,8 +4,10 @@ import ( "fmt" "io" "io/ioutil" + "log" "net" "net/http" + "net/url" "strconv" "strings" "sync/atomic" @@ -14,7 +16,6 @@ import ( "github.com/coredns/coredns/middleware" "github.com/coredns/coredns/middleware/pkg/dnsutil" "github.com/coredns/coredns/middleware/pkg/tls" - "github.com/mholt/caddy/caddyfile" "github.com/miekg/dns" ) @@ -229,16 +230,38 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error { func (u *staticUpstream) healthCheck() { for _, host := range u.Hosts { - port := "" + var hostName, checkPort string + + // The DNS server might be an HTTP server. If so, extract its name. + if url, err := url.Parse(host.Name); err == nil { + hostName = url.Host + } else { + hostName = host.Name + } + + // Extract the port number from the parsed server name. + checkHostName, checkPort, err := net.SplitHostPort(hostName) + if err != nil { + checkHostName = hostName + } + if u.HealthCheck.Port != "" { - port = ":" + u.HealthCheck.Port + checkPort = u.HealthCheck.Port } - hostURL := host.Name + port + u.HealthCheck.Path + + hostURL := "http://" + net.JoinHostPort(checkHostName, checkPort) + u.HealthCheck.Path + host.Unhealthy = false + if r, err := http.Get(hostURL); err == nil { io.Copy(ioutil.Discard, r.Body) r.Body.Close() - host.Unhealthy = r.StatusCode < 200 || r.StatusCode >= 400 + if r.StatusCode < 200 || r.StatusCode >= 400 { + log.Printf("[WARNING] Health check URL %s returned HTTP code %d\n", + hostURL, r.StatusCode) + host.Unhealthy = true + } } else { + log.Printf("[WARNING] Health check probe failed: %v\n", err) host.Unhealthy = true } } |