Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make v3 discovery and etcdctl reuse the same ClientConfig #13745

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions client/pkg/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ go 1.17

require (
github.com/coreos/go-systemd/v22 v22.3.2
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.17.0
golang.org/x/sys v0.0.0-20210603125802-9665404d3644
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
Expand Down
556 changes: 554 additions & 2 deletions client/pkg/go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ require (
require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

Expand Down
418 changes: 417 additions & 1 deletion client/v2/go.sum

Large diffs are not rendered by default.

103 changes: 103 additions & 0 deletions client/v3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"crypto/tls"
"time"

"go.etcd.io/etcd/client/pkg/v3/cobrautl"
"go.etcd.io/etcd/client/pkg/v3/transport"

"go.uber.org/zap"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -90,3 +93,103 @@ type Config struct {

// TODO: support custom balancer picker
}

type ClientConfig 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"`
}

func (cc *ClientConfig) MustClient() *Client {
cfg, err := NewClientCfg(cc.Endpoints, cc.DialTimeout, cc.KeepAliveTime, cc.KeepAliveTimeout, cc.Secure, cc.Auth)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
}

client, err := New(*cfg)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadConnection, err)
}

return client
}

func NewClientCfg(endpoints []string, dialTimeout, keepAliveTime, keepAliveTimeout time.Duration, scfg *SecureConfig, acfg *AuthConfig) (*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
cfgtls = &tlsinfo
}

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

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

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

cfg := &Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
DialKeepAliveTime: keepAliveTime,
DialKeepAliveTimeout: keepAliveTimeout,
}

if cfgtls != nil {
clientTLS, err := cfgtls.ClientConfig()
if err != nil {
return nil, err
}
cfg.TLS = clientTLS
}

// 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 {
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 {
cfg.TLS.InsecureSkipVerify = true
}

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

return cfg, nil
}
3 changes: 3 additions & 0 deletions client/v3/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/cobra v1.2.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
Expand Down
Loading