Skip to content

Commit af16c76

Browse files
authoredApr 27, 2023
fix ovn lb gc (#2728)
1 parent 4a4397c commit af16c76

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed
 

‎pkg/controller/endpoint.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,8 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
115115
}
116116
svc := orisvc.DeepCopy()
117117

118-
clusterIPs := svc.Spec.ClusterIPs
119-
if len(clusterIPs) == 0 && svc.Spec.ClusterIP != "" && svc.Spec.ClusterIP != v1.ClusterIPNone {
120-
clusterIPs = []string{svc.Spec.ClusterIP}
121-
}
122-
if len(clusterIPs) == 0 || clusterIPs[0] == v1.ClusterIPNone {
118+
clusterIPs := util.ServiceClusterIPs(*svc)
119+
if len(clusterIPs) == 0 {
123120
return nil
124121
}
125122

‎pkg/controller/gc.go

+30-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
corev1 "k8s.io/api/core/v1"
910
k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -322,6 +323,7 @@ func (c *Controller) markAndCleanLSP() error {
322323

323324
func (c *Controller) gcLoadBalancer() error {
324325
klog.Infof("start to gc loadbalancers")
326+
start := time.Now()
325327
if !c.config.EnableLb {
326328
// remove lb from logical switch
327329
vpcs, err := c.vpcsLister.List(labels.Everything())
@@ -385,24 +387,27 @@ func (c *Controller) gcLoadBalancer() error {
385387
klog.Errorf("failed to list svc, %v", err)
386388
return err
387389
}
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)
392394
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+
}
404405
} 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+
}
406411
}
407412
}
408413
}
@@ -430,7 +435,8 @@ func (c *Controller) gcLoadBalancer() error {
430435
return err
431436
}
432437
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)
434440
err := c.ovnLegacyClient.DeleteLoadBalancerVip(vip, tcpLb)
435441
if err != nil {
436442
klog.Errorf("failed to delete vip %s from tcp lb %s, %v", vip, tcpLb, err)
@@ -451,7 +457,8 @@ func (c *Controller) gcLoadBalancer() error {
451457
return err
452458
}
453459
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)
455462
err := c.ovnLegacyClient.DeleteLoadBalancerVip(vip, tcpSessLb)
456463
if err != nil {
457464
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 {
473480
return err
474481
}
475482
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)
477485
err := c.ovnLegacyClient.DeleteLoadBalancerVip(vip, udpLb)
478486
if err != nil {
479487
klog.Errorf("failed to delete vip %s from tcp lb %s, %v", vip, udpLb, err)
@@ -495,7 +503,8 @@ func (c *Controller) gcLoadBalancer() error {
495503
return err
496504
}
497505
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)
499508
err := c.ovnLegacyClient.DeleteLoadBalancerVip(vip, udpSessLb)
500509
if err != nil {
501510
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 {
523532
return err
524533
}
525534
}
535+
klog.Infof("took %.2fs to gc load balancers", time.Since(start).Seconds())
526536
return nil
527537
}
528538

‎pkg/controller/network_policy.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,7 @@ func svcMatchPods(svcs []*corev1.Service, pod *corev1.Pod, protocol string) ([]s
743743
return nil, err
744744
}
745745
if isMatch {
746-
clusterIPs := svc.Spec.ClusterIPs
747-
if len(clusterIPs) == 0 && svc.Spec.ClusterIP != "" && svc.Spec.ClusterIP != corev1.ClusterIPNone {
748-
clusterIPs = []string{svc.Spec.ClusterIP}
749-
}
746+
clusterIPs := util.ServiceClusterIPs(*svc)
750747
protocolClusterIPs := getProtocolSvcIp(clusterIPs, protocol)
751748
if len(protocolClusterIPs) != 0 {
752749
matchSvcs = append(matchSvcs, protocolClusterIPs...)

‎pkg/util/k8s.go

+8
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ func GetNodeInternalIP(node v1.Node) (ipv4, ipv6 string) {
4444

4545
return SplitStringIP(strings.Join(ips, ","))
4646
}
47+
48+
func ServiceClusterIPs(svc v1.Service) []string {
49+
ips := svc.Spec.ClusterIPs
50+
if len(ips) == 0 && svc.Spec.ClusterIP != v1.ClusterIPNone && svc.Spec.ClusterIP != "" {
51+
ips = []string{svc.Spec.ClusterIP}
52+
}
53+
return ips
54+
}

0 commit comments

Comments
 (0)
Please sign in to comment.