Skip to content

Commit 8942970

Browse files
authored
Merge pull request #10782 from jingyih/cherrypick_9540_to_release3p3
ctlv3: cherry pick of #9540 to release 3.3
2 parents b0babe5 + f835a85 commit 8942970

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

etcdctl/ctlv3/command/ep_command.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,17 @@ func newEpHashKVCommand() *cobra.Command {
7676
return hc
7777
}
7878

79+
type epHealth struct {
80+
Ep string `json:"endpoint"`
81+
Health bool `json:"health"`
82+
Took string `json:"took"`
83+
Error string `json:"error,omitempty"`
84+
}
85+
7986
// epHealthCommandFunc executes the "endpoint-health" command.
8087
func epHealthCommandFunc(cmd *cobra.Command, args []string) {
8188
flags.SetPflagsFromEnv("ETCDCTL", cmd.InheritedFlags())
89+
initDisplayFromCmd(cmd)
8290

8391
sec := secureCfgFromCmd(cmd)
8492
dt := dialTimeoutFromCmd(cmd)
@@ -95,15 +103,15 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) {
95103
}
96104

97105
var wg sync.WaitGroup
98-
errc := make(chan error, len(cfgs))
106+
hch := make(chan epHealth, len(cfgs))
99107
for _, cfg := range cfgs {
100108
wg.Add(1)
101109
go func(cfg *v3.Config) {
102110
defer wg.Done()
103111
ep := cfg.Endpoints[0]
104112
cli, err := v3.New(*cfg)
105113
if err != nil {
106-
errc <- fmt.Errorf("%s is unhealthy: failed to connect: %v", ep, err)
114+
hch <- epHealth{Ep: ep, Health: false, Error: err.Error()}
107115
return
108116
}
109117
st := time.Now()
@@ -112,25 +120,29 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) {
112120
ctx, cancel := commandCtx(cmd)
113121
_, err = cli.Get(ctx, "health")
114122
cancel()
123+
eh := epHealth{Ep: ep, Health: false, Took: time.Since(st).String()}
115124
// permission denied is OK since proposal goes through consensus to get it
116125
if err == nil || err == rpctypes.ErrPermissionDenied {
117-
fmt.Printf("%s is healthy: successfully committed proposal: took = %v\n", ep, time.Since(st))
126+
eh.Health = true
118127
} else {
119-
errc <- fmt.Errorf("%s is unhealthy: failed to commit proposal: %v", ep, err)
128+
eh.Error = err.Error()
120129
}
130+
hch <- eh
121131
}(cfg)
122132
}
123133

124134
wg.Wait()
125-
close(errc)
135+
close(hch)
126136

