Skip to content

Commit 47043f5

Browse files
committed
fix ip cr not delete with pod in the case of subnet not exist (#5364)
--------- Signed-off-by: zbb88888 <[email protected]>
1 parent d02ca48 commit 47043f5

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

pkg/controller/gc.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func (c *Controller) gc() error {
3838
c.gcStaticRoute,
3939
c.gcVpcNatGateway,
4040
c.gcLogicalRouterPort,
41+
c.gcIP,
4142
c.gcVip,
4243
c.gcLbSvcPods,
4344
c.gcVPCDNS,
@@ -315,6 +316,24 @@ func (c *Controller) gcLogicalSwitchPort() error {
315316
return c.markAndCleanLSP()
316317
}
317318

319+
func (c *Controller) gcIP() error {
320+
klog.Infof("start to gc ips")
321+
ips, err := c.ipsLister.List(labels.Everything())
322+
if err != nil {
323+
klog.Errorf("failed to list ip, %v", err)
324+
return err
325+
}
326+
for _, ip := range ips {
327+
if _, ok := c.ipam.Subnets[ip.Spec.Subnet]; !ok {
328+
klog.Infof("subnet %s already not exist, gc ip %s", ip.Spec.Subnet, ip.Name)
329+
if err := c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), ip.Name, metav1.DeleteOptions{}); err != nil {
330+
klog.Errorf("failed to gc ip %s, %v", ip.Name, err)
331+
}
332+
}
333+
}
334+
return nil
335+
}
336+
318337
func (c *Controller) markAndCleanLSP() error {
319338
klog.V(4).Infof("start to gc logical switch ports")
320339
pods, err := c.podsLister.List(labels.Everything())

pkg/controller/pod.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,12 @@ func (c *Controller) getPodKubeovnNets(pod *v1.Pod) ([]*kubeovnNet, error) {
524524
return nil, err
525525
}
526526

527+
// pod annotation default subnet not found
528+
if defaultSubnet == nil {
529+
klog.Errorf("pod %s/%s has no default subnet, skip adding default network", pod.Namespace, pod.Name)
530+
return attachmentNets, nil
531+
}
532+
527533
podNets := attachmentNets
528534
if _, hasOtherDefaultNet := pod.Annotations[util.DefaultNetworkAnnotation]; !hasOtherDefaultNet {
529535
podNets = append(attachmentNets, &kubeovnNet{
@@ -1507,11 +1513,21 @@ func needRouteSubnets(pod *v1.Pod, nets []*kubeovnNet) []*kubeovnNet {
15071513
}
15081514

15091515
func (c *Controller) getPodDefaultSubnet(pod *v1.Pod) (*kubeovnv1.Subnet, error) {
1516+
// ignore to clean its ip crd in existing subnets
1517+
ignoreSubnetNotExist := !pod.DeletionTimestamp.IsZero()
1518+
15101519
// check pod annotations
15111520
if lsName := pod.Annotations[util.LogicalSwitchAnnotation]; lsName != "" {
1521+
// annotations only has one default subnet
15121522
subnet, err := c.subnetsLister.Get(lsName)
15131523
if err != nil {
15141524
klog.Errorf("failed to get subnet %s: %v", lsName, err)
1525+
if k8serrors.IsNotFound(err) {
1526+
if ignoreSubnetNotExist {
1527+
klog.Errorf("deletting pod %s/%s default subnet %s already not exist, gc will clean its ip cr", pod.Namespace, pod.Name, lsName)
1528+
return nil, nil
1529+
}
1530+
}
15151531
return nil, err
15161532
}
15171533
return subnet, nil
@@ -1538,6 +1554,14 @@ func (c *Controller) getPodDefaultSubnet(pod *v1.Pod) (*kubeovnv1.Subnet, error)
15381554
subnet, err := c.subnetsLister.Get(subnetName)
15391555
if err != nil {
15401556
klog.Errorf("failed to get subnet %s: %v", subnetName, err)
1557+
if k8serrors.IsNotFound(err) {
1558+
if ignoreSubnetNotExist {
1559+
klog.Errorf("deletting pod %s/%s namespace subnet %s already not exist, gc will clean its ip cr", pod.Namespace, pod.Name, subnetName)
1560+
// ip name is unique, it is ok if any subnet release it
1561+
// gc will handle their ip cr, if all subnets are not exist
1562+
continue
1563+
}
1564+
}
15411565
return nil, err
15421566
}
15431567

@@ -1616,9 +1640,13 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
16161640
}
16171641
subnets, err := c.subnetsLister.List(labels.Everything())
16181642
if err != nil {
1643+
klog.Errorf("failed to list subnets: %v", err)
16191644
return nil, err
16201645
}
16211646

1647+
// ignore to return all existing subnets to clean its ip crd
1648+
ignoreSubnetNotExist := !pod.DeletionTimestamp.IsZero()
1649+
16221650
result := make([]*kubeovnNet, 0, len(multusNets))
16231651
for _, attach := range multusNets {
16241652
networkClient := c.config.AttachNetClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(attach.Namespace)
@@ -1656,15 +1684,32 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
16561684
}
16571685
var subnet *kubeovnv1.Subnet
16581686
if subnetName == "" {
1687+
// attachment network not specify subnet, use pod default subnet or namespace subnet
16591688
subnet, err = c.getPodDefaultSubnet(pod)
16601689
if err != nil {
16611690
klog.Errorf("failed to pod default subnet, %v", err)
1691+
if k8serrors.IsNotFound(err) {
1692+
if ignoreSubnetNotExist {
1693+
klog.Errorf("deletting pod %s/%s attach subnet %s already not exist, gc will clean its ip cr", pod.Namespace, pod.Name, subnetName)
1694+
continue
1695+
}
1696+
}
16621697
return nil, err
16631698
}
1699+
// default subnet may change after pod restart
1700+
klog.Infof("pod %s/%s attachment network %s use default subnet %s", pod.Namespace, pod.Name, attach.Name, subnet.Name)
16641701
} else {
16651702
subnet, err = c.subnetsLister.Get(subnetName)
16661703
if err != nil {
16671704
klog.Errorf("failed to get subnet %s, %v", subnetName, err)
1705+
if k8serrors.IsNotFound(err) {
1706+
if ignoreSubnetNotExist {
1707+
klog.Errorf("deletting pod %s/%s attach subnet %s already not exist, gc will clean its ip cr", pod.Namespace, pod.Name, subnetName)
1708+
// just continue to next attach subnet
1709+
// ip name is unique, so it is ok if the other subnet release it
1710+
continue
1711+
}
1712+
}
16681713
return nil, err
16691714
}
16701715
}

0 commit comments

Comments
 (0)