Skip to content

Commit d0f9028

Browse files
authored
Merge pull request #380 from lihezhong93/back-port-drop-endless-underlay
backport drop endless underlay traffic
2 parents bfb1fd9 + 62ca151 commit d0f9028

File tree

6 files changed

+855
-348
lines changed

6 files changed

+855
-348
lines changed

dist/images/hybridnet-daemon

30 MB
Binary file not shown.

pkg/daemon/controller/controller.go

+100-53
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"golang.org/x/sys/unix"
4444
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4545
"k8s.io/apimachinery/pkg/labels"
46+
"k8s.io/apimachinery/pkg/util/sets"
4647
"k8s.io/apimachinery/pkg/util/wait"
4748
"k8s.io/client-go/informers"
4849
corev1 "k8s.io/client-go/listers/core/v1"
@@ -600,79 +601,125 @@ func (c *Controller) iptablesSyncLoop() {
600601
}
601602
}
602603
}
604+
}
603605

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)
606617
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)
608619
}
609620

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+
}
615630

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+
}
620642

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)
623647
}
624648

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+
}
628653

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
632662
}
633663

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)
638668
}
669+
}
670+
iptablesManager.RecordSubnet(cidr, networkingv1.GetNetworkType(network) == networkingv1.NetworkTypeOverlay, isLocal)
671+
}
639672

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.
652676

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)
663697
}
698+
continue
664699
}
665700

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)
671710
}
711+
}
712+
}
672713

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)
675719
}
720+
721+
c.getIPtablesManager(remoteSubnet.Spec.Range.Version).
722+
RecordRemoteSubnet(cidr, networkingv1.GetRemoteSubnetType(remoteSubnet) == networkingv1.NetworkTypeOverlay)
676723
}
677724
}
678725

pkg/daemon/controller/utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,17 @@ func isIPListEqual(a, b []string) bool {
211211

212212
return gset.NewStrSetFrom(a).Equal(gset.NewStrSetFrom(b))
213213
}
214+
215+
func nodeBelongsToNetwork(nodeName string, network *networkingv1.Network) bool {
216+
if networkingv1.GetNetworkType(network) == networkingv1.NetworkTypeOverlay {
217+
return true
218+
}
219+
isUnderlayOnHost := false
220+
for _, n := range network.Status.NodeList {
221+
if n == nodeName {
222+
isUnderlayOnHost = true
223+
break
224+
}
225+
}
226+
return isUnderlayOnHost
227+
}

0 commit comments

Comments
 (0)