-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtinydns.go
More file actions
103 lines (95 loc) · 2.18 KB
/
tinydns.go
File metadata and controls
103 lines (95 loc) · 2.18 KB
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
package main
import (
"encoding/json"
"fmt"
"github.com/Sirupsen/logrus"
"strings"
)
const EtcdTinydnsKey = "/lain/config/tinydns_fqdns"
func (self *Agent) RunTinydns() {
if self.tinydnsIsRunning {
return
}
log.Info("Run tinydns")
self.tinydnsIsRunning = true
stopCh := make(chan struct{})
eventCh := self.WatchTinydnsIps(stopCh)
go func() {
self.wg.Add(1)
defer func() {
log.Info("tinydns done")
self.wg.Done()
}()
defer close(stopCh)
for {
select {
case <-eventCh:
self.ApplyTinydnsIps()
case <-self.tinydnsStopCh:
stopCh <- struct{}{}
return
}
}
}()
}
func (self *Agent) StopTinydns() {
if self.tinydnsIsRunning {
log.Debug("Stop tinydns")
self.tinydnsIsRunning = false
self.tinydnsStopCh <- struct{}{}
}
}
// TODO(xutao) move to tinydns app
func (self *Agent) WatchTinydnsIps(stopWatchCh <-chan struct{}) <-chan int {
return self.WatchProcIps(stopWatchCh, "tinydns", "worker")
}
func (self *Agent) ApplyTinydnsIps() {
kv := self.libkv
var servers []string
key := fmt.Sprintf("%s/tinydns/worker", EtcdAppNetworkdKey)
entries, err := kv.List(key)
if err != nil {
log.WithFields(logrus.Fields{
"key": key,
"err": err,
}).Error("Cannot get etcd key")
return
}
for _, pair := range entries {
log.WithFields(logrus.Fields{
"key": pair.Key,
"value": string(pair.Value),
}).Debug("Get tinydns key")
ipKey := pair.Key[len(key)+1:]
splitKey := strings.SplitN(ipKey, ":", 2)
ip, port := splitKey[0], splitKey[1]
servers = append(servers, fmt.Sprintf("%s#%s", ip, port))
}
self.godns.AddDomainServer("lain", servers)
if self.domain != "" {
self.godns.AddDomainServer(self.domain, servers)
}
}
func (self *Agent) AddTinydnsDomain(domain string, data []string) {
kv := self.libkv
key := fmt.Sprintf("%s/%s", EtcdTinydnsKey, domain)
value, err := json.Marshal(data)
if err != nil {
log.WithFields(logrus.Fields{
"key": key,
"data": data,
"err": err,
}).Error("Cannot convert server to json")
return
}
// TODO(xutao) retry
err = kv.Put(key, value, nil)
if err != nil {
log.WithFields(logrus.Fields{
"key": key,
"value": data,
"err": err,
}).Error("Cannot put tinydns fqdns")
return
}
}