From 581e3389c456d641240d8b009ca2b4b518903eeb Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Fri, 24 May 2019 07:49:49 -0700 Subject: [PATCH] Reorder etcd servers list --- cluster/plan.go | 2 +- services/etcd_util.go | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cluster/plan.go b/cluster/plan.go index 96c29f7d..8f0305d9 100644 --- a/cluster/plan.go +++ b/cluster/plan.go @@ -100,7 +100,7 @@ func BuildRKEConfigNodePlan(ctx context.Context, myCluster *Cluster, host *hosts func (c *Cluster) BuildKubeAPIProcess(host *hosts.Host, prefixPath string) v3.Process { // check if external etcd is used - etcdConnectionString := services.GetEtcdConnString(c.EtcdHosts) + etcdConnectionString := services.GetEtcdConnString(c.EtcdHosts, host.Address) etcdPathPrefix := EtcdPathPrefix etcdClientCert := pki.GetCertPath(pki.KubeNodeCertName) etcdClientKey := pki.GetKeyPath(pki.KubeNodeCertName) diff --git a/services/etcd_util.go b/services/etcd_util.go index 04c74bc0..5d6f7223 100644 --- a/services/etcd_util.go +++ b/services/etcd_util.go @@ -6,8 +6,10 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math/rand" "net" "net/http" + "strings" "time" etcdclient "github.com/coreos/etcd/client" @@ -112,15 +114,24 @@ func getEtcdDialer(localConnDialerFactory hosts.DialerFactory, etcdHost *hosts.H return etcdFactory(etcdHost) } -func GetEtcdConnString(hosts []*hosts.Host) string { - connString := "" - for i, host := range hosts { - connString += "https://" + host.InternalAddress + ":2379" - if i < (len(hosts) - 1) { - connString += "," +func GetEtcdConnString(hosts []*hosts.Host, hostAddress string) string { + connHosts := []string{} + containsHostAddress := false + for _, host := range hosts { + if host.InternalAddress == hostAddress { + containsHostAddress = true + continue } + connHosts = append(connHosts, "https://"+host.InternalAddress+":2379") } - return connString + rand.Seed(time.Now().UnixNano()) + rand.Shuffle(len(connHosts), func(i, j int) { + connHosts[i], connHosts[j] = connHosts[j], connHosts[i] + }) + if containsHostAddress { + connHosts = append([]string{"https://" + hostAddress + ":2379"}, connHosts...) + } + return strings.Join(connHosts, ",") } func getEtcdTLSConfig(certificate, key []byte) (*tls.Config, error) {