@@ -76,9 +76,17 @@ func newEpHashKVCommand() *cobra.Command {
76
76
return hc
77
77
}
78
78
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
+
79
86
// epHealthCommandFunc executes the "endpoint-health" command.
80
87
func epHealthCommandFunc (cmd * cobra.Command , args []string ) {
81
88
flags .SetPflagsFromEnv ("ETCDCTL" , cmd .InheritedFlags ())
89
+ initDisplayFromCmd (cmd )
82
90
83
91
sec := secureCfgFromCmd (cmd )
84
92
dt := dialTimeoutFromCmd (cmd )
@@ -95,15 +103,15 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) {
95
103
}
96
104
97
105
var wg sync.WaitGroup
98
- errc := make (chan error , len (cfgs ))
106
+ hch := make (chan epHealth , len (cfgs ))
99
107
for _ , cfg := range cfgs {
100
108
wg .Add (1 )
101
109
go func (cfg * v3.Config ) {
102
110
defer wg .Done ()
103
111
ep := cfg .Endpoints [0 ]
104
112
cli , err := v3 .New (* cfg )
105
113
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 ()}
107
115
return
108
116
}
109
117
st := time .Now ()
@@ -112,25 +120,29 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) {
112
120
ctx , cancel := commandCtx (cmd )
113
121
_ , err = cli .Get (ctx , "health" )
114
122
cancel ()
123
+ eh := epHealth {Ep : ep , Health : false , Took : time .Since (st ).String ()}
115
124
// permission denied is OK since proposal goes through consensus to get it
116
125
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
118
127
} else {
119
- errc <- fmt . Errorf ( "%s is unhealthy: failed to commit proposal: %v" , ep , err )
128
+ eh . Error = err . Error ( )
120
129
}
130
+ hch <- eh
121
131
}(cfg )
122
132
}
123
133
124
134
wg .Wait ()
125
- close (errc )
135
+ close (hch )
126
136
127
137
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 != "" {
130
142
errs = true
131
- fmt .Fprintln (os .Stderr , err )
132
143
}
133
144
}
145
+ display .EndpointHealth (healthList )
134
146
if errs {
135
147
ExitWithError (ExitError , fmt .Errorf ("unhealthy cluster" ))
136
148
}
0 commit comments