forked from unRob/coredns-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
185 lines (156 loc) · 4.48 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
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
// Copyright © 2022 Roberto Hidalgo <coredns-consul@un.rob.mx>
// SPDX-License-Identifier: Apache-2.0
package catalog
import (
"net"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
var Log = clog.NewWithPlugin("consul_catalog")
func init() {
plugin.Register("consul_catalog", setup)
}
func setup(c *caddy.Controller) error {
catalog, err := parse(c)
if err != nil {
return plugin.Error("Failed to parse", err)
}
config := dnsserver.GetConfig(c)
config.AddPlugin(func(next plugin.Handler) plugin.Handler {
catalog.Next = next
catalog.Zone = config.Zone
return catalog
})
onUpdateError := func(err error, cooldown time.Duration) {
Log.Errorf("Could not obtain services from catalog, retrying in %vs: %v", cooldown.Truncate(time.Second), err)
}
onUpdateKVError := func(err error, cooldown time.Duration) {
Log.Errorf("Could not obtain config from consul, retrying in %vs: %v", cooldown.Truncate(time.Second), err)
}
c.OnStartup(func() error {
Log.Infof("Starting consul catalog watch at %s", catalog.Endpoint)
go func() {
// poll for changes, backing off in case of failures
for {
Log.Debug("Waiting for consul catalog services...")
err := backoff.RetryNotify(catalog.FetchServices, backoff.NewExponentialBackOff(), onUpdateError)
if err != nil {
Log.Error(plugin.Error("Failed to obtain services from catalog", err))
continue
}
}
}()
if catalog.ConfigKey != "" {
Log.Infof("Starting consul kv watch at %s/kv/%s", catalog.Endpoint, catalog.ConfigKey)
go func() {
// poll for changes, backing off in case of failures
for {
Log.Debug("Waiting for consul kv config...")
err := backoff.RetryNotify(catalog.FetchConfig, backoff.NewExponentialBackOff(), onUpdateKVError)
if err != nil {
Log.Error(plugin.Error("Failed to obtain kv config", err))
continue
}
}
}()
}
return nil
})
return nil
}
func parse(c *caddy.Controller) (cc *Catalog, err error) {
cc = New()
token := ""
networks := map[string]*net.IPNet{}
for c.Next() {
tags := c.RemainingArgs()
if len(tags) > 0 {
cc.Tag = tags[0]
}
for c.NextBlock() {
switch c.Val() {
case "endpoint":
if !c.NextArg() {
return nil, c.ArgErr()
}
cc.Endpoint = c.Val()
case "scheme":
if !c.NextArg() {
return nil, c.ArgErr()
}
cc.Scheme = c.Val()
case "token":
if !c.NextArg() {
return nil, c.ArgErr()
}
token = c.Val()
case "ttl":
if !c.NextArg() {
return nil, c.ArgErr()
}
ttl, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.Errf("Could not parse ttl as golang duration: %v", err)
}
cc.TTL = uint32(ttl.Seconds())
case "acl_metadata_tag":
if !c.NextArg() {
return nil, c.ArgErr()
}
cc.MetadataTag = c.Val()
case "acl_zone":
remaining := c.RemainingArgs()
if len(remaining) < 1 {
return nil, c.Errf("must supply a name and cidr range for acl_zone")
}
name := remaining[0]
_, network, err := net.ParseCIDR(remaining[1])
if err != nil {
return nil, c.Errf("unable to parse network range <%s>", remaining[1])
}
networks[name] = network
case "acl_ignore_tag":
if !c.NextArg() {
return nil, c.ArgErr()
}
cc.ACLIgnoreTag = c.Val()
Log.Debugf("Found ACL-ignore tag config for tag %s", cc.ACLIgnoreTag)
case "service_proxy":
remaining := c.RemainingArgs()
if len(remaining) < 1 {
return nil, c.Errf("service_proxy needs a tag and service")
}
cc.ProxyTag = remaining[0]
cc.ProxyService = remaining[1]
Log.Debugf("Found proxy config for tag %s and service %s", cc.ProxyTag, cc.ProxyService)
case "alias_tag":
if !c.NextArg() {
return nil, c.ArgErr()
}
cc.AliasTag = c.Val()
Log.Debugf("Found alias tag config for tag %s", cc.AliasTag)
case "config_kv_path":
if !c.NextArg() {
return nil, c.ArgErr()
}
cc.ConfigKey = c.Val()
default:
return nil, c.Errf("unknown property %q", c.Val())
}
}
}
cc.Networks = networks
catalogClient, kvClient, err := CreateClient(cc.Scheme, cc.Endpoint, token)
if err != nil {
return nil, c.Errf("Could not create consul client: %v", err)
}
cc.SetClients(catalogClient, kvClient)
for _, server := range c.ServerBlockKeys {
cc.FQDN = append(cc.FQDN, plugin.Host(server).Normalize())
}
return cc, nil
}