Skip to content

Commit

Permalink
fix ovn lb gc (#2728)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Apr 27, 2023
1 parent 4a4397c commit af16c76
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
7 changes: 2 additions & 5 deletions pkg/controller/endpoint.go
Expand Up @@ -115,11 +115,8 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
}
svc := orisvc.DeepCopy()

clusterIPs := svc.Spec.ClusterIPs
if len(clusterIPs) == 0 && svc.Spec.ClusterIP != "" && svc.Spec.ClusterIP != v1.ClusterIPNone {
clusterIPs = []string{svc.Spec.ClusterIP}
}
if len(clusterIPs) == 0 || clusterIPs[0] == v1.ClusterIPNone {
clusterIPs := util.ServiceClusterIPs(*svc)
if len(clusterIPs) == 0 {
return nil
}

Expand Down
50 changes: 30 additions & 20 deletions pkg/controller/gc.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

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

func (c *Controller) gcLoadBalancer() error {
klog.Infof("start to gc loadbalancers")
start := time.Now()
if !c.config.EnableLb {
// remove lb from logical switch
vpcs, err := c.vpcsLister.List(labels.Everything())
Expand Down Expand Up @@ -385,24 +387,27 @@ func (c *Controller) gcLoadBalancer() error {
klog.Errorf("failed to list svc, %v", err)
return err
}
tcpVips := []string{}
udpVips := []string{}
tcpSessionVips := []string{}
udpSessionVips := []string{}
tcpVips := make(map[string]struct{}, len(svcs)*2)
udpVips := make(map[string]struct{}, len(svcs)*2)
tcpSessionVips := make(map[string]struct{}, len(svcs)*2)
udpSessionVips := make(map[string]struct{}, len(svcs)*2)
for _, svc := range svcs {
ip := svc.Spec.ClusterIP
for _, port := range svc.Spec.Ports {
if port.Protocol == corev1.ProtocolTCP {
if svc.Spec.SessionAffinity == corev1.ServiceAffinityClientIP {
tcpSessionVips = append(tcpSessionVips, fmt.Sprintf("%s:%d", ip, port.Port))
} else {
tcpVips = append(tcpVips, fmt.Sprintf("%s:%d", ip, port.Port))
}
} else {
if svc.Spec.SessionAffinity == corev1.ServiceAffinityClientIP {
udpSessionVips = append(udpSessionVips, fmt.Sprintf("%s:%d", ip, port.Port))
ips := util.ServiceClusterIPs(*svc)
for _, ip := range ips {
for _, port := range svc.Spec.Ports {
vip := util.JoinHostPort(ip, port.Port)
if port.Protocol == corev1.ProtocolTCP {
if svc.Spec.SessionAffinity == corev1.ServiceAffinityClientIP {
tcpSessionVips[vip] = struct{}{}
} else {
tcpVips[vip] = struct{}{}
}
} else {
udpVips = append(udpVips, fmt.Sprintf("%s:%d", ip, port.Port))
if svc.Spec.SessionAffinity == corev1.ServiceAffinityClientIP {
udpSessionVips[vip] = struct{}{}
} else {
udpVips[vip] = struct{}{}
}
}
}
}
Expand Down Expand Up @@ -430,7 +435,8 @@ func (c *Controller) gcLoadBalancer() error {
return err
}
for vip := range vips {
if !util.IsStringIn(vip, tcpVips) {
if _, ok := tcpVips[vip]; !ok {
klog.Infof("gc vip %s in LB %s", vip, tcpLb)
err := c.ovnLegacyClient.DeleteLoadBalancerVip(vip, tcpLb)
if err != nil {
klog.Errorf("failed to delete vip %s from tcp lb %s, %v", vip, tcpLb, err)
Expand All @@ -451,7 +457,8 @@ func (c *Controller) gcLoadBalancer() error {
return err
}
for vip := range vips {
if !util.IsStringIn(vip, tcpSessionVips) {
if _, ok := tcpSessionVips[vip]; !ok {
klog.Infof("gc vip %s in LB %s", vip, tcpSessLb)
err := c.ovnLegacyClient.DeleteLoadBalancerVip(vip, tcpSessLb)
if err != nil {
klog.Errorf("failed to delete vip %s from tcp session lb %s, %v", vip, tcpSessLb, err)
Expand All @@ -473,7 +480,8 @@ func (c *Controller) gcLoadBalancer() error {
return err
}
for vip := range vips {
if !util.IsStringIn(vip, udpVips) {
if _, ok := udpVips[vip]; !ok {
klog.Infof("gc vip %s in LB %s", vip, udpLb)
err := c.ovnLegacyClient.DeleteLoadBalancerVip(vip, udpLb)
if err != nil {
klog.Errorf("failed to delete vip %s from tcp lb %s, %v", vip, udpLb, err)
Expand All @@ -495,7 +503,8 @@ func (c *Controller) gcLoadBalancer() error {
return err
}
for vip := range vips {
if !util.IsStringIn(vip, udpSessionVips) {
if _, ok := udpSessionVips[vip]; !ok {
klog.Infof("gc vip %s in LB %s", vip, udpSessLb)
err := c.ovnLegacyClient.DeleteLoadBalancerVip(vip, udpSessLb)
if err != nil {
klog.Errorf("failed to delete vip %s from udp session lb %s, %v", vip, udpSessLb, err)
Expand Down Expand Up @@ -523,6 +532,7 @@ func (c *Controller) gcLoadBalancer() error {
return err
}
}
klog.Infof("took %.2fs to gc load balancers", time.Since(start).Seconds())
return nil
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/controller/network_policy.go
Expand Up @@ -743,10 +743,7 @@ func svcMatchPods(svcs []*corev1.Service, pod *corev1.Pod, protocol string) ([]s
return nil, err
}
if isMatch {
clusterIPs := svc.Spec.ClusterIPs
if len(clusterIPs) == 0 && svc.Spec.ClusterIP != "" && svc.Spec.ClusterIP != corev1.ClusterIPNone {
clusterIPs = []string{svc.Spec.ClusterIP}
}
clusterIPs := util.ServiceClusterIPs(*svc)
protocolClusterIPs := getProtocolSvcIp(clusterIPs, protocol)
if len(protocolClusterIPs) != 0 {
matchSvcs = append(matchSvcs, protocolClusterIPs...)
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/k8s.go
Expand Up @@ -44,3 +44,11 @@ func GetNodeInternalIP(node v1.Node) (ipv4, ipv6 string) {

return SplitStringIP(strings.Join(ips, ","))
}

func ServiceClusterIPs(svc v1.Service) []string {
ips := svc.Spec.ClusterIPs
if len(ips) == 0 && svc.Spec.ClusterIP != v1.ClusterIPNone && svc.Spec.ClusterIP != "" {
ips = []string{svc.Spec.ClusterIP}
}
return ips
}

0 comments on commit af16c76

Please sign in to comment.