-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
411 lines (341 loc) · 9.93 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/hex"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"golang.org/x/net/context"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"sort"
"strconv"
"strings"
"syscall"
"text/template"
"time"
)
type Endpoint struct {
Name string
IP string
Port int
}
type GroupKey struct {
Port int
IP string
Ssl string
}
type ServiceConfiguration struct {
Publish GroupKey
Backends []Endpoint
}
type WholeConfiguration struct {
Services []ServiceConfiguration
StatsPort int
Stats string
}
type HaProxyTemplateModel struct {
Services []ServiceConfiguration
Stats string
StatsPort int
PidFile string
SockFile string
}
var haproxyBinary string
var lastHash = "-1"
var statsPort = -1
var containerCheckTime = 5
var haproxyPidFile = "/tmp/haproxy.pid"
var haproxySock = "/tmp/haproxy.sock"
var noServicesPrinted = false
const haproxyConfig = "/usr/local/etc/haproxy/haproxy.cfg"
func main() {
readEnvironmentConfiguration()
startHAProxyIdleInstance()
exit_chan := make(chan int, 1)
signal_chan := make(chan os.Signal, 1)
signal.Notify(signal_chan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-signal_chan
log.Println("Exit signal received")
if pid, process := findRunningHAProxyPid(); pid > 0 {
log.Println("Sending 0x9 (SIGKILL) to HAProxy pid ", pid)
process.Signal(syscall.SIGKILL)
}
exit_chan <- 0
}()
go func() {
ctx := context.Background()
cli, err := client.NewClientWithOpts()
if err != nil {
panic(err)
}
for {
services := readServices(cli, ctx)
if len(services) == 0 {
if !noServicesPrinted {
log.Println("No container found with label lb.enable=si")
noServicesPrinted = true
}
} else {
noServicesPrinted = false
}
conf := WholeConfiguration{StatsPort: statsPort, Services: services}
config, hash, _ := generateHAProxyConfig(conf)
if strings.Compare(lastHash, hash) != 0 {
printCurrentServices(conf)
applyConfiguration(config, hash)
}
time.Sleep(time.Duration(containerCheckTime) * time.Second)
}
}()
code := <-exit_chan
log.Println("auto-lb terminated")
os.Exit(code)
}
func startHAProxyIdleInstance() {
var err error
if haproxyBinary, err = exec.LookPath("haproxy"); err == nil {
conf := WholeConfiguration{StatsPort: statsPort, Services: make([]ServiceConfiguration, 0)}
config, hash, _ := generateHAProxyConfig(conf)
applyConfiguration(config, hash)
} else {
log.Fatal("haproxy executable not found ")
}
}
func applyConfiguration(config, hash string) {
writeFile(config, haproxyConfig)
startNewHAProxy()
lastHash = hash
}
func readEnvironmentConfiguration() {
statsPort = readEnvInteger("LB_STATS_PORT")
if statsPort > 0 {
log.Println("HAProxy statistics port is", statsPort)
}
containerCheckTime = readEnvInteger("CHECK_TIME")
if containerCheckTime <= 0 {
containerCheckTime = 5
}
log.Println("Container refresh interval check is", containerCheckTime, "seconds")
if len(os.Getenv("HAPROXY_PID_FILE")) > 0 {
haproxyPidFile = os.Getenv("HAPROXY_PID_FILE")
}
if len(os.Getenv("HAPROXY_SOCK_FILE")) > 0 {
haproxySock = os.Getenv("HAPROXY_SOCK_FILE")
}
}
func readEnvInteger(name string) (retval int) {
strValue := os.Getenv(name)
retval = -1
if len(strValue) != 0 {
stat, err := strconv.Atoi(strValue)
if err == nil {
retval = stat
} else {
panic("Label " + name + " port not convertible to integer: " + strValue)
}
}
return
}
func printCurrentServices(whole WholeConfiguration) {
log.Println("Backends change dectected. Reconfiguring haproxy with:")
for _, service := range whole.Services {
proto := "HTTP"
if len(service.Publish.Ssl) > 0 {
proto = "SSL"
}
log.Println("")
log.Println("Publish port", service.Publish.Port, proto, service.Publish.Ssl)
for _, backend := range service.Backends {
log.Println(" |- Backend", backend.Name, "at", backend.IP, "port", backend.Port)
}
}
}
func readServices(cli *client.Client, ctx context.Context) []ServiceConfiguration {
group := make(map[GroupKey][]Endpoint)
filters := filters.NewArgs(filters.KeyValuePair{Key: "label", Value: "lb.enable=Y"})
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{Filters: filters})
if err != nil {
panic(err)
}
// group containers by publish port
for _, val := range containers {
processContainer(val, group)
}
services := make([]ServiceConfiguration, 0)
// order endpoints by name
for key, value := range group {
sort.Slice(value, func(i, j int) bool {
return strings.Compare(value[i].Name, value[j].Name) < 0
})
services = append(services, ServiceConfiguration{Backends: value, Publish: key})
}
// order services by publish port asc
sort.Slice(services, func(i, j int) bool {
return services[i].Publish.IP + " " + strconv.Itoa(services[i].Publish.Port) < services[j].Publish.IP + " " + strconv.Itoa(services[j].Publish.Port)
})
return services
}
func processContainer(container types.Container, group map[GroupKey][]Endpoint) (err error) {
defer func() {
if r := recover(); r != nil {
log.Println("Container", container.Names[0][1:], "skipped due to error: ", r)
}
}()
publish, err := strconv.Atoi(container.Labels["lb.publish"])
if err != nil {
panic("Label lb.publish not found or not convertible to integer")
}
target, err := strconv.Atoi(container.Labels["lb.target"])
if err != nil {
panic("Label lb.target not found or not convertible to integer")
}
dst_address := container.Labels["lb.dst_addr"]
key := GroupKey{Port: publish, IP:dst_address}
if len(container.Labels["lb.ssl"]) != 0 {
sslFile := container.Labels["lb.ssl"]
if _, err := os.Stat(sslFile); os.IsNotExist(err) {
panic("Label lb.ssl pem file does not exist: " + sslFile)
} else {
key.Ssl = sslFile
}
}
for m := range container.NetworkSettings.Networks {
group[key] = append(group[key], Endpoint{container.Names[0][1:], container.NetworkSettings.Networks[m].IPAddress,target})
}
return nil
}
func generateHAProxyConfig(whole WholeConfiguration) (config string, hash string, err error) {
conf := `
global
daemon
stats socket {{$.SockFile}} mode 600 expose-fd listeners level user
stats timeout 30s
pidfile {{$.PidFile}}
log /dev/log local0 debug
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option redispatch
option forwardfor
option originalto
compression algo gzip
compression type text/css text/html text/javascript application/javascript text/plain text/xml application/json
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000{{if .Stats}}
listen stats
bind *:{{.StatsPort}}
stats enable
stats hide-version
stats refresh 5s
stats show-node
stats uri /{{end}}
{{range $_, $value := .Services}}frontend port_{{$value.Publish.IP}}_{{$value.Publish.Port}}
bind {{if $value.Publish.IP}}{{$value.Publish.IP}}{{else}}*{{end}}:{{$value.Publish.Port}}{{if $value.Publish.Ssl}} ssl crt {{$value.Publish.Ssl}}{{end}}
default_backend port_{{$value.Publish.IP}}_{{$value.Publish.Port}}_backends
rspdel ^ETag:.*
backend port_{{$value.Publish.IP}}_{{$value.Publish.Port}}_backends
balance leastconn
stick-table type ip size 200k expire 520m
stick on src
{{range $value.Backends}}server {{.Name}} {{.IP}}:{{.Port}}
{{end}}
{{end}}
`
if _, err := os.Stat("/haproxy.tmpl"); err == nil {
b, err := ioutil.ReadFile("/haproxy.tmpl")
if err == nil {
conf = string(b)
}
}
t := template.Must(template.New("conf").Parse(conf))
buf := new(bytes.Buffer)
stats := ""
if whole.StatsPort > 0 {
stats = "Y"
}
model := HaProxyTemplateModel{Services: whole.Services,
Stats: stats,
StatsPort: whole.StatsPort,
PidFile: haproxyPidFile,
SockFile: haproxySock}
err = t.Execute(buf, model)
if err != nil {
panic(err)
}
config = buf.String()
//log.Println(config)
hasher := md5.New()
hasher.Write([]byte(config))
hash = hex.EncodeToString(hasher.Sum(nil))
return
}
func writeFile(config, name string) error {
f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()
n3, err := f.WriteString(config)
log.Println("Wrote", n3, "bytes to", f.Name())
return err
}
func findRunningHAProxyPid() (pid int, process *os.Process) {
pid = -1
if file, err := os.Open(haproxyPidFile); err == nil {
defer file.Close()
if scanner := bufio.NewScanner(file); scanner.Scan() {
firstLine := scanner.Text()
if pid, err = strconv.Atoi(string(firstLine)); err != nil {
pid = -1
log.Println(err)
}
}
if pid > 0 {
if process, err = os.FindProcess(pid); err == nil {
if process.Signal(syscall.Signal(0x0)) != nil { // el signal 0 no fa res, pero dona error si el pid no existeix
pid = -1
}
} else {
pid = -1
}
}
}
return
}
func startNewHAProxy() {
args := make([]string, 0)
args = append(args, "-W", "-f", haproxyConfig)
if pid, _ := findRunningHAProxyPid(); pid > 0 {
args = append(args, "-x", haproxySock, "-sf", strconv.Itoa(pid))
}
log.Println("Starting new HAProxy instance: ", haproxyBinary, strings.Join(args, " "))
procAttr := &os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
}
if currentHAProxy, err := os.StartProcess(haproxyBinary, args, procAttr); err == nil {
go func(process *os.Process) {
process.Wait()
log.Println("Master HAProxy started with pid", currentHAProxy.Pid, "has finished")
}(currentHAProxy)
time.Sleep(1 * time.Second)
} else {
log.Fatal("Error ocurred while starting a new HAProxy instance", err)
}
}