/
etcd.go
231 lines (208 loc) · 5.14 KB
/
etcd.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// 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 etcd provides the default SkyDNS server Backend implementation,
// which looks up records stored under the `/skydns` key in etcd when queried.
package etcd
import (
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/coreos/go-etcd/etcd"
"github.com/skynetservices/skydns/msg"
)
// Config represents configuration for the Etcd backend - these values
// should be taken directly from server.Config
type Config struct {
Ttl uint32
Priority uint16
}
type Backend struct {
client *etcd.Client
config *Config
inflight *etcdSingle
}
// NewBackend returns a new Backend for SkyDNS, backed by etcd.
func NewBackend(client *etcd.Client, config *Config) *Backend {
return &Backend{
client: client,
config: config,
inflight: new(etcdSingle),
}
}
func (g *Backend) Records(name string, exact bool) ([]msg.Service, error) {
path, star := msg.PathWithWildcard(name)
r, err := g.get(path, true)
if err != nil {
return nil, err
}
segments := strings.Split(msg.Path(name), "/")
switch {
case exact && r.Node.Dir:
return nil, nil
case r.Node.Dir:
return g.loopNodes(&r.Node.Nodes, segments, star, nil)
default:
return g.loopNodes(&etcd.Nodes{r.Node}, segments, false, nil)
}
}
func (g *Backend) ReverseRecord(name string) (*msg.Service, error) {
path, star := msg.PathWithWildcard(name)
if star {
return nil, fmt.Errorf("reverse can not contain wildcards")
}
r, err := g.get(path, true)
if err != nil {
return nil, err
}
if r.Node.Dir {
return nil, fmt.Errorf("reverse must not be a directory")
}
segments := strings.Split(msg.Path(name), "/")
records, err := g.loopNodes(&etcd.Nodes{r.Node}, segments, false, nil)
if err != nil {
return nil, err
}
if len(records) != 1 {
return nil, fmt.Errorf("must be only one service record")
}
return &records[0], nil
}
// get is a wrapper for client.Get that uses SingleInflight to suppress multiple
// outstanding queries.
func (g *Backend) get(path string, recursive bool) (*etcd.Response, error) {
resp, err, _ := g.inflight.Do(path, func() (*etcd.Response, error) {
r, e := g.client.Get(path, false, recursive)
if e != nil {
return nil, e
}
return r, e
})
if err != nil {
return resp, err
}
// shared?
return resp, err
}
type bareService struct {
Host string
Port int
Priority int
Weight int
Text string
}
// skydns/local/skydns/east/staging/web
// skydns/local/skydns/west/production/web
//
// skydns/local/skydns/*/*/web
// skydns/local/skydns/*/web
// loopNodes recursively loops through the nodes and returns all the values. The nodes' keyname
// will be match against any wildcards when star is true.
func (g *Backend) loopNodes(n *etcd.Nodes, nameParts []string, star bool, bx map[bareService]bool) (sx []msg.Service, err error) {
if bx == nil {
bx = make(map[bareService]bool)
}
Nodes:
for _, n := range *n {
if n.Dir {
nodes, err := g.loopNodes(&n.Nodes, nameParts, star, bx)
if err != nil {
return nil, err
}
sx = append(sx, nodes...)
continue
}
if star {
keyParts := strings.Split(n.Key, "/")
for i, n := range nameParts {
if i > len(keyParts)-1 {
// name is longer than key
continue Nodes
}
if n == "*" || n == "any" {
continue
}
if keyParts[i] != n {
continue Nodes
}
}
}
serv := new(msg.Service)
if err := json.Unmarshal([]byte(n.Value), serv); err != nil {
return nil, err
}
b := bareService{serv.Host, serv.Port, serv.Priority, serv.Weight, serv.Text}
if _, ok := bx[b]; ok {
continue
}
bx[b] = true
serv.Key = n.Key
serv.Ttl = g.calculateTtl(n, serv)
if serv.Priority == 0 {
serv.Priority = int(g.config.Priority)
}
sx = append(sx, *serv)
}
return sx, nil
}
// calculateTtl returns the smaller of the etcd TTL and the service's
// TTL. If neither of these are set (have a zero value), the server
// default is used.
func (g *Backend) calculateTtl(node *etcd.Node, serv *msg.Service) uint32 {
etcdTtl := uint32(node.TTL)
if etcdTtl == 0 && serv.Ttl == 0 {
return g.config.Ttl
}
if etcdTtl == 0 {
return serv.Ttl
}
if serv.Ttl == 0 {
return etcdTtl
}
if etcdTtl < serv.Ttl {
return etcdTtl
}
return serv.Ttl
}
// Client exposes the underlying Etcd client.
func (g *Backend) Client() *etcd.Client {
return g.client
}
// UpdateClient allows the etcd client used by this backend
// to be replaced on the fly.
func (g *Backend) UpdateClient(client *etcd.Client) {
g.client = client
}
type etcdCall struct {
wg sync.WaitGroup
val *etcd.Response
err error
dups int
}
type etcdSingle struct {
sync.Mutex
m map[string]*etcdCall
}
func (g *etcdSingle) Do(key string, fn func() (*etcd.Response, error)) (*etcd.Response, error, bool) {
g.Lock()
if g.m == nil {
g.m = make(map[string]*etcdCall)
}
if c, ok := g.m[key]; ok {
c.dups++
g.Unlock()
c.wg.Wait()
return c.val, c.err, true
}
c := new(etcdCall)
c.wg.Add(1)
g.m[key] = c
g.Unlock()
c.val, c.err = fn()
c.wg.Done()
g.Lock()
delete(g.m, key)
g.Unlock()
return c.val, c.err, c.dups > 0
}