From 74085136b38fc90d71b1e5843fd6c5668393e1e5 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Thu, 3 Nov 2022 04:55:27 +0800 Subject: [PATCH] etcdctl: connect to the same endpoint as the target to be maintained The client may connect to a different endpint as the target to be maintained. When auth is enabled, the target endpoint might haven't finished applying the authentiation request, so it might reject the corresponding maintenance request due to permission denied. Signed-off-by: Benjamin Wang --- etcdctl/ctlv3/command/defrag_command.go | 6 ++++-- etcdctl/ctlv3/command/ep_command.go | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/etcdctl/ctlv3/command/defrag_command.go b/etcdctl/ctlv3/command/defrag_command.go index 5ebf4483d..dfccafb74 100644 --- a/etcdctl/ctlv3/command/defrag_command.go +++ b/etcdctl/ctlv3/command/defrag_command.go @@ -35,10 +35,11 @@ func NewDefragCommand() *cobra.Command { } func defragCommandFunc(cmd *cobra.Command, args []string) { - failures := 0 - c := mustClientFromCmd(cmd) + cfg := clientConfigFromCmd(cmd) for _, ep := range endpointsFromCluster(cmd) { + cfg.Endpoints = []string{ep} + c := mustClient(cfg) ctx, cancel := commandCtx(cmd) start := time.Now() _, err := c.Defragment(ctx, ep) @@ -50,6 +51,7 @@ func defragCommandFunc(cmd *cobra.Command, args []string) { } else { fmt.Printf("Finished defragmenting etcd member[%s]. took %s\n", ep, d.String()) } + c.Close() } if failures != 0 { diff --git a/etcdctl/ctlv3/command/ep_command.go b/etcdctl/ctlv3/command/ep_command.go index 6abfef397..0964f564c 100644 --- a/etcdctl/ctlv3/command/ep_command.go +++ b/etcdctl/ctlv3/command/ep_command.go @@ -191,14 +191,17 @@ type epStatus struct { } func epStatusCommandFunc(cmd *cobra.Command, args []string) { - c := mustClientFromCmd(cmd) + cfg := clientConfigFromCmd(cmd) var statusList []epStatus var err error for _, ep := range endpointsFromCluster(cmd) { + cfg.Endpoints = []string{ep} + c := mustClient(cfg) ctx, cancel := commandCtx(cmd) resp, serr := c.Status(ctx, ep) cancel() + c.Close() if serr != nil { err = serr fmt.Fprintf(os.Stderr, "Failed to get the status of endpoint %s (%v)\n", ep, serr) @@ -220,14 +223,17 @@ type epHashKV struct { } func epHashKVCommandFunc(cmd *cobra.Command, args []string) { - c := mustClientFromCmd(cmd) + cfg := clientConfigFromCmd(cmd) var hashList []epHashKV var err error for _, ep := range endpointsFromCluster(cmd) { + cfg.Endpoints = []string{ep} + c := mustClient(cfg) ctx, cancel := commandCtx(cmd) resp, serr := c.HashKV(ctx, ep, epHashKVRev) cancel() + c.Close() if serr != nil { err = serr fmt.Fprintf(os.Stderr, "Failed to get the hash of endpoint %s (%v)\n", ep, serr)