forked from d2iq-archive/mesos-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (96 loc) · 2.6 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
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/mesos/mesos-go/detector"
"github.com/mesosphere/mesos-dns/detect"
"github.com/mesosphere/mesos-dns/logging"
"github.com/mesosphere/mesos-dns/records"
"github.com/mesosphere/mesos-dns/resolver"
"github.com/mesosphere/mesos-dns/util"
)
func main() {
util.PanicHandlers = append(util.PanicHandlers, func(_ interface{}) {
// by default the handler already logs the panic
os.Exit(1)
})
var versionFlag bool
// parse flags
cjson := flag.String("config", "config.json", "path to config file (json)")
flag.BoolVar(&versionFlag, "version", false, "output the version")
flag.Parse()
// -version
if versionFlag {
fmt.Println(Version)
os.Exit(0)
}
// initialize logging
logging.SetupLogs()
initAuth()
// initialize resolver
config := records.SetConfig(*cjson)
res := resolver.New(Version, config)
errch := make(chan error)
// launch DNS server
if config.DNSOn {
go func() {
logging.Verbose.Println("waiting for a reload before serving DNS")
<-res.Ready()
logging.Verbose.Println("reload occurred, serving DNS")
errch <- <-res.LaunchDNS()
}()
}
// launch HTTP server
if config.HTTPOn {
go func() {
logging.Verbose.Println("waiting for a reload before serving HTTP")
<-res.Ready()
logging.Verbose.Println("reload occurred, serving HTTP")
errch <- <-res.LaunchHTTP()
}()
}
changed := detectMasters(config.Zk, config.Masters)
reload := time.NewTicker(time.Second * time.Duration(config.RefreshSeconds))
zkTimeout := time.Second * time.Duration(config.ZkDetectionTimeout)
timeout := time.AfterFunc(zkTimeout, func() {
if zkTimeout > 0 {
errch <- fmt.Errorf("master detection timed out after %s", zkTimeout)
}
})
defer reload.Stop()
defer util.HandleCrash()
for {
select {
case <-reload.C:
res.Reload()
case masters := <-changed:
if len(masters) == 0 || masters[0] == "" { // no leader
timeout.Reset(zkTimeout)
} else {
timeout.Stop()
}
logging.Verbose.Printf("new masters detected: %v", masters)
res.SetMasters(masters)
res.Reload()
case err := <-errch:
logging.Error.Fatal(err)
}
}
}
func detectMasters(zk string, masters []string) <-chan []string {
changed := make(chan []string, 1)
if zk != "" {
logging.Verbose.Println("Starting master detector for ZK ", zk)
if md, err := detector.New(zk); err != nil {
log.Fatalf("failed to create master detector: %v", err)
} else if err := md.Detect(detect.NewMasters(masters, changed)); err != nil {
log.Fatalf("failed to initialize master detector: %v", err)
}
} else {
changed <- masters
}
return changed
}