Skip to content

Commit cb4fd9c

Browse files
committed
make v3 discovery and etcdctl reuse the same ClientConfig
move the original clientConfig from etcdctl to clientv3. The original pks/cobrautil is only used by etcdctl or etcdutl, so move it into the clientv3 package.
1 parent af7154c commit cb4fd9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2053
-255
lines changed
File renamed without changes.
File renamed without changes.

client/pkg/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ go 1.17
44

55
require (
66
github.com/coreos/go-systemd/v22 v22.3.2
7+
github.com/spf13/cobra v1.2.1
8+
github.com/spf13/pflag v1.0.5
79
github.com/stretchr/testify v1.7.0
810
go.uber.org/zap v1.17.0
911
golang.org/x/sys v0.0.0-20210603125802-9665404d3644
1012
)
1113

1214
require (
1315
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1417
github.com/pmezard/go-difflib v1.0.0 // indirect
1518
go.uber.org/atomic v1.7.0 // indirect
1619
go.uber.org/multierr v1.6.0 // indirect

client/pkg/go.sum

+554-2
Large diffs are not rendered by default.

client/v2/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ require (
1111
require (
1212
github.com/coreos/go-semver v0.3.0 // indirect
1313
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/kr/pretty v0.1.0 // indirect
1415
github.com/pmezard/go-difflib v1.0.0 // indirect
1516
github.com/stretchr/testify v1.7.0 // indirect
17+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
1618
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
1719
)
1820

client/v2/go.sum

+417-1
Large diffs are not rendered by default.

client/v3/config.go

+103
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
"crypto/tls"
2020
"time"
2121

22+
"go.etcd.io/etcd/client/pkg/v3/cobrautl"
23+
"go.etcd.io/etcd/client/pkg/v3/transport"
24+
2225
"go.uber.org/zap"
2326
"google.golang.org/grpc"
2427
)
@@ -90,3 +93,103 @@ type Config struct {
9093

9194
// TODO: support custom balancer picker
9295
}
96+
97+
type ClientConfig struct {
98+
Endpoints []string `json:"endpoints"`
99+
RequestTimeOut time.Duration `json:"request-timeout"`
100+
DialTimeout time.Duration `json:"dial-timeout"`
101+
KeepAliveTime time.Duration `json:"keepalive-time"`
102+
KeepAliveTimeout time.Duration `json:"keepalive-timeout"`
103+
Scfg *SecureCfg `json:"secure-cfg"`
104+
Acfg *AuthCfg `json:"auth-cfg"`
105+
}
106+
107+
type SecureCfg struct {
108+
Cert string `json:"cert"`
109+
Key string `json:"key"`
110+
Cacert string `json:"cacert"`
111+
ServerName string `json:"servername"`
112+
113+
InsecureTransport bool `json:"insecure-transport"`
114+
InsecureSkipVerify bool `json:"insecure-skip-tls-verify"`
115+
}
116+
117+
type AuthCfg struct {
118+
Username string `json:"username"`
119+
Password string `json:"password"`
120+
}
121+
122+
func (cc *ClientConfig) MustClient() *Client {
123+
cfg, err := NewClientCfg(cc.Endpoints, cc.DialTimeout, cc.KeepAliveTime, cc.KeepAliveTimeout, cc.Scfg, cc.Acfg)
124+
if err != nil {
125+
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)
126+
}
127+
128+
client, err := New(*cfg)
129+
if err != nil {
130+
cobrautl.ExitWithError(cobrautl.ExitBadConnection, err)
131+
}
132+
133+
return client
134+
}
135+
136+
func NewClientCfg(endpoints []string, dialTimeout, keepAliveTime, keepAliveTimeout time.Duration, scfg *SecureCfg, acfg *AuthCfg) (*Config, error) {
137+
// set tls if any one tls option set
138+
var cfgtls *transport.TLSInfo
139+
tlsinfo := transport.TLSInfo{}
140+
tlsinfo.Logger, _ = zap.NewProduction()
141+
if scfg.Cert != "" {
142+
tlsinfo.CertFile = scfg.Cert
143+
cfgtls = &tlsinfo
144+
}
145+
146+
if scfg.Key != "" {
147+
tlsinfo.KeyFile = scfg.Key
148+
cfgtls = &tlsinfo
149+
}
150+
151+
if scfg.Cacert != "" {
152+
tlsinfo.TrustedCAFile = scfg.Cacert
153+
cfgtls = &tlsinfo
154+
}
155+
156+
if scfg.ServerName != "" {
157+
tlsinfo.ServerName = scfg.ServerName
158+
cfgtls = &tlsinfo
159+
}
160+
161+
cfg := &Config{
162+
Endpoints: endpoints,
163+
DialTimeout: dialTimeout,
164+
DialKeepAliveTime: keepAliveTime,
165+
DialKeepAliveTimeout: keepAliveTimeout,
166+
}
167+
168+
if cfgtls != nil {
169+
clientTLS, err := cfgtls.ClientConfig()
170+
if err != nil {
171+
return nil, err
172+
}
173+
cfg.TLS = clientTLS
174+
}
175+
176+
// if key/cert is not given but user wants secure connection, we
177+
// should still setup an empty tls configuration for gRPC to setup
178+
// secure connection.
179+
if cfg.TLS == nil && !scfg.InsecureTransport {
180+
cfg.TLS = &tls.Config{}
181+
}
182+
183+
// If the user wants to skip TLS verification then we should set
184+
// the InsecureSkipVerify flag in tls configuration.
185+
if scfg.InsecureSkipVerify && cfg.TLS != nil {
186+
cfg.TLS.InsecureSkipVerify = true
187+
}
188+
189+
if acfg != nil {
190+
cfg.Username = acfg.Username
191+
cfg.Password = acfg.Password
192+
}
193+
194+
return cfg, nil
195+
}

client/v3/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ require (
2121
github.com/davecgh/go-spew v1.1.1 // indirect
2222
github.com/gogo/protobuf v1.3.2 // indirect
2323
github.com/golang/protobuf v1.5.2 // indirect
24+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2425
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
2526
github.com/pmezard/go-difflib v1.0.0 // indirect
2627
github.com/prometheus/client_model v0.2.0 // indirect
2728
github.com/prometheus/common v0.26.0 // indirect
2829
github.com/prometheus/procfs v0.6.0 // indirect
30+
github.com/spf13/cobra v1.2.1 // indirect
31+
github.com/spf13/pflag v1.0.5 // indirect
2932
github.com/stretchr/testify v1.7.0 // indirect
3033
go.uber.org/atomic v1.7.0 // indirect
3134
go.uber.org/multierr v1.6.0 // indirect

0 commit comments

Comments
 (0)