forked from skynetservices/skydns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (172 loc) · 5.98 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
// Copyright (c) 2014 The SkyDNS Authors. All rights reserved.
// Use of this source code is governed by The MIT License (MIT) that can be
// found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/miekg/dns"
"github.com/skynetservices/skydns/server"
)
var (
tlskey = ""
tlspem = ""
cacert = ""
config = &server.Config{ReadTimeout: 0, Domain: "", DnsAddr: "", DNSSEC: ""}
nameserver = ""
machine = ""
discover = false
)
func env(key, def string) string {
if x := os.Getenv(key); x != "" {
return x
}
return def
}
func init() {
flag.StringVar(&config.Domain, "domain", env("SKYDNS_DOMAIN", "skydns.local."), "domain to anchor requests to (SKYDNS_DOMAIN)")
flag.StringVar(&config.DnsAddr, "addr", env("SKYDNS_ADDR", "127.0.0.1:53"), "ip:port to bind to (SKYDNS_ADDR)")
flag.StringVar(&nameserver, "nameservers", env("SKYDNS_NAMESERVERS", ""), "nameserver address(es) to forward (non-local) queries to e.g. 8.8.8.8:53,8.8.4.4:53")
flag.StringVar(&machine, "machines", env("ETCD_MACHINES", ""), "machine address(es) running etcd")
flag.StringVar(&config.DNSSEC, "dnssec", "", "basename of DNSSEC key file e.q. Kskydns.local.+005+38250")
flag.StringVar(&config.Local, "local", "", "optional unique value for this skydns instance")
flag.StringVar(&tlskey, "tls-key", env("ETCD_TLSKEY", ""), "TLS Private Key path")
flag.StringVar(&tlspem, "tls-pem", env("ETCD_TLSPEM", ""), "X509 Certificate")
flag.StringVar(&cacert, "ca-cert", env("ECTD_CACERT", ""), "CA Certificate")
flag.DurationVar(&config.ReadTimeout, "rtimeout", 2*time.Second, "read timeout")
flag.BoolVar(&config.RoundRobin, "round-robin", true, "round robin A/AAAA replies")
flag.BoolVar(&discover, "discover", false, "discover new machines by watching /v2/_etcd/machines")
flag.BoolVar(&config.Verbose, "verbose", false, "log queries")
flag.BoolVar(&config.Systemd, "systemd", false, "bind to socket(s) activated by systemd (ignore -addr)")
// TTl
// Minttl
flag.StringVar(&config.Hostmaster, "hostmaster", "[email protected].", "hostmaster email address to use")
flag.IntVar(&config.SCache, "scache", server.SCacheCapacity, "capacity of the signature cache")
flag.IntVar(&config.RCache, "rcache", 0, "capacity of the response cache") // default to 0 for now
flag.IntVar(&config.RCacheTtl, "rcache-ttl", server.RCacheTtl, "TTL of the response cache")
}
func main() {
flag.Parse()
machines := strings.Split(machine, ",")
client := newClient(machines, tlspem, tlskey, cacert)
if nameserver != "" {
for _, hostPort := range strings.Split(nameserver, ",") {
if err := validateHostPort(hostPort); err != nil {
log.Fatalf("skydns: nameserver is invalid: %s", err)
}
config.Nameservers = append(config.Nameservers, hostPort)
}
}
if err := validateHostPort(config.DnsAddr); err != nil {
log.Fatalf("skydns: addr is invalid: %s", err)
}
config, err := server.LoadConfig(client, config)
if err != nil {
log.Fatalf("skydns: %s", err)
}
if config.Local != "" {
config.Local = dns.Fqdn(config.Local)
}
s := server.New(config, client)
if discover {
go func() {
recv := make(chan *etcd.Response)
go client.Watch("/_etcd/machines/", 0, true, recv, nil)
duration := 1 * time.Second
for {
select {
case n := <-recv:
if n != nil {
if client := updateClient(n, tlskey, tlspem, cacert); client != nil {
s.UpdateClient(client)
}
duration = 1 * time.Second // reset
} else {
// we can see an n == nil, probably when we can't connect to etcd.
log.Printf("skydns: ectd machine cluster update failed, sleeping %s + ~3s", duration)
time.Sleep(duration + (time.Duration(rand.Float32() * 3e9))) // Add some random.
duration *= 2
if duration > 32*time.Second {
duration = 32 * time.Second
}
}
}
}
}()
}
server.StatsCollect()
if err := s.Run(); err != nil {
log.Fatalf("skydns: %s", err)
}
}
func validateHostPort(hostPort string) error {
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
return err
}
if ip := net.ParseIP(host); ip == nil {
return fmt.Errorf("bad IP address: %s", host)
}
if p, _ := strconv.Atoi(port); p < 1 || p > 65535 {
return fmt.Errorf("bad port number %s", port)
}
return nil
}
func newClient(machines []string, tlsCert, tlsKey, tlsCACert string) (client *etcd.Client) {
// set default if not specified in env
if len(machines) == 1 && machines[0] == "" {
machines[0] = "http://127.0.0.1:4001"
}
if strings.HasPrefix(machines[0], "https://") {
var err error
// TODO(miek): machines is local, the rest is global, ugly.
if client, err = etcd.NewTLSClient(machines, tlsCert, tlsKey, tlsCACert); err != nil {
// TODO(miek): would be nice if this wasn't a fatal error
log.Fatalf("skydns: failure to connect: %s", err)
}
client.SyncCluster()
} else {
client = etcd.NewClient(machines)
client.SyncCluster()
}
return client
}
// updateClient updates the client with the machines found in v2/_etcd/machines.
func updateClient(resp *etcd.Response, tlsCert, tlsKey, tlsCACert string) (client *etcd.Client) {
machines := make([]string, 0, 3)
for _, m := range resp.Node.Nodes {
u, e := url.Parse(m.Value)
if e != nil {
continue
}
// etcd=bla&raft=bliep
// TODO(miek): surely there is a better way to do this
ms := strings.Split(u.String(), "&")
if len(ms) == 0 {
continue
}
if len(ms[0]) < 5 {
continue
}
machines = append(machines, ms[0][5:])
}
// When CoreOS (and thus ectd) crash this call back seems to also trigger, but we
// don't have any machines. Don't trigger this update then, as a) it
// crashes SkyDNS and b) potentially leaves with no machines to connect to.
// Keep the old ones and hope they still work and wait for another update
// in the future.
if len(machines) > 0 {
log.Printf("skydns: setting new etcd cluster to %v", machines)
return newClient(machines, tlsCert, tlsKey, tlsCACert)
}
return nil
}