Skip to content

Commit 53b59d1

Browse files
keykisodre90
authored andcommitted
CB-9064 restart minions if requested, even if the master IP is the same
In case we repaired the master instance the IP address might change. On AWS it's changing almost all the time, however on Azure the new instance often gets the same IP address as the previous master instance. If the IP changes the salt-bootstrap automatically detects it and restarts all the minions. If the IP remains the same I cannot detect the change and doesn't restart the minions. This leads to public key caching issues and minions are not trying to re-connect to the master. To solve this problem we introduced a new parameter on the API for the minions that a restart can be requested in such cases. To handle backward compatibility we will restart the minions even if this new flag is not set or even set to false, but the master IP changes.
1 parent 7d2675c commit 53b59d1

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

saltboot/salt.go

+21-15
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ type SaltMaster struct {
5757
}
5858

5959
type SaltMinion struct {
60-
Address string `json:"address"`
61-
Roles []string `json:"roles,omitempty"`
62-
Server string `json:"server,omitempty"`
63-
Servers []string `json:"servers,omitempty"`
64-
HostGroup string `json:"hostGroup,omitempty"`
65-
Hostname *string `json:"hostName,omitempty"`
66-
Domain string `json:"domain,omitempty"`
60+
Address string `json:"address"`
61+
Roles []string `json:"roles,omitempty"`
62+
Server string `json:"server,omitempty"`
63+
Servers []string `json:"servers,omitempty"`
64+
HostGroup string `json:"hostGroup,omitempty"`
65+
Hostname *string `json:"hostName,omitempty"`
66+
Domain string `json:"domain,omitempty"`
67+
RestartNeeded *bool `json:"restartNeeded,omitempty"`
6768
}
6869

6970
type SaltPillar struct {
@@ -77,6 +78,10 @@ func (saltMinion SaltMinion) AsByteArray() []byte {
7778
return b
7879
}
7980

81+
func (saltMinion SaltMinion) IsRestartNeeded() bool {
82+
return saltMinion.RestartNeeded != nil && *saltMinion.RestartNeeded
83+
}
84+
8085
func (saltMaster SaltMaster) AsByteArray() []byte {
8186
b, _ := json.Marshal(saltMaster)
8287
return b
@@ -176,14 +181,15 @@ func SaltMinionRunRequestHandler(w http.ResponseWriter, req *http.Request) {
176181
var masterConf []byte
177182
servers := saltMinion.Servers
178183
var restartNeeded bool
184+
log.Printf("[SaltMinionRunRequestHandler] Restart needed flag on minion: %v", saltMinion.IsRestartNeeded())
179185
if servers != nil && len(servers) > 0 {
180186
log.Printf("[SaltMinionRunRequestHandler] salt master list: %s", servers)
181187
masterConf, _ = yaml.Marshal(map[string][]string{"master": servers})
182-
restartNeeded = isSaltMinionRestartNeeded(servers)
188+
restartNeeded = saltMinion.IsRestartNeeded() || isSaltMasterIpDiffers(servers)
183189
} else {
184190
log.Printf("[SaltMinionRunRequestHandler] salt master (depricated): %s", saltMinion.Server)
185191
masterConf, _ = yaml.Marshal(map[string][]string{"master": {saltMinion.Server}})
186-
restartNeeded = isSaltMinionRestartNeeded([]string{saltMinion.Server})
192+
restartNeeded = saltMinion.IsRestartNeeded() || isSaltMasterIpDiffers([]string{saltMinion.Server})
187193
}
188194

189195
err = ioutil.WriteFile(baseDir+"/etc/salt/minion.d/master.conf", masterConf, 0644)
@@ -506,18 +512,18 @@ func shouldAppendPrewarmedRoles(prewarmRoleLocation string) bool {
506512
return true
507513
}
508514

509-
func isSaltMinionRestartNeeded(servers []string) bool {
510-
log.Println("[isSaltMinionRestartNeeded] check whether salt-minion requires restart")
515+
func isSaltMasterIpDiffers(servers []string) bool {
516+
log.Println("[isSaltMasterIpDiffers] check whether salt-minion requires restart")
511517
masterConfFile := "/etc/salt/minion.d/master.conf"
512518
b, err := ioutil.ReadFile(masterConfFile)
513519
if err == nil && len(b) > 0 {
514520
var saltMasterIps = make(map[string][]string)
515521
if err := yaml.Unmarshal(b, saltMasterIps); err != nil {
516-
log.Printf("[isSaltMinionRestartNeeded] [ERROR] failed to unmarshal salt master config file: %s", err.Error())
522+
log.Printf("[isSaltMasterIpDiffers] [ERROR] failed to unmarshal salt master config file: %s", err.Error())
517523
return false
518524
}
519525
ipList := saltMasterIps["master"]
520-
log.Printf("[isSaltMinionRestartNeeded] original master IP list: %s", ipList)
526+
log.Printf("[isSaltMasterIpDiffers] original master IP list: %s", ipList)
521527
for _, server := range servers {
522528
newMaster := true
523529
for _, ip := range ipList {
@@ -526,11 +532,11 @@ func isSaltMinionRestartNeeded(servers []string) bool {
526532
}
527533
}
528534
if newMaster {
529-
log.Printf("[isSaltMinionRestartNeeded] found new salt-master: %s, restart needed", server)
535+
log.Printf("[isSaltMasterIpDiffers] found new salt-master: %s, restart needed", server)
530536
return true
531537
}
532538
}
533539
}
534-
log.Println("[isSaltMinionRestartNeeded] there is no new salt-master")
540+
log.Println("[isSaltMasterIpDiffers] there is no new salt-master")
535541
return false
536542
}

0 commit comments

Comments
 (0)