-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
190 lines (163 loc) · 4.62 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
186
187
188
189
190
// Copyright © 2022 Roberto Hidalgo <[email protected]>
// Contributions by Charles Powell, 2023
// SPDX-License-Identifier: Apache-2.0
package catalog
import (
"net"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
var pluginName = "consul_catalog"
var Log = clog.NewWithPlugin(pluginName)
func init() { plugin.Register(pluginName, 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
})
c.OnStartup(func() error {
Log.Infof("Starting %d consul catalog watches for %s", len(catalog.Sources), catalog.Endpoint)
m := dnsserver.GetConfig(c).Handler("prometheus")
if m != nil {
catalog.metrics = m.(*metrics.Metrics)
}
for _, watch := range catalog.Sources {
go func(w *Watch) {
Log.Infof("Starting lookup for %s", w.Name())
for {
Log.Debugf("Looking up %s", w.Name())
onUpdateError := func(err error, cooldown time.Duration) {
Log.Errorf("Could not lookup %s, retrying in %vs: %v", w.Name(), cooldown.Truncate(time.Second), err)
}
err := backoff.RetryNotify(func() error {
_, err := w.Resolve(catalog)
return err
}, backoff.NewExponentialBackOff(), onUpdateError)
if err != nil {
continue
}
catalog.Lock()
catalog.lastUpdate = time.Now()
catalog.Unlock()
}
}(watch)
}
return nil
})
return nil
}
func parse(c *caddy.Controller) (cc *Catalog, err error) { // nolint: gocyclo
cc = New()
token := ""
networks := map[string][]*net.IPNet{}
tag := defaultTag
for c.Next() {
tags := c.RemainingArgs()
if len(tags) > 0 {
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.ACLTag = 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")
}
zoneName := remaining[0]
networks[zoneName] = []*net.IPNet{}
for idx, netRange := range remaining {
if idx == 0 {
continue
}
_, network, err := net.ParseCIDR(netRange)
if err != nil {
return nil, c.Errf("unable to parse network range <%s> of acl zone <%s>", netRange, zoneName)
}
networks[zoneName] = append(networks[zoneName], network)
}
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_metadata_tag":
if !c.NextArg() {
return nil, c.ArgErr()
}
cc.AliasTag = c.Val()
case "static_entries_path":
if !c.NextArg() {
return nil, c.ArgErr()
}
kvPath := c.Val()
watcher := NewWatch(&WatchKVPath{Key: kvPath})
cc.Sources = append(cc.Sources, watcher)
case "static_entries_prefix":
if !c.NextArg() {
return nil, c.ArgErr()
}
prefix := strings.TrimSuffix(c.Val(), "/") + "/"
watcher := NewWatch(&WatcKVPrefix{Prefix: prefix})
cc.Sources = append(cc.Sources, watcher)
default:
return nil, c.Errf("unknown property %q", c.Val())
}
}
}
// Add catalog services watcher last
cc.Sources = append(cc.Sources, NewWatch(&WatchConsulCatalog{Tag: tag}))
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).NormalizeExact()...)
}
return cc, nil
}