diff --git a/pkg/controller/keepalived.go b/pkg/controller/keepalived.go index 771efde2..f3b8b938 100644 --- a/pkg/controller/keepalived.go +++ b/pkg/controller/keepalived.go @@ -26,6 +26,7 @@ import ( "strings" "syscall" "text/template" + "time" "github.com/golang/glog" @@ -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 @@ -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() @@ -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