-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdockerdiscovery.go
346 lines (296 loc) · 10.7 KB
/
dockerdiscovery.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package dockerdiscovery
import (
"context"
"errors"
"fmt"
"log"
"net"
"strings"
"sync"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/request"
dockerapi "github.com/fsouza/go-dockerclient"
"github.com/miekg/dns"
)
type ContainerInfo struct {
container *dockerapi.Container
address net.IP
address6 net.IP
domains []string // resolved domain
}
type ContainerInfoMap map[string]*ContainerInfo
type ContainerDomainResolver interface {
// return domains without trailing dot
resolve(container *dockerapi.Container) ([]string, error)
}
// DockerDiscovery is a plugin that conforms to the coredns plugin interface
type DockerDiscovery struct {
Next plugin.Handler
dockerEndpoint string
resolvers []ContainerDomainResolver
dockerClient *dockerapi.Client
mutex sync.RWMutex
containerInfoMap ContainerInfoMap
ttl uint32
}
// NewDockerDiscovery constructs a new DockerDiscovery object
func NewDockerDiscovery(dockerEndpoint string) *DockerDiscovery {
return &DockerDiscovery{
dockerEndpoint: dockerEndpoint,
containerInfoMap: make(ContainerInfoMap),
ttl: 3600,
}
}
func (dd *DockerDiscovery) resolveDomainsByContainer(container *dockerapi.Container) ([]string, error) {
var domains []string
for _, resolver := range dd.resolvers {
var d, err = resolver.resolve(container)
if err != nil {
log.Printf("[docker] Error resolving container domains %s", err)
}
domains = append(domains, d...)
}
return domains, nil
}
func (dd *DockerDiscovery) containerInfoByDomain(requestName string) (*ContainerInfo, error) {
dd.mutex.RLock()
defer dd.mutex.RUnlock()
for _, containerInfo := range dd.containerInfoMap {
for _, d := range containerInfo.domains {
if fmt.Sprintf("%s.", d) == requestName { // qualified domain name must be specified with a trailing dot
return containerInfo, nil
}
}
}
return nil, nil
}
// ServeDNS implements plugin.Handler
func (dd *DockerDiscovery) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
var answers []dns.RR
switch state.QType() {
case dns.TypeA:
containerInfo, _ := dd.containerInfoByDomain(state.QName())
if containerInfo != nil {
answers = getAnswer(state.Name(), []net.IP{containerInfo.address}, dd.ttl, false)
}
case dns.TypeAAAA:
containerInfo, _ := dd.containerInfoByDomain(state.QName())
if containerInfo != nil && containerInfo.address6 != nil {
answers = getAnswer(state.Name(), []net.IP{containerInfo.address6}, dd.ttl, true)
} else if containerInfo != nil && containerInfo.address != nil {
// in acordance with https://tools.ietf.org/html/rfc6147#section-5.1.2 we should return an empty answer section if no AAAA records are available and a A record is available when the client requested AAAA
record := new(dns.AAAA)
record.Hdr = dns.RR_Header{
Name: state.Name(),
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: dd.ttl,
Rdlength: 0,
}
answers = append(answers, record)
}
}
if len(answers) == 0 {
return plugin.NextOrFailure(dd.Name(), dd.Next, ctx, w, r)
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative, m.RecursionAvailable, m.Compress = true, false, true
m.Answer = answers
state.SizeAndDo(m)
m = state.Scrub(m)
err := w.WriteMsg(m)
if err != nil {
log.Printf("[docker] Error: %s", err.Error())
}
return dns.RcodeSuccess, nil
}
// Name implements plugin.Handler
func (dd *DockerDiscovery) Name() string {
return "docker"
}
func (dd *DockerDiscovery) getContainerAddress(container *dockerapi.Container, v6 bool) (net.IP, error) {
// save this away
netName, hasNetName := container.Config.Labels["coredns.dockerdiscovery.network"]
var networkMode string
for {
if container.NetworkSettings.IPAddress != "" && !hasNetName && !v6 {
return net.ParseIP(container.NetworkSettings.IPAddress), nil
}
if container.NetworkSettings.GlobalIPv6Address != "" && !hasNetName && v6 {
return net.ParseIP(container.NetworkSettings.GlobalIPv6Address), nil
}
networkMode = container.HostConfig.NetworkMode
// TODO: Deal with containers run with host ip (--net=host)
if networkMode == "host" {
log.Println("[docker] Container uses host network")
return nil, nil
}
if strings.HasPrefix(networkMode, "container:") {
log.Printf("Container %s is in another container's network namspace", container.ID[:12])
otherID := container.HostConfig.NetworkMode[len("container:"):]
var err error
container, err = dd.dockerClient.InspectContainerWithOptions(dockerapi.InspectContainerOptions{ID: otherID})
if err != nil {
return nil, err
}
} else {
break
}
}
var (
network dockerapi.ContainerNetwork
ok = false
)
if hasNetName {
log.Printf("[docker] network name %s specified (%s)", netName, container.ID[:12])
network, ok = container.NetworkSettings.Networks[netName]
} else if len(container.NetworkSettings.Networks) == 1 {
for netName, network = range container.NetworkSettings.Networks {
ok = true
}
}
if !ok { // sometime while "network:disconnect" event fire
return nil, fmt.Errorf("unable to find network settings for the network %s", networkMode)
}
if !v6 {
return net.ParseIP(network.IPAddress), nil // ParseIP return nil when IPAddress equals ""
} else if v6 && len(network.GlobalIPv6Address) > 0 {
return net.ParseIP(network.GlobalIPv6Address), nil
}
return nil, nil
}
func (dd *DockerDiscovery) updateContainerInfo(container *dockerapi.Container) error {
dd.mutex.Lock()
defer dd.mutex.Unlock()
_, isExist := dd.containerInfoMap[container.ID]
if isExist { // remove previous resolved container info
delete(dd.containerInfoMap, container.ID)
}
containerAddress, err := dd.getContainerAddress(container, false)
if err != nil || containerAddress == nil {
log.Printf("[docker] Remove container entry %s (%s)", normalizeContainerName(container), container.ID[:12])
return err
}
containerAddress6, err := dd.getContainerAddress(container, true)
domains, _ := dd.resolveDomainsByContainer(container)
if len(domains) > 0 {
dd.containerInfoMap[container.ID] = &ContainerInfo{
container: container,
address: containerAddress,
address6: containerAddress6,
domains: domains,
}
if !isExist {
log.Printf("[docker] Add entry of container %s (%s). IP: %v", normalizeContainerName(container), container.ID[:12], containerAddress)
}
} else if isExist {
log.Printf("[docker] Remove container entry %s (%s)", normalizeContainerName(container), container.ID[:12])
}
return nil
}
func (dd *DockerDiscovery) removeContainerInfo(containerID string) error {
dd.mutex.Lock()
defer dd.mutex.Unlock()
containerInfo, ok := dd.containerInfoMap[containerID]
if !ok {
log.Printf("[docker] No entry associated with the container %s", containerID[:12])
return nil
}
log.Printf("[docker] Deleting entry %s (%s)", normalizeContainerName(containerInfo.container), containerInfo.container.ID[:12])
delete(dd.containerInfoMap, containerID)
return nil
}
func (dd *DockerDiscovery) start() error {
log.Println("[docker] start")
events := make(chan *dockerapi.APIEvents)
if err := dd.dockerClient.AddEventListener(events); err != nil {
return err
}
containers, err := dd.dockerClient.ListContainers(dockerapi.ListContainersOptions{})
if err != nil {
return err
}
for _, apiContainer := range containers {
container, err := dd.dockerClient.InspectContainerWithOptions(dockerapi.InspectContainerOptions{ID: apiContainer.ID})
if err != nil {
// TODO err
}
if err := dd.updateContainerInfo(container); err != nil {
log.Printf("[docker] Error adding A/AAAA records for container %s: %s\n", container.ID[:12], err)
}
}
for msg := range events {
go func(msg *dockerapi.APIEvents) {
event := fmt.Sprintf("%s:%s", msg.Type, msg.Action)
switch event {
case "container:start":
log.Println("[docker] New container spawned. Attempt to add A/AAAA records for it")
container, err := dd.dockerClient.InspectContainerWithOptions(dockerapi.InspectContainerOptions{ID: msg.Actor.ID})
if err != nil {
log.Printf("[docker] Event error %s #%s: %s", event, msg.Actor.ID[:12], err)
return
}
if err := dd.updateContainerInfo(container); err != nil {
log.Printf("[docker] Error adding A/AAAA records for container %s: %s", container.ID[:12], err)
}
case "container:die":
log.Println("[docker] Container being stopped. Attempt to remove its A/AAAA records from the DNS", msg.Actor.ID[:12])
if err := dd.removeContainerInfo(msg.Actor.ID); err != nil {
log.Printf("[docker] Error deleting A/AAAA records for container: %s: %s", msg.Actor.ID[:12], err)
}
case "network:connect":
// take a look https://gist.github.com/josefkarasek/be9bac36921f7bc9a61df23451594fbf for example of same event's types attributes
log.Printf("[docker] Container %s being connected to network %s.", msg.Actor.Attributes["container"][:12], msg.Actor.Attributes["name"])
container, err := dd.dockerClient.InspectContainerWithOptions(dockerapi.InspectContainerOptions{ID: msg.Actor.Attributes["container"]})
if err != nil {
log.Printf("[docker] Event error %s #%s: %s", event, msg.Actor.Attributes["container"][:12], err)
return
}
if err := dd.updateContainerInfo(container); err != nil {
log.Printf("[docker] Error adding A/AAAA records for container %s: %s", container.ID[:12], err)
}
case "network:disconnect":
log.Printf("[docker] Container %s being disconnected from network %s", msg.Actor.Attributes["container"][:12], msg.Actor.Attributes["name"])
container, err := dd.dockerClient.InspectContainerWithOptions(dockerapi.InspectContainerOptions{ID: msg.Actor.Attributes["container"]})
if err != nil {
log.Printf("[docker] Event error %s #%s: %s", event, msg.Actor.Attributes["container"][:12], err)
return
}
if err := dd.updateContainerInfo(container); err != nil {
log.Printf("[docker] Error adding A/AAAA records for container %s: %s", container.ID[:12], err)
}
}
}(msg)
}
return errors.New("docker event loop closed")
}
// getAnswer function takes a slice of net.IPs and returns a slice of A/AAAA RRs.
func getAnswer(zone string, ips []net.IP, ttl uint32, v6 bool) []dns.RR {
answers := []dns.RR{}
for _, ip := range ips {
if !v6 {
record := new(dns.A)
record.Hdr = dns.RR_Header{
Name: zone,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: ttl,
}
record.A = ip
answers = append(answers, record)
} else if v6 {
record := new(dns.AAAA)
record.Hdr = dns.RR_Header{
Name: zone,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: ttl,
}
record.AAAA = ip
answers = append(answers, record)
}
}
return answers
}