-
Notifications
You must be signed in to change notification settings - Fork 1
/
catalog.go
161 lines (142 loc) · 3.59 KB
/
catalog.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
// Copyright © 2022 Roberto Hidalgo <[email protected]>
// Contributions by Charles Powell, 2023
// SPDX-License-Identifier: Apache-2.0
package catalog
import (
"context"
"fmt"
"net"
"regexp"
"strings"
"sync"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
var defaultTag = "coredns.enabled"
var defaultEndpoint = "consul.service.consul:8500"
var defaultTTL = uint32((5 * time.Minute).Seconds())
var defaultACLTag = "coredns-acl"
var defaultAliasTag = "coredns-alias"
var DefaultLookup = func(ctx context.Context, state request.Request, target string) (*dns.Msg, error) {
recursor := upstream.New()
req := state.NewWithQuestion(target, dns.TypeA)
return recursor.Lookup(ctx, req, target, dns.TypeA)
}
// Catalog holds published Consul Catalog services.
type Catalog struct {
sync.RWMutex
Endpoint string
Scheme string
FQDN []string
TTL uint32
Token string
ProxyService string
ProxyTag string
Networks map[string][]*net.IPNet
ACLTag string
AliasTag string
Next plugin.Handler
Zone string
lastUpdate time.Time
client Client
kv KVClient
Sources []*Watch
metrics *metrics.Metrics
}
// New returns a Catalog plugin.
func New() *Catalog {
return &Catalog{
Endpoint: defaultEndpoint,
Scheme: "http",
TTL: defaultTTL,
ACLTag: defaultACLTag,
AliasTag: defaultAliasTag,
Sources: []*Watch{},
}
}
// SetClient sets a consul client for a catalog.
func (c *Catalog) SetClients(client Client, kv KVClient) {
c.client = client
c.kv = kv
}
// Ready implements ready.Readiness.
func (c *Catalog) Ready() bool {
return c.client != nil && c.kv != nil
}
// LastUpdated returns the last time services changed.
func (c *Catalog) LastUpdated() time.Time {
return c.lastUpdate
}
// Services returns a map of services to their target.
func (c *Catalog) Services() ServiceMap {
m := ServiceMap{}
for _, src := range c.Sources {
for n, s := range src.Known() {
if _, ok := m[n]; ok {
Log.Warningf("Repeated service named %s from %s", n, src.Name())
continue
}
m[n] = s
}
}
return m
}
// Name implements plugin.Handler.
func (c *Catalog) Name() string { return "consul_catalog" }
func (c *Catalog) ServiceFor(name string) *Service {
c.RLock()
defer c.RUnlock()
for _, src := range c.Sources {
if svc := src.Get(name); svc != nil {
return svc
}
}
return nil
}
func (c *Catalog) ReloadAll() error {
didUpdate := false
for _, src := range c.Sources {
changed, err := src.Resolve(c)
if err != nil {
return err
}
if changed {
didUpdate = true
}
}
if didUpdate {
c.Lock()
c.lastUpdate = time.Now()
c.Unlock()
}
return nil
}
func (c *Catalog) parseACLString(svc *Service, acl string) error {
aclRules := regexp.MustCompile(`;\s*`).Split(acl, -1)
return c.parseACL(svc, aclRules)
}
func (c *Catalog) parseACL(svc *Service, rules []string) error {
Log.Debugf("Parsing ACL for %s: %s", svc.Name, rules)
for _, rule := range rules {
ruleParts := strings.SplitN(rule, " ", 2)
if len(ruleParts) != 2 {
return fmt.Errorf("could not parse acl rule <%s>", rule)
}
action := ruleParts[0]
for _, networkName := range regexp.MustCompile(`,\s*`).Split(ruleParts[1], -1) {
if ranges, ok := c.Networks[networkName]; ok {
svc.ACL = append(svc.ACL, &ServiceACL{
Action: action,
Networks: ranges,
})
} else {
return fmt.Errorf("unknown network %s", networkName)
}
}
}
return nil
}