forked from kubeovn/kube-ovn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathovn-nb-load_balancer.go
More file actions
755 lines (662 loc) · 20.3 KB
/
ovn-nb-load_balancer.go
File metadata and controls
755 lines (662 loc) · 20.3 KB
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
package ovs
import (
"context"
"fmt"
"maps"
"net"
"slices"
"sort"
"strconv"
"strings"
"github.com/ovn-kubernetes/libovsdb/model"
"github.com/ovn-kubernetes/libovsdb/ovsdb"
"k8s.io/klog/v2"
ovsclient "github.com/kubeovn/kube-ovn/pkg/ovsdb/client"
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
"github.com/kubeovn/kube-ovn/pkg/util"
)
// CreateLoadBalancer create loadbalancer
func (c *OVNNbClient) CreateLoadBalancer(lbName, protocol, selectFields string) error {
var (
exist bool
err error
)
if exist, err = c.LoadBalancerExists(lbName); err != nil {
klog.Errorf("failed to get lb: %v", err)
return err
}
// found, ignore
if exist {
return nil
}
var (
ops []ovsdb.Operation
lb *ovnnb.LoadBalancer
)
lb = &ovnnb.LoadBalancer{
UUID: ovsclient.NamedUUID(),
Name: lbName,
Protocol: &protocol,
ExternalIDs: map[string]string{
"vendor": util.CniTypeName,
},
}
if len(selectFields) != 0 {
lb.SelectionFields = []string{selectFields}
}
if ops, err = c.Create(lb); err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for creating load balancer %s: %w", lbName, err)
}
if err = c.Transact("lb-add", ops); err != nil {
klog.Error(err)
return fmt.Errorf("create load balancer %s: %w", lbName, err)
}
return nil
}
// UpdateLoadBalancer update load balancer
func (c *OVNNbClient) UpdateLoadBalancer(lb *ovnnb.LoadBalancer, fields ...any) error {
var (
ops []ovsdb.Operation
err error
)
if ops, err = c.ovsDbClient.Where(lb).Update(lb, fields...); err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for updating load balancer %s: %w", lb.Name, err)
}
if err = c.Transact("lb-update", ops); err != nil {
klog.Error(err)
return fmt.Errorf("update load balancer %s: %w", lb.Name, err)
}
return nil
}
// LoadBalancerAddVips adds or updates a vip
func (c *OVNNbClient) LoadBalancerAddVip(lbName, vip string, backends ...string) error {
var (
ops []ovsdb.Operation
err error
)
if _, err = c.GetLoadBalancer(lbName, false); err != nil {
klog.Errorf("failed to get lb health check: %v", err)
return err
}
sort.Strings(backends)
if ops, err = c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
var (
mutations = make([]model.Mutation, 0, 2)
value = strings.Join(backends, ",")
)
if len(lb.Vips) != 0 {
if lb.Vips[vip] == value {
return nil
}
mutations = append(
mutations,
model.Mutation{
Field: &lb.Vips,
Value: map[string]string{vip: lb.Vips[vip]},
Mutator: ovsdb.MutateOperationDelete,
},
)
}
mutations = append(
mutations,
model.Mutation{
Field: &lb.Vips,
Value: map[string]string{vip: value},
Mutator: ovsdb.MutateOperationInsert,
},
)
return mutations
},
); err != nil {
return fmt.Errorf("failed to generate operations when adding vip %s with backends %v to load balancers %s: %w", vip, backends, lbName, err)
}
if ops != nil {
if err = c.Transact("lb-add", ops); err != nil {
klog.Error(err)
return fmt.Errorf("failed to add vip %s with backends %v to load balancers %s: %w", vip, backends, lbName, err)
}
}
return nil
}
// LoadBalancerDeleteVip deletes load balancer vip
func (c *OVNNbClient) LoadBalancerDeleteVip(lbName, vipEndpoint string, ignoreHealthCheck bool) error {
var (
ops []ovsdb.Operation
lb *ovnnb.LoadBalancer
lbhc *ovnnb.LoadBalancerHealthCheck
err error
)
lb, lbhc, err = c.GetLoadBalancerHealthCheck(lbName, vipEndpoint, true)
if err != nil {
klog.Errorf("failed to get lb health check: %v", err)
return err
}
if len(lb.IPPortMappings) != 0 {
ignoreHealthCheck = false
}
if !ignoreHealthCheck && lbhc != nil {
klog.Infof("clean health check for lb %s with vip %s", lbName, vipEndpoint)
// delete ip port mapping
if err = c.LoadBalancerDeleteVipIPPortMapping(lbName, vipEndpoint); err != nil {
klog.Errorf("failed to delete lb ip port mapping: %v", err)
return err
}
if err = c.LoadBalancerDeleteHealthCheck(lbName, lbhc.UUID); err != nil {
klog.Errorf("failed to delete lb health check: %v", err)
return err
}
}
if lb == nil || len(lb.Vips) == 0 {
return nil
}
if _, ok := lb.Vips[vipEndpoint]; !ok {
return nil
}
ops, err = c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
return []model.Mutation{
{
Field: &lb.Vips,
Value: map[string]string{vipEndpoint: lb.Vips[vipEndpoint]},
Mutator: ovsdb.MutateOperationDelete,
},
}
},
)
if err != nil {
klog.Error(err)
return fmt.Errorf("failed to generate operations when deleting vip %s from load balancers %s: %w", vipEndpoint, lbName, err)
}
if len(ops) == 0 {
return nil
}
if err = c.Transact("lb-add", ops); err != nil {
klog.Error(err)
return fmt.Errorf("failed to delete vip %s from load balancers %s: %w", vipEndpoint, lbName, err)
}
return nil
}
// SetLoadBalancerAffinityTimeout sets the LB's affinity timeout in seconds
func (c *OVNNbClient) SetLoadBalancerAffinityTimeout(lbName string, timeout int) error {
var (
options map[string]string
lb *ovnnb.LoadBalancer
value string
err error
)
if lb, err = c.GetLoadBalancer(lbName, false); err != nil {
klog.Errorf("failed to get lb: %v", err)
return err
}
if value = strconv.Itoa(timeout); len(lb.Options) != 0 && lb.Options["affinity_timeout"] == value {
return nil
}
options = make(map[string]string, len(lb.Options)+1)
maps.Copy(options, lb.Options)
options["affinity_timeout"] = value
lb.Options = options
if err = c.UpdateLoadBalancer(lb, &lb.Options); err != nil {
klog.Error(err)
return fmt.Errorf("failed to set affinity timeout of lb %s to %d: %w", lbName, timeout, err)
}
return nil
}
// SetLoadBalancerPreferLocalBackend sets the LB's affinity timeout in seconds
func (c *OVNNbClient) SetLoadBalancerPreferLocalBackend(lbName string, preferLocalBackend bool) error {
var (
options map[string]string
lb *ovnnb.LoadBalancer
value string
err error
)
if lb, err = c.GetLoadBalancer(lbName, false); err != nil {
klog.Errorf("failed to get lb: %v", err)
return err
}
if preferLocalBackend {
value = "true"
} else {
value = "false"
}
if len(lb.Options) != 0 && lb.Options["prefer_local_backend"] == value {
return nil
}
options = make(map[string]string, len(lb.Options)+1)
maps.Copy(options, lb.Options)
options["prefer_local_backend"] = value
lb.Options = options
if err = c.UpdateLoadBalancer(lb, &lb.Options); err != nil {
klog.Error(err)
return fmt.Errorf("failed to set prefer local backend of lb %s to %s: %w", lbName, value, err)
}
return nil
}
// DeleteLoadBalancers delete several loadbalancer once
func (c *OVNNbClient) DeleteLoadBalancers(filter func(lb *ovnnb.LoadBalancer) bool) error {
var (
ops []ovsdb.Operation
err error
)
if ops, err = c.ovsDbClient.WhereCache(
func(lb *ovnnb.LoadBalancer) bool {
if filter != nil {
return filter(lb)
}
return true
},
).Delete(); err != nil {
klog.Error(err)
return fmt.Errorf("generate operations for delete load balancers: %w", err)
}
if err = c.Transact("lb-del", ops); err != nil {
klog.Errorf("failed to del lbs: %v", err)
return fmt.Errorf("delete load balancers : %w", err)
}
return nil
}
// DeleteLoadBalancer delete loadbalancer
func (c *OVNNbClient) DeleteLoadBalancer(lbName string) error {
var (
ops []ovsdb.Operation
err error
)
ops, err = c.DeleteLoadBalancerOp(lbName)
if err != nil {
klog.Errorf("failed to get delete lb op: %v", err)
return err
}
if err = c.Transact("lb-del", ops); err != nil {
klog.Errorf("failed to del lb: %v", err)
return fmt.Errorf("delete load balancer %s: %w", lbName, err)
}
return nil
}
// GetLoadBalancer get load balancer by name,
// it is because of lack name index that doesn't use OVNNbClient.Get
func (c *OVNNbClient) GetLoadBalancer(lbName string, ignoreNotFound bool) (*ovnnb.LoadBalancer, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
var (
lbList []ovnnb.LoadBalancer
err error
)
lbList = make([]ovnnb.LoadBalancer, 0)
if err = c.ovsDbClient.WhereCache(
func(lb *ovnnb.LoadBalancer) bool {
return lb.Name == lbName
},
).List(ctx, &lbList); err != nil {
klog.Error(err)
return nil, fmt.Errorf("failed to list load balancer %q: %w", lbName, err)
}
switch {
// not found
case len(lbList) == 0:
if ignoreNotFound {
return nil, nil
}
return nil, fmt.Errorf("not found load balancer %q", lbName)
case len(lbList) > 1:
return nil, fmt.Errorf("more than one load balancer with same name %q", lbName)
default:
// #nosec G602
return &lbList[0], nil
}
}
func (c *OVNNbClient) LoadBalancerExists(lbName string) (bool, error) {
lrp, err := c.GetLoadBalancer(lbName, true)
return lrp != nil, err
}
// ListLoadBalancers list all load balancers
func (c *OVNNbClient) ListLoadBalancers(filter func(lb *ovnnb.LoadBalancer) bool) ([]ovnnb.LoadBalancer, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
var (
lbList []ovnnb.LoadBalancer
err error
)
lbList = make([]ovnnb.LoadBalancer, 0)
if err = c.ovsDbClient.WhereCache(
func(lb *ovnnb.LoadBalancer) bool {
if filter != nil {
return filter(lb)
}
return true
},
).List(ctx, &lbList); err != nil {
klog.Error(err)
return nil, fmt.Errorf("failed to list load balancer: %w", err)
}
return lbList, nil
}
func (c *OVNNbClient) LoadBalancerOp(lbName string, mutationsFunc ...func(lb *ovnnb.LoadBalancer) []model.Mutation) ([]ovsdb.Operation, error) {
var (
mutations []model.Mutation
ops []ovsdb.Operation
lb *ovnnb.LoadBalancer
err error
)
if lb, err = c.GetLoadBalancer(lbName, false); err != nil {
klog.Error(err)
return nil, err
}
if len(mutationsFunc) == 0 {
return nil, nil
}
mutations = make([]model.Mutation, 0, len(mutationsFunc))
for _, f := range mutationsFunc {
if mutation := f(lb); mutation != nil {
mutations = append(mutations, mutation...)
}
}
if len(mutations) == 0 {
return nil, nil
}
if ops, err = c.ovsDbClient.Where(lb).Mutate(lb, mutations...); err != nil {
klog.Error(err)
return nil, fmt.Errorf("generate operations for mutating load balancer %s: %w", lb.Name, err)
}
return ops, nil
}
// DeleteLoadBalancerOp create operation which delete load balancer
func (c *OVNNbClient) DeleteLoadBalancerOp(lbName string) ([]ovsdb.Operation, error) {
var (
ops []ovsdb.Operation
lb *ovnnb.LoadBalancer
err error
)
if lb, err = c.GetLoadBalancer(lbName, true); err != nil {
klog.Error(err)
return nil, err
}
// not found, skip
if lb == nil {
return nil, nil
}
if ops, err = c.Where(lb).Delete(); err != nil {
klog.Error(err)
return nil, err
}
return ops, nil
}
// LoadBalancerAddIPPortMapping add load balancer ip port mapping
func (c *OVNNbClient) LoadBalancerAddIPPortMapping(lbName, vipEndpoint string, mappings map[string]string) error {
if len(mappings) == 0 {
return nil
}
var (
ops []ovsdb.Operation
err error
)
if ops, err = c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
return []model.Mutation{
{
Field: &lb.IPPortMappings,
Value: mappings,
Mutator: ovsdb.MutateOperationInsert,
},
}
},
); err != nil {
klog.Error(err)
return fmt.Errorf("failed to generate operations when adding ip port mapping with vip %v to load balancers %s: %w", vipEndpoint, lbName, err)
}
if err = c.Transact("lb-add", ops); err != nil {
klog.Error(err)
return fmt.Errorf("failed to add ip port mapping with vip %v to load balancers %s: %w", vipEndpoint, lbName, err)
}
return nil
}
// LoadBalancerDeleteIPPortMapping deletes IP port mappings for a specific backend IP from a load balancer.
func (c *OVNNbClient) LoadBalancerDeleteIPPortMapping(lbName, backendIP string) error {
ops, err := c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
if _, ok := lb.IPPortMappings[backendIP]; !ok {
return nil
}
return []model.Mutation{
{
Field: &lb.IPPortMappings,
Value: map[string]string{backendIP: lb.IPPortMappings[backendIP]},
Mutator: ovsdb.MutateOperationDelete,
},
}
},
)
if err != nil {
klog.Error(err)
return fmt.Errorf("failed to generate operations when deleting IP port mapping for backend %s from load balancer %s: %w", backendIP, lbName, err)
}
if len(ops) == 0 {
return nil
}
if err = c.Transact("lb-del", ops); err != nil {
return fmt.Errorf("failed to delete IP port mapping for backend %s from load balancer %s: %w", backendIP, lbName, err)
}
klog.Infof("successfully deleted ip port mapping for backend %s from load balancer %s", backendIP, lbName)
return nil
}
// LoadBalancerDeleteVipIPPortMapping deletes IP port mappings for a specific VIP from a load balancer.
// This function ensures that only backend IPs that are no longer referenced by any VIP are removed.
func (c *OVNNbClient) LoadBalancerDeleteVipIPPortMapping(lbName, vipEndpoint string) error {
lb, err := c.getLoadBalancerForDeletion(lbName)
if err != nil {
klog.Errorf("failed to get load balancer for deletion: %v", err)
return err
}
if lb == nil {
return nil
}
targetBackendIPs, err := c.extractBackendIPsFromVIP(lb, vipEndpoint)
if err != nil {
klog.Errorf("failed to extract backend IPs from VIP: %v", err)
return err
}
if len(targetBackendIPs) == 0 {
return nil
}
unusedBackendIPs := c.findUnusedBackendIPs(lb, vipEndpoint, targetBackendIPs)
return c.deleteUnusedIPPortMappings(lbName, vipEndpoint, unusedBackendIPs)
}
// getLoadBalancerForDeletion retrieves the load balancer and performs initial validation
func (c *OVNNbClient) getLoadBalancerForDeletion(lbName string) (*ovnnb.LoadBalancer, error) {
lb, err := c.GetLoadBalancer(lbName, true)
if err != nil {
klog.Errorf("failed to get load balancer %s: %v", lbName, err)
return nil, err
}
if lb == nil {
klog.Infof("load balancer %s already deleted", lbName)
return nil, nil
}
if len(lb.IPPortMappings) == 0 {
klog.Infof("load balancer %s has no IP port mappings", lbName)
return nil, nil
}
return lb, nil
}
// extractBackendIPsFromVIP extracts the backend IPs that are used by a specific VIP
func (c *OVNNbClient) extractBackendIPsFromVIP(lb *ovnnb.LoadBalancer, vipEndpoint string) (map[string]bool, error) {
vipBackends, exists := lb.Vips[vipEndpoint]
if !exists {
klog.Infof("VIP %s not found in load balancer", vipEndpoint)
return nil, nil
}
backendIPs := make(map[string]bool)
for backend := range strings.SplitSeq(vipBackends, ",") {
if backendIP, _, err := net.SplitHostPort(backend); err == nil {
backendIPs[backendIP] = true
}
}
klog.V(4).Infof("VIP %s uses %d backend IPs: %v", vipEndpoint, len(backendIPs), getMapKeys(backendIPs))
return backendIPs, nil
}
// findUnusedBackendIPs identifies which backend IPs are no longer used by any other VIP
func (c *OVNNbClient) findUnusedBackendIPs(lb *ovnnb.LoadBalancer, targetVIP string, targetBackendIPs map[string]bool) map[string]string {
unusedBackendIPs := make(map[string]string)
for backendIP := range targetBackendIPs {
if !c.isBackendIPStillUsed(lb, targetVIP, backendIP) {
if portMapping, exists := lb.IPPortMappings[backendIP]; exists {
unusedBackendIPs[backendIP] = portMapping
}
}
}
klog.V(4).Infof("Found %d unused backend IPs for VIP %s", len(unusedBackendIPs), targetVIP)
return unusedBackendIPs
}
// isBackendIPStillUsed checks if a backend IP is still referenced by any other VIP
func (c *OVNNbClient) isBackendIPStillUsed(lb *ovnnb.LoadBalancer, targetVIP, backendIP string) bool {
for otherVIP, otherBackends := range lb.Vips {
if otherVIP == targetVIP {
continue
}
for otherBackend := range strings.SplitSeq(otherBackends, ",") {
if otherBackendIP, _, err := net.SplitHostPort(otherBackend); err == nil && otherBackendIP == backendIP {
klog.V(5).Infof("Backend IP %s is still used by VIP %s", backendIP, otherVIP)
return true
}
}
}
klog.V(5).Infof("Backend IP %s is no longer used by any other VIP", backendIP)
return false
}
// deleteUnusedIPPortMappings performs the actual deletion of unused IP port mappings
func (c *OVNNbClient) deleteUnusedIPPortMappings(lbName, vipEndpoint string, unusedBackendIPs map[string]string) error {
if len(unusedBackendIPs) == 0 {
klog.Infof("no unused backend IPs to delete for VIP %s in load balancer %s", vipEndpoint, lbName)
return nil
}
ops, err := c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
return []model.Mutation{
{
Field: &lb.IPPortMappings,
Value: unusedBackendIPs,
Mutator: ovsdb.MutateOperationDelete,
},
}
},
)
if err != nil {
klog.Error(err)
return fmt.Errorf("failed to generate operations when deleting IP port mappings for VIP %s from load balancer %s: %w", vipEndpoint, lbName, err)
}
if len(ops) == 0 {
return nil
}
if err = c.Transact("lb-del", ops); err != nil {
return fmt.Errorf("failed to delete IP port mappings for VIP %s from load balancer %s: %w", vipEndpoint, lbName, err)
}
klog.Infof("successfully deleted %d unused backend IPs for VIP %s from load balancer %s",
len(unusedBackendIPs), vipEndpoint, lbName)
return nil
}
// getMapKeys returns the keys of a map as a slice
func getMapKeys(m map[string]bool) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
// LoadBalancerUpdateIPPortMapping update load balancer ip port mapping
func (c *OVNNbClient) LoadBalancerUpdateIPPortMapping(lbName, vipEndpoint string, ipPortMappings map[string]string) error {
ops, err := c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
return []model.Mutation{
{
Field: &lb.IPPortMappings,
Value: ipPortMappings,
Mutator: ovsdb.MutateOperationInsert,
},
}
},
)
if err != nil {
return fmt.Errorf("failed to generate operations when adding ip port mapping with vip %v to load balancers %s: %w", vipEndpoint, lbName, err)
}
if err = c.Transact("lb-add", ops); err != nil {
return fmt.Errorf("failed to add ip port mapping with vip %v to load balancers %s: %w", vipEndpoint, lbName, err)
}
return nil
}
// LoadBalancerAddHealthCheck adds health check
func (c *OVNNbClient) LoadBalancerAddHealthCheck(lbName, vipEndpoint string, ignoreHealthCheck bool, ipPortMapping, externals map[string]string) error {
klog.Infof("lb %s health check use ip port mapping %v", lbName, ipPortMapping)
if err := c.LoadBalancerUpdateIPPortMapping(lbName, vipEndpoint, ipPortMapping); err != nil {
klog.Errorf("failed to update lb ip port mapping: %v", err)
return err
}
if !ignoreHealthCheck {
klog.Infof("add health check for lb %s with vip %s and health check vip maps %v", lbName, vipEndpoint, ipPortMapping)
if err := c.AddLoadBalancerHealthCheck(lbName, vipEndpoint, externals); err != nil {
klog.Errorf("failed to create lb health check: %v", err)
return err
}
}
return nil
}
// LoadBalancerDeleteHealthCheck delete load balancer health check
func (c *OVNNbClient) LoadBalancerDeleteHealthCheck(lbName, uuid string) error {
var (
ops []ovsdb.Operation
lb *ovnnb.LoadBalancer
err error
)
if lb, err = c.GetLoadBalancer(lbName, false); err != nil {
klog.Errorf("failed to get lb: %v", err)
return err
}
if slices.Contains(lb.HealthCheck, uuid) {
ops, err = c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
return []model.Mutation{
{
Field: &lb.HealthCheck,
Value: []string{uuid},
Mutator: ovsdb.MutateOperationDelete,
},
}
},
)
if err != nil {
return fmt.Errorf("failed to generate operations when deleting health check %s from load balancers %s: %w", uuid, lbName, err)
}
if len(ops) == 0 {
return nil
}
if err = c.Transact("lb-hc-del", ops); err != nil {
return fmt.Errorf("failed to delete health check %s from load balancers %s: %w", uuid, lbName, err)
}
}
return nil
}
// LoadBalancerUpdateHealthCheckOp create operations add to or delete health check from it
func (c *OVNNbClient) LoadBalancerUpdateHealthCheckOp(lbName string, lbhcUUIDs []string, op ovsdb.Mutator) ([]ovsdb.Operation, error) {
if len(lbhcUUIDs) == 0 {
return nil, nil
}
return c.LoadBalancerOp(
lbName,
func(lb *ovnnb.LoadBalancer) []model.Mutation {
return []model.Mutation{
{
Field: &lb.HealthCheck,
Value: lbhcUUIDs,
Mutator: op,
},
}
},
)
}