127137
errs := false
128-
for err := range errc {
129-
if err != nil {
138+
healthList := []epHealth{}
139+
for h := range hch {
140+
healthList = append(healthList, h)
141+
if h.Error != "" {
130142
errs = true
131-
fmt.Fprintln(os.Stderr, err)
132143
}
133144
}
145+
display.EndpointHealth(healthList)
134146
if errs {
135147
ExitWithError(ExitError, fmt.Errorf("unhealthy cluster"))
136148
}

etcdctl/ctlv3/command/printer.go

+15
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type printer interface {
4343
MemberUpdate(id uint64, r v3.MemberUpdateResponse)
4444
MemberList(v3.MemberListResponse)
4545

46+
EndpointHealth([]epHealth)
4647
EndpointStatus([]epStatus)
4748
EndpointHashKV([]epHashKV)
4849
MoveLeader(leader, target uint64, r v3.MoveLeaderResponse)
@@ -148,6 +149,7 @@ func newPrinterUnsupported(n string) printer {
148149
return &printerUnsupported{printerRPC{nil, f}}
149150
}
150151

152+
func (p *printerUnsupported) EndpointHealth([]epHealth) { p.p(nil) }
151153
func (p *printerUnsupported) EndpointStatus([]epStatus) { p.p(nil) }
152154
func (p *printerUnsupported) EndpointHashKV([]epHashKV) { p.p(nil) }
153155
func (p *printerUnsupported) DBStatus(dbstatus) { p.p(nil) }
@@ -172,6 +174,19 @@ func makeMemberListTable(r v3.MemberListResponse) (hdr []string, rows [][]string
172174
return hdr, rows
173175
}
174176

177+
func makeEndpointHealthTable(healthList []epHealth) (hdr []string, rows [][]string) {
178+
hdr = []string{"endpoint", "health", "took", "error"}
179+
for _, h := range healthList {
180+
rows = append(rows, []string{
181+
h.Ep,
182+
fmt.Sprintf("%v", h.Health),
183+
h.Took,
184+
h.Error,
185+
})
186+
}
187+
return hdr, rows
188+
}
189+
175190
func makeEndpointStatusTable(statusList []epStatus) (hdr []string, rows [][]string) {
176191
hdr = []string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"}
177192
for _, status := range statusList {

etcdctl/ctlv3/command/printer_fields.go

+10
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ func (p *fieldsPrinter) MemberList(r v3.MemberListResponse) {
140140
}
141141
}
142142

143+
func (p *fieldsPrinter) EndpointHealth(hs []epHealth) {
144+
for _, h := range hs {
145+
fmt.Printf("\"Endpoint\" : %q\n", h.Ep)
146+
fmt.Println(`"Health" :`, h.Health)
147+
fmt.Println(`"Took" :`, h.Took)
148+
fmt.Println(`"Error" :`, h.Error)
149+
fmt.Println()
150+
}
151+
}
152+
143153
func (p *fieldsPrinter) EndpointStatus(eps []epStatus) {
144154
for _, ep := range eps {
145155
p.hdr(ep.Resp.Header)

etcdctl/ctlv3/command/printer_json.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func newJSONPrinter() printer {
2828
}
2929
}
3030

31+
func (p *jsonPrinter) EndpointHealth(r []epHealth) { printJSON(r) }
3132
func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
3233
func (p *jsonPrinter) EndpointHashKV(r []epHashKV) { printJSON(r) }
3334
func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) }

etcdctl/ctlv3/command/printer_simple.go

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package command
1616

1717
import (
1818
"fmt"
19+
"os"
1920
"strings"
2021

2122
v3 "github.com/coreos/etcd/clientv3"
@@ -141,6 +142,16 @@ func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
141142
}
142143
}
143144

145+
func (s *simplePrinter) EndpointHealth(hs []epHealth) {
146+
for _, h := range hs {
147+
if h.Error == "" {
148+
fmt.Fprintf(os.Stderr, "%s is healthy: successfully committed proposal: took = %v\n", h.Ep, h.Took)
149+
} else {
150+
fmt.Fprintf(os.Stderr, "%s is unhealthy: failed to commit proposal: %v", h.Ep, h.Error)
151+
}
152+
}
153+
}
154+
144155
func (s *simplePrinter) EndpointStatus(statusList []epStatus) {
145156
_, rows := makeEndpointStatusTable(statusList)
146157
for _, row := range rows {

etcdctl/ctlv3/command/printer_table.go

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ func (tp *tablePrinter) MemberList(r v3.MemberListResponse) {
3434
table.SetAlignment(tablewriter.ALIGN_RIGHT)
3535
table.Render()
3636
}
37+
func (tp *tablePrinter) EndpointHealth(r []epHealth) {
38+
hdr, rows := makeEndpointHealthTable(r)
39+
table := tablewriter.NewWriter(os.Stdout)
40+
table.SetHeader(hdr)
41+
for _, row := range rows {
42+
table.Append(row)
43+
}
44+
table.SetAlignment(tablewriter.ALIGN_RIGHT)
45+
table.Render()
46+
}
3747
func (tp *tablePrinter) EndpointStatus(r []epStatus) {
3848
hdr, rows := makeEndpointStatusTable(r)
3949
table := tablewriter.NewWriter(os.Stdout)

0 commit comments

Comments
 (0)