@@ -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
15091515func (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