Skip to content

Commit

Permalink
fix node gc (#4040)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
  • Loading branch information
zhangzujian committed May 22, 2024
1 parent 7bec212 commit ad3ddaf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
76 changes: 56 additions & 20 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -450,18 +451,46 @@ func (c *Controller) handleNodeAnnotationsForProviderNetworks(node *v1.Node) err

func (c *Controller) handleDeleteNode(key string) error {
portName := fmt.Sprintf("node-%s", key)
klog.Infof("delete logical switch port %s", portName)
if err := c.ovnLegacyClient.DeleteLogicalSwitchPort(portName); err != nil {
klog.Errorf("failed to delete node switch port node-%s: %v", key, err)
ok, err := c.ovnLegacyClient.LogicalSwitchPortExists(portName)
if err != nil {
klog.Error(err)
return err
}
if err := c.ovnLegacyClient.DeleteChassisByNode(key); err != nil {
klog.Errorf("failed to delete chassis for node %s: %v", key, err)
return err
if ok {
addresses, err := c.ovnLegacyClient.GetLogicalSwitchPortAddress(portName)
if err != nil {
klog.Error(err)
return err
}
ips := make([]string, 0, 2)
for _, addr := range addresses {
fields := strings.Fields(addr)
for idx := len(fields) - 1; idx >= 0; idx-- {
if ip := net.ParseIP(fields[idx]); ip != nil {
ips = append(ips, ip.String())
continue
}
break
}
}

for _, ip := range ips {
klog.Infof("deleting logical router policy with nexthop %q from %s for node %s", ip, c.config.ClusterRouter, key)
if err = c.ovnLegacyClient.DeletePolicyRouteByNexthop(c.config.ClusterRouter, util.NodeRouterPolicyPriority, ip); err != nil {
klog.Errorf("failed to delete logical router policy with nexthop %q from %s for node %s: %v", ip, c.config.ClusterRouter, key, err)
return err
}
}

klog.Infof("delete logical switch port %s", portName)
if err = c.ovnLegacyClient.DeleteLogicalSwitchPort(portName); err != nil {
klog.Errorf("failed to delete node switch port %s: %v", portName, err)
return err
}
}

if err := c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), portName, metav1.DeleteOptions{}); err != nil && !k8serrors.IsNotFound(err) {
klog.Error(err)
if err := c.ovnLegacyClient.DeleteChassisByNode(key); err != nil {
klog.Errorf("failed to delete chassis for node %s: %v", key, err)
return err
}

Expand All @@ -471,21 +500,11 @@ func (c *Controller) handleDeleteNode(key string) error {
klog.Errorf("failed to delete port group %s for node, %v", portName, err)
return err
}
if err := c.deletePolicyRouteForNode(key); err != nil {
if err = c.deletePolicyRouteForNode(key, portName); err != nil {
klog.Errorf("failed to delete policy route for node %s: %v", key, err)
return err
}

addresses := c.ipam.GetPodAddress(portName)
for _, addr := range addresses {
if addr.Ip == "" {
continue
}
if err := c.ovnLegacyClient.DeletePolicyRouteByNexthop(c.config.ClusterRouter, util.NodeRouterPolicyPriority, addr.Ip); err != nil {
klog.Errorf("failed to delete router policy for node %s: %v", key, err)
return err
}
}
if err := c.ovnLegacyClient.DeleteAddressSet(nodeUnderlayAddressSetName(key, 4)); err != nil {
klog.Errorf("failed to delete address set for node %s: %v", key, err)
return err
Expand All @@ -510,6 +529,11 @@ func (c *Controller) handleDeleteNode(key string) error {
}
}

if err = c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), portName, metav1.DeleteOptions{}); err != nil && !k8serrors.IsNotFound(err) {
klog.Errorf("failed to delete ip %s: %v", portName, err)
return err
}

return nil
}

Expand Down Expand Up @@ -1121,13 +1145,25 @@ func (c *Controller) checkPolicyRouteConsistent(cidr, nexthop string, priority i
return false, nil
}

func (c *Controller) deletePolicyRouteForNode(nodeName string) error {
func (c *Controller) deletePolicyRouteForNode(nodeName, portName string) error {
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to get subnets %v", err)
return err
}

addresses := c.ipam.GetPodAddress(portName)
for _, addr := range addresses {
if addr.Ip == "" {
continue
}
klog.Infof("deleting logical router policy with nexthop %q from %s for node %s", addr.Ip, c.config.ClusterRouter, nodeName)
if err = c.ovnLegacyClient.DeletePolicyRouteByNexthop(c.config.ClusterRouter, util.NodeRouterPolicyPriority, addr.Ip); err != nil {
klog.Errorf("failed to delete logical router policy with nexthop %q from %s for node %s: %v", addr.Ip, c.config.ClusterRouter, nodeName, err)
return err
}
}

for _, subnet := range subnets {
if (subnet.Spec.Vlan != "" && !subnet.Spec.LogicalGateway) || subnet.Spec.Vpc != util.DefaultVpc || subnet.Name == c.config.NodeSwitch {
continue
Expand Down
10 changes: 3 additions & 7 deletions pkg/util/arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package util

import (
"fmt"
"math/rand"
"math/rand/v2"
"net"
"net/netip"
"sync"
Expand All @@ -15,10 +15,6 @@ import (
"k8s.io/klog/v2"
)

func init() {
rand.Seed(int64(time.Now().Nanosecond()))
}

func ArpResolve(nic, srcIP, dstIP string, timeout time.Duration, maxRetry int) (net.HardwareAddr, int, error) {
target, err := netip.ParseAddr(dstIP)
if err != nil {
Expand Down Expand Up @@ -115,12 +111,12 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
durations := make([]time.Duration, probeNum)
// wait for a random time interval selected uniformly in the range zero to
// PROBE_WAIT seconds
durations[0] = time.Duration(rand.Int63n(int64(probeWait)))
durations[0] = time.Duration(rand.Int64N(int64(probeWait)))
deadline = deadline.Add(durations[0])
for i := 1; i < probeNum; i++ {
// send PROBE_NUM probe packets, each of these probe packets spaced
// randomly and uniformly, PROBE_MIN to PROBE_MAX seconds apart
durations[i] = probeMin + time.Duration(rand.Int63n(int64(probeMax-probeMin)))
durations[i] = probeMin + time.Duration(rand.Int64N(int64(probeMax-probeMin)))
deadline = deadline.Add(durations[i])
}

Expand Down
7 changes: 1 addition & 6 deletions test/unittest/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package ipam

import (
"fmt"
"math/rand"
"math/rand/v2"
"strings"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -13,10 +12,6 @@ import (
"github.com/kubeovn/kube-ovn/pkg/util"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

var _ = Describe("[IPAM]", func() {
subnetName := "test"
ipv4CIDR, ipv6CIDR := "10.16.0.0/16", "fd00::/112"
Expand Down

0 comments on commit ad3ddaf

Please sign in to comment.