aboutsummaryrefslogtreecommitdiff
path: root/middleware/health
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2017-09-14 09:36:06 +0100
committerGravatar GitHub <noreply@github.com> 2017-09-14 09:36:06 +0100
commitd8714e64e400ef873c2adc4d929a07d7890727b9 (patch)
treec9fa4c157e6af12eb1517654f8d23ca5d5619513 /middleware/health
parentb984aa45595dc95253b91191afe7d3ee29e71b48 (diff)
downloadcoredns-d8714e64e400ef873c2adc4d929a07d7890727b9.tar.gz
coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.tar.zst
coredns-d8714e64e400ef873c2adc4d929a07d7890727b9.zip
Remove the word middleware (#1067)
* Rename middleware to plugin first pass; mostly used 'sed', few spots where I manually changed text. This still builds a coredns binary. * fmt error * Rename AddMiddleware to AddPlugin * Readd AddMiddleware to remain backwards compat
Diffstat (limited to 'middleware/health')
-rw-r--r--middleware/health/README.md23
-rw-r--r--middleware/health/health.go69
-rw-r--r--middleware/health/health_test.go47
-rw-r--r--middleware/health/healther.go42
-rw-r--r--middleware/health/setup.go73
-rw-r--r--middleware/health/setup_test.go35
6 files changed, 0 insertions, 289 deletions
diff --git a/middleware/health/README.md b/middleware/health/README.md
deleted file mode 100644
index 195a480c0..000000000
--- a/middleware/health/README.md
+++ /dev/null
@@ -1,23 +0,0 @@
-# health
-
-This module enables a simple health check endpoint. By default it will listen on port 8080.
-
-## Syntax
-
-~~~
-health [ADDRESS]
-~~~
-
-Optionally takes an address; the default is `:8080`. The health path is fixed to `/health`. The
-health endpoint returns a 200 response code and the word "OK" when CoreDNS is healthy. It returns
-a 503. *health* periodically (1s) polls middleware that exports health information. If any of the
-middleware signals that it is unhealthy, the server will go unhealthy too. Each middleware that
-supports health checks has a section "Health" in their README.
-
-## Examples
-
-Run another health endpoint on http://localhost:8091.
-
-~~~
-health localhost:8091
-~~~
diff --git a/middleware/health/health.go b/middleware/health/health.go
deleted file mode 100644
index 5e5c4c32f..000000000
--- a/middleware/health/health.go
+++ /dev/null
@@ -1,69 +0,0 @@
-// Package health implements an HTTP handler that responds to health checks.
-package health
-
-import (
- "io"
- "log"
- "net"
- "net/http"
- "sync"
-)
-
-var once sync.Once
-
-type health struct {
- Addr string
-
- ln net.Listener
- mux *http.ServeMux
-
- // A slice of Healthers that the health middleware will poll every second for their health status.
- h []Healther
- sync.RWMutex
- ok bool // ok is the global boolean indicating an all healthy middleware stack
-}
-
-func (h *health) Startup() error {
- if h.Addr == "" {
- h.Addr = defAddr
- }
-
- once.Do(func() {
- ln, err := net.Listen("tcp", h.Addr)
- if err != nil {
- log.Printf("[ERROR] Failed to start health handler: %s", err)
- return
- }
-
- h.ln = ln
-
- h.mux = http.NewServeMux()
-
- h.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
- if h.Ok() {
- w.WriteHeader(http.StatusOK)
- io.WriteString(w, ok)
- return
- }
- w.WriteHeader(http.StatusServiceUnavailable)
- })
-
- go func() {
- http.Serve(h.ln, h.mux)
- }()
- })
- return nil
-}
-
-func (h *health) Shutdown() error {
- if h.ln != nil {
- return h.ln.Close()
- }
- return nil
-}
-
-const (
- ok = "OK"
- defAddr = ":8080"
- path = "/health"
-)
diff --git a/middleware/health/health_test.go b/middleware/health/health_test.go
deleted file mode 100644
index ea22ad8b6..000000000
--- a/middleware/health/health_test.go
+++ /dev/null
@@ -1,47 +0,0 @@
-package health
-
-// TODO(miek): enable again if middleware gets health check.
-/*
-func TestHealth(t *testing.T) {
- h := health{Addr: ":0"}
- h.h = append(h.h, &erratic.Erratic{})
-
- if err := h.Startup(); err != nil {
- t.Fatalf("Unable to startup the health server: %v", err)
- }
- defer h.Shutdown()
-
- // Reconstruct the http address based on the port allocated by operating system.
- address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), path)
-
- // Norhing set should be unhealthy
- response, err := http.Get(address)
- if err != nil {
- t.Fatalf("Unable to query %s: %v", address, err)
- }
- if response.StatusCode != 503 {
- t.Errorf("Invalid status code: expecting '503', got '%d'", response.StatusCode)
- }
- response.Body.Close()
-
- // Make healthy
- h.Poll()
-
- response, err = http.Get(address)
- if err != nil {
- t.Fatalf("Unable to query %s: %v", address, err)
- }
- if response.StatusCode != 200 {
- t.Errorf("Invalid status code: expecting '200', got '%d'", response.StatusCode)
- }
- content, err := ioutil.ReadAll(response.Body)
- if err != nil {
- t.Fatalf("Unable to get response body from %s: %v", address, err)
- }
- response.Body.Close()
-
- if string(content) != ok {
- t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
- }
-}
-*/
diff --git a/middleware/health/healther.go b/middleware/health/healther.go
deleted file mode 100644
index 1b794e03b..000000000
--- a/middleware/health/healther.go
+++ /dev/null
@@ -1,42 +0,0 @@
-package health
-
-// Healther interface needs to be implemented by each middleware willing to
-// provide healthhceck information to the health middleware. As a second step
-// the middleware needs to registered against the health middleware, by addding
-// it to healthers map. Note this method should return quickly, i.e. just
-// checking a boolean status, as it is called every second from the health
-// middleware.
-type Healther interface {
- // Health returns a boolean indicating the health status of a middleware.
- // False indicates unhealthy.
- Health() bool
-}
-
-// Ok returns the global health status of all middleware configured in this server.
-func (h *health) Ok() bool {
- h.RLock()
- defer h.RUnlock()
- return h.ok
-}
-
-// SetOk sets the global health status of all middleware configured in this server.
-func (h *health) SetOk(ok bool) {
- h.Lock()
- defer h.Unlock()
- h.ok = ok
-}
-
-// poll polls all healthers and sets the global state.
-func (h *health) poll() {
- for _, m := range h.h {
- if !m.Health() {
- h.SetOk(false)
- return
- }
- }
- h.SetOk(true)
-}
-
-// Middleware that implements the Healther interface.
-// TODO(miek): none yet.
-var healthers = map[string]bool{}
diff --git a/middleware/health/setup.go b/middleware/health/setup.go
deleted file mode 100644
index bf465bd74..000000000
--- a/middleware/health/setup.go
+++ /dev/null
@@ -1,73 +0,0 @@
-package health
-
-import (
- "net"
- "time"
-
- "github.com/coredns/coredns/core/dnsserver"
- "github.com/coredns/coredns/middleware"
-
- "github.com/mholt/caddy"
-)
-
-func init() {
- caddy.RegisterPlugin("health", caddy.Plugin{
- ServerType: "dns",
- Action: setup,
- })
-}
-
-func setup(c *caddy.Controller) error {
- addr, err := healthParse(c)
- if err != nil {
- return middleware.Error("health", err)
- }
-
- h := &health{Addr: addr}
-
- c.OnStartup(func() error {
- for he := range healthers {
- m := dnsserver.GetConfig(c).Handler(he)
- if x, ok := m.(Healther); ok {
- h.h = append(h.h, x)
- }
- }
- return nil
- })
-
- c.OnStartup(func() error {
- h.poll()
- go func() {
- for {
- <-time.After(1 * time.Second)
- h.poll()
- }
- }()
- return nil
- })
-
- c.OnStartup(h.Startup)
- c.OnFinalShutdown(h.Shutdown)
-
- // Don't do AddMiddleware, as health is not *really* a middleware just a separate webserver running.
- return nil
-}
-
-func healthParse(c *caddy.Controller) (string, error) {
- addr := ""
- for c.Next() {
- args := c.RemainingArgs()
-
- switch len(args) {
- case 0:
- case 1:
- addr = args[0]
- if _, _, e := net.SplitHostPort(addr); e != nil {
- return "", e
- }
- default:
- return "", c.ArgErr()
- }
- }
- return addr, nil
-}
diff --git a/middleware/health/setup_test.go b/middleware/health/setup_test.go
deleted file mode 100644
index 87f4fc5fd..000000000
--- a/middleware/health/setup_test.go
+++ /dev/null
@@ -1,35 +0,0 @@
-package health
-
-import (
- "testing"
-
- "github.com/mholt/caddy"
-)
-
-func TestSetupHealth(t *testing.T) {
- tests := []struct {
- input string
- shouldErr bool
- }{
- {`health`, false},
- {`health localhost:1234`, false},
- {`health bla:a`, false},
- {`health bla`, true},
- {`health bla bla`, true},
- }
-
- for i, test := range tests {
- c := caddy.NewTestController("dns", test.input)
- _, err := healthParse(c)
-
- if test.shouldErr && err == nil {
- t.Errorf("Test %d: Expected error but found %s for input %s", i, err, test.input)
- }
-
- if err != nil {
- if !test.shouldErr {
- t.Errorf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err)
- }
- }
- }
-}