-
Notifications
You must be signed in to change notification settings - Fork 1
/
dispatcher.go
390 lines (330 loc) · 9.51 KB
/
dispatcher.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2024 Andres Morey
//
// 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 grpcdispatcher
import (
"context"
"fmt"
"math/rand"
"net/url"
"os"
"strings"
"sync"
"time"
eventbus "github.com/asaskevich/EventBus"
mapset "github.com/deckarep/golang-set/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
resolver "google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)
type key string
const dispatcherAddrCtxKey key = "dispatcherAddr"
var balancerName string
func init() {
balancerName = fmt.Sprintf("dispatcher_balancer-%d", rand.Intn(100000))
builder := base.NewBalancerBuilder(balancerName, &pickerBuilder{}, base.Config{HealthCheck: true})
balancer.Register(builder)
}
// Represents the callback argument to the dispatch methods
type DispatchHandler func(ctx context.Context, conn *grpc.ClientConn)
// Represents interest in pod ips that are part of a Kubernetes service
type Subscription struct {
ipCh chan string
cleanup func()
}
// Ends subscription
func (sub *Subscription) Unsubscribe() {
close(sub.ipCh)
sub.cleanup()
}
// A Dispatcher is a utility that facilitates sending queries to multiple grpc servers
// simultaneously. It maintains an up-to-date list of all pod ips that are part of a
// Kubernetes service and directs queries to the ips.
type Dispatcher struct {
opts *dispatcherOptions
connectArgs *connectArgs
informer cache.SharedInformer
informerReg cache.ResourceEventHandlerRegistration
resolver *manual.Resolver
conn *grpc.ClientConn
ips mapset.Set[string]
mu sync.Mutex
eventbus eventbus.Bus
stopCh chan struct{}
}
// Sends queries to all available ips at query-time
func (d *Dispatcher) Fanout(ctx context.Context, fn DispatchHandler) {
var wg sync.WaitGroup
d.mu.Lock()
ips := d.ips.ToSlice()
d.mu.Unlock()
for _, ip := range ips {
connCtx := context.WithValue(ctx, dispatcherAddrCtxKey, fmt.Sprintf("%s:%s", ip, d.connectArgs.Port))
wg.Add(1)
go func(lclCtx context.Context) {
defer wg.Done()
fn(lclCtx, d.conn)
}(connCtx)
}
doneCh := make(chan struct{})
go func() {
wg.Wait()
close(doneCh)
}()
// wait for funcs or ctx to finish whichever comes first
select {
case <-ctx.Done():
case <-doneCh:
}
}
// Sends queries to all available ips at query-time and all subsequent ips when
// they become available until Unsubscribe() is called
func (d *Dispatcher) FanoutSubscribe(ctx context.Context, fn DispatchHandler) (*Subscription, error) {
ipCh := make(chan string)
// ip handler
handleNewIps := func(newIps []string) {
for _, ip := range newIps {
ipCh <- ip
}
}
// worker
go func() {
for {
select {
case <-ctx.Done():
return
case ip, ok := <-ipCh:
if !ok {
// unsubscribe was called
return
}
// execute dispatch handler in goroutine
connCtx := context.WithValue(ctx, dispatcherAddrCtxKey, fmt.Sprintf("%s:%s", ip, d.connectArgs.Port))
go fn(connCtx, d.conn)
}
}
}()
// get current ips and subscribe to new ones in a lock
d.mu.Lock()
currentIps := d.ips.ToSlice()
err := d.eventbus.SubscribeAsync("add:addrs", handleNewIps, false)
if err != nil {
d.mu.Unlock()
return nil, err
}
d.mu.Unlock()
handleNewIps(currentIps)
return &Subscription{
ipCh: ipCh,
cleanup: func() {
d.eventbus.Unsubscribe("add:addrs", handleNewIps)
},
}, nil
}
// Start background processes and initialize subconns
func (d *Dispatcher) Start() {
// exit if already started
if d.stopCh != nil {
return
}
// add event handler
reg, _ := d.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
es := obj.(*discoveryv1.EndpointSlice)
d.handleAddEndpointSlice(es)
},
UpdateFunc: func(oldObj interface{}, newObj interface{}) {
esOld := oldObj.(*discoveryv1.EndpointSlice)
esNew := newObj.(*discoveryv1.EndpointSlice)
d.handleUpdateEndpointSlice(esOld, esNew)
},
})
d.informerReg = reg
// start informer and connect subconns
d.stopCh = make(chan struct{})
go d.informer.Run(d.stopCh)
}
// Wait until informer has synced with Kubernetes API
func (d *Dispatcher) Ready(ctx context.Context) {
cache.WaitForCacheSync(ctx.Done(), d.informerReg.HasSynced)
}
// Closes connection which also stops resolver background processes
func (d *Dispatcher) Shutdown() error {
if d.stopCh != nil {
// stop informer
close(d.stopCh)
// remove event handler
d.informer.RemoveEventHandler(d.informerReg)
}
return d.conn.Close()
}
// Handle add
func (d *Dispatcher) handleAddEndpointSlice(es *discoveryv1.EndpointSlice) {
newIps := getIpsFromEndpointSlice(es)
d.updateState(newIps, nil)
}
// Handle updates
func (d *Dispatcher) handleUpdateEndpointSlice(esOld *discoveryv1.EndpointSlice, esNew *discoveryv1.EndpointSlice) {
oldIps := mapset.NewSet(getIpsFromEndpointSlice(esOld)...)
newIps := mapset.NewSet(getIpsFromEndpointSlice(esNew)...)
toDelete := oldIps.Difference(newIps)
toAdd := newIps.Difference(oldIps)
d.updateState(toAdd.ToSlice(), toDelete.ToSlice())
}
// Adds and deletes ips, updates clientconn state, publishes change to eventbus
func (d *Dispatcher) updateState(toAdd []string, toDelete []string) {
d.mu.Lock()
// update local state
if len(toDelete) > 0 {
d.ips.RemoveAll(toDelete...)
}
if len(toAdd) > 0 {
d.ips.Append(toAdd...)
}
// exit if no changes
if len(toAdd) == 0 && len(toDelete) == 0 {
d.mu.Unlock()
return
}
// update clientconn state
ips := d.ips.ToSlice()
addrs := make([]resolver.Address, len(ips))
for i, ip := range ips {
addrs[i] = resolver.Address{Addr: fmt.Sprintf("%s:%s", ip, d.connectArgs.Port)}
}
d.resolver.UpdateState(resolver.State{Addresses: addrs})
d.mu.Unlock()
// publish change
if len(toAdd) > 0 {
d.eventbus.Publish("add:addrs", toAdd)
}
}
func NewDispatcher(connectUrl string, options ...DispatcherOption) (*Dispatcher, error) {
// init opts
opts := newDispatcherOptions(options...)
// init clientset
var clientset kubernetes.Interface
if opts.clientset != nil {
clientset = opts.clientset
} else {
k8scfg, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err = kubernetes.NewForConfig(k8scfg)
if err != nil {
return nil, err
}
}
// get service name and namespace
connectArgs, err := parseConnectUrl(connectUrl)
if err != nil {
return nil, err
}
// init informer with 10 minute resync period
labelSelector := labels.Set{
discoveryv1.LabelServiceName: connectArgs.ServiceName,
}.String()
informer := cache.NewSharedInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.LabelSelector = labelSelector
return clientset.DiscoveryV1().EndpointSlices(connectArgs.Namespace).List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.LabelSelector = labelSelector
return clientset.DiscoveryV1().EndpointSlices(connectArgs.Namespace).Watch(context.TODO(), options)
},
},
&discoveryv1.EndpointSlice{},
10*time.Minute,
)
// init resolver
resolver := manual.NewBuilderWithScheme("kubernetes")
// init conn
dialOpts := append(opts.dialOpts,
grpc.WithResolvers(resolver),
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"loadBalancingConfig": [{"%s":{}}]}`, balancerName)),
grpc.WithDefaultCallOptions(grpc.WaitForReady(true)),
)
conn, err := grpc.NewClient(connectUrl, dialOpts...)
if err != nil {
return nil, err
}
// eager connect
conn.Connect()
return &Dispatcher{
opts: opts,
connectArgs: connectArgs,
informer: informer,
resolver: resolver,
conn: conn,
ips: mapset.NewSet[string](),
eventbus: eventbus.New(),
}, nil
}
type connectArgs struct {
Namespace string
ServiceName string
Port string
}
// Parse connect url and return connect args
func parseConnectUrl(connectUrl string) (*connectArgs, error) {
u, err := url.Parse(connectUrl)
if err != nil {
return nil, err
}
parts := strings.Split(u.Hostname(), ".")
serviceName := parts[0]
// get namespace
var namespace string
if len(parts) > 1 {
namespace = parts[1]
} else {
nsPathname := "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
nsBytes, err := os.ReadFile(nsPathname)
if err != nil {
return nil, fmt.Errorf("unable to read current namespace from %s: %v", nsPathname, err)
}
namespace = string(nsBytes)
}
// get port
port := u.Port()
if port == "" {
port = "50051"
}
return &connectArgs{
Namespace: namespace,
ServiceName: serviceName,
Port: port,
}, nil
}
func getIpsFromEndpointSlice(es *discoveryv1.EndpointSlice) []string {
var ips []string
for _, endpoint := range es.Endpoints {
if *endpoint.Conditions.Serving {
ips = append(ips, endpoint.Addresses...)
}
}
return ips
}