-
Notifications
You must be signed in to change notification settings - Fork 19
/
hadiscover.go
57 lines (46 loc) · 1.53 KB
/
hadiscover.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
package main
import (
"flag"
"github.com/coreos/go-etcd/etcd"
"log"
)
var filename = flag.String("config", "./haproxy.cfg.tpl", "Template config file used for HAproxy")
var name = flag.String("name", "back", "Base name used for backends")
var haproxy = flag.String("ha", "/usr/bin/haproxy", "Path to the `haproxy` executable")
var etcdHost = flag.String("etcd", "http://localhost:4001", "etcd server(s)")
var etcdKey = flag.String("key", "services", "etcd root key to look for")
var configFile = ".haproxy.cfg"
func reloadConf(etcdClient *etcd.Client) error {
backends, _ := GetBackends(etcdClient, *etcdKey, *name)
err := createConfigFile(backends, *filename, configFile)
if err != nil {
log.Println("Cannot generate haproxy configuration: ", err)
return err
}
return reloadHAproxy(*haproxy, configFile)
}
func main() {
flag.Parse()
var etcdClient = etcd.NewClient([]string{*etcdHost})
err := reloadConf(etcdClient)
if err != nil {
log.Println("Cannot reload haproxy: ", err)
}
changeChan := make(chan *etcd.Response)
stopChan := make(chan bool)
go func() {
for msg := range changeChan {
reload := (msg.PrevNode == nil) || (msg.PrevNode.Key != msg.Node.Key) || (msg.Action != "set")
if reload {
err := reloadConf(etcdClient)
if err != nil {
log.Println("Cannot reload haproxy: ", err, msg)
}
}
}
}()
log.Println("Start watching changes in etcd")
if _, err := etcdClient.Watch(*etcdKey, 0, true, changeChan, stopChan); err != nil {
log.Println("Cannot register watcher for changes in etcd: ", err)
}
}