Skip to content

Commit

Permalink
cni-server: reconcile ovn0 routes periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Jun 19, 2023
1 parent e4f6826 commit 7d9968d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
7 changes: 6 additions & 1 deletion pkg/daemon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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)
Expand Down
92 changes: 47 additions & 45 deletions pkg/daemon/controller_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit 7d9968d

Please sign in to comment.