Sfoglia il codice sorgente

etcdctl/cluster_health: provide better message for empty client urls

It skips sync when init client, and prints out unreachable messagen and
points to notice when checking health of etcd members one by one.
Yicheng Qin 10 anni fa
parent
commit
423e3bbbd8
2 ha cambiato i file con 54 aggiunte e 31 eliminazioni
  1. 8 1
      etcdctl/command/cluster_health.go
  2. 46 30
      etcdctl/command/util.go

+ 8 - 1
etcdctl/command/cluster_health.go

@@ -10,6 +10,7 @@ import (
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
 	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/client"
 )
 
 func NewClusterHealthCommand() cli.Command {
@@ -44,7 +45,8 @@ func handleClusterHealth(c *cli.Context) {
 		Transport: tr,
 	}
 
-	mi := mustNewMembersAPI(c)
+	cln := mustNewClientNoSync(c)
+	mi := client.NewMembersAPI(cln)
 	ms, err := mi.List(context.TODO())
 	if err != nil {
 		fmt.Println("cluster may be unhealthy: failed to list members")
@@ -54,6 +56,11 @@ func handleClusterHealth(c *cli.Context) {
 	for {
 		health := false
 		for _, m := range ms {
+			if len(m.ClientURLs) == 0 {
+				fmt.Printf("member %s is unreachable: no available published client urls\n", m.ID)
+				continue
+			}
+
 			checked := false
 			for _, url := range m.ClientURLs {
 				resp, err := hc.Get(url + "/health")

+ 46 - 30
etcdctl/command/util.go

@@ -181,18 +181,60 @@ func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
 }
 
 func mustNewClient(c *cli.Context) client.Client {
-	eps, err := getEndpoints(c)
+	hc, err := newClient(c)
 	if err != nil {
 		fmt.Fprintln(os.Stderr, err.Error())
 		os.Exit(1)
 	}
 
-	tr, err := getTransport(c)
+	if !c.GlobalBool("no-sync") {
+		ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
+		err := hc.Sync(ctx)
+		cancel()
+		if err != nil {
+			if err == client.ErrNoEndpoints {
+				fmt.Fprintf(os.Stderr, "etcd cluster has no published client endpoints.\n")
+				fmt.Fprintf(os.Stderr, "Try '--no-sync' if you want to access non-published client endpoints(%s).\n", strings.Join(hc.Endpoints(), ","))
+			}
+			handleError(ExitServerError, err)
+			os.Exit(1)
+		}
+	}
+
+	if c.GlobalBool("debug") {
+		fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
+		client.EnablecURLDebug()
+	}
+
+	return hc
+}
+
+func mustNewClientNoSync(c *cli.Context) client.Client {
+	hc, err := newClient(c)
 	if err != nil {
 		fmt.Fprintln(os.Stderr, err.Error())
 		os.Exit(1)
 	}
 
+	if c.GlobalBool("debug") {
+		fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
+		client.EnablecURLDebug()
+	}
+
+	return hc
+}
+
+func newClient(c *cli.Context) (client.Client, error) {
+	eps, err := getEndpoints(c)
+	if err != nil {
+		return nil, err
+	}
+
+	tr, err := getTransport(c)
+	if err != nil {
+		return nil, err
+	}
+
 	cfg := client.Config{
 		Transport:               tr,
 		Endpoints:               eps,
@@ -203,37 +245,11 @@ func mustNewClient(c *cli.Context) client.Client {
 	if uFlag != "" {
 		username, password, err := getUsernamePasswordFromFlag(uFlag)
 		if err != nil {
-			fmt.Fprintln(os.Stderr, err.Error())
-			os.Exit(1)
+			return nil, err
 		}
 		cfg.Username = username
 		cfg.Password = password
 	}
 
-	hc, err := client.New(cfg)
-	if err != nil {
-		fmt.Fprintln(os.Stderr, err.Error())
-		os.Exit(1)
-	}
-
-	if !c.GlobalBool("no-sync") {
-		ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
-		err := hc.Sync(ctx)
-		cancel()
-		if err != nil {
-			if err == client.ErrNoEndpoints {
-				fmt.Fprintf(os.Stderr, "etcd cluster has no published client endpoints.\n")
-				fmt.Fprintf(os.Stderr, "Try '--no-sync' if you want to access non-published client endpoints(%s).\n", strings.Join(eps, ","))
-			}
-			handleError(ExitServerError, err)
-			os.Exit(1)
-		}
-	}
-
-	if c.GlobalBool("debug") {
-		fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
-		client.EnablecURLDebug()
-	}
-
-	return hc
+	return client.New(cfg)
 }