-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
271 lines (228 loc) · 6.21 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
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
package main
import (
"errors"
"fmt"
"log"
"net"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/miekg/dns"
"github.com/gliderlabs/resolvable/resolver"
dockerapi "github.com/fsouza/go-dockerclient"
)
var Version string
func getopt(name, def string) string {
if env := os.Getenv(name); env != "" {
return env
}
return def
}
func ipAddress() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && !ipnet.IP.IsMulticast() {
if ipv4 := ipnet.IP.To4(); ipv4 != nil {
return ipv4.String(), nil
}
}
}
return "", errors.New("no addresses found")
}
func parseContainerEnv(containerEnv []string, prefix string) map[string]string {
parsed := make(map[string]string)
for _, env := range containerEnv {
if !strings.HasPrefix(env, prefix) {
continue
}
keyVal := strings.SplitN(env, "=", 2)
if len(keyVal) > 1 {
parsed[keyVal[0]] = keyVal[1]
} else {
parsed[keyVal[0]] = ""
}
}
return parsed
}
func registerContainers(docker *dockerapi.Client, events chan *dockerapi.APIEvents, dns resolver.Resolver, containerDomain string, hostIP net.IP) error {
// TODO add an options struct instead of passing all as parameters
// though passing the events channel from an options struct was triggering
// data race warnings within AddEventListener, so needs more investigation
if events == nil {
events = make(chan *dockerapi.APIEvents)
}
if err := docker.AddEventListener(events); err != nil {
return err
}
if !strings.HasPrefix(containerDomain, ".") {
containerDomain = "." + containerDomain
}
getAddress := func(container *dockerapi.Container) (net.IP, error) {
for {
if container.NetworkSettings.IPAddress != "" {
return net.ParseIP(container.NetworkSettings.IPAddress), nil
}
if container.HostConfig.NetworkMode == "host" {
if hostIP == nil {
return nil, errors.New("IP not available with network mode \"host\"")
} else {
return hostIP, nil
}
}
if strings.HasPrefix(container.HostConfig.NetworkMode, "container:") {
otherId := container.HostConfig.NetworkMode[len("container:"):]
var err error
container, err = docker.InspectContainer(otherId)
if err != nil {
return nil, err
}
continue
}
return nil, fmt.Errorf("unknown network mode", container.HostConfig.NetworkMode)
}
}
addContainer := func(containerId string) error {
container, err := docker.InspectContainer(containerId)
if err != nil {
return err
}
addr, err := getAddress(container)
if err != nil {
return err
}
err = dns.AddHost(containerId, addr, container.Config.Hostname, container.Name[1:]+containerDomain)
if err != nil {
return err
}
env := parseContainerEnv(container.Config.Env, "DNS_")
if dnsDomains, ok := env["DNS_RESOLVES"]; ok {
if dnsDomains == "" {
return errors.New("empty DNS_RESOLVES, should contain a comma-separated list with at least one domain")
}
port := 53
if portString := env["DNS_PORT"]; portString != "" {
port, err = strconv.Atoi(portString)
if err != nil {
return errors.New("invalid DNS_PORT \"" + portString + "\", should contain a number")
}
}
domains := strings.Split(dnsDomains, ",")
err = dns.AddUpstream(containerId, addr, port, domains...)
if err != nil {
return err
}
}
if bridge := container.NetworkSettings.Bridge; bridge != "" {
bridgeAddr := net.ParseIP(container.NetworkSettings.Gateway)
err = dns.AddHost("bridge:"+bridge, bridgeAddr, bridge)
if err != nil {
return err
}
}
return nil
}
containers, err := docker.ListContainers(dockerapi.ListContainersOptions{})
if err != nil {
return err
}
for _, listing := range containers {
if err := addContainer(listing.ID); err != nil {
log.Printf("error adding container %s: %s\n", listing.ID[:12], err)
}
}
if err = dns.Listen(); err != nil {
return err
}
defer dns.Close()
for msg := range events {
go func(msg *dockerapi.APIEvents) {
switch msg.Status {
case "start":
if err := addContainer(msg.ID); err != nil {
log.Printf("error adding container %s: %s\n", msg.ID[:12], err)
}
case "die":
dns.RemoveHost(msg.ID)
dns.RemoveUpstream(msg.ID)
}
}(msg)
}
return errors.New("docker event loop closed")
}
func run() error {
// set up the signal handler first to ensure cleanup is handled if a signal is
// caught while initializing
exitReason := make(chan error)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
sig := <-c
log.Println("exit requested by signal:", sig)
exitReason <- nil
}()
docker, err := dockerapi.NewClient(getopt("DOCKER_HOST", "unix:///tmp/docker.sock"))
if err != nil {
return err
}
address, err := ipAddress()
if err != nil {
return err
}
log.Println("got local address:", address)
for name, conf := range resolver.HostResolverConfigs.All() {
err := conf.StoreAddress(address)
if err != nil {
log.Printf("[ERROR] error in %s: %s", name, err)
}
defer conf.Clean()
}
var hostIP net.IP
if envHostIP := os.Getenv("HOST_IP"); envHostIP != "" {
hostIP = net.ParseIP(envHostIP)
log.Println("using address for --net=host:", hostIP)
}
dnsResolver, err := resolver.NewResolver()
if err != nil {
return err
}
defer dnsResolver.Close()
localDomain := "docker"
dnsResolver.AddUpstream(localDomain, nil, 0, localDomain)
resolvConfig, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return err
}
resolvConfigPort, err := strconv.Atoi(resolvConfig.Port)
if err != nil {
return err
}
for _, server := range resolvConfig.Servers {
if server != address {
dnsResolver.AddUpstream("resolv.conf:"+server, net.ParseIP(server), resolvConfigPort)
}
}
go func() {
dnsResolver.Wait()
exitReason <- errors.New("dns resolver exited")
}()
go func() {
exitReason <- registerContainers(docker, nil, dnsResolver, localDomain, hostIP)
}()
return <-exitReason
}
func main() {
if len(os.Args) == 2 && os.Args[1] == "--version" {
fmt.Println(Version)
os.Exit(0)
}
log.Printf("Starting resolvable %s ...", Version)
err := run()
if err != nil {
log.Fatal("resolvable: ", err)
}
}