Skip to content

Commit 8a5f001

Browse files
authored
Merge pull request #8909 from fabriziopandini/small-improvement-etcd-client
🌱 Change endpoints to endpoint in the etcd client
2 parents e174c3a + 8ffe0b5 commit 8a5f001

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

controlplane/kubeadm/internal/etcd/etcd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func pbMemberToMember(m *etcdserverpb.Member) *Member {
131131

132132
// ClientConfiguration describes the configuration for an etcd client.
133133
type ClientConfiguration struct {
134-
Endpoints []string
134+
Endpoint string
135135
Proxy proxy.Proxy
136136
TLSConfig *tls.Config
137137
DialTimeout time.Duration
@@ -146,7 +146,7 @@ func NewClient(ctx context.Context, config ClientConfiguration) (*Client, error)
146146
}
147147

148148
etcdClient, err := clientv3.New(clientv3.Config{
149-
Endpoints: config.Endpoints,
149+
Endpoints: []string{config.Endpoint}, // NOTE: endpoint is used only as a host for certificate validation, the network connection is defined by DialOptions.
150150
DialTimeout: config.DialTimeout,
151151
DialOptions: []grpc.DialOption{
152152
grpc.WithBlock(), // block until the underlying connection is up

controlplane/kubeadm/internal/etcd_client_generator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ type EtcdClientGenerator struct {
3838
createClient clientCreator
3939
}
4040

41-
type clientCreator func(ctx context.Context, endpoints []string) (*etcd.Client, error)
41+
type clientCreator func(ctx context.Context, endpoint string) (*etcd.Client, error)
4242

4343
var errEtcdNodeConnection = errors.New("failed to connect to etcd node")
4444

4545
// NewEtcdClientGenerator returns a new etcdClientGenerator instance.
4646
func NewEtcdClientGenerator(restConfig *rest.Config, tlsConfig *tls.Config, etcdDialTimeout, etcdCallTimeout time.Duration) *EtcdClientGenerator {
4747
ecg := &EtcdClientGenerator{restConfig: restConfig, tlsConfig: tlsConfig}
4848

49-
ecg.createClient = func(ctx context.Context, endpoints []string) (*etcd.Client, error) {
49+
ecg.createClient = func(ctx context.Context, endpoint string) (*etcd.Client, error) {
5050
p := proxy.Proxy{
5151
Kind: "pods",
5252
Namespace: metav1.NamespaceSystem,
5353
KubeConfig: ecg.restConfig,
5454
Port: 2379,
5555
}
5656
return etcd.NewClient(ctx, etcd.ClientConfiguration{
57-
Endpoints: endpoints,
57+
Endpoint: endpoint,
5858
Proxy: p,
5959
TLSConfig: tlsConfig,
6060
DialTimeout: etcdDialTimeout,
@@ -75,8 +75,8 @@ func (c *EtcdClientGenerator) forFirstAvailableNode(ctx context.Context, nodeNam
7575
// Loop through the existing control plane nodes.
7676
var errs []error
7777
for _, name := range nodeNames {
78-
endpoints := []string{staticPodName("etcd", name)}
79-
client, err := c.createClient(ctx, endpoints)
78+
endpoint := staticPodName("etcd", name)
79+
client, err := c.createClient(ctx, endpoint)
8080
if err != nil {
8181
errs = append(errs, err)
8282
continue

controlplane/kubeadm/internal/etcd_client_generator_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func TestFirstAvailableNode(t *testing.T) {
5454
{
5555
name: "Returns client successfully",
5656
nodes: []string{"node-1"},
57-
cc: func(ctx context.Context, endpoints []string) (*etcd.Client, error) {
58-
return &etcd.Client{Endpoint: endpoints[0]}, nil
57+
cc: func(ctx context.Context, endpoint string) (*etcd.Client, error) {
58+
return &etcd.Client{Endpoint: endpoint}, nil
5959
},
6060
expectedClient: etcd.Client{Endpoint: "etcd-node-1"},
6161
},
@@ -68,20 +68,20 @@ func TestFirstAvailableNode(t *testing.T) {
6868
{
6969
name: "Returns error from client",
7070
nodes: []string{"node-1", "node-2"},
71-
cc: func(ctx context.Context, endpoints []string) (*etcd.Client, error) {
71+
cc: func(ctx context.Context, endpoint string) (*etcd.Client, error) {
7272
return nil, errors.New("something went wrong")
7373
},
7474
expectedErr: "could not establish a connection to any etcd node: something went wrong",
7575
},
7676
{
7777
name: "Returns client when some of the nodes are down but at least one node is up",
7878
nodes: []string{"node-down-1", "node-down-2", "node-up"},
79-
cc: func(ctx context.Context, endpoints []string) (*etcd.Client, error) {
80-
if strings.Contains(endpoints[0], "node-down") {
79+
cc: func(ctx context.Context, endpoint string) (*etcd.Client, error) {
80+
if strings.Contains(endpoint, "node-down") {
8181
return nil, errors.New("node down")
8282
}
8383

84-
return &etcd.Client{Endpoint: endpoints[0]}, nil
84+
return &etcd.Client{Endpoint: endpoint}, nil
8585
},
8686
expectedClient: etcd.Client{Endpoint: "etcd-node-up"},
8787
},
@@ -117,9 +117,9 @@ func TestForLeader(t *testing.T) {
117117
{
118118
name: "Returns client for leader successfully",
119119
nodes: []string{"node-1", "node-leader"},
120-
cc: func(ctx context.Context, endpoints []string) (*etcd.Client, error) {
120+
cc: func(ctx context.Context, endpoint string) (*etcd.Client, error) {
121121
return &etcd.Client{
122-
Endpoint: endpoints[0],
122+
Endpoint: endpoint,
123123
LeaderID: 1729,
124124
EtcdClient: &etcdfake.FakeEtcdClient{
125125
MemberListResponse: &clientv3.MemberListResponse{
@@ -146,12 +146,12 @@ func TestForLeader(t *testing.T) {
146146
{
147147
name: "Returns client for leader even when one or more nodes are down",
148148
nodes: []string{"node-down-1", "node-down-2", "node-leader"},
149-
cc: func(ctx context.Context, endpoints []string) (*etcd.Client, error) {
150-
if strings.Contains(endpoints[0], "node-down") {
149+
cc: func(ctx context.Context, endpoint string) (*etcd.Client, error) {
150+
if strings.Contains(endpoint, "node-down") {
151151
return nil, errors.New("node down")
152152
}
153153
return &etcd.Client{
154-
Endpoint: endpoints[0],
154+
Endpoint: endpoint,
155155
LeaderID: 1729,
156156
EtcdClient: &etcdfake.FakeEtcdClient{
157157
MemberListResponse: &clientv3.MemberListResponse{
@@ -182,9 +182,9 @@ func TestForLeader(t *testing.T) {
182182
{
183183
name: "Returns error when the leader does not have a corresponding node",
184184
nodes: []string{"node-1"},
185-
cc: func(ctx context.Context, endpoints []string) (*etcd.Client, error) {
185+
cc: func(ctx context.Context, endpoint string) (*etcd.Client, error) {
186186
return &etcd.Client{
187-
Endpoint: endpoints[0],
187+
Endpoint: endpoint,
188188
LeaderID: 1729,
189189
EtcdClient: &etcdfake.FakeEtcdClient{
190190
MemberListResponse: &clientv3.MemberListResponse{
@@ -201,7 +201,7 @@ func TestForLeader(t *testing.T) {
201201
{
202202
name: "Returns error when all nodes are down",
203203
nodes: []string{"node-down-1", "node-down-2", "node-down-3"},
204-
cc: func(ctx context.Context, endpoints []string) (*etcd.Client, error) {
204+
cc: func(ctx context.Context, endpoint string) (*etcd.Client, error) {
205205
return nil, errors.New("node down")
206206
},
207207
expectedErr: "could not establish a connection to the etcd leader: [could not establish a connection to any etcd node: node down, failed to connect to etcd node]",

0 commit comments

Comments
 (0)