-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepalived_process.go
65 lines (54 loc) · 1.96 KB
/
keepalived_process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
ps "github.com/mitchellh/go-ps"
"github.com/sirupsen/logrus"
)
const (
keepalivedProcessName string = "keepalived"
)
type keepalivedProcess struct {
Proc *ps.UnixProcess
}
// Attempt to find Keepalived process in process parents and wait until Keepalived has terminated and call given function once that happens
func WaitForKeepalivedTermination(ctx context.Context, stop context.CancelFunc) {
// Keepalived does not terminate long-running notification programs when
// exiting. In addition Keepalived may be terminated through other means
// such as SIGKILL. In such cases the IP address updates must stop as soon
// as possible. As of Keepalived 1.2, shipped with OpenShift 3.9, there is
// no mechanism to reliably detect that Keepalived has terminated. Later
// versions have support for FIFOs to communicate to notification programs.
// Therefore the only reasonable method is to locate the process ID of
// Keepalived and polling for its validity in a regular interval.
keepalivedProcess, err := findKeepalivedProcessParent()
if err != nil {
logrus.Warningf("Keepalived not found: %s", err)
keepalivedProcess = nil
} else {
go keepalivedProcess.waitForTermination(ctx, stop)
}
}
// Attempt to find Keepalived process in process parents.
func findKeepalivedProcessParent() (*keepalivedProcess, error) {
proc, err := findParentProcess(keepalivedProcessName)
if err != nil {
return nil, err
}
logrus.WithField("pid", proc.Pid()).Debug("Keepalived process found")
return &keepalivedProcess{
Proc: proc.(*ps.UnixProcess),
}, nil
}
// Wait until Keepalived has terminated and call given function once that
// happens.
func (p *keepalivedProcess) waitForTermination(ctx context.Context,
terminatedFunc context.CancelFunc) {
terminated, err := waitForProcessToTerminate(ctx, p.Proc,
keepalivedProcessName)
if err != nil {
logrus.Errorf("Waiting for Keepalived: %s", err)
}
if err != nil || terminated {
terminatedFunc()
}
}