-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.go
More file actions
190 lines (166 loc) · 4.15 KB
/
setup.go
File metadata and controls
190 lines (166 loc) · 4.15 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
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
package etcdhosts
import (
"context"
"strconv"
"strings"
"time"
"go.etcd.io/etcd/clientv3"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
mwtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/caddy"
)
var log = clog.NewWithPlugin("etcdhosts")
func init() { plugin.Register("etcdhosts", setup) }
func periodicHostsUpdate(h *Hosts) chan bool {
parseChan := make(chan bool)
go func() {
watchCh := h.etcdClient.Watch(context.Background(), h.etcdHostsKey)
for {
select {
case <-parseChan:
return
case <-watchCh:
log.Info("etcdhosts reloading...")
h.readHosts()
}
}
}()
return parseChan
}
func setup(c *caddy.Controller) error {
h, err := hostsParse(c)
if err != nil {
return plugin.Error("etcdhosts", err)
}
parseChan := periodicHostsUpdate(&h)
c.OnStartup(func() error {
h.readHosts()
return nil
})
c.OnShutdown(func() error {
close(parseChan)
_ = h.etcdClient.Close()
return nil
})
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
h.Next = next
return h
})
return nil
}
func hostsParse(c *caddy.Controller) (Hosts, error) {
h := Hosts{
Hostsfile: &Hostsfile{
hmap: newMap(),
inline: newMap(),
options: newOptions(),
},
}
var inline []string
i := 0
for c.Next() {
if i > 0 {
return h, plugin.ErrOnce
}
i++
origins := make([]string, len(c.ServerBlockKeys))
copy(origins, c.ServerBlockKeys)
args := c.RemainingArgs()
if len(args) > 0 {
origins = args
}
for i := range origins {
origins[i] = plugin.Host(origins[i]).Normalize()
}
h.Origins = origins
for c.NextBlock() {
switch c.Val() {
case "fallthrough":
h.Fall.SetZonesFromArgs(c.RemainingArgs())
case "no_reverse":
h.options.autoReverse = false
case "ttl":
remaining := c.RemainingArgs()
if len(remaining) < 1 {
return h, c.Errf("ttl needs a time in second")
}
ttl, err := strconv.Atoi(remaining[0])
if err != nil {
return h, c.Errf("ttl needs a number of second")
}
if ttl <= 0 || ttl > 65535 {
return h, c.Errf("ttl provided is invalid")
}
h.options.ttl = uint32(ttl)
case "tls":
remaining := c.RemainingArgs()
tlsConfig, err := mwtls.NewTLSConfigFromArgs(remaining...)
if err != nil {
return h, c.Errf("failed to load etcd tls config: %s", err.Error())
}
h.etcdTLSConfig = tlsConfig
case "endpoint":
remaining := c.RemainingArgs()
if len(remaining) == 0 {
return h, c.ArgErr()
}
h.etcdEndpoints = remaining
case "timeout":
remaining := c.RemainingArgs()
if len(remaining) != 1 {
return h, c.Errf("etcd client timeout needs a duration")
}
timeout, err := time.ParseDuration(remaining[0])
if err != nil {
return h, c.Errf("invalid duration for etcd client timeout '%s'", remaining[0])
}
h.etcdTimeout = timeout
case "key":
remaining := c.RemainingArgs()
if len(remaining) != 1 {
return h, c.Errf("etcd hosts key needs a string")
}
h.etcdHostsKey = remaining[0]
case "credentials":
remaining := c.RemainingArgs()
if len(remaining) == 0 {
return h, c.ArgErr()
}
if len(remaining) != 2 {
return h, c.Errf("credentials requires 2 arguments, username and password")
}
h.etcdUserName, h.etcdPassword = remaining[0], remaining[1]
default:
if len(h.Fall.Zones) == 0 {
line := strings.Join(append([]string{c.Val()}, c.RemainingArgs()...), " ")
inline = append(inline, line)
continue
}
return h, c.Errf("unknown property '%s'", c.Val())
}
}
}
// default etcd key
if h.etcdHostsKey == "" {
h.etcdHostsKey = "/etcdhosts"
}
// default etcd client timeout
if h.etcdTimeout == 0 {
h.etcdTimeout = 3 * time.Second
}
cli, err := clientv3.New(clientv3.Config{
Username: h.etcdUserName,
Password: h.etcdPassword,
Endpoints: h.etcdEndpoints,
DialTimeout: 5 * time.Second,
TLS: h.etcdTLSConfig,
})
if err != nil {
return h, c.Errf("failed to create etcd client: %s", err.Error())
}
h.etcdClient = cli
h.initInline(inline)
return h, nil
}