Browse Source

Merge pull request #3415 from yichengq/better-err

etcdctl/command: print more details about ErrNoEndpoint
Yicheng Qin 10 years ago
parent
commit
3a8db488ca
2 changed files with 54 additions and 27 deletions
  1. 8 1
      etcdctl/command/cluster_health.go
  2. 46 26
      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/github.com/codegangsta/cli"
 	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
 	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/client"
 )
 )
 
 
 func NewClusterHealthCommand() cli.Command {
 func NewClusterHealthCommand() cli.Command {
@@ -44,7 +45,8 @@ func handleClusterHealth(c *cli.Context) {
 		Transport: tr,
 		Transport: tr,
 	}
 	}
 
 
-	mi := mustNewMembersAPI(c)
+	cln := mustNewClientNoSync(c)
+	mi := client.NewMembersAPI(cln)
 	ms, err := mi.List(context.TODO())
 	ms, err := mi.List(context.TODO())
 	if err != nil {
 	if err != nil {
 		fmt.Println("cluster may be unhealthy: failed to list members")
 		fmt.Println("cluster may be unhealthy: failed to list members")
@@ -54,6 +56,11 @@ func handleClusterHealth(c *cli.Context) {
 	for {
 	for {
 		health := false
 		health := false
 		for _, m := range ms {
 		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
 			checked := false
 			for _, url := range m.ClientURLs {
 			for _, url := range m.ClientURLs {
 				resp, err := hc.Get(url + "/health")
 				resp, err := hc.Get(url + "/health")

+ 46 - 26
etcdctl/command/util.go

@@ -181,18 +181,60 @@ func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
 }
 }
 
 
 func mustNewClient(c *cli.Context) client.Client {
 func mustNewClient(c *cli.Context) client.Client {
-	eps, err := getEndpoints(c)
+	hc, err := newClient(c)
 	if err != nil {
 	if err != nil {
 		fmt.Fprintln(os.Stderr, err.Error())
 		fmt.Fprintln(os.Stderr, err.Error())
 		os.Exit(1)
 		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 {
 	if err != nil {
 		fmt.Fprintln(os.Stderr, err.Error())
 		fmt.Fprintln(os.Stderr, err.Error())
 		os.Exit(1)
 		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{
 	cfg := client.Config{
 		Transport:               tr,
 		Transport:               tr,
 		Endpoints:               eps,
 		Endpoints:               eps,
@@ -203,33 +245,11 @@ func mustNewClient(c *cli.Context) client.Client {
 	if uFlag != "" {
 	if uFlag != "" {
 		username, password, err := getUsernamePasswordFromFlag(uFlag)
 		username, password, err := getUsernamePasswordFromFlag(uFlag)
 		if err != nil {
 		if err != nil {
-			fmt.Fprintln(os.Stderr, err.Error())
-			os.Exit(1)
+			return nil, err
 		}
 		}
 		cfg.Username = username
 		cfg.Username = username
 		cfg.Password = password
 		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 {
-			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)
 }
 }