4
4
"context"
5
5
"fmt"
6
6
"strings"
7
+ "time"
7
8
8
9
corev1 "k8s.io/api/core/v1"
9
10
k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -322,6 +323,7 @@ func (c *Controller) markAndCleanLSP() error {
322
323
323
324
func (c * Controller ) gcLoadBalancer () error {
324
325
klog .Infof ("start to gc loadbalancers" )
326
+ start := time .Now ()
325
327
if ! c .config .EnableLb {
326
328
// remove lb from logical switch
327
329
vpcs , err := c .vpcsLister .List (labels .Everything ())
@@ -385,24 +387,27 @@ func (c *Controller) gcLoadBalancer() error {
385
387
klog .Errorf ("failed to list svc, %v" , err )
386
388
return err
387
389
}
388
- tcpVips := [] string {}
389
- udpVips := [] string {}
390
- tcpSessionVips := [] string {}
391
- udpSessionVips := [] string {}
390
+ tcpVips := make ( map [ string ] struct {}, len ( svcs ) * 2 )
391
+ udpVips := make ( map [ string ] struct {}, len ( svcs ) * 2 )
392
+ tcpSessionVips := make ( map [ string ] struct {}, len ( svcs ) * 2 )
393
+ udpSessionVips := make ( map [ string ] struct {}, len ( svcs ) * 2 )
392
394
for _ , svc := range svcs {
393
- ip := svc .Spec .ClusterIP
394
- for _ , port := range svc .Spec .Ports {
395
- if port .Protocol == corev1 .ProtocolTCP {
396
- if svc .Spec .SessionAffinity == corev1 .ServiceAffinityClientIP {
397
- tcpSessionVips = append (tcpSessionVips , fmt .Sprintf ("%s:%d" , ip , port .Port ))
398
- } else {
399
- tcpVips = append (tcpVips , fmt .Sprintf ("%s:%d" , ip , port .Port ))
400
- }
401
- } else {
402
- if svc .Spec .SessionAffinity == corev1 .ServiceAffinityClientIP {
403
- udpSessionVips = append (udpSessionVips , fmt .Sprintf ("%s:%d" , ip , port .Port ))
395
+ ips := util .ServiceClusterIPs (* svc )
396
+ for _ , ip := range ips {
397
+ for _ , port := range svc .Spec .Ports {
398
+ vip := util .JoinHostPort (ip , port .Port )
399
+ if port .Protocol == corev1 .ProtocolTCP {
400
+ if svc .Spec .SessionAffinity == corev1 .ServiceAffinityClientIP {
401
+ tcpSessionVips [vip ] = struct {}{}
402
+ } else {
403
+ tcpVips [vip ] = struct {}{}
404
+ }
404
405
} else {
405
- udpVips = append (udpVips , fmt .Sprintf ("%s:%d" , ip , port .Port ))
406
+ if svc .Spec .SessionAffinity == corev1 .ServiceAffinityClientIP {
407
+ udpSessionVips [vip ] = struct {}{}
408
+ } else {
409
+ udpVips [vip ] = struct {}{}
410
+ }
406
411
}
407
412
}
408
413
}
@@ -430,7 +435,8 @@ func (c *Controller) gcLoadBalancer() error {
430
435
return err
431
436
}
432
437
for vip := range vips {
433
- if ! util .IsStringIn (vip , tcpVips ) {
438
+ if _ , ok := tcpVips [vip ]; ! ok {
439
+ klog .Infof ("gc vip %s in LB %s" , vip , tcpLb )
434
440
err := c .ovnLegacyClient .DeleteLoadBalancerVip (vip , tcpLb )
435
441
if err != nil {
436
442
klog .Errorf ("failed to delete vip %s from tcp lb %s, %v" , vip , tcpLb , err )
@@ -451,7 +457,8 @@ func (c *Controller) gcLoadBalancer() error {
451
457
return err
452
458
}
453
459
for vip := range vips {
454
- if ! util .IsStringIn (vip , tcpSessionVips ) {
460
+ if _ , ok := tcpSessionVips [vip ]; ! ok {
461
+ klog .Infof ("gc vip %s in LB %s" , vip , tcpSessLb )
455
462
err := c .ovnLegacyClient .DeleteLoadBalancerVip (vip , tcpSessLb )
456
463
if err != nil {
457
464
klog .Errorf ("failed to delete vip %s from tcp session lb %s, %v" , vip , tcpSessLb , err )
@@ -473,7 +480,8 @@ func (c *Controller) gcLoadBalancer() error {
473
480
return err
474
481
}
475
482
for vip := range vips {
476
- if ! util .IsStringIn (vip , udpVips ) {
483
+ if _ , ok := udpVips [vip ]; ! ok {
484
+ klog .Infof ("gc vip %s in LB %s" , vip , udpLb )
477
485
err := c .ovnLegacyClient .DeleteLoadBalancerVip (vip , udpLb )
478
486
if err != nil {
479
487
klog .Errorf ("failed to delete vip %s from tcp lb %s, %v" , vip , udpLb , err )
@@ -495,7 +503,8 @@ func (c *Controller) gcLoadBalancer() error {
495
503
return err
496
504
}
497
505
for vip := range vips {
498
- if ! util .IsStringIn (vip , udpSessionVips ) {
506
+ if _ , ok := udpSessionVips [vip ]; ! ok {
507
+ klog .Infof ("gc vip %s in LB %s" , vip , udpSessLb )
499
508
err := c .ovnLegacyClient .DeleteLoadBalancerVip (vip , udpSessLb )
500
509
if err != nil {
501
510
klog .Errorf ("failed to delete vip %s from udp session lb %s, %v" , vip , udpSessLb , err )
@@ -523,6 +532,7 @@ func (c *Controller) gcLoadBalancer() error {
523
532
return err
524
533
}
525
534
}
535
+ klog .Infof ("took %.2fs to gc load balancers" , time .Since (start ).Seconds ())
526
536
return nil
527
537
}
528
538
0 commit comments