Skip to content

Commit

Permalink
fix: make ip deletion the same as creation (#2002)
Browse files Browse the repository at this point in the history
Co-authored-by: zhangbingbing <[email protected]>
(cherry picked from commit de5ef51)
  • Loading branch information
bobz965 authored and oilbeater committed Nov 1, 2022
1 parent 1bf5fa9 commit 071bebc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 48 deletions.
12 changes: 12 additions & 0 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,18 @@ func (c *Controller) createOrUpdateCrdIPs(podName, ip, mac, subnetName, ns, node
return nil
}

func (c *Controller) deleteCrdIPs(podName, ns, providerName string) error {
portName := ovs.PodNameToPortName(podName, ns, providerName)
klog.Infof("delete cr ip '%s' for pod %s/%s", portName, ns, podName)
if err := c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), portName, metav1.DeleteOptions{}); err != nil {
if !k8serrors.IsNotFound(err) {
klog.Errorf("failed to delete ip %s, %v", portName, err)
return err
}
}
return nil
}

func (c *Controller) CheckGatewayReady() {
if err := c.checkGatewayReady(); err != nil {
klog.Errorf("failed to check gateway ready %v", err)
Expand Down
57 changes: 9 additions & 48 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,33 +680,25 @@ func (c *Controller) handleDeletePod(pod *v1.Pod) error {
klog.Errorf("failed to delete lsp %s, %v", port.Name, err)
return err
}
if !keepIpCR {
if err = c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), port.Name, metav1.DeleteOptions{}); err != nil {
if !k8serrors.IsNotFound(err) {
klog.Errorf("failed to delete ip %s, %v", port.Name, err)
return err
}
}
}
for _, sg := range sgs {
c.syncSgPortsQueue.Add(sg)
}
}
if !keepIpCR {
if err = c.deleteAttachmentNetWorkIP(pod); err != nil {
klog.Errorf("failed to delete attach ip for pod %v, %v, please delete attach ip manually", pod.Name, err)
}
}
c.ipam.ReleaseAddressByPod(key)

podNets, err := c.getPodKubeovnNets(pod)
if err != nil {
klog.Errorf("failed to get pod nets %v", err)
} else {
}
if !keepIpCR {
for _, podNet := range podNets {
c.syncVirtualPortsQueue.Add(podNet.Subnet.Name)
if err = c.deleteCrdIPs(pod.Name, pod.Namespace, podNet.ProviderName); err != nil {
klog.Errorf("failed to delete ip for pod %s, %v, please delete manually", pod.Name, err)
}
}
}
c.ipam.ReleaseAddressByPod(key)
for _, podNet := range podNets {
c.syncVirtualPortsQueue.Add(podNet.Subnet.Name)
}
return nil
}

Expand Down Expand Up @@ -1339,37 +1331,6 @@ func (c *Controller) acquireStaticAddress(key, nicName, ip, mac, subnet string,
return v4IP, v6IP, mac, nil
}

func (c *Controller) deleteAttachmentNetWorkIP(pod *v1.Pod) error {
var providers []string
for k, v := range pod.Annotations {
if !strings.Contains(k, util.AllocatedAnnotationSuffix) || v != "true" {
continue
}
providerName := strings.ReplaceAll(k, util.AllocatedAnnotationSuffix, "")
if providerName == util.OvnProvider {
continue
} else {
providers = append(providers, providerName)
}
}
if len(providers) == 0 {
return nil
}
klog.Infof("providers are %v for pod %v", providers, pod.Name)

podName := c.getNameByPod(pod)
for _, providerName := range providers {
portName := ovs.PodNameToPortName(podName, pod.Namespace, providerName)
if err := c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), portName, metav1.DeleteOptions{}); err != nil {
if !k8serrors.IsNotFound(err) {
klog.Errorf("failed to delete ip %s, %v", portName, err)
return err
}
}
}
return nil
}

func appendCheckPodToDel(c *Controller, pod *v1.Pod, ownerRefName, ownerRefKind string) (bool, error) {
// subnet for ns has been changed, and statefulset pod's ip is not in the range of subnet's cidr anymore
podNs, err := c.namespacesLister.Get(pod.Namespace)
Expand Down

0 comments on commit 071bebc

Please sign in to comment.