From e80b2474fa60dba981b531079893035af090b809 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Wed, 11 Oct 2017 10:04:42 -0700 Subject: [PATCH] etcdctl/ctlv3: inherit/update flags only once in 'check' command When creating multiple clients, 'mustClientFromCmd' overwrites inherited flags with environment variables, so later clients were printing warnings on duplicate key updates. Signed-off-by: Gyu-Ho Lee --- etcdctl/ctlv3/command/check.go | 5 ++- etcdctl/ctlv3/command/global.go | 42 +++++++++++++------- etcdctl/ctlv3/command/make_mirror_command.go | 10 ++++- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/etcdctl/ctlv3/command/check.go b/etcdctl/ctlv3/command/check.go index 590234ee5..2ad568217 100644 --- a/etcdctl/ctlv3/command/check.go +++ b/etcdctl/ctlv3/command/check.go @@ -112,9 +112,10 @@ func newCheckPerfCommand(cmd *cobra.Command, args []string) { requests := make(chan v3.Op, cfg.clients) limit := rate.NewLimiter(rate.Limit(cfg.limit), 1) - var clients []*v3.Client + cc := clientConfigFromCmd(cmd) + clients := make([]*v3.Client, cfg.clients) for i := 0; i < cfg.clients; i++ { - clients = append(clients, mustClientFromCmd(cmd)) + clients[i] = cc.mustClient() } ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.duration)*time.Second) diff --git a/etcdctl/ctlv3/command/global.go b/etcdctl/ctlv3/command/global.go index 825e28391..5566f3846 100644 --- a/etcdctl/ctlv3/command/global.go +++ b/etcdctl/ctlv3/command/global.go @@ -92,13 +92,22 @@ func initDisplayFromCmd(cmd *cobra.Command) { } } -func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client { +type clientConfig struct { + endpoints []string + dialTimeout time.Duration + keepAliveTime time.Duration + keepAliveTimeout time.Duration + scfg *secureCfg + acfg *authCfg +} + +func clientConfigFromCmd(cmd *cobra.Command) *clientConfig { fs := cmd.InheritedFlags() flags.SetPflagsFromEnv("ETCDCTL", fs) - debug, derr := cmd.Flags().GetBool("debug") - if derr != nil { - ExitWithError(ExitError, derr) + debug, err := cmd.Flags().GetBool("debug") + if err != nil { + ExitWithError(ExitError, err) } if debug { clientv3.SetLogger(grpclog.NewLoggerV2WithVerbosity(os.Stderr, os.Stderr, os.Stderr, 4)) @@ -107,25 +116,30 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client { }) } - endpoints, err := endpointsFromCmd(cmd) + cfg := &clientConfig{} + cfg.endpoints, err = endpointsFromCmd(cmd) if err != nil { ExitWithError(ExitError, err) } - dialTimeout := dialTimeoutFromCmd(cmd) - keepAliveTime := keepAliveTimeFromCmd(cmd) - keepAliveTimeout := keepAliveTimeoutFromCmd(cmd) + cfg.dialTimeout = dialTimeoutFromCmd(cmd) + cfg.keepAliveTime = keepAliveTimeFromCmd(cmd) + cfg.keepAliveTimeout = keepAliveTimeoutFromCmd(cmd) - sec := secureCfgFromCmd(cmd) - auth := authCfgFromCmd(cmd) + cfg.scfg = secureCfgFromCmd(cmd) + cfg.acfg = authCfgFromCmd(cmd) initDisplayFromCmd(cmd) - - return mustClient(endpoints, dialTimeout, keepAliveTime, keepAliveTimeout, sec, auth) + return cfg } -func mustClient(endpoints []string, dialTimeout, keepAliveTime, keepAliveTimeout time.Duration, scfg *secureCfg, acfg *authCfg) *clientv3.Client { - cfg, err := newClientCfg(endpoints, dialTimeout, keepAliveTime, keepAliveTimeout, scfg, acfg) +func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client { + cfg := clientConfigFromCmd(cmd) + return cfg.mustClient() +} + +func (cc *clientConfig) mustClient() *clientv3.Client { + cfg, err := newClientCfg(cc.endpoints, cc.dialTimeout, cc.keepAliveTime, cc.keepAliveTimeout, cc.scfg, cc.acfg) if err != nil { ExitWithError(ExitBadArgs, err) } diff --git a/etcdctl/ctlv3/command/make_mirror_command.go b/etcdctl/ctlv3/command/make_mirror_command.go index 1f9ae9458..8afa479d4 100644 --- a/etcdctl/ctlv3/command/make_mirror_command.go +++ b/etcdctl/ctlv3/command/make_mirror_command.go @@ -75,7 +75,15 @@ func makeMirrorCommandFunc(cmd *cobra.Command, args []string) { insecureTransport: mminsecureTr, } - dc := mustClient([]string{args[0]}, dialTimeout, keepAliveTime, keepAliveTimeout, sec, nil) + cc := &clientConfig{ + endpoints: []string{args[0]}, + dialTimeout: dialTimeout, + keepAliveTime: keepAliveTime, + keepAliveTimeout: keepAliveTimeout, + scfg: sec, + acfg: nil, + } + dc := cc.mustClient() c := mustClientFromCmd(cmd) err := makeMirror(context.TODO(), c, dc)