Skip to content

Commit

Permalink
fix: when delete node recycle related ip/route resource
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Nov 28, 2019
1 parent 1383a6f commit 7617fa7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
13 changes: 7 additions & 6 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (c *Controller) handleAddNode(key string) error {

nodeAddr := getNodeInternalIP(node)
if util.CheckProtocol(nodeAddr) == util.CheckProtocol(nic.IpAddress) {
err = c.ovnClient.AddStaticRouter("", nodeAddr, strings.Split(nic.IpAddress, "/")[0], c.config.ClusterRouter)
err = c.ovnClient.AddStaticRoute("", nodeAddr, strings.Split(nic.IpAddress, "/")[0], c.config.ClusterRouter)
if err != nil {
klog.Errorf("failed to add static router from node to ovn0 %v", err)
return err
Expand Down Expand Up @@ -297,25 +297,26 @@ func (c *Controller) handleAddNode(key string) error {
}

func (c *Controller) handleDeleteNode(key string) error {
err := c.ovnClient.DeletePort(fmt.Sprintf("node-%s", key))
portName := fmt.Sprintf("node-%s", key)
err := c.ovnClient.DeletePort(portName)
if err != nil {
klog.Infof("failed to delete node switch port node-%s %v", key, err)
klog.Errorf("failed to delete node switch port node-%s %v", key, err)
return err
}

ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(key, metav1.GetOptions{})
ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(portName, metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}

if err := c.ovnClient.DeleteStaticRouter(ipCr.Spec.IPAddress, c.config.ClusterRouter); err != nil {
if err := c.ovnClient.DeleteStaticRouteByNextHop(ipCr.Spec.IPAddress); err != nil {
return err
}

err = c.config.KubeOvnClient.KubeovnV1().IPs().Delete(key, &metav1.DeleteOptions{})
err = c.config.KubeOvnClient.KubeovnV1().IPs().Delete(portName, &metav1.DeleteOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
Expand Down
19 changes: 15 additions & 4 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,22 @@ func (c *Controller) handleDeletePod(key string) error {
return err
}
} else {
if err := c.ovnClient.DeleteStaticRouter(portAddr[1], c.config.ClusterRouter); err != nil {
if err := c.ovnClient.DeleteStaticRoute(portAddr[1], c.config.ClusterRouter); err != nil {
return err
}
}
return c.ovnClient.DeletePort(ovs.PodNameToPortName(name, namespace))
if err := c.ovnClient.DeletePort(ovs.PodNameToPortName(name, namespace)); err != nil {
klog.Errorf("failed to delete lsp %s, %v", ovs.PodNameToPortName(name, namespace), err)
return err
}

err = c.config.KubeOvnClient.KubeovnV1().IPs().Delete(ovs.PodNameToPortName(name, namespace), &metav1.DeleteOptions{})
if err == nil || k8serrors.IsNotFound(err) {
return nil
}

klog.Errorf("failed to delete ip %s, %v", ovs.PodNameToPortName(name, namespace), err)
return err
}

func (c *Controller) handleUpdatePod(key string) error {
Expand Down Expand Up @@ -698,10 +709,10 @@ func (c *Controller) handleUpdatePod(key string) error {
if err != nil {
return err
}
if err := c.ovnClient.DeleteStaticRouter(podIP, c.config.ClusterRouter); err != nil {
if err := c.ovnClient.DeleteStaticRoute(podIP, c.config.ClusterRouter); err != nil {
return errors.Annotate(err, "del static route failed")
}
if err := c.ovnClient.AddStaticRouter(ovs.PolicySrcIP, podIP, nodeTunlIPAddr.String(), c.config.ClusterRouter); err != nil {
if err := c.ovnClient.AddStaticRoute(ovs.PolicySrcIP, podIP, nodeTunlIPAddr.String(), c.config.ClusterRouter); err != nil {
return errors.Annotate(err, "add static route failed")
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,10 @@ func (c *Controller) reconcileCentralizedGateway(subnet *kubeovnv1.Subnet) error
return err
}

if err := c.ovnClient.DeleteStaticRouter(subnet.Spec.CIDRBlock, c.config.ClusterRouter); err != nil {
if err := c.ovnClient.DeleteStaticRoute(subnet.Spec.CIDRBlock, c.config.ClusterRouter); err != nil {
return errors.Annotate(err, "del static route failed")
}
if err := c.ovnClient.AddStaticRouter(ovs.PolicySrcIP, subnet.Spec.CIDRBlock, nodeTunlIPAddr.String(), c.config.ClusterRouter); err != nil {
if err := c.ovnClient.AddStaticRoute(ovs.PolicySrcIP, subnet.Spec.CIDRBlock, nodeTunlIPAddr.String(), c.config.ClusterRouter); err != nil {
return errors.Annotate(err, "add static route failed")
}

Expand Down Expand Up @@ -669,7 +669,7 @@ func (c *Controller) handleDeleteRoute(key string) error {
return nil
}

return c.ovnClient.DeleteStaticRouter(key, c.config.ClusterRouter)
return c.ovnClient.DeleteStaticRoute(key, c.config.ClusterRouter)
}

func (c *Controller) handleDeleteSubnet(key string) error {
Expand Down
24 changes: 20 additions & 4 deletions pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,37 @@ func (c Client) createRouterPort(ls, lr, ip, mac string) error {
return nil
}

// AddStaticRouter add a static route rule in ovn
func (c Client) AddStaticRouter(policy, cidr, nextHop, router string) error {
// AddStaticRoute add a static route rule in ovn
func (c Client) AddStaticRoute(policy, cidr, nextHop, router string) error {
if policy == "" {
policy = PolicyDstIP
}
_, err := c.ovnNbCommand(MayExist, fmt.Sprintf("%s=%s", Policy, policy), "lr-route-add", router, cidr, nextHop)
return err
}

// DeleteStaticRouter delete a static route rule in ovn
func (c Client) DeleteStaticRouter(cidr, router string) error {
// DeleteStaticRoute delete a static route rule in ovn
func (c Client) DeleteStaticRoute(cidr, router string) error {
_, err := c.ovnNbCommand(IfExists, "lr-route-del", router, cidr)
return err
}

func (c Client) DeleteStaticRouteByNextHop(nextHop string) error {
output, err := c.ovnNbCommand("--format=csv", "--no-heading", "--data=bare", "--columns=ip_prefix", "find", "Logical_Router_Static_Route", fmt.Sprintf("nexthop=%s", nextHop))
if err != nil {
klog.Errorf("failed to list static route %s, %v", nextHop, err)
return err
}
ipPrefixes := strings.Split(output, "\n")
for _, ipPre := range ipPrefixes {
if err := c.DeleteStaticRoute(ipPre, c.ClusterRouter); err != nil {
klog.Errorf("failed to delete route %s, %v", ipPre, err)
return err
}
}
return nil
}

// FindLoadbalancer find ovn loadbalancer uuid by name
func (c Client) FindLoadbalancer(lb string) (string, error) {
output, err := c.ovnNbCommand("--data=bare", "--no-heading", "--columns=_uuid",
Expand Down

0 comments on commit 7617fa7

Please sign in to comment.