diff --git a/go-controller/pkg/node/gateway_init_linux_test.go b/go-controller/pkg/node/gateway_init_linux_test.go index 1e12e1a513..3e332f7f0e 100644 --- a/go-controller/pkg/node/gateway_init_linux_test.go +++ b/go-controller/pkg/node/gateway_init_linux_test.go @@ -26,6 +26,7 @@ import ( "k8s.io/client-go/kubernetes/fake" nadfake "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned/fake" + "github.com/ovn-org/ovn-kubernetes/go-controller/pkg/config" adminpolicybasedrouteclient "github.com/ovn-org/ovn-kubernetes/go-controller/pkg/crd/adminpolicybasedroute/v1/apis/clientset/versioned/fake" udnfakeclient "github.com/ovn-org/ovn-kubernetes/go-controller/pkg/crd/userdefinednetwork/v1/apis/clientset/versioned/fake" @@ -1275,6 +1276,7 @@ OFPT_GET_CONFIG_REPLY (xid=0x4): frags=normal miss_send_len=0` ) expectedTables["nat"]["OVN-KUBE-UDN-MASQUERADE"] = append(expectedTables["nat"]["OVN-KUBE-UDN-MASQUERADE"], "-s 169.254.169.2/29 -j RETURN", // this guarantees we don't SNAT default network masqueradeIPs + "-d 172.16.1.0/24 -j RETURN", // this guarantees we don't SNAT service traffic "-s 169.254.169.0/29 -j MASQUERADE", // this guarantees we SNAT all UDN MasqueradeIPs traffic leaving the node ) } diff --git a/go-controller/pkg/node/gateway_iptables.go b/go-controller/pkg/node/gateway_iptables.go index 39dea4842a..8654961d05 100644 --- a/go-controller/pkg/node/gateway_iptables.go +++ b/go-controller/pkg/node/gateway_iptables.go @@ -12,6 +12,7 @@ import ( utilnet "k8s.io/utils/net" "github.com/coreos/go-iptables/iptables" + "github.com/ovn-org/ovn-kubernetes/go-controller/pkg/config" "github.com/ovn-org/ovn-kubernetes/go-controller/pkg/node/controllers/egressservice" nodeipt "github.com/ovn-org/ovn-kubernetes/go-controller/pkg/node/iptables" @@ -493,6 +494,7 @@ func getUDNMasqueradeRules(protocol iptables.Protocol) []nodeipt.Rule { // the following rules are actively used only for the UDN Feature: // -A POSTROUTING -j OVN-KUBE-UDN-MASQUERADE // -A OVN-KUBE-UDN-MASQUERADE -s 169.254.0.0/29 -j RETURN + // -A OVN-KUBE-UDN-MASQUERADE -d 10.96.0.0/16 -j RETURN // -A OVN-KUBE-UDN-MASQUERADE -s 169.254.0.0/17 -j MASQUERADE // NOTE: Ordering is important here, the RETURN must come before // the MASQUERADE rule. Please don't change the ordering. @@ -500,11 +502,13 @@ func getUDNMasqueradeRules(protocol iptables.Protocol) []nodeipt.Rule { // defaultNetworkReservedMasqueradePrefix contains the first 6IPs in the masquerade // range that shouldn't be MASQUERADED. Hence /29 and /125 is intentionally hardcoded here defaultNetworkReservedMasqueradePrefix := config.Gateway.MasqueradeIPs.V4HostMasqueradeIP.String() + "/29" + ipFamily := utilnet.IPv4 if protocol == iptables.ProtocolIPv6 { srcUDNMasqueradePrefix = config.Gateway.V6MasqueradeSubnet defaultNetworkReservedMasqueradePrefix = config.Gateway.MasqueradeIPs.V6HostMasqueradeIP.String() + "/125" + ipFamily = utilnet.IPv6 } - return []nodeipt.Rule{ + rules := []nodeipt.Rule{ { Table: "nat", Chain: "POSTROUTING", @@ -520,7 +524,25 @@ func getUDNMasqueradeRules(protocol iptables.Protocol) []nodeipt.Rule { }, Protocol: protocol, }, - { + } + for _, svcCIDR := range config.Kubernetes.ServiceCIDRs { + if utilnet.IPFamilyOfCIDR(svcCIDR) != ipFamily { + continue + } + rules = append(rules, + nodeipt.Rule{ + Table: "nat", + Chain: iptableUDNMasqueradeChain, + Args: []string{ + "-d", svcCIDR.String(), + "-j", "RETURN", + }, + Protocol: protocol, + }, + ) + } + rules = append(rules, + nodeipt.Rule{ Table: "nat", Chain: iptableUDNMasqueradeChain, Args: []string{ @@ -529,7 +551,8 @@ func getUDNMasqueradeRules(protocol iptables.Protocol) []nodeipt.Rule { }, Protocol: protocol, }, - } + ) + return rules } // initLocalGatewayNATRules sets up iptables rules for interfaces