forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepalived.go
185 lines (153 loc) · 4.34 KB
/
keepalived.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"syscall"
"text/template"
"github.com/golang/glog"
k8sexec "k8s.io/kubernetes/pkg/util/exec"
"k8s.io/kubernetes/pkg/util/iptables"
)
const (
iptablesChain = "KUBE-KEEPALIVED-VIP"
keepalivedCfg = "/etc/keepalived/keepalived.conf"
)
var keepalivedTmpl = "keepalived.tmpl"
type keepalived struct {
iface string
ip string
netmask int
priority int
nodes []string
neighbors []string
useUnicast bool
started bool
vips []string
tmpl *template.Template
cmd *exec.Cmd
ipt iptables.Interface
}
// WriteCfg creates a new keepalived configuration file.
// In case of an error with the generation it returns the error
func (k *keepalived) WriteCfg(svcs []vip) error {
w, err := os.Create(keepalivedCfg)
if err != nil {
return err
}
defer w.Close()
k.vips = getVIPs(svcs)
conf := make(map[string]interface{})
conf["iptablesChain"] = iptablesChain
conf["iface"] = k.iface
conf["myIP"] = k.ip
conf["netmask"] = k.netmask
conf["svcs"] = svcs
conf["vips"] = getVIPs(svcs)
conf["nodes"] = k.neighbors
conf["priority"] = k.priority
conf["useUnicast"] = k.useUnicast
if glog.V(2) {
b, _ := json.Marshal(conf)
glog.Infof("%v", string(b))
}
return k.tmpl.Execute(w, conf)
}
// getVIPs returns a list of the virtual IP addresses to be used in keepalived
// without duplicates (a service can use more than one port)
func getVIPs(svcs []vip) []string {
result := []string{}
for _, svc := range svcs {
result = appendIfMissing(result, svc.IP)
}
return result
}
// Start starts a keepalived process in foreground.
// In case of any error it will terminate the execution with a fatal error
func (k *keepalived) Start() {
ae, err := k.ipt.EnsureChain(iptables.TableFilter, iptables.Chain(iptablesChain))
if err != nil {
glog.Fatalf("unexpected error: %v", err)
}
if ae {
glog.V(2).Infof("chain %v already existed", iptablesChain)
}
k.cmd = exec.Command("keepalived",
"--dont-fork",
"--log-console",
"--release-vips",
"--pid", "/keepalived.pid")
k.cmd.Stdout = os.Stdout
k.cmd.Stderr = os.Stderr
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)
}
}
// 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("reloading keepalived")
err := syscall.Kill(k.cmd.Process.Pid, syscall.SIGHUP)
if err != nil {
return fmt.Errorf("error reloading keepalived: %v", err)
}
return nil
}
// Stop stop keepalived process
func (k *keepalived) Stop() {
for _, vip := range k.vips {
k.removeVIP(vip)
}
err := k.ipt.FlushChain(iptables.TableFilter, iptables.Chain(iptablesChain))
if err != nil {
glog.V(2).Infof("unexpected error flushing iptables chain %v: %v", err, iptablesChain)
}
err = syscall.Kill(k.cmd.Process.Pid, syscall.SIGTERM)
if err != nil {
fmt.Errorf("error stopping keepalived: %v", err)
}
}
func resetIPVS() error {
glog.Info("cleaning ipvs configuration")
_, err := k8sexec.New().Command("ipvsadm", "-C").CombinedOutput()
if err != nil {
return fmt.Errorf("error removing ipvs configuration: %v", err)
}
return nil
}
func (k *keepalived) removeVIP(vip string) error {
glog.Infof("removing configured VIP %v", vip)
out, err := k8sexec.New().Command("ip", "addr", "del", vip+"/32", "dev", k.iface).CombinedOutput()
if err != nil {
return fmt.Errorf("error reloading keepalived: %v\n%s", err, out)
}
return nil
}
func (k *keepalived) loadTemplate() error {
tmpl, err := template.ParseFiles(keepalivedTmpl)
if err != nil {
return err
}
k.tmpl = tmpl
return nil
}