forked from kevinjqiu/coredns-dockerdiscovery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
98 lines (87 loc) · 2.18 KB
/
setup.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
package dockerdiscovery
import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
dockerapi "github.com/fsouza/go-dockerclient"
"github.com/caddyserver/caddy"
)
const defaultDockerEndpoint = "unix:///var/run/docker.sock"
const defaultDockerDomain = "docker.local"
func init() {
caddy.RegisterPlugin("docker", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
// TODO(kevinjqiu): add docker endpoint verification
func createPlugin(c *caddy.Controller) (DockerDiscovery, error) {
dd := NewDockerDiscovery(defaultDockerEndpoint)
labelResolver := &LabelResolver{hostLabel: "coredns.dockerdiscovery.host"}
dd.resolvers = append(dd.resolvers, labelResolver)
for c.Next() {
args := c.RemainingArgs()
if len(args) == 1 {
dd.dockerEndpoint = args[0]
}
if len(args) > 1 {
return dd, c.ArgErr()
}
for c.NextBlock() {
var value = c.Val()
switch value {
case "domain":
var resolver = &SubDomainContainerNameResolver{
domain: defaultDockerDomain,
}
dd.resolvers = append(dd.resolvers, resolver)
if !c.NextArg() {
return dd, c.ArgErr()
}
resolver.domain = c.Val()
case "hostname_domain":
var resolver = &SubDomainHostResolver{
domain: defaultDockerDomain,
}
dd.resolvers = append(dd.resolvers, resolver)
if !c.NextArg() {
return dd, c.ArgErr()
}
resolver.domain = c.Val()
case "network_aliases":
var resolver = &NetworkAliasesResolver{
network: "",
}
dd.resolvers = append(dd.resolvers, resolver)
if !c.NextArg() {
return dd, c.ArgErr()
}
resolver.network = c.Val()
case "label":
if !c.NextArg() {
return dd, c.ArgErr()
}
labelResolver.hostLabel = c.Val()
default:
return dd, c.Errf("unknown property: '%s'", c.Val())
}
}
}
dockerClient, err := dockerapi.NewClient(dd.dockerEndpoint)
if err != nil {
return dd, err
}
dd.dockerClient = dockerClient
go dd.start()
return dd, nil
}
func setup(c *caddy.Controller) error {
dd, err := createPlugin(c)
if err != nil {
return err
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
dd.Next = next
return dd
})
return nil
}