mirror of
https://github.com/docker/docs.git
synced 2026-04-04 18:28:58 +07:00
leader election: Display replica status in docker info.
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
@@ -119,7 +119,7 @@ var (
|
||||
}
|
||||
|
||||
flLeaderElection = cli.BoolFlag{
|
||||
Name: "leader-election",
|
||||
Usage: "Enable cluster leader election between Swarm managers",
|
||||
Name: "replication",
|
||||
Usage: "Enable Swarm manager replication",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
@@ -41,6 +40,30 @@ func (h *logHandler) Handle(e *cluster.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type statusHandler struct {
|
||||
cluster cluster.Cluster
|
||||
candidate *leadership.Candidate
|
||||
follower *leadership.Follower
|
||||
}
|
||||
|
||||
func (h *statusHandler) Status() [][]string {
|
||||
var status [][]string
|
||||
|
||||
if h.candidate != nil && !h.candidate.IsLeader() {
|
||||
status = [][]string{
|
||||
{"\bRole", "replica"},
|
||||
{"\bPrimary", h.follower.Leader()},
|
||||
}
|
||||
} else {
|
||||
status = [][]string{
|
||||
{"\bRole", "primary"},
|
||||
}
|
||||
}
|
||||
|
||||
status = append(status, h.cluster.Info()...)
|
||||
return status
|
||||
}
|
||||
|
||||
// Load the TLS certificates/keys and, if verify is true, the CA.
|
||||
func loadTLSConfig(ca, cert, key string, verify bool) (*tls.Config, error) {
|
||||
c, err := tls.LoadX509KeyPair(cert, key)
|
||||
@@ -91,7 +114,7 @@ func createDiscovery(uri string, c *cli.Context) discovery.Discovery {
|
||||
return discovery
|
||||
}
|
||||
|
||||
func setupLeaderElection(server *api.Server, apiHandler http.Handler, discovery discovery.Discovery, addr string, tlsConfig *tls.Config) {
|
||||
func setupReplication(c *cli.Context, cluster cluster.Cluster, server *api.Server, discovery discovery.Discovery, addr string, tlsConfig *tls.Config) {
|
||||
kvDiscovery, ok := discovery.(*kvdiscovery.Discovery)
|
||||
if !ok {
|
||||
log.Fatal("Leader election is only supported with consul, etcd and zookeeper discovery.")
|
||||
@@ -101,7 +124,8 @@ func setupLeaderElection(server *api.Server, apiHandler http.Handler, discovery
|
||||
candidate := leadership.NewCandidate(client, leaderElectionPath, addr)
|
||||
follower := leadership.NewFollower(client, leaderElectionPath)
|
||||
|
||||
proxy := api.NewReverseProxy(tlsConfig)
|
||||
router := api.NewRouter(cluster, tlsConfig, &statusHandler{cluster, candidate, follower}, c.Bool("cors"))
|
||||
proxy := api.NewReverseProxy(router, tlsConfig)
|
||||
|
||||
go func() {
|
||||
candidate.RunForElection()
|
||||
@@ -109,7 +133,7 @@ func setupLeaderElection(server *api.Server, apiHandler http.Handler, discovery
|
||||
for isElected := range electedCh {
|
||||
if isElected {
|
||||
log.Info("Cluster leadership acquired")
|
||||
server.SetHandler(apiHandler)
|
||||
server.SetHandler(router)
|
||||
} else {
|
||||
log.Info("Cluster leadership lost")
|
||||
server.SetHandler(proxy)
|
||||
@@ -129,6 +153,8 @@ func setupLeaderElection(server *api.Server, apiHandler http.Handler, discovery
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
server.SetHandler(router)
|
||||
}
|
||||
|
||||
func manage(c *cli.Context) {
|
||||
@@ -208,9 +234,7 @@ func manage(c *cli.Context) {
|
||||
}
|
||||
|
||||
server := api.NewServer(hosts, tlsConfig)
|
||||
router := api.NewRouter(cl, tlsConfig, c.Bool("cors"))
|
||||
|
||||
if c.Bool("leader-election") {
|
||||
if c.Bool("replication") {
|
||||
addr := c.String("advertise")
|
||||
if addr == "" {
|
||||
log.Fatal("--advertise address must be provided when using --leader-election")
|
||||
@@ -219,9 +243,9 @@ func manage(c *cli.Context) {
|
||||
log.Fatal("--advertise should be of the form ip:port or hostname:port")
|
||||
}
|
||||
|
||||
setupLeaderElection(server, router, discovery, addr, tlsConfig)
|
||||
setupReplication(c, cl, server, discovery, addr, tlsConfig)
|
||||
} else {
|
||||
server.SetHandler(router)
|
||||
server.SetHandler(api.NewRouter(cl, tlsConfig, &statusHandler{cl, nil, nil}, c.Bool("cors")))
|
||||
}
|
||||
|
||||
log.Fatal(server.ListenAndServe())
|
||||
|
||||
Reference in New Issue
Block a user