Skip to content
This repository was archived by the owner on Aug 19, 2020. It is now read-only.

Commit 89bb4c9

Browse files
authored
Merge pull request #75 from steven-sheehy/keepalived-crash-startup
Fix race condition crash on startup
2 parents 34a46af + e9ffc21 commit 89bb4c9

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

pkg/controller/keepalived.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727
"syscall"
2828
"text/template"
29+
"time"
2930

3031
"github.com/golang/glog"
3132

@@ -37,7 +38,9 @@ const (
3738
iptablesChain = "KUBE-KEEPALIVED-VIP"
3839
keepalivedCfg = "/etc/keepalived/keepalived.conf"
3940
haproxyCfg = "/etc/haproxy/haproxy.cfg"
41+
keepalivedPid = "/var/run/keepalived.pid"
4042
keepalivedState = "/var/run/keepalived.state"
43+
vrrpPid = "/var/run/vrrp.pid"
4144
)
4245

4346
var (
@@ -140,8 +143,7 @@ func (k *keepalived) Start() {
140143
"--dont-fork",
141144
"--log-console",
142145
"--release-vips",
143-
"--log-detail",
144-
"--pid", "/keepalived.pid")
146+
"--log-detail")
145147

146148
k.cmd.Stdout = os.Stdout
147149
k.cmd.Stderr = os.Stderr
@@ -153,20 +155,16 @@ func (k *keepalived) Start() {
153155

154156
k.started = true
155157

156-
if err := k.cmd.Start(); err != nil {
157-
glog.Errorf("keepalived error: %v", err)
158-
}
159-
160-
if err := k.cmd.Wait(); err != nil {
161-
glog.Fatalf("keepalived error: %v", err)
158+
if err := k.cmd.Run(); err != nil {
159+
glog.Fatalf("Error starting keepalived: %v", err)
162160
}
163161
}
164162

165163
// Reload sends SIGHUP to keepalived to reload the configuration.
166164
func (k *keepalived) Reload() error {
167-
if !k.started {
168-
// TODO: add a warning indicating that keepalived is not started?
169-
return nil
165+
glog.Info("Waiting for keepalived to start")
166+
for !k.IsRunning() {
167+
time.Sleep(time.Second)
170168
}
171169

172170
k.Cleanup()
@@ -179,8 +177,31 @@ func (k *keepalived) Reload() error {
179177
return nil
180178
}
181179

182-
// Whether keepalived child process is currently running
180+
// Whether keepalived process is currently running
181+
func (k *keepalived) IsRunning() bool {
182+
if !k.started {
183+
glog.Error("keepalived not started")
184+
return false
185+
}
186+
187+
if _, err := os.Stat(keepalivedPid); os.IsNotExist(err) {
188+
glog.Error("Missing keepalived.pid")
189+
return false
190+
}
191+
192+
return true
193+
}
194+
195+
// Whether keepalived child process is currently running and VIPs are assigned
183196
func (k *keepalived) Healthy() error {
197+
if !k.IsRunning() {
198+
return fmt.Errorf("keepalived is not running")
199+
}
200+
201+
if _, err := os.Stat(vrrpPid); os.IsNotExist(err) {
202+
return fmt.Errorf("VRRP child process not running")
203+
}
204+
184205
b, err := ioutil.ReadFile(keepalivedState)
185206
if err != nil {
186207
return err

0 commit comments

Comments
 (0)