Skip to content

Commit

Permalink
Merge pull request #13751 from ahrtr/reuse_client_config2
Browse files Browse the repository at this point in the history
Move clientconfig into clientv3 so that it can be reused by both etcd…
  • Loading branch information
serathius authored Mar 14, 2022
2 parents 5ed7f00 + 1a3822f commit bfb9aa4
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 153 deletions.
28 changes: 28 additions & 0 deletions client/v3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,31 @@ type Config struct {

// TODO: support custom balancer picker
}

// ConfigSpec is the configuration from users, which comes from command-line flags,
// environment variables or config file. It is a fully declarative configuration,
// and can be serialized & deserialized to/from JSON.
type ConfigSpec struct {
Endpoints []string `json:"endpoints"`
RequestTimeout time.Duration `json:"request-timeout"`
DialTimeout time.Duration `json:"dial-timeout"`
KeepAliveTime time.Duration `json:"keepalive-time"`
KeepAliveTimeout time.Duration `json:"keepalive-timeout"`
Secure *SecureConfig `json:"secure"`
Auth *AuthConfig `json:"auth"`
}

type SecureConfig struct {
Cert string `json:"cert"`
Key string `json:"key"`
Cacert string `json:"cacert"`
ServerName string `json:"server-name"`

InsecureTransport bool `json:"insecure-transport"`
InsecureSkipVerify bool `json:"insecure-skip-tls-verify"`
}

type AuthConfig struct {
Username string `json:"username"`
Password string `json:"password"`
}
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func newCheckPerfCommand(cmd *cobra.Command, args []string) {
cc := clientConfigFromCmd(cmd)
clients := make([]*v3.Client, cfg.clients)
for i := 0; i < cfg.clients; i++ {
clients[i] = cc.mustClient()
clients[i] = mustClient(cc)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.duration)*time.Second)
Expand Down Expand Up @@ -331,7 +331,7 @@ func newCheckDatascaleCommand(cmd *cobra.Command, args []string) {
cc := clientConfigFromCmd(cmd)
clients := make([]*v3.Client, cfg.clients)
for i := 0; i < cfg.clients; i++ {
clients[i] = cc.mustClient()
clients[i] = mustClient(cc)
}

// get endpoints
Expand Down
106 changes: 41 additions & 65 deletions etcdctl/ctlv3/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,6 @@ type GlobalFlags struct {
Debug bool
}

type secureCfg struct {
cert string
key string
cacert string
serverName string

insecureTransport bool
insecureSkipVerify bool
}

type authCfg struct {
username string
password string
}

type discoveryCfg struct {
domain string
insecure bool
Expand All @@ -97,22 +82,13 @@ func initDisplayFromCmd(cmd *cobra.Command) {
}
}

type clientConfig struct {
endpoints []string
dialTimeout time.Duration
keepAliveTime time.Duration
keepAliveTimeout time.Duration
scfg *secureCfg
acfg *authCfg
}

type discardValue struct{}

func (*discardValue) String() string { return "" }
func (*discardValue) Set(string) error { return nil }
func (*discardValue) Type() string { return "" }

func clientConfigFromCmd(cmd *cobra.Command) *clientConfig {
func clientConfigFromCmd(cmd *cobra.Command) *clientv3.ConfigSpec {
lg, err := zap.NewProduction()
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
Expand Down Expand Up @@ -143,26 +119,26 @@ func clientConfigFromCmd(cmd *cobra.Command) *clientConfig {
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, os.Stderr))
}

cfg := &clientConfig{}
cfg.endpoints, err = endpointsFromCmd(cmd)
cfg := &clientv3.ConfigSpec{}
cfg.Endpoints, err = endpointsFromCmd(cmd)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}

cfg.dialTimeout = dialTimeoutFromCmd(cmd)
cfg.keepAliveTime = keepAliveTimeFromCmd(cmd)
cfg.keepAliveTimeout = keepAliveTimeoutFromCmd(cmd)
cfg.DialTimeout = dialTimeoutFromCmd(cmd)
cfg.KeepAliveTime = keepAliveTimeFromCmd(cmd)
cfg.KeepAliveTimeout = keepAliveTimeoutFromCmd(cmd)

cfg.scfg = secureCfgFromCmd(cmd)
cfg.acfg = authCfgFromCmd(cmd)
cfg.Secure = secureCfgFromCmd(cmd)
cfg.Auth = authCfgFromCmd(cmd)

initDisplayFromCmd(cmd)
return cfg
}

