Make sure there are no duplicates in etcd member list (#4025)

* Make sure there are no duplicates in etcd member list

Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com>

* fix node names with hyphens

Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com>

* use full server name for etcd node name

Signed-off-by: galal-hussein <hussein.galal.ahmed.11@gmail.com>
This commit is contained in:
Hussein Galal 2021-09-18 00:51:18 +02:00 committed by GitHub
parent 1d21491094
commit 7826407a2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View File

@ -223,6 +223,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
if err != nil {
return err
}
serverConfig.ControlConfig.ServerNodeName = nodeName
serverConfig.ControlConfig.SANs = append(serverConfig.ControlConfig.SANs, "127.0.0.1", "localhost", nodeName)
for _, ip := range nodeIPs {
serverConfig.ControlConfig.SANs = append(serverConfig.ControlConfig.SANs, ip.String())

View File

@ -168,6 +168,7 @@ type Control struct {
EtcdS3Region string
EtcdS3Folder string
EtcdS3Insecure bool
ServerNodeName string
BindAddress string
SANs []string

View File

@ -329,6 +329,16 @@ func (e *ETCD) join(ctx context.Context, clientAccessInfo *clientaccess.Info) er
}
for _, member := range members.Members {
lastHyphen := strings.LastIndex(member.Name, "-")
memberNodeName := member.Name[:lastHyphen]
if memberNodeName == e.config.ServerNodeName {
// make sure to remove the name file if a duplicate node name is used
nameFile := nameFile(e.config)
if err := os.Remove(nameFile); err != nil {
return err
}
return errors.New("Failed to join etcd cluster due to duplicate node names, please use unique node name for the server")
}
for _, peer := range member.PeerURLs {
u, err := url.Parse(peer)
if err != nil {
@ -414,11 +424,7 @@ func (e *ETCD) setName(force bool) error {
fileName := nameFile(e.config)
data, err := ioutil.ReadFile(fileName)
if os.IsNotExist(err) || force {
h, err := os.Hostname()
if err != nil {
return err
}
e.name = strings.SplitN(h, ".", 2)[0] + "-" + uuid.New().String()[:8]
e.name = e.config.ServerNodeName + "-" + uuid.New().String()[:8]
if err := os.MkdirAll(filepath.Dir(fileName), 0700); err != nil {
return err
}