From 2b445cc57bc1687814d9d924476768f3f71f2ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=96=E5=BB=BA?= Date: Mon, 19 Jun 2023 18:06:13 +0800 Subject: [PATCH] cni-server: reconcile ovn0 routes periodically --- pkg/daemon/controller.go | 7 ++- pkg/daemon/controller_linux.go | 92 ++++++++++++++++---------------- pkg/daemon/controller_windows.go | 2 +- 3 files changed, 54 insertions(+), 47 deletions(-) diff --git a/pkg/daemon/controller.go b/pkg/daemon/controller.go index 4c6172c6444..a141bb1f032 100644 --- a/pkg/daemon/controller.go +++ b/pkg/daemon/controller.go @@ -463,7 +463,7 @@ func (c *Controller) processNextSubnetWorkItem() bool { utilruntime.HandleError(fmt.Errorf("expected subnetEvent in workqueue but got %#v", obj)) return nil } - if err := c.reconcileRouters(event); err != nil { + if err := c.reconcileRouters(&event); err != nil { c.subnetQueue.AddRateLimited(event) return fmt.Errorf("error syncing '%s': %s, requeuing", event, err.Error()) } @@ -614,6 +614,11 @@ func (c *Controller) Run(stopCh <-chan struct{}) { go wait.Until(c.runGateway, 3*time.Second, stopCh) go wait.Until(c.loopEncapIpCheck, 3*time.Second, stopCh) go wait.Until(c.ovnMetricsUpdate, 3*time.Second, stopCh) + go wait.Until(func() { + if err := c.reconcileRouters(nil); err != nil { + klog.Errorf("failed to reconcile ovn0 routes: %v", err) + } + }, 3*time.Second, stopCh) go wait.Until(func() { if err := c.markAndCleanInternalPort(); err != nil { klog.Errorf("gc ovs port error: %v", err) diff --git a/pkg/daemon/controller_linux.go b/pkg/daemon/controller_linux.go index bdafce64406..1178a06851c 100644 --- a/pkg/daemon/controller_linux.go +++ b/pkg/daemon/controller_linux.go @@ -121,66 +121,68 @@ func (c *Controller) initRuntime() error { return nil } -func (c *Controller) reconcileRouters(event subnetEvent) error { +func (c *Controller) reconcileRouters(event *subnetEvent) error { subnets, err := c.subnetsLister.List(labels.Everything()) if err != nil { klog.Errorf("failed to list subnets %v", err) return err } - var ok bool - var oldSubnet, newSubnet *kubeovnv1.Subnet - if event.old != nil { - if oldSubnet, ok = event.old.(*kubeovnv1.Subnet); !ok { - klog.Errorf("expected old subnet in subnetEvent but got %#v", event.old) - return nil + if event != nil { + var ok bool + var oldSubnet, newSubnet *kubeovnv1.Subnet + if event.old != nil { + if oldSubnet, ok = event.old.(*kubeovnv1.Subnet); !ok { + klog.Errorf("expected old subnet in subnetEvent but got %#v", event.old) + return nil + } } - } - if event.new != nil { - if newSubnet, ok = event.new.(*kubeovnv1.Subnet); !ok { - klog.Errorf("expected new subnet in subnetEvent but got %#v", event.new) - return nil + if event.new != nil { + if newSubnet, ok = event.new.(*kubeovnv1.Subnet); !ok { + klog.Errorf("expected new subnet in subnetEvent but got %#v", event.new) + return nil + } } - } - // handle policy routing - rulesToAdd, rulesToDel, routesToAdd, routesToDel, err := c.diffPolicyRouting(oldSubnet, newSubnet) - if err != nil { - klog.Errorf("failed to get policy routing difference: %v", err) - return err - } - // add new routes first - for _, r := range routesToAdd { - if err = netlink.RouteReplace(&r); err != nil && !errors.Is(err, syscall.EEXIST) { - klog.Errorf("failed to replace route for subnet %s: %v", newSubnet.Name, err) + // handle policy routing + rulesToAdd, rulesToDel, routesToAdd, routesToDel, err := c.diffPolicyRouting(oldSubnet, newSubnet) + if err != nil { + klog.Errorf("failed to get policy routing difference: %v", err) return err } - } - // next, add new rules - for _, r := range rulesToAdd { - if err = netlink.RuleAdd(&r); err != nil && !errors.Is(err, syscall.EEXIST) { - klog.Errorf("failed to add network rule for subnet %s: %v", newSubnet.Name, err) - return err + // add new routes first + for _, r := range routesToAdd { + if err = netlink.RouteReplace(&r); err != nil && !errors.Is(err, syscall.EEXIST) { + klog.Errorf("failed to replace route for subnet %s: %v", newSubnet.Name, err) + return err + } } - } - // then delete old network rules - for _, r := range rulesToDel { - // loop to delete all matched rules - for { - if err = netlink.RuleDel(&r); err != nil { - if !errors.Is(err, syscall.ENOENT) { - klog.Errorf("failed to delete network rule for subnet %s: %v", oldSubnet.Name, err) - return err + // next, add new rules + for _, r := range rulesToAdd { + if err = netlink.RuleAdd(&r); err != nil && !errors.Is(err, syscall.EEXIST) { + klog.Errorf("failed to add network rule for subnet %s: %v", newSubnet.Name, err) + return err + } + } + // then delete old network rules + for _, r := range rulesToDel { + // loop to delete all matched rules + for { + if err = netlink.RuleDel(&r); err != nil { + if !errors.Is(err, syscall.ENOENT) { + klog.Errorf("failed to delete network rule for subnet %s: %v", oldSubnet.Name, err) + return err + } + break } - break } } - } - // last, delete old network routes - for _, r := range routesToDel { - if err = netlink.RouteDel(&r); err != nil && !errors.Is(err, syscall.ENOENT) { - klog.Errorf("failed to delete route for subnet %s: %v", oldSubnet.Name, err) - return err + // last, delete old network routes + for _, r := range routesToDel { + if err = netlink.RouteDel(&r); err != nil && !errors.Is(err, syscall.ENOENT) { + klog.Errorf("failed to delete route for subnet %s: %v", oldSubnet.Name, err) + return err + } } } diff --git a/pkg/daemon/controller_windows.go b/pkg/daemon/controller_windows.go index 56f9f01588f..65921999a64 100644 --- a/pkg/daemon/controller_windows.go +++ b/pkg/daemon/controller_windows.go @@ -24,7 +24,7 @@ func (c *Controller) initRuntime() error { return nil } -func (c *Controller) reconcileRouters(_ subnetEvent) error { +func (c *Controller) reconcileRouters(_ *subnetEvent) error { klog.Info("reconcile routes") node, err := c.nodesLister.Get(c.config.NodeName) if err != nil {