Skip to content

Commit

Permalink
delete ipam record when gc lsp (#1483)
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Apr 26, 2022
1 parent 73405b2 commit 59e4ae7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ func (c *Controller) markAndCleanLSP() error {
if !lastNoPodLSP[lsp] {
noPodLSP[lsp] = true
} else {
nameNsMap := 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 @@ -311,6 +313,11 @@ func (c *Controller) markAndCleanLSP() error {
return err
}
}

for podName, nsName := range nameNsMap {
key := fmt.Sprintf("%s/%s", nsName, podName)
c.ipam.ReleaseAddressByPod(key)
}
}
}
}
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 @@ -2543,3 +2543,38 @@ func (c Client) UpdateSubnetACL(ls string, acls []kubeovnv1.Acl) error {
}
return nil
}

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

0 comments on commit 59e4ae7

Please sign in to comment.