-
Notifications
You must be signed in to change notification settings - Fork 301
/
discover.go
293 lines (259 loc) · 7.28 KB
/
discover.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package discover implements a gRPC discovery middleware.
package discover
import (
"context"
"fmt"
"net"
"strings"
"sync"
"time"
stderrors "errors"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"google.golang.org/grpc/attributes"
"google.golang.org/grpc/resolver"
)
var errAddress = errors.DefineInvalidArgument("address", "invalid address")
// DefaultPort appends port if target does not already have one.
func DefaultPort(target string, port int) (string, error) {
i := strings.LastIndexByte(target, ':')
if i < 0 {
return fmt.Sprintf("%s:%d", target, port), nil
}
// Check if target is an IPv6 host, i.e. [::1]:8886.
if target[0] == '[' {
end := strings.IndexByte(target, ']')
if end < 0 || end+1 != i {
return "", errAddress.New()
}
return target, nil
}
// No IPv6 hostport, so target with colon must be a hostport or IPv6.
ip := net.ParseIP(target)
if len(ip) == net.IPv6len {
return fmt.Sprintf("[%s]:%d", ip.String(), port), nil
}
return target, nil
}
var clusterRoleServices = map[ttnpb.ClusterRole]string{
ttnpb.ClusterRole_ENTITY_REGISTRY: "is",
ttnpb.ClusterRole_ACCESS: "is",
ttnpb.ClusterRole_GATEWAY_SERVER: "gs",
ttnpb.ClusterRole_NETWORK_SERVER: "ns",
ttnpb.ClusterRole_APPLICATION_SERVER: "as",
ttnpb.ClusterRole_JOIN_SERVER: "js",
ttnpb.ClusterRole_DEVICE_TEMPLATE_CONVERTER: "dtc",
ttnpb.ClusterRole_DEVICE_REPOSITORY: "dr",
ttnpb.ClusterRole_DEVICE_CLAIMING_SERVER: "dcs",
ttnpb.ClusterRole_GATEWAY_CONFIGURATION_SERVER: "gcs",
ttnpb.ClusterRole_QR_CODE_GENERATOR: "qrg",
}
// DefaultPorts is a map of the default gRPC ports, with/without TLS.
var DefaultPorts = map[bool]int{
false: 1884,
true: 8884,
}
var errRole = errors.DefineFailedPrecondition("role", "service role `{role}` not discoverable")
// Scheme returns the gRPC scheme for service discovery.
func Scheme(role ttnpb.ClusterRole) (string, error) {
svc, ok := clusterRoleServices[role]
if !ok {
return "", errRole.WithAttributes("role", strings.Title(strings.Replace(role.String(), "_", " ", -1)))
}
return fmt.Sprintf("ttn-v3-%s", svc), nil
}
// Address returns the host with service discovery gRPC scheme for the given role.
func Address(role ttnpb.ClusterRole, host string) (string, error) {
scheme, err := Scheme(role)
if err != nil {
return "", err
}
return fmt.Sprintf("%s:///%s", scheme, host), nil
}
// DefaultHTTPPorts is a map of the default HTTP ports, with/without TLS.
var DefaultHTTPPorts = map[bool]int{
false: 80,
true: 443,
}
// HTTPScheme is a map of the HTTP schemes, with/without TLS.
var HTTPScheme = map[bool]string{
false: "http",
true: "https",
}
// DefaultURL appends protocol and port if target does not already have one.
func DefaultURL(target string, port int, tls bool) (string, error) {
if port != DefaultHTTPPorts[tls] {
var err error
target, err = DefaultPort(target, port)
if err != nil {
return "", nil
}
}
return fmt.Sprintf("%s://%s", HTTPScheme[tls], target), nil
}
// DNS provides DNS lookup functionality for service discovery.
type DNS interface {
LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error)
}
type options struct {
dns DNS
}
// Option configures the discovery dialing.
type Option interface {
apply(*options)
}
type optionFunc func(*options)
func (f optionFunc) apply(opts *options) {
f(opts)
}
// WithDNS configures the resolver with a custom DNS resolver.
func WithDNS(dns DNS) Option {
return optionFunc(func(opts *options) {
opts.dns = dns
})
}
// NewBuilder returns a new resolver builder for service discovery.
func NewBuilder(scheme string, opts ...Option) resolver.Builder {
options := options{
dns: net.DefaultResolver,
}
for _, opt := range opts {
opt.apply(&options)
}
return &clusterBuilder{
scheme: scheme,
options: options,
}
}
type clusterBuilder struct {
scheme string
options options
}
func (r *clusterBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
// Use passthrough when the endpoint is an address.
if _, _, err := net.SplitHostPort(target.Endpoint); err == nil {
return resolver.Get("passthrough").Build(target, cc, opts)
}
ctx, cancel := context.WithCancel(context.Background())
service := target.Scheme + "-grpc"
res := &clusterResolver{
ctx: ctx,
cancel: cancel,
dns: r.options.dns,
service: service,
name: target.Endpoint,
cc: cc,
rn: make(chan struct{}, 1),
}
res.wg.Add(1)
go res.watch()
res.ResolveNow(resolver.ResolveNowOptions{})
return res, nil
}
func (r *clusterBuilder) Scheme() string {
return r.scheme
}
type clusterResolver struct {
ctx context.Context
cancel context.CancelFunc
dns DNS
service,
name string
cc resolver.ClientConn
rn chan struct{}
wg sync.WaitGroup
}
func (r *clusterResolver) ResolveNow(opts resolver.ResolveNowOptions) {
select {
case r.rn <- struct{}{}:
default:
}
}
func (r *clusterResolver) Close() {
r.cancel()
r.wg.Wait()
}
func (r *clusterResolver) watch() {
defer r.wg.Done()
for {
select {
case <-r.ctx.Done():
return
case <-r.rn:
}
state, err := r.lookup()
if err != nil {
r.cc.ReportError(err)
} else {
r.cc.UpdateState(*state)
}
debounceTimer := time.NewTimer(30 * time.Second)
select {
case <-r.ctx.Done():
debounceTimer.Stop()
return
case <-debounceTimer.C:
}
}
}
func (r *clusterResolver) lookup() (*resolver.State, error) {
_, addrs, err := r.dns.LookupSRV(r.ctx, r.service, "tcp", r.name)
var dnsErr *net.DNSError
if err != nil && stderrors.As(err, &dnsErr) && !dnsErr.IsNotFound {
return nil, err
}
state := new(resolver.State)
if len(addrs) == 0 {
addr, err := DefaultPort(r.name, DefaultPorts[true])
if err != nil {
return nil, err
}
state.Addresses = []resolver.Address{
{
Addr: addr,
ServerName: r.name,
},
}
} else {
state.Addresses = make([]resolver.Address, len(addrs))
for i, addr := range addrs {
name := strings.TrimSuffix(addr.Target, ".")
state.Addresses[i] = resolver.Address{
Addr: fmt.Sprintf("%s:%d", name, addr.Port),
ServerName: name,
Attributes: attributes.New(
"priority", int(addr.Priority),
"weight", int(addr.Weight),
),
}
}
}
return state, nil
}
func init() {
m := make(map[string]struct{}, len(clusterRoleServices))
for role := range clusterRoleServices {
scheme, err := Scheme(role)
if err != nil {
panic(err)
}
if _, ok := m[scheme]; ok {
continue
}
resolver.Register(NewBuilder(scheme))
m[scheme] = struct{}{}
}
}