-
Notifications
You must be signed in to change notification settings - Fork 71
/
bridges.go
716 lines (664 loc) · 18.1 KB
/
bridges.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
package main
import (
"encoding/json"
"fmt"
"log"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1informer "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/informers/internalinterfaces"
"k8s.io/client-go/tools/cache"
"github.com/skupperproject/skupper/api/types"
)
type Bridge struct {
Name string
Host string
Port int
Address string
SiteId string
}
func getBridgeName(address string, host string) string {
if host == "" {
return address
} else {
return address + "@" + host
}
}
func (b *Bridge) checkName() string {
if b.Name == "" {
b.Name = getBridgeName(b.Address, b.Host)
}
return b.Name
}
func (b *Bridge) toMap() map[string]interface{} {
return map[string]interface{}{
"name": b.Name,
"host": b.Host,
"port": b.Port,
"address": b.Address,
"siteId": b.SiteId,
}
}
func (a Bridge) equivalent(b Bridge) bool {
return a.Host == b.Host && a.Port == b.Port && a.Address == b.Address && a.SiteId == b.SiteId
}
type HttpBridge struct {
Bridge
Http2 bool
Aggregation string
EventChannel bool
HostOverride string
}
func (b *HttpBridge) toMap() map[string]interface{} {
return map[string]interface{}{
"name": b.Name,
"host": b.Host,
"port": b.Port,
"address": b.Address,
"siteId": b.SiteId,
"http2": b.Http2,
"aggregation": b.Aggregation,
"eventChannel": b.EventChannel,
"hostOverride": b.HostOverride,
}
}
func (a HttpBridge) equivalent(b HttpBridge) bool {
return a.Host == b.Host && a.Port == b.Port && a.Address == b.Address && a.SiteId == b.SiteId && a.Http2 == b.Http2 && a.Aggregation == b.Aggregation && a.EventChannel == b.EventChannel
}
type BridgeMap map[string]Bridge
type HttpBridgeMap map[string]HttpBridge
type NestedBridgeMap map[string]BridgeMap
type NestedHttpBridgeMap map[string]HttpBridgeMap
type BridgeConfiguration struct {
HttpConnectors NestedHttpBridgeMap
HttpListeners HttpBridgeMap
TcpConnectors NestedBridgeMap
TcpListeners BridgeMap
Http2Connectors NestedHttpBridgeMap
Http2Listeners HttpBridgeMap
}
type EgressBindings struct {
name string
selector string
service string
egressPort int
informer cache.SharedIndexInformer
stopper chan struct{}
}
type ServiceBindings struct {
origin string
protocol string
address string
publicPort int
ingressPort int
aggregation string
eventChannel bool
headless *types.Headless
targets map[string]*EgressBindings
}
func asServiceInterface(bindings *ServiceBindings) types.ServiceInterface {
return types.ServiceInterface{
Address: bindings.address,
Protocol: bindings.protocol,
Port: bindings.publicPort,
Aggregate: bindings.aggregation,
EventChannel: bindings.eventChannel,
Headless: bindings.headless,
Origin: bindings.origin,
}
}
type ServiceController struct {
bindings map[string]*ServiceBindings
ports *FreePorts
}
func newServiceController() *ServiceController {
return &ServiceController{
bindings: map[string]*ServiceBindings{},
ports: newFreePorts(),
}
}
func getTargetPort(service types.ServiceInterface, target types.ServiceInterfaceTarget) int {
targetPort := target.TargetPort
if targetPort == 0 {
targetPort = service.Port
}
return targetPort
}
func hasTargetForSelector(si types.ServiceInterface, selector string) bool {
for _, t := range si.Targets {
if t.Selector == selector {
return true
}
}
return false
}
func hasTargetForService(si types.ServiceInterface, service string) bool {
for _, t := range si.Targets {
if t.Service == service {
return true
}
}
return false
}
func (c *Controller) updateServiceBindings(required types.ServiceInterface, portAllocations map[string]int) error {
bindings := c.bindings[required.Address]
if bindings == nil {
//create it
var port int
if portAllocations != nil {
//existing bridge configuration is used on initiaising map to recover
//any previous port allocations
port = portAllocations[required.Address]
}
if port == 0 {
var err error
port, err = c.ports.nextFreePort()
if err != nil {
return err
}
}
sb := newServiceBindings(required.Origin, required.Protocol, required.Address, required.Port, required.Headless, port, required.Aggregate, required.EventChannel)
for _, t := range required.Targets {
if t.Selector != "" {
sb.addSelectorTarget(t.Name, t.Selector, getTargetPort(required, t), c)
} else if t.Service != "" {
sb.addServiceTarget(t.Name, t.Service, getTargetPort(required, t), c)
}
}
c.bindings[required.Address] = sb
} else {
//check it is configured correctly
if bindings.protocol != required.Protocol {
bindings.protocol = required.Protocol
}
if bindings.publicPort != required.Port {
bindings.publicPort = required.Port
}
if required.Headless != nil {
if bindings.headless == nil {
bindings.headless = required.Headless
} else if bindings.headless.Name != required.Headless.Name {
bindings.headless.Name = required.Headless.Name
} else if bindings.headless.Size != required.Headless.Size {
bindings.headless.Size = required.Headless.Size
} else if bindings.headless.TargetPort != required.Headless.TargetPort {
bindings.headless.TargetPort = required.Headless.TargetPort
}
} else if bindings.headless != nil {
bindings.headless = nil
}
for _, t := range required.Targets {
targetPort := getTargetPort(required, t)
if t.Selector != "" {
target := bindings.targets[t.Selector]
if target == nil {
bindings.addSelectorTarget(t.Name, t.Selector, targetPort, c)
} else if target.egressPort != targetPort {
target.egressPort = targetPort
}
} else if t.Service != "" {
target := bindings.targets[t.Service]
if target == nil {
bindings.addServiceTarget(t.Name, t.Service, targetPort, c)
} else if target.egressPort != targetPort {
target.egressPort = targetPort
}
}
}
for k, v := range bindings.targets {
if v.selector != "" {
if !hasTargetForSelector(required, k) {
bindings.removeSelectorTarget(k)
}
} else if v.service != "" {
if !hasTargetForService(required, k) {
bindings.removeServiceTarget(k)
}
}
}
}
return nil
}
func newServiceBindings(origin string, protocol string, address string, publicPort int, headless *types.Headless, ingressPort int, aggregation string, eventChannel bool) *ServiceBindings {
return &ServiceBindings{
origin: origin,
protocol: protocol,
address: address,
publicPort: publicPort,
ingressPort: ingressPort,
aggregation: aggregation,
eventChannel: eventChannel,
headless: headless,
targets: map[string]*EgressBindings{},
}
}
func (sb *ServiceBindings) addSelectorTarget(name string, selector string, port int, controller *Controller) error {
sb.targets[selector] = &EgressBindings{
name: name,
selector: selector,
egressPort: port,
informer: corev1informer.NewFilteredPodInformer(
controller.vanClient.KubeClient,
controller.vanClient.Namespace,
time.Second*30,
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
internalinterfaces.TweakListOptionsFunc(func(options *metav1.ListOptions) {
options.LabelSelector = selector
})),
stopper: make(chan struct{}),
}
sb.targets[selector].informer.AddEventHandler(controller.newEventHandler("targetpods@"+sb.address, FixedKey, PodResourceVersionTest))
return sb.targets[selector].start()
}
func (sb *ServiceBindings) removeSelectorTarget(selector string) {
sb.targets[selector].stop()
delete(sb.targets, selector)
}
func (sb *ServiceBindings) addServiceTarget(name string, service string, port int, controller *Controller) error {
sb.targets[service] = &EgressBindings{
name: name,
service: service,
egressPort: port,
stopper: make(chan struct{}),
}
return nil
}
func (sb *ServiceBindings) removeServiceTarget(service string) {
delete(sb.targets, service)
}
func (sb *ServiceBindings) stop() {
for _, v := range sb.targets {
if v != nil {
v.stop()
}
}
}
func (sb *ServiceBindings) updateBridgeConfiguration(siteId string, bridges *BridgeConfiguration) {
if sb.headless == nil {
addIngressBridge(sb, siteId, bridges)
for _, eb := range sb.targets {
eb.updateBridgeConfiguration(sb.protocol, sb.address, siteId, bridges)
}
} // headless proxies are not specified through the main bridge configuration
}
func (eb *EgressBindings) start() error {
go eb.informer.Run(eb.stopper)
if ok := cache.WaitForCacheSync(eb.stopper, eb.informer.HasSynced); !ok {
return fmt.Errorf("Failed to wait for service targetcache to sync")
}
return nil
}
func (eb *EgressBindings) stop() {
close(eb.stopper)
}
func (eb *EgressBindings) updateBridgeConfiguration(protocol string, address string, siteId string, bridges *BridgeConfiguration) {
if eb.selector != "" {
pods := eb.informer.GetStore().List()
for _, p := range pods {
pod := p.(*corev1.Pod)
log.Printf("Adding pod for %s: %s", address, pod.ObjectMeta.Name)
addEgressBridge(protocol, pod.Status.PodIP, eb.egressPort, address, eb.name, siteId, "", bridges)
}
} else if eb.service != "" {
addEgressBridge(protocol, eb.service, eb.egressPort, address, eb.name, siteId, eb.service, bridges)
}
}
func newBridgeConfiguration() *BridgeConfiguration {
return &BridgeConfiguration{
HttpConnectors: make(map[string]HttpBridgeMap),
HttpListeners: make(map[string]HttpBridge),
Http2Connectors: make(map[string]HttpBridgeMap),
Http2Listeners: make(map[string]HttpBridge),
TcpConnectors: make(map[string]BridgeMap),
TcpListeners: make(map[string]Bridge),
}
}
func getStringByKey(attributes map[string]interface{}, key string) string {
s, ok := attributes[key].(string)
if !ok {
return ""
}
return s
}
func getIntByKey(attributes map[string]interface{}, key string) int {
// Unmarshal stores float64 in interface value for JSON numbers
i, ok := attributes[key].(float64)
if !ok {
return 0
}
return int(i)
}
func getBoolByKey(attributes map[string]interface{}, key string) bool {
if b, ok := attributes[key].(bool); ok {
return b
} else {
return false
}
}
func (bc *BridgeConfiguration) addBridgeFromMap(entityType string, attributes map[string]interface{}) {
if isHttpBridgeEntity(entityType) {
bridge := HttpBridge{
Bridge: Bridge{
Name: getStringByKey(attributes, "name"),
Host: getStringByKey(attributes, "host"),
Port: getIntByKey(attributes, "port"),
Address: getStringByKey(attributes, "address"),
SiteId: getStringByKey(attributes, "siteId"),
},
Http2: getBoolByKey(attributes, "http2"),
Aggregation: getStringByKey(attributes, "aggregation"),
EventChannel: getBoolByKey(attributes, "eventChannel"),
}
bridge.checkName()
switch entityType {
case "httpConnector":
bc.HttpConnectors.add(bridge)
case "httpListener":
bc.HttpListeners[bridge.Address] = bridge
case "http2Connector":
bc.Http2Connectors.add(bridge)
case "http2Listener":
bc.Http2Listeners[bridge.Address] = bridge
default:
}
} else {
bridge := Bridge{
Name: getStringByKey(attributes, "name"),
Host: getStringByKey(attributes, "host"),
Port: getIntByKey(attributes, "port"),
Address: getStringByKey(attributes, "address"),
SiteId: getStringByKey(attributes, "siteId"),
}
bridge.checkName()
switch entityType {
case "tcpConnector":
bc.TcpConnectors.add(bridge)
case "tcpListener":
bc.TcpListeners[bridge.Address] = bridge
default:
}
}
}
func isBridgeEntity(typename string) bool {
switch typename {
case
"tcpListener",
"tcpConnector":
return true
}
return isHttpBridgeEntity(typename)
}
func isHttpBridgeEntity(typename string) bool {
switch typename {
case
"httpListener",
"httpConnector",
"http2Listener",
"http2Connector":
return true
}
return false
}
const (
ProtocolTCP string = "tcp"
ProtocolHTTP string = "http"
ProtocolHTTP2 string = "http2"
)
func (m NestedBridgeMap) add(b Bridge) {
nm := m[b.Address]
if nm == nil {
m[b.Address] = BridgeMap{
b.Host: b,
}
} else {
nm[b.Host] = b
}
}
func (m NestedHttpBridgeMap) add(b HttpBridge) {
nm := m[b.Address]
if nm == nil {
m[b.Address] = HttpBridgeMap{
b.Host: b,
}
} else {
nm[b.Host] = b
}
}
func addEgressBridge(protocol string, host string, port int, address string, target string, siteId string, hostOverride string, bridges *BridgeConfiguration) (bool, error) {
switch protocol {
case ProtocolHTTP:
b := HttpBridge{
Bridge: Bridge{
Name: getBridgeName(target, host),
Host: host,
Port: port,
Address: address,
SiteId: siteId,
},
}
if hostOverride != "" {
b.HostOverride = hostOverride
}
bridges.HttpConnectors.add(b)
case ProtocolHTTP2:
bridges.Http2Connectors.add(HttpBridge{
Bridge: Bridge{
Name: getBridgeName(target, host),
Host: host,
Port: port,
Address: address,
SiteId: siteId,
},
})
case ProtocolTCP:
bridges.TcpConnectors.add(Bridge{
Name: getBridgeName(target, host),
Host: host,
Port: port,
Address: address,
SiteId: siteId,
})
default:
return false, fmt.Errorf("Unrecognised protocol for service %s: %s", address, protocol)
}
return true, nil
}
func addIngressBridge(sb *ServiceBindings, siteId string, bridges *BridgeConfiguration) (bool, error) {
switch sb.protocol {
case ProtocolHTTP:
bridges.HttpListeners[sb.address] = HttpBridge{
Bridge: Bridge{
Name: getBridgeName(sb.address, ""),
Host: "0.0.0.0",
Port: sb.ingressPort,
Address: sb.address,
SiteId: siteId,
},
Aggregation: sb.aggregation,
EventChannel: sb.eventChannel,
}
case ProtocolHTTP2:
bridges.Http2Listeners[sb.address] = HttpBridge{
Bridge: Bridge{
Name: getBridgeName(sb.address, ""),
Host: "0.0.0.0",
Port: sb.ingressPort,
Address: sb.address,
SiteId: siteId,
},
Aggregation: sb.aggregation,
EventChannel: sb.eventChannel,
}
case ProtocolTCP:
bridges.TcpListeners[sb.address] = Bridge{
Name: getBridgeName(sb.address, ""),
Host: "0.0.0.0",
Port: sb.ingressPort,
Address: sb.address,
SiteId: siteId,
}
default:
return false, fmt.Errorf("Unrecognised protocol for service %s: %s", sb.address, sb.protocol)
}
return true, nil
}
func requiredBridges(services map[string]*ServiceBindings, siteId string) *BridgeConfiguration {
//TODO: headless services not yet handled
//TODO: update for multicast when merged
bridges := newBridgeConfiguration()
for _, service := range services {
service.updateBridgeConfiguration(siteId, bridges)
}
return bridges
}
func readBridgeConfiguration(data []byte) (*BridgeConfiguration, error) {
bridges := newBridgeConfiguration()
var obj interface{}
json.Unmarshal(data, &obj)
if obj == nil {
return bridges, nil
}
elements, ok := obj.([]interface{})
if !ok {
return nil, fmt.Errorf("Invalid JSON for bridge configuration, expected array at top level got %#v", obj)
}
for _, e := range elements {
element, ok := e.([]interface{})
if !ok || len(element) != 2 {
return nil, fmt.Errorf("Invalid JSON for bridge configuration, expected array with type and value got %#v", e)
}
entityType, ok := element[0].(string)
if !ok {
return nil, fmt.Errorf("Invalid JSON for bridge configuration, expected entity type as string got %#v", element[0])
}
if !isBridgeEntity(entityType) {
return nil, fmt.Errorf("Invalid JSON for bridge configuration, unexpected entity type %s", entityType)
}
attributes, ok := element[1].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Invalid JSON for bridge configuration, expected object got %#v", element[1])
}
bridges.addBridgeFromMap(entityType, attributes)
}
return bridges, nil
}
func writeBridgeConfiguration(bridges *BridgeConfiguration) ([]byte, error) {
elements := []interface{}{}
for _, m := range bridges.HttpConnectors {
for _, b := range m {
elements = append(elements, []interface{}{
"httpConnector",
b.toMap(),
})
}
}
for _, b := range bridges.HttpListeners {
elements = append(elements, []interface{}{
"httpListener",
b.toMap(),
})
}
for _, m := range bridges.Http2Connectors {
for _, b := range m {
elements = append(elements, []interface{}{
"http2Connector",
b.toMap(),
})
}
}
for _, b := range bridges.Http2Listeners {
elements = append(elements, []interface{}{
"http2Listener",
b.toMap(),
})
}
for _, m := range bridges.TcpConnectors {
for _, b := range m {
elements = append(elements, []interface{}{
"tcpConnector",
b.toMap(),
})
}
}
for _, b := range bridges.TcpListeners {
elements = append(elements, []interface{}{
"tcpListener",
b.toMap(),
})
}
return json.Marshal(elements)
}
func (a BridgeMap) equivalent(b BridgeMap) bool {
for k, v := range a {
if !v.equivalent(b[k]) {
return false
}
}
for k, v := range b {
if !v.equivalent(a[k]) {
return false
}
}
return true
}
func (a HttpBridgeMap) equivalent(b HttpBridgeMap) bool {
for k, v := range a {
if !v.equivalent(b[k]) {
return false
}
}
for k, v := range b {
if !v.equivalent(a[k]) {
return false
}
}
return true
}
func (a NestedBridgeMap) equivalent(b NestedBridgeMap) bool {
for k, v := range a {
if !v.equivalent(b[k]) {
return false
}
}
for k, v := range b {
if !v.equivalent(a[k]) {
return false
}
}
return true
}
func (a NestedHttpBridgeMap) equivalent(b NestedHttpBridgeMap) bool {
for k, v := range a {
if !v.equivalent(b[k]) {
return false
}
}
for k, v := range b {
if !v.equivalent(a[k]) {
return false
}
}
return true
}
func updateBridgeConfiguration(desired *BridgeConfiguration, actual *BridgeConfiguration) bool {
if !desired.HttpConnectors.equivalent(actual.HttpConnectors) {
return true
}
if !desired.HttpListeners.equivalent(actual.HttpListeners) {
return true
}
if !desired.TcpConnectors.equivalent(actual.TcpConnectors) {
return true
}
if !desired.TcpListeners.equivalent(actual.TcpListeners) {
return true
}
if !desired.Http2Connectors.equivalent(actual.Http2Connectors) {
return true
}
if !desired.Http2Listeners.equivalent(actual.Http2Listeners) {
return true
}
return false
}