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

Commit

Permalink
Fix crashing race condition on startup
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Sheehy <ssheehy@firescope.com>
  • Loading branch information
Steven Sheehy committed Dec 19, 2018
1 parent 34a46af commit c4b3fb6
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions pkg/controller/keepalived.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"syscall"
"text/template"
"time"

"github.com/golang/glog"

Expand Down Expand Up @@ -140,8 +141,7 @@ func (k *keepalived) Start() {
"--dont-fork",
"--log-console",
"--release-vips",
"--log-detail",
"--pid", "/keepalived.pid")
"--log-detail")

k.cmd.Stdout = os.Stdout
k.cmd.Stderr = os.Stderr
Expand All @@ -153,20 +153,16 @@ func (k *keepalived) Start() {

k.started = true

if err := k.cmd.Start(); err != nil {
glog.Errorf("keepalived error: %v", err)
}

if err := k.cmd.Wait(); err != nil {
glog.Fatalf("keepalived error: %v", err)
if err := k.cmd.Run(); err != nil {
glog.Fatalf("Error starting keepalived: %v", err)
}
}

// Reload sends SIGHUP to keepalived to reload the configuration.
func (k *keepalived) Reload() error {
if !k.started {
// TODO: add a warning indicating that keepalived is not started?
return nil
glog.Info("Waiting for keepalived to start")
for !k.IsRunning() {
time.Sleep(time.Second)
}

k.Cleanup()
Expand All @@ -179,8 +175,31 @@ func (k *keepalived) Reload() error {
return nil
}

// Whether keepalived child process is currently running
// Whether keepalived process is currently running
func (k *keepalived) IsRunning() bool {
if !k.started {
glog.Error("keepalived not started")
return false
}

if _, err := os.Stat("/var/run/keepalived.pid"); os.IsNotExist(err) {
glog.Error("Missing keepalived.pid")
return false
}

return true
}

// Whether keepalived child process is currently running and VIPs are assigned
func (k *keepalived) Healthy() error {
if !k.IsRunning() {
return fmt.Errorf("keepalived is not running")
}

if _, err := os.Stat("/var/run/vrrp.pid"); os.IsNotExist(err) {
return fmt.Errorf("VRRP child process not running")
}

b, err := ioutil.ReadFile(keepalivedState)
if err != nil {
return err
Expand Down

0 comments on commit c4b3fb6

Please sign in to comment.