Skip to content

Commit

Permalink
ENH: Adds seamless reloads (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjpfan authored May 8, 2018
1 parent bbb0113 commit 52c0f86
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions haproxy.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.sock mode 660 level admin expose-fd listeners

defaults
mode http
Expand Down
1 change: 1 addition & 0 deletions haproxy.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.sock mode 660 level admin expose-fd listeners
tune.ssl.default-dh-param 2048{{.ExtraGlobal}}

# disable sslv3, prefer modern ciphers
Expand Down
10 changes: 9 additions & 1 deletion proxy/ha_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,15 @@ func (m HaProxy) Reload() error {
return fmt.Errorf("Could not read the %s file\n%s", pidPath, err.Error())
}
reloadStrategy := m.getReloadStrategy()
cmdArgs := []string{reloadStrategy, string(pid)}
haproxySocket := "/var/run/haproxy.sock"
socketOn := haSocketOn(haproxySocket)

var cmdArgs []string
if socketOn {
cmdArgs = []string{"-x", haproxySocket, reloadStrategy, string(pid)}
} else {
cmdArgs = []string{reloadStrategy, string(pid)}
}
reloadErr = HaProxy{}.RunCmd(cmdArgs)
if reloadErr == nil {
logPrintf("Proxy config was reloaded")
Expand Down
32 changes: 32 additions & 0 deletions proxy/ha_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestHaProxyUnitTestSuite(t *testing.T) {
s := new(HaProxyTestSuite)
s.TemplateContent = `global
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.sock mode 660 level admin expose-fd listeners
tune.ssl.default-dh-param 2048
# disable sslv3, prefer modern ciphers
Expand Down Expand Up @@ -122,6 +123,9 @@ func (s *HaProxyTestSuite) SetupTest() {
readPidFile = func(fileName string) ([]byte, error) {
return []byte(s.Pid), nil
}
haSocketOn = func(adddress string) bool {
return true
}
}

// GetCertPaths
Expand Down Expand Up @@ -2570,6 +2574,30 @@ func (s *HaProxyTestSuite) Test_Reload_ReturnsError_WhenReadPidFails() {
s.Error(err)
}

func (s *HaProxyTestSuite) Test_HaProxySocketNotOn_RunsRunCmd() {
actual := HaProxyTestSuite{}.mockHaExecCmd()
haSocketOnOrig := haSocketOn
defer func() {
haSocketOn = haSocketOnOrig
}()
haSocketOn = func(address string) bool {
return false
}
expected := []string{
"-f",
"/cfg/haproxy.cfg",
"-D",
"-p",
"/var/run/haproxy.pid",
"-sf",
s.Pid,
}

HaProxy{}.Reload()

s.Equal(expected, *actual)
}

func (s *HaProxyTestSuite) Test_Reload_RunsRunCmd() {
actual := HaProxyTestSuite{}.mockHaExecCmd()
expected := []string{
Expand All @@ -2578,6 +2606,8 @@ func (s *HaProxyTestSuite) Test_Reload_RunsRunCmd() {
"-D",
"-p",
"/var/run/haproxy.pid",
"-x",
"/var/run/haproxy.sock",
"-sf",
s.Pid,
}
Expand All @@ -2596,6 +2626,8 @@ func (s *HaProxyTestSuite) Test_Reload_Terminate_RunsRunCmd() {
"-D",
"-p",
"/var/run/haproxy.pid",
"-x",
"/var/run/haproxy.sock",
"-st",
s.Pid,
}
Expand Down
1 change: 1 addition & 0 deletions proxy/test_configs/tmpl/haproxy.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.sock mode 660 level admin expose-fd listeners
tune.ssl.default-dh-param 2048{{.ExtraGlobal}}

# disable sslv3, prefer modern ciphers
Expand Down
5 changes: 5 additions & 0 deletions proxy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ var readFile = ioutil.ReadFile
var logPrintf = log.Printf
var readPidFile = ioutil.ReadFile
var readConfigsDir = ioutil.ReadDir
var haSocketOn = func(address string) bool {
conn, err := net.Dial("unix", address)
defer conn.Close()
return err == nil
}
var getSecretOrEnvVarSplit = func(key, defaultValue string) string {
value := getSecretOrEnvVar(key, defaultValue)
if len(value) > 0 {
Expand Down
1 change: 1 addition & 0 deletions test_configs/reload-error.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.sock mode 660 level admin expose-fd listeners
tune.ssl.default-dh-param 2048

defaults
Expand Down
1 change: 1 addition & 0 deletions test_configs/reload.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global
pidfile /var/run/haproxy.pid
stats socket /var/run/haproxy.sock mode 660 level admin expose-fd listeners
tune.ssl.default-dh-param 2048

defaults
Expand Down

0 comments on commit 52c0f86

Please sign in to comment.