Skip to content

Commit

Permalink
delete ipam record and static route when gc lsp (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Apr 27, 2022
1 parent 6273d29 commit 51dc924
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"fmt"
"net"
"strings"
"time"

Expand Down Expand Up @@ -247,6 +248,8 @@ func (c *Controller) markAndCleanLSP() error {
if !lastNoPodLSP[lsp] {
noPodLSP[lsp] = true
} else {
nameNsMap, podAddres := c.ovnClient.GetLspExternalIds(lsp)

klog.Infof("gc logical switch port %s", lsp)
if err := c.ovnClient.DeleteLogicalSwitchPort(lsp); err != nil {
klog.Errorf("failed to delete lsp %s, %v", lsp, err)
Expand All @@ -258,6 +261,20 @@ func (c *Controller) markAndCleanLSP() error {
return err
}
}

for podName, nsName := range nameNsMap {
key := fmt.Sprintf("%s/%s", nsName, podName)
c.ipam.ReleaseAddressByPod(key)
}

for _, podAddr := range podAddres {
if net.ParseIP(podAddr).To4() != nil || net.ParseIP(podAddr).To16() != nil {
if err := c.ovnClient.DeleteStaticRoute(podAddr, c.config.ClusterRouter); err != nil {
klog.Errorf("failed to delete static route when gc lsp %s, ip %v", lsp, podAddr)
continue
}
}
}
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2003,3 +2003,38 @@ func (c *Client) SetLBCIDR(svccidr string) error {
}
return nil
}

func (c *Client) GetLspExternalIds(lsp string) (map[string]string, []string) {
result, err := c.CustomFindEntity("Logical_Switch_Port", []string{"external_ids", "addresses"}, fmt.Sprintf("name=%s", lsp))
if err != nil {
klog.Errorf("customFindEntity failed, %v", err)
return nil, nil
}
if len(result) == 0 {
return nil, nil
}

nameNsMap := make(map[string]string, 1)
for _, l := range result[0]["external_ids"] {
if len(strings.TrimSpace(l)) == 0 {
continue
}
parts := strings.Split(strings.TrimSpace(l), "=")
if len(parts) != 2 {
continue
}
if strings.TrimSpace(parts[0]) != "pod" {
continue
}

podInfo := strings.Split(strings.TrimSpace(parts[1]), "/")
if len(podInfo) != 2 {
continue
}
podNs := podInfo[0]
podName := podInfo[1]
nameNsMap[podName] = podNs
}

return nameNsMap, result[0]["addresses"]
}

0 comments on commit 51dc924

Please sign in to comment.