-
Notifications
You must be signed in to change notification settings - Fork 332
/
loadbalancer.go
536 lines (471 loc) · 16.7 KB
/
loadbalancer.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
package services
import (
"fmt"
"reflect"
"strings"
libovsdbclient "github.com/ovn-org/libovsdb/client"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/config"
libovsdbops "github.com/ovn-org/ovn-kubernetes/go-controller/pkg/libovsdb/ops"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/metrics"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/nbdb"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/types"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/apis/core"
"k8s.io/klog/v2"
)
// LB is a desired or existing load_balancer configuration in OVN.
type LB struct {
Name string
UUID string
Protocol string // one of TCP, UDP, SCTP
ExternalIDs map[string]string
Opts LBOpts
Rules []LBRule
Templates TemplateMap // Templates that this LB uses as backends.
// the names of logical switches, routers and LB groups that this LB should be attached to
Switches []string
Routers []string
Groups []string
}
type LBOpts struct {
// if the service should send back tcp REJECT in case of no endpoints
Reject bool
// if the service should raise empty_lb events
EmptyLBEvents bool
// If greater than 0, then enable per-client-IP affinity.
AffinityTimeOut int32
// If true, then disable SNAT entirely
SkipSNAT bool
// If true, this is a LB template.
Template bool
// Only useful for template LBs.
AddressFamily corev1.IPFamily
}
type Addr struct {
IP string
Port int32
Template *Template
}
type LBRule struct {
Source Addr
Targets []Addr
}
func (a *Addr) String() string {
if a.Template == nil {
return util.JoinHostPortInt32(a.IP, a.Port)
} else if a.Port != 0 {
return fmt.Sprintf("%s:%d", a.Template.toReferenceString(), a.Port)
} else {
return a.Template.toReferenceString()
}
}
// addrsToString joins together the textual representation of a list of Addr.
func addrsToString(addrs []Addr) string {
s := ""
for _, a := range addrs {
s += a.String() + ","
}
s = strings.TrimRight(s, ",")
return s
}
// templateLoadBalancer enriches a NB load balancer record with the
// associated template maps it requires provisioned in the NB database.
type templateLoadBalancer struct {
nbLB *nbdb.LoadBalancer
templates TemplateMap
}
func toNBLoadBalancerList(tlbs []*templateLoadBalancer) []*nbdb.LoadBalancer {
result := make([]*nbdb.LoadBalancer, 0, len(tlbs))
for _, tlb := range tlbs {
result = append(result, tlb.nbLB)
}
return result
}
func toNBTemplateList(tlbs []*templateLoadBalancer) []TemplateMap {
templateVars := make([]TemplateMap, 0, len(tlbs))
for _, tlb := range tlbs {
templateVars = append(templateVars, tlb.templates)
}
return templateVars
}
// EnsureLBs provides a generic load-balancer reconciliation engine.
//
// It assures that, for a given set of ExternalIDs, only the configured
// list of load balancers exist. Existing load-balancers will be updated,
// new ones will be created as needed, and stale ones will be deleted.
//
// For example, you might want to ensure that service ns/foo has the
// correct set of load balancers. You would call it with something like
//
// EnsureLBs( { kind: Service, owner: ns/foo}, { {Name: Service_ns/foo_cluster_tcp, ...}})
//
// This will ensure that, for this example, only that one LB exists and
// has the desired configuration.
//
// It will commit all updates in a single transaction, so updates will be
// atomic and users should see no disruption. However, concurrent calls
// that modify the same externalIDs are not allowed.
//
// It is assumed that names are meaningful and somewhat stable, to minimize churn. This
// function doesn't work with Load_Balancers without a name.
func EnsureLBs(nbClient libovsdbclient.Client, service *corev1.Service, existingCacheLBs []LB, LBs []LB) error {
externalIDs := util.ExternalIDsForObject(service)
existingByName := make(map[string]*LB, len(existingCacheLBs))
toDelete := make(map[string]*LB, len(existingCacheLBs))
for i := range existingCacheLBs {
lb := &existingCacheLBs[i]
existingByName[lb.Name] = lb
toDelete[lb.UUID] = lb
}
tlbs := make([]*templateLoadBalancer, 0, len(LBs))
addLBsToSwitch := map[string][]*templateLoadBalancer{}
removeLBsFromSwitch := map[string][]*templateLoadBalancer{}
addLBsToRouter := map[string][]*templateLoadBalancer{}
removesLBsFromRouter := map[string][]*templateLoadBalancer{}
addLBsToGroups := map[string][]*templateLoadBalancer{}
removeLBsFromGroups := map[string][]*templateLoadBalancer{}
wantedByName := make(map[string]*LB, len(LBs))
for i, lb := range LBs {
wantedByName[lb.Name] = &LBs[i]
blb := buildLB(&lb)
tlbs = append(tlbs, blb)
existingLB := existingByName[lb.Name]
existingRouters := sets.Set[string]{}
existingSwitches := sets.Set[string]{}
existingGroups := sets.Set[string]{}
if existingLB != nil {
blb.nbLB.UUID = existingLB.UUID
delete(toDelete, existingLB.UUID)
existingRouters = sets.New[string](existingLB.Routers...)
existingSwitches = sets.New[string](existingLB.Switches...)
existingGroups = sets.New[string](existingLB.Groups...)
}
wantRouters := sets.New(lb.Routers...)
wantSwitches := sets.New(lb.Switches...)
wantGroups := sets.New(lb.Groups...)
mapLBDifferenceByKey(addLBsToSwitch, wantSwitches, existingSwitches, blb)
mapLBDifferenceByKey(removeLBsFromSwitch, existingSwitches, wantSwitches, blb)
mapLBDifferenceByKey(addLBsToRouter, wantRouters, existingRouters, blb)
mapLBDifferenceByKey(removesLBsFromRouter, existingRouters, wantRouters, blb)
mapLBDifferenceByKey(addLBsToGroups, wantGroups, existingGroups, blb)
mapLBDifferenceByKey(removeLBsFromGroups, existingGroups, wantGroups, blb)
}
ops, err := libovsdbops.CreateOrUpdateLoadBalancersOps(nbClient, nil, toNBLoadBalancerList(tlbs)...)
if err != nil {
return err
}
ops, err = svcCreateOrUpdateTemplateVarOps(nbClient, ops, toNBTemplateList(tlbs))
if err != nil {
return fmt.Errorf("failed to create ops for ensuring creation of service %s/%s load balancers: %w",
service.Namespace, service.Name, err)
}
// cache switches for this round of ops
lswitches := map[string]*nbdb.LogicalSwitch{}
getSwitch := func(name string) *nbdb.LogicalSwitch {
var lswitch *nbdb.LogicalSwitch
var found bool
if lswitch, found = lswitches[name]; !found {
lswitch = &nbdb.LogicalSwitch{Name: name}
lswitches[name] = lswitch
}
return lswitch
}
for k, v := range addLBsToSwitch {
ops, err = libovsdbops.AddLoadBalancersToLogicalSwitchOps(nbClient, ops, getSwitch(k), toNBLoadBalancerList(v)...)
if err != nil {
return fmt.Errorf("failed to create ops for adding load balancers to switch %s for service %s/%s: %w",
k, service.Namespace, service.Name, err)
}
}
for k, v := range removeLBsFromSwitch {
ops, err = libovsdbops.RemoveLoadBalancersFromLogicalSwitchOps(nbClient, ops, getSwitch(k), toNBLoadBalancerList(v)...)
if err != nil {
return fmt.Errorf("failed to create ops for removing load balancers from switch %s for service %s/%s: %w",
k, service.Namespace, service.Name, err)
}
}
// cache routers for this round of ops
routers := map[string]*nbdb.LogicalRouter{}
getRouter := func(name string) *nbdb.LogicalRouter {
var router *nbdb.LogicalRouter
var found bool
if router, found = routers[name]; !found {
router = &nbdb.LogicalRouter{Name: name}
routers[name] = router
}
return router
}
for k, v := range addLBsToRouter {
ops, err = libovsdbops.AddLoadBalancersToLogicalRouterOps(nbClient, ops, getRouter(k), toNBLoadBalancerList(v)...)
if err != nil {
return fmt.Errorf("failed to create ops for adding load balancers to router %s for service %s/%s: %w",
k, service.Namespace, service.Name, err)
}
}
for k, v := range removesLBsFromRouter {
ops, err = libovsdbops.RemoveLoadBalancersFromLogicalRouterOps(nbClient, ops, getRouter(k), toNBLoadBalancerList(v)...)
if err != nil {
return fmt.Errorf("failed to create ops for removing load balancers from router %s for service %s/%s: %w",
k, service.Namespace, service.Name, err)
}
}
// cache groups for this round of ops
groups := map[string]*nbdb.LoadBalancerGroup{}
getGroup := func(name string) *nbdb.LoadBalancerGroup {
var group *nbdb.LoadBalancerGroup
var found bool
if group, found = groups[name]; !found {
group = &nbdb.LoadBalancerGroup{Name: name}
groups[name] = group
}
return group
}
for k, v := range addLBsToGroups {
ops, err = libovsdbops.AddLoadBalancersToGroupOps(nbClient, ops, getGroup(k), toNBLoadBalancerList(v)...)
if err != nil {
return fmt.Errorf("failed to create ops for adding load balancers to group %s for service %s/%s: %w",
k, service.Namespace, service.Name, err)
}
}
for k, v := range removeLBsFromGroups {
ops, err = libovsdbops.RemoveLoadBalancersFromGroupOps(nbClient, ops, getGroup(k), toNBLoadBalancerList(v)...)
if err != nil {
return fmt.Errorf("failed to create ops for removing load balancers from group %s for service %s/%s: %w",
k, service.Namespace, service.Name, err)
}
}
deleteLBs := make([]*nbdb.LoadBalancer, 0, len(toDelete))
deleteTemplates := make([]TemplateMap, 0, len(toDelete))
for _, clb := range toDelete {
deleteLBs = append(deleteLBs, &nbdb.LoadBalancer{UUID: clb.UUID})
deleteTemplates = append(deleteTemplates, clb.Templates)
}
ops, err = libovsdbops.DeleteLoadBalancersOps(nbClient, ops, deleteLBs...)
if err != nil {
return fmt.Errorf("failed to create ops for removing %d load balancers for service %s/%s: %w",
len(deleteLBs), service.Namespace, service.Name, err)
}
ops, err = svcDeleteTemplateVarOps(nbClient, ops, deleteTemplates)
if err != nil {
return err
}
recordOps, txOkCallBack, _, err := metrics.GetConfigDurationRecorder().AddOVN(nbClient, "service",
service.Namespace, service.Name)
if err != nil {
klog.Errorf("Failed to record config duration: %v", err)
}
ops = append(ops, recordOps...)
_, err = libovsdbops.TransactAndCheckAndSetUUIDs(nbClient, toNBLoadBalancerList(tlbs), ops)
if err != nil {
return fmt.Errorf("failed to ensure load balancers for service %s/%s: %w", service.Namespace, service.Name, err)
}
txOkCallBack()
// Store UUID of newly created load balancers for future calls.
// This is accomplished by the caching of LBs by the caller of this function.
for _, tlb := range tlbs {
wantedByName[tlb.nbLB.Name].UUID = tlb.nbLB.UUID
}
klog.V(5).Infof("Deleted %d stale LBs for %#v", len(toDelete), externalIDs)
return nil
}
// LoadBalancersEqualNoUUID compares load balancer objects excluding uuid
func LoadBalancersEqualNoUUID(lbs1, lbs2 []LB) bool {
if len(lbs1) != len(lbs2) {
return false
}
new1 := make([]LB, len(lbs1))
new2 := make([]LB, len(lbs2))
for _, lb := range lbs1 {
lb.UUID = ""
new1 = append(new1, lb)
}
for _, lb := range lbs2 {
lb.UUID = ""
new2 = append(new2, lb)
}
return reflect.DeepEqual(new1, new2)
}
func mapLBDifferenceByKey(keyMap map[string][]*templateLoadBalancer, keyIn sets.Set[string], keyNotIn sets.Set[string], lb *templateLoadBalancer) {
for _, k := range keyIn.Difference(keyNotIn).UnsortedList() {
l := keyMap[k]
if l == nil {
l = []*templateLoadBalancer{}
}
l = append(l, lb)
keyMap[k] = l
}
}
func buildLB(lb *LB) *templateLoadBalancer {
skipSNAT := "false"
if lb.Opts.SkipSNAT {
skipSNAT = "true"
}
reject := "false"
if lb.Opts.Reject {
reject = "true"
}
emptyLb := "false"
if lb.Opts.EmptyLBEvents {
emptyLb = "true"
}
options := map[string]string{
"reject": reject,
"event": emptyLb,
"skip_snat": skipSNAT,
"neighbor_responder": "none",
"hairpin_snat_ip": fmt.Sprintf("%s %s", config.Gateway.MasqueradeIPs.V4OVNServiceHairpinMasqueradeIP.String(), config.Gateway.MasqueradeIPs.V6OVNServiceHairpinMasqueradeIP.String()),
}
// Session affinity
// If enabled, then bucket flows by 3-tuple (proto, srcip, dstip) for the specific timeout value
// otherwise, use default ovn value
selectionFields := []nbdb.LoadBalancerSelectionFields{}
if lb.Opts.AffinityTimeOut > 0 {
if lb.Opts.AffinityTimeOut != core.MaxClientIPServiceAffinitySeconds {
options["affinity_timeout"] = fmt.Sprintf("%d", lb.Opts.AffinityTimeOut)
} else {
selectionFields = []string{
nbdb.LoadBalancerSelectionFieldsIPSrc,
nbdb.LoadBalancerSelectionFieldsIPDst,
}
}
}
if lb.Opts.Template {
options["template"] = "true"
// Address family is used only for template LBs.
if lb.Opts.AddressFamily != "" {
// OVN expects the family as lowercase...
options["address-family"] = strings.ToLower(fmt.Sprintf("%v", lb.Opts.AddressFamily))
}
}
return &templateLoadBalancer{
nbLB: libovsdbops.BuildLoadBalancer(lb.Name, strings.ToLower(lb.Protocol), selectionFields, buildVipMap(lb.Rules), options, lb.ExternalIDs),
templates: lb.Templates,
}
}
// buildVipMap returns a viups map from a set of rules
func buildVipMap(rules []LBRule) map[string]string {
vipMap := make(map[string]string, len(rules))
for _, r := range rules {
tgts := make([]string, 0, len(r.Targets))
for _, tgt := range r.Targets {
tgts = append(tgts, tgt.String())
}
vipMap[r.Source.String()] = strings.Join(tgts, ",")
}
return vipMap
}
// DeleteLBs deletes all load balancer uuids supplied
// Note: this also automatically removes them from the switches, routers, and the groups :-)
func DeleteLBs(nbClient libovsdbclient.Client, uuids []string) error {
if len(uuids) == 0 {
return nil
}
lbs := make([]*nbdb.LoadBalancer, 0, len(uuids))
for _, uuid := range uuids {
lbs = append(lbs, &nbdb.LoadBalancer{UUID: uuid})
}
err := libovsdbops.DeleteLoadBalancers(nbClient, lbs)
if err != nil {
return err
}
return nil
}
// getLBs returns a slice of load balancers found in OVN.
func getLBs(nbClient libovsdbclient.Client, allTemplates TemplateMap) ([]*LB, error) {
_, out, err := _getLBsCommon(nbClient, allTemplates, false)
return out, err
}
// getServiceLBs returns a set of services as well as a slice of load balancers found in OVN.
func getServiceLBs(nbClient libovsdbclient.Client, allTemplates TemplateMap) (sets.Set[string], []*LB, error) {
return _getLBsCommon(nbClient, allTemplates, true)
}
func _getLBsCommon(nbClient libovsdbclient.Client, allTemplates TemplateMap, withServiceOwner bool) (sets.Set[string], []*LB, error) {
lbs, err := libovsdbops.ListLoadBalancers(nbClient)
if err != nil {
return nil, nil, fmt.Errorf("could not list load_balancer: %w", err)
}
services := sets.New[string]()
outMap := make(map[string]*LB, len(lbs))
for _, lb := range lbs {
// Skip load balancers unrelated to service, or w/out an owner (aka namespace+name)
if lb.ExternalIDs[types.LoadBalancerKindExternalID] != "Service" {
continue
}
if withServiceOwner {
service, ok := lb.ExternalIDs[types.LoadBalancerOwnerExternalID]
if !ok {
continue
}
services.Insert(service)
}
// Note: no need to fill in Opts and Rules: syncServices populates them later.
// Switches, Routers and Groups for each load balancer will get filled in below.
res := LB{
UUID: lb.UUID,
Name: lb.Name,
ExternalIDs: lb.ExternalIDs,
Opts: LBOpts{},
Rules: []LBRule{},
Templates: getLoadBalancerTemplates(lb, allTemplates),
Switches: []string{},
Routers: []string{},
Groups: []string{},
}
if lb.Protocol != nil {
res.Protocol = *lb.Protocol
}
outMap[lb.UUID] = &res
}
// Switches
ps := func(item *nbdb.LogicalSwitch) bool {
return len(item.LoadBalancer) > 0
}
switches, err := libovsdbops.FindLogicalSwitchesWithPredicate(nbClient, ps)
if err != nil {
return nil, nil, fmt.Errorf("could not list logical switches: %w", err)
}
for _, ls := range switches {
for _, lbuuid := range ls.LoadBalancer {
if ovnLb, ok := outMap[lbuuid]; ok {
outMap[lbuuid].Switches = append(ovnLb.Switches, ls.Name)
}
}
}
// Routers
pr := func(item *nbdb.LogicalRouter) bool {
return len(item.LoadBalancer) > 0
}
routers, err := libovsdbops.FindLogicalRoutersWithPredicate(nbClient, pr)
if err != nil {
return nil, nil, fmt.Errorf("could not list logical routers: %w", err)
}
for _, router := range routers {
for _, lbuuid := range router.LoadBalancer {
if ovnLb, ok := outMap[lbuuid]; ok {
outMap[lbuuid].Routers = append(ovnLb.Routers, router.Name)
}
}
}
// Groups
pg := func(item *nbdb.LoadBalancerGroup) bool {
return len(item.LoadBalancer) > 0
}
groups, err := libovsdbops.FindLoadBalancerGroupsWithPredicate(nbClient, pg)
if err != nil {
return nil, nil, fmt.Errorf("could not list load balancer groups: %w", err)
}
for _, group := range groups {
for _, lbuuid := range group.LoadBalancer {
if ovnLb, ok := outMap[lbuuid]; ok {
outMap[lbuuid].Groups = append(ovnLb.Groups, group.Name)
}
}
}
out := make([]*LB, 0, len(outMap))
for _, value := range outMap {
out = append(out, value)
}
return services, out, nil
}