func mustClientCfgFromCmd(cmd *cobra.Command) *clientv3.Config {
cc := clientConfigFromCmd(cmd)
cfg, err := newClientCfg(cc.endpoints, cc.dialTimeout, cc.keepAliveTime, cc.keepAliveTimeout, cc.scfg, cc.acfg)
cfg, err := newClientCfg(cc.Endpoints, cc.DialTimeout, cc.KeepAliveTime, cc.KeepAliveTimeout, cc.Secure, cc.Auth)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
}
Expand All @@ -171,11 +147,11 @@ func mustClientCfgFromCmd(cmd *cobra.Command) *clientv3.Config {

func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
cfg := clientConfigFromCmd(cmd)
return cfg.mustClient()
return mustClient(cfg)
}

func (cc *clientConfig) mustClient() *clientv3.Client {
cfg, err := newClientCfg(cc.endpoints, cc.dialTimeout, cc.keepAliveTime, cc.keepAliveTimeout, cc.scfg, cc.acfg)
func mustClient(cc *clientv3.ConfigSpec) *clientv3.Client {
cfg, err := newClientCfg(cc.Endpoints, cc.DialTimeout, cc.KeepAliveTime, cc.KeepAliveTimeout, cc.Secure, cc.Auth)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
}
Expand All @@ -188,28 +164,28 @@ func (cc *clientConfig) mustClient() *clientv3.Client {
return client
}

func newClientCfg(endpoints []string, dialTimeout, keepAliveTime, keepAliveTimeout time.Duration, scfg *secureCfg, acfg *authCfg) (*clientv3.Config, error) {
func newClientCfg(endpoints []string, dialTimeout, keepAliveTime, keepAliveTimeout time.Duration, scfg *clientv3.SecureConfig, acfg *clientv3.AuthConfig) (*clientv3.Config, error) {
// set tls if any one tls option set
var cfgtls *transport.TLSInfo
tlsinfo := transport.TLSInfo{}
tlsinfo.Logger, _ = zap.NewProduction()
if scfg.cert != "" {
tlsinfo.CertFile = scfg.cert
if scfg.Cert != "" {
tlsinfo.CertFile = scfg.Cert
cfgtls = &tlsinfo
}

if scfg.key != "" {
tlsinfo.KeyFile = scfg.key
if scfg.Key != "" {
tlsinfo.KeyFile = scfg.Key
cfgtls = &tlsinfo
}

if scfg.cacert != "" {
tlsinfo.TrustedCAFile = scfg.cacert
if scfg.Cacert != "" {
tlsinfo.TrustedCAFile = scfg.Cacert
cfgtls = &tlsinfo
}

if scfg.serverName != "" {
tlsinfo.ServerName = scfg.serverName
if scfg.ServerName != "" {
tlsinfo.ServerName = scfg.ServerName
cfgtls = &tlsinfo
}

Expand All @@ -231,19 +207,19 @@ func newClientCfg(endpoints []string, dialTimeout, keepAliveTime, keepAliveTimeo
// if key/cert is not given but user wants secure connection, we
// should still setup an empty tls configuration for gRPC to setup
// secure connection.
if cfg.TLS == nil && !scfg.insecureTransport {
if cfg.TLS == nil && !scfg.InsecureTransport {
cfg.TLS = &tls.Config{}
}

// If the user wants to skip TLS verification then we should set
// the InsecureSkipVerify flag in tls configuration.
if scfg.insecureSkipVerify && cfg.TLS != nil {
if scfg.InsecureSkipVerify && cfg.TLS != nil {
cfg.TLS.InsecureSkipVerify = true
}

if acfg != nil {
cfg.Username = acfg.username
cfg.Password = acfg.password
cfg.Username = acfg.Username
cfg.Password = acfg.Password
}

return cfg, nil
Expand Down Expand Up @@ -284,7 +260,7 @@ func keepAliveTimeoutFromCmd(cmd *cobra.Command) time.Duration {
return keepAliveTimeout
}

func secureCfgFromCmd(cmd *cobra.Command) *secureCfg {
func secureCfgFromCmd(cmd *cobra.Command) *clientv3.SecureConfig {
cert, key, cacert := keyAndCertFromCmd(cmd)
insecureTr := insecureTransportFromCmd(cmd)
skipVerify := insecureSkipVerifyFromCmd(cmd)
Expand All @@ -294,14 +270,14 @@ func secureCfgFromCmd(cmd *cobra.Command) *secureCfg {
discoveryCfg.domain = ""
}

return &secureCfg{
cert: cert,
key: key,
cacert: cacert,
serverName: discoveryCfg.domain,
return &clientv3.SecureConfig{
Cert: cert,
Key: key,
Cacert: cacert,
ServerName: discoveryCfg.domain,

insecureTransport: insecureTr,
insecureSkipVerify: skipVerify,
InsecureTransport: insecureTr,
InsecureSkipVerify: skipVerify,
}
}

Expand Down Expand Up @@ -344,7 +320,7 @@ func keyAndCertFromCmd(cmd *cobra.Command) (cert, key, cacert string) {
return cert, key, cacert
}

func authCfgFromCmd(cmd *cobra.Command) *authCfg {
func authCfgFromCmd(cmd *cobra.Command) *clientv3.AuthConfig {
userFlag, err := cmd.Flags().GetString("user")
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
Expand All @@ -358,23 +334,23 @@ func authCfgFromCmd(cmd *cobra.Command) *authCfg {
return nil
}

var cfg authCfg
var cfg clientv3.AuthConfig

if passwordFlag == "" {
splitted := strings.SplitN(userFlag, ":", 2)
if len(splitted) < 2 {
cfg.username = userFlag
cfg.password, err = speakeasy.Ask("Password: ")
cfg.Username = userFlag
cfg.Password, err = speakeasy.Ask("Password: ")
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
} else {
cfg.username = splitted[0]
cfg.password = splitted[1]
cfg.Username = splitted[0]
cfg.Password = splitted[1]
}
} else {
cfg.username = userFlag
cfg.password = passwordFlag
cfg.Username = userFlag
cfg.Password = passwordFlag
}

return &cfg
Expand Down
42 changes: 21 additions & 21 deletions etcdctl/ctlv3/command/make_mirror_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,29 @@ func NewMakeMirrorCommand() *cobra.Command {
return c
}

func authDestCfg() *authCfg {
func authDestCfg() *clientv3.AuthConfig {
if mmuser == "" {
return nil
}

var cfg authCfg
var cfg clientv3.AuthConfig

if mmpassword == "" {
splitted := strings.SplitN(mmuser, ":", 2)
if len(splitted) < 2 {
var err error
cfg.username = mmuser
cfg.password, err = speakeasy.Ask("Destination Password: ")
cfg.Username = mmuser
cfg.Password, err = speakeasy.Ask("Destination Password: ")
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
} else {
cfg.username = splitted[0]
cfg.password = splitted[1]
cfg.Username = splitted[0]
cfg.Password = splitted[1]
}
} else {
cfg.username = mmuser
cfg.password = mmpassword
cfg.Username = mmuser
cfg.Password = mmpassword
}

return &cfg
Expand All @@ -105,24 +105,24 @@ func makeMirrorCommandFunc(cmd *cobra.Command, args []string) {
dialTimeout := dialTimeoutFromCmd(cmd)
keepAliveTime := keepAliveTimeFromCmd(cmd)
keepAliveTimeout := keepAliveTimeoutFromCmd(cmd)
sec := &secureCfg{
cert: mmcert,
key: mmkey,
cacert: mmcacert,
insecureTransport: mminsecureTr,
sec := &clientv3.SecureConfig{
Cert: mmcert,
Key: mmkey,
Cacert: mmcacert,
InsecureTransport: mminsecureTr,
}

auth := authDestCfg()

cc := &clientConfig{
endpoints: []string{args[0]},
dialTimeout: dialTimeout,
keepAliveTime: keepAliveTime,
keepAliveTimeout: keepAliveTimeout,
scfg: sec,
acfg: auth,
cc := &clientv3.ConfigSpec{
Endpoints: []string{args[0]},
DialTimeout: dialTimeout,
KeepAliveTime: keepAliveTime,
KeepAliveTimeout: keepAliveTimeout,
Secure: sec,
Auth: auth,
}
dc := cc.mustClient()
dc := mustClient(cc)
c := mustClientFromCmd(cmd)

err := makeMirror(context.TODO(), c, dc)
Expand Down
4 changes: 2 additions & 2 deletions etcdctl/ctlv3/command/move_leader_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func transferLeadershipCommandFunc(cmd *cobra.Command, args []string) {
var leaderID uint64
for _, ep := range eps {
cfg := clientConfigFromCmd(cmd)
cfg.endpoints = []string{ep}
cli := cfg.mustClient()
cfg.Endpoints = []string{ep}
cli := mustClient(cfg)
resp, serr := cli.Status(ctx, ep)
if serr != nil {
cobrautl.ExitWithError(cobrautl.ExitError, serr)
Expand Down
Loading

0 comments on commit bfb9aa4

Please sign in to comment.