leader election: Display replica status in docker info.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2015-06-01 13:30:43 -07:00
parent 3e2fb13d82
commit d63de2da48
6 changed files with 63 additions and 15 deletions

View File

@@ -27,7 +27,7 @@ func getInfo(c *context, w http.ResponseWriter, r *http.Request) {
info := dockerclient.Info{
Containers: int64(len(c.cluster.Containers())),
Images: int64(len(c.cluster.Images())),
DriverStatus: c.cluster.Info(),
DriverStatus: c.statusHandler.Status(),
NEventsListener: int64(c.eventsHandler.Size()),
Debug: c.debug,
MemoryLimit: true,

View File

@@ -2,18 +2,24 @@ package api
import (
"crypto/tls"
"fmt"
"net/http"
"strings"
)
var localRoutes = []string{"/info", "/_ping"}
// ReverseProxy is a Docker reverse proxy.
type ReverseProxy struct {
api http.Handler
tlsConfig *tls.Config
dest string
}
// NewReverseProxy creates a new reverse proxy.
func NewReverseProxy(tlsConfig *tls.Config) *ReverseProxy {
func NewReverseProxy(api http.Handler, tlsConfig *tls.Config) *ReverseProxy {
return &ReverseProxy{
api: api,
tlsConfig: tlsConfig,
}
}
@@ -26,12 +32,21 @@ func (p *ReverseProxy) SetDestination(dest string) {
// ServeHTTP is the http.Handler.
func (p *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Check whether we should handle this request locally.
for _, route := range localRoutes {
if strings.HasSuffix(r.URL.Path, route) {
p.api.ServeHTTP(w, r)
return
}
}
// Otherwise, forward.
if p.dest == "" {
httpError(w, "No cluster leader", http.StatusInternalServerError)
return
}
if err := hijack(p.tlsConfig, p.dest, w, r); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
httpError(w, fmt.Sprintf("Unable to reach cluster leader: %v", err), http.StatusInternalServerError)
}
}

View File

@@ -13,6 +13,7 @@ import (
type context struct {
cluster cluster.Cluster
eventsHandler *eventsHandler
statusHandler StatusHandler
debug bool
tlsConfig *tls.Config
}
@@ -83,7 +84,7 @@ func writeCorsHeaders(w http.ResponseWriter, r *http.Request) {
}
// NewRouter creates a new API router.
func NewRouter(cluster cluster.Cluster, tlsConfig *tls.Config, enableCors bool) *mux.Router {
func NewRouter(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHandler, enableCors bool) *mux.Router {
// Register the API events handler in the cluster.
eventsHandler := newEventsHandler()
cluster.RegisterEventHandler(eventsHandler)
@@ -91,6 +92,7 @@ func NewRouter(cluster cluster.Cluster, tlsConfig *tls.Config, enableCors bool)
context := &context{
cluster: cluster,
eventsHandler: eventsHandler,
statusHandler: status,
tlsConfig: tlsConfig,
}

7
api/status.go Normal file
View File

@@ -0,0 +1,7 @@
package api
// StatusHandler allows the API to display extra information on docker info.
type StatusHandler interface {
// Info provides key/values to be added to docker info.
Status() [][]string
}