@@ -43,6 +43,7 @@ import (
43
43
"golang.org/x/sys/unix"
44
44
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
45
45
"k8s.io/apimachinery/pkg/labels"
46
+ "k8s.io/apimachinery/pkg/util/sets"
46
47
"k8s.io/apimachinery/pkg/util/wait"
47
48
"k8s.io/client-go/informers"
48
49
corev1 "k8s.io/client-go/listers/core/v1"
@@ -600,79 +601,125 @@ func (c *Controller) iptablesSyncLoop() {
600
601
}
601
602
}
602
603
}
604
+ }
603
605
604
- // Record subnet cidr.
605
- subnetList , err := c .subnetLister .List (labels .Everything ())
606
+ ipInstanceList , err := c .ipInstanceLister .List (labels .SelectorFromSet (map [string ]string {constants .LabelNode : c .config .NodeName }))
607
+ if err != nil {
608
+ return fmt .Errorf ("failed to list pod ip instances of node %v: %v" , c .config .NodeName , err )
609
+ }
610
+ for _ , ipInstance := range ipInstanceList {
611
+ // skip terminating ip instance
612
+ if ipInstance .DeletionTimestamp != nil {
613
+ continue
614
+ }
615
+
616
+ podIP , _ , err := net .ParseCIDR (ipInstance .Spec .Address .IP )
606
617
if err != nil {
607
- return fmt .Errorf ("list subnet failed : %v" , err )
618
+ return fmt .Errorf ("parse pod ip %v error : %v" , ipInstance . Spec . Address . IP , err )
608
619
}
609
620
610
- for _ , subnet := range subnetList {
611
- _ , cidr , err := net .ParseCIDR (subnet .Spec .Range .CIDR )
612
- if err != nil {
613
- return fmt .Errorf ("parse subnet cidr %v failed: %v" , subnet .Spec .Range .CIDR , err )
614
- }
621
+ subnet , err := c .subnetLister .Get (ipInstance .Spec .Subnet )
622
+ if err != nil {
623
+ return fmt .Errorf ("failed to get subnet for ipinstance %s: %v" , ipInstance .Name , err )
624
+ }
625
+ reservedIPs := sets .NewString (subnet .Spec .Range .ReservedIPs ... )
626
+ // skip reserved ip instance
627
+ if reservedIPs .Has (podIP .String ()) {
628
+ continue
629
+ }
615
630
616
- network , err := c .networkLister .Get (subnet .Spec .Network )
617
- if err != nil {
618
- return fmt .Errorf ("failed to get network for subnet %v" , subnet .Name )
619
- }
631
+ if podIP .To4 () == nil {
632
+ c .iptablesV6Manager .RecordLocalPodIP (podIP )
633
+ } else {
634
+ c .iptablesV4Manager .RecordLocalPodIP (podIP )
635
+ }
636
+ }
637
+ // Record subnet cidr.
638
+ subnetList , err := c .subnetLister .List (labels .Everything ())
639
+ if err != nil {
640
+ return fmt .Errorf ("list subnet failed: %v" , err )
641
+ }
620
642
621
- iptablesManager := c .getIPtablesManager (subnet .Spec .Range .Version )
622
- iptablesManager .RecordSubnet (cidr , networkingv1 .GetNetworkType (network ) == networkingv1 .NetworkTypeOverlay )
643
+ for _ , subnet := range subnetList {
644
+ _ , cidr , err := net .ParseCIDR (subnet .Spec .Range .CIDR )
645
+ if err != nil {
646
+ return fmt .Errorf ("parse subnet cidr %v failed: %v" , subnet .Spec .Range .CIDR , err )
623
647
}
624
648
625
- if feature .MultiClusterEnabled () {
626
- // If remote overlay network des not exist, the rcmanager will not fetch
627
- // RemoteSubnet and RemoteVtep. Thus, existence check is redundant here.
649
+ network , err := c .networkLister .Get (subnet .Spec .Network )
650
+ if err != nil {
651
+ return fmt .Errorf ("failed to get network for subnet %v" , subnet .Name )
652
+ }
628
653
629
- remoteSubnetList , err := c .remoteSubnetLister .List (labels .Everything ())
630
- if err != nil {
631
- return fmt .Errorf ("list remote network failed: %v" , err )
654
+ iptablesManager := c .getIPtablesManager (subnet .Spec .Range .Version )
655
+ // isLocal means whether this node belongs to this network
656
+ isLocal := nodeBelongsToNetwork (c .config .NodeName , network )
657
+ // if network is local vlan, record vlan forward interface names
658
+ if isLocal && networkingv1 .GetNetworkType (network ) == networkingv1 .NetworkTypeUnderlay {
659
+ netID := subnet .Spec .NetID
660
+ if netID == nil {
661
+ netID = network .Spec .NetID
632
662
}
633
663
634
- // Record remote vtep ip.
635
- vtepList , err := c . remoteVtepLister . List ( labels . Everything () )
636
- if err != nil {
637
- return fmt .Errorf ("list remote vtep failed : %v" , err )
664
+ if vlanForwardIfName , err := containernetwork . GenerateVlanNetIfName ( c . config . NodeVlanIfName , netID ); err == nil {
665
+ iptablesManager . RecordVlanForwardIfName ( vlanForwardIfName )
666
+ } else {
667
+ return fmt .Errorf ("failed to generate vlan %d net interface name with node vlan name %s, err : %v" , * netID , c . config . NodeVlanIfName , err )
638
668
}
669
+ }
670
+ iptablesManager .RecordSubnet (cidr , networkingv1 .GetNetworkType (network ) == networkingv1 .NetworkTypeOverlay , isLocal )
671
+ }
639
672
640
- for _ , vtep := range vtepList {
641
- if _ , exist := vtep .Annotations [constants .AnnotationNodeLocalVxlanIPList ]; ! exist {
642
- ip := net .ParseIP (vtep .Spec .VtepIP )
643
- if ip .To4 () != nil {
644
- // v4 address
645
- c .iptablesV4Manager .RecordRemoteNodeIP (ip )
646
- } else {
647
- // v6 address
648
- c .iptablesV6Manager .RecordRemoteNodeIP (ip )
649
- }
650
- continue
651
- }
673
+ if feature .MultiClusterEnabled () {
674
+ // If remote overlay network des not exist, the rcmanager will not fetch
675
+ // RemoteSubnet and RemoteVtep. Thus, existence check is redundant here.
652
676
653
- nodeLocalVxlanIPStringList := strings .Split (vtep .Annotations [constants .AnnotationNodeLocalVxlanIPList ], "," )
654
- for _ , ipString := range nodeLocalVxlanIPStringList {
655
- ip := net .ParseIP (ipString )
656
- if ip .To4 () != nil {
657
- // v4 address
658
- c .iptablesV4Manager .RecordRemoteNodeIP (ip )
659
- } else {
660
- // v6 address
661
- c .iptablesV6Manager .RecordRemoteNodeIP (ip )
662
- }
677
+ remoteSubnetList , err := c .remoteSubnetLister .List (labels .Everything ())
678
+ if err != nil {
679
+ return fmt .Errorf ("list remote network failed: %v" , err )
680
+ }
681
+
682
+ // Record remote vtep ip.
683
+ vtepList , err := c .remoteVtepLister .List (labels .Everything ())
684
+ if err != nil {
685
+ return fmt .Errorf ("list remote vtep failed: %v" , err )
686
+ }
687
+
688
+ for _ , vtep := range vtepList {
689
+ if _ , exist := vtep .Annotations [constants .AnnotationNodeLocalVxlanIPList ]; ! exist {
690
+ ip := net .ParseIP (vtep .Spec .VtepIP )
691
+ if ip .To4 () != nil {
692
+ // v4 address
693
+ c .iptablesV4Manager .RecordRemoteNodeIP (ip )
694
+ } else {
695
+ // v6 address
696
+ c .iptablesV6Manager .RecordRemoteNodeIP (ip )
663
697
}
698
+ continue
664
699
}
665
700
666
- // Record remote subnet cidr
667
- for _ , remoteSubnet := range remoteSubnetList {
668
- _ , cidr , err := net .ParseCIDR (remoteSubnet .Spec .Range .CIDR )
669
- if err != nil {
670
- return fmt .Errorf ("parse remote subnet cidr %v failed: %v" , remoteSubnet .Spec .Range .CIDR , err )
701
+ nodeLocalVxlanIPStringList := strings .Split (vtep .Annotations [constants .AnnotationNodeLocalVxlanIPList ], "," )
702
+ for _ , ipString := range nodeLocalVxlanIPStringList {
703
+ ip := net .ParseIP (ipString )
704
+ if ip .To4 () != nil {
705
+ // v4 address
706
+ c .iptablesV4Manager .RecordRemoteNodeIP (ip )
707
+ } else {
708
+ // v6 address
709
+ c .iptablesV6Manager .RecordRemoteNodeIP (ip )
671
710
}
711
+ }
712
+ }
672
713
673
- c .getIPtablesManager (remoteSubnet .Spec .Range .Version ).
674
- RecordRemoteSubnet (cidr , networkingv1 .GetRemoteSubnetType (remoteSubnet ) == networkingv1 .NetworkTypeOverlay )
714
+ // Record remote subnet cidr
715
+ for _ , remoteSubnet := range remoteSubnetList {
716
+ _ , cidr , err := net .ParseCIDR (remoteSubnet .Spec .Range .CIDR )
717
+ if err != nil {
718
+ return fmt .Errorf ("parse remote subnet cidr %v failed: %v" , remoteSubnet .Spec .Range .CIDR , err )
675
719
}
720
+
721
+ c .getIPtablesManager (remoteSubnet .Spec .Range .Version ).
722
+ RecordRemoteSubnet (cidr , networkingv1 .GetRemoteSubnetType (remoteSubnet ) == networkingv1 .NetworkTypeOverlay )
676
723
}
677
724
}
678
725
0 commit comments