Skip to content

Commit 00b8f3d

Browse files
ivanmatmatioktalz
authored andcommitted
BUG/MINOR: restart haproxy when aux config file added
1 parent 3f05cbe commit 00b8f3d

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

controller/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ func (c *HAProxyController) updateHAProxy() {
148148
c.Client.APIDisposeTransaction()
149149
}()
150150

151-
reload, c.restart = c.handleGlobalConfig()
151+
reload, restart := c.handleGlobalConfig()
152152
c.reload = c.reload || reload
153+
c.restart = c.restart || restart
153154

154155
if len(route.CustomRoutes) != 0 {
155156
logger.Error(route.CustomRoutesReset(c.Client))

controller/haproxy.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package controller
1616

1717
import (
18+
"os"
1819
"os/exec"
1920
"strings"
2021

@@ -36,11 +37,16 @@ func (c *HAProxyController) haproxyStartup() {
3637
} else {
3738
logger.Error(err)
3839
}
40+
var msgAuxConfigFile string
3941
if c.OSArgs.UseWiths6Overlay {
4042
c.haproxyProcess = process.NewControlOverS6(c.Cfg.Env, c.OSArgs, c.Client)
4143
} else {
4244
c.haproxyProcess = process.NewDirectControl(c.Cfg.Env, c.OSArgs, c.Client)
45+
if _, err := os.Stat(c.Cfg.Env.AuxCFGFile); err == nil {
46+
c.haproxyProcess.UseAuxFile(true)
47+
msgAuxConfigFile = "and aux config file " + c.Cfg.Env.AuxCFGFile
48+
}
4349
}
44-
logger.Printf("Starting HAProxy with %s", c.Cfg.Env.MainCFGFile)
50+
logger.Printf("Starting HAProxy with %s %s", c.Cfg.Env.MainCFGFile, msgAuxConfigFile)
4551
logger.Panic(c.haproxyService("start"))
4652
}

controller/monitor.go

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func (c *HAProxyController) SyncData() {
9494
change := false
9595
switch job.SyncType {
9696
case COMMAND:
97-
c.reload = c.auxCfgUpdated()
98-
if hadChanges || c.reload {
97+
c.restart, c.reload = c.auxCfgManager()
98+
if hadChanges || c.reload || c.restart {
9999
c.updateHAProxy()
100100
hadChanges = false
101101
continue
@@ -199,28 +199,52 @@ func (c *HAProxyController) getWhitelistedNamespaces() []string {
199199
return namespaces
200200
}
201201

202-
// auxCfgUpdate returns true if auxiliary HAProxy config file was updated, false otherwise.
203-
func (c *HAProxyController) auxCfgUpdated() bool {
202+
// auxCfgManager returns restart or reload requirement based on state and transition of auxiliary configuration file.
203+
func (c *HAProxyController) auxCfgManager() (restart, reload bool) {
204204
info, errStat := os.Stat(c.Cfg.Env.AuxCFGFile)
205+
var (
206+
modifTime int64
207+
auxCfgFile string = c.Cfg.Env.AuxCFGFile
208+
useAuxFile bool
209+
)
210+
211+
defer func() {
212+
// Nothing changed
213+
if c.AuxCfgModTime == modifTime {
214+
return
215+
}
216+
// Apply decisions
217+
c.Client.SetAuxCfgFile(auxCfgFile)
218+
c.haproxyProcess.UseAuxFile(useAuxFile)
219+
// The file exists now (modifTime !=0 otherwise nothing changed case).
220+
if c.AuxCfgModTime == 0 {
221+
restart = true
222+
} else {
223+
// File already exists,
224+
// already in command line parameters just need to reload for modifications.
225+
reload = true
226+
}
227+
c.AuxCfgModTime = modifTime
228+
if c.AuxCfgModTime != 0 {
229+
logger.Infof("Auxiliary HAProxy config '%s' updated", auxCfgFile)
230+
}
231+
}()
232+
205233
// File does not exist
206234
if errStat != nil {
235+
// nullify it
236+
auxCfgFile = ""
207237
if c.AuxCfgModTime == 0 {
208-
return false
238+
// never existed before
239+
return
209240
}
210241
logger.Infof("Auxiliary HAProxy config '%s' removed", c.Cfg.Env.AuxCFGFile)
211-
c.AuxCfgModTime = 0
212-
c.Client.SetAuxCfgFile("")
213-
c.haproxyProcess.UseAuxFile(false)
214-
return true
215-
}
216-
// Check modification time
217-
modifTime := info.ModTime().Unix()
218-
if c.AuxCfgModTime == modifTime {
219-
return false
242+
// but existed so need to restart
243+
restart = true
244+
return
220245
}
221-
logger.Infof("Auxiliary HAProxy config '%s' updated", c.Cfg.Env.AuxCFGFile)
222-
c.AuxCfgModTime = modifTime
223-
c.Client.SetAuxCfgFile(c.Cfg.Env.AuxCFGFile)
224-
c.haproxyProcess.UseAuxFile(true)
225-
return true
246+
// File exists
247+
useAuxFile = true
248+
modifTime = info.ModTime().Unix()
249+
return
226250
}

0 commit comments

Comments
 (0)