Skip to content

Commit

Permalink
add timeout to etcd member add call
Browse files Browse the repository at this point in the history
  • Loading branch information
kinarashah committed Jun 27, 2024
1 parent 8d11342 commit f492ab2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
13 changes: 12 additions & 1 deletion services/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/rancher/rke/util"
"github.com/sirupsen/logrus"
etcdclientv2 "go.etcd.io/etcd/client/v2"
etcdclientv3 "go.etcd.io/etcd/client/v3"
"golang.org/x/sync/errgroup"
)

Expand All @@ -29,6 +30,7 @@ const (
EtcdDataDir = "/var/lib/rancher/etcd/"
EtcdInitWaitTime = 10
EtcdSnapshotWaitTime = 5
EtcdRequestWaitTimeout = 10
EtcdPermFixContainerName = "etcd-fix-perm"
)

Expand Down Expand Up @@ -169,6 +171,14 @@ func RemoveEtcdPlane(ctx context.Context, etcdHosts []*hosts.Host, force bool) e
return nil
}

func memberAdd(ctx context.Context, etcdClient *etcdclientv3.Client, peerURL string) error {
ctxTimeout, cancel := context.WithTimeout(ctx, EtcdRequestWaitTimeout*time.Second)
_, err := etcdClient.MemberAdd(ctxTimeout, []string{peerURL})
cancel()
_ = etcdClient.Close()
return err
}

func AddEtcdMember(ctx context.Context, toAddEtcdHost *hosts.Host, etcdHosts []*hosts.Host, localConnDialerFactory hosts.DialerFactory,
k8sVersion string, cert, key []byte) error {
log.Infof(ctx, "[add/%s] Adding member [etcd-%s] to etcd cluster", ETCDRole, toAddEtcdHost.HostnameOverride)
Expand All @@ -184,7 +194,7 @@ func AddEtcdMember(ctx context.Context, toAddEtcdHost *hosts.Host, etcdHosts []*
logrus.Debugf("Failed to create etcd client for host [%s]: %v", host.Address, err)
continue
}
if _, err := etcdClient.MemberAdd(ctx, []string{peerURL}); err != nil {
if err := memberAdd(ctx, etcdClient, peerURL); err != nil {
logrus.Debugf("Failed to Add etcd member [%s] from host: %v", host.Address, err)
continue
}
Expand Down Expand Up @@ -228,6 +238,7 @@ func removeEtcdMemberV3(ctx context.Context, toDeleteEtcdHost *hosts.Host, host
logrus.Debugf("Failed to create etcd client for host [%s]: %v", host.Address, err)
return false
}
defer etcdClient.Close()
members, err := etcdClient.MemberList(ctx)
if err != nil {
logrus.Debugf("Failed to list etcd members from host [%s]: %v", host.Address, err)
Expand Down
1 change: 1 addition & 0 deletions services/etcd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func getEtcdClientV3(ctx context.Context, etcdHost *hosts.Host, localConnDialerF
Endpoints: []string{"https://" + etcdHost.InternalAddress + ":2379"},
TLS: tlsConfig,
DialOptions: []grpc.DialOption{grpc.WithContextDialer(wrapper(dialer))},
DialTimeout: 5 * time.Second,
}

return etcdclientv3.New(cfg)
Expand Down

0 comments on commit f492ab2

Please sign in to comment.