Skip to content

Commit

Permalink
Do not masquerade service traffic
Browse files Browse the repository at this point in the history
Signed-off-by: Patryk Diak <[email protected]>
  • Loading branch information
kyrtapz committed Sep 20, 2024
1 parent 1b9ae6a commit d89b8e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go-controller/pkg/node/gateway_init_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
)
}
Expand Down
29 changes: 26 additions & 3 deletions go-controller/pkg/node/gateway_iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -493,18 +494,21 @@ 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.
srcUDNMasqueradePrefix := config.Gateway.V4MasqueradeSubnet
// 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",
Expand All @@ -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{
Expand All @@ -529,7 +551,8 @@ func getUDNMasqueradeRules(protocol iptables.Protocol) []nodeipt.Rule {
},
Protocol: protocol,
},
}
)
return rules
}

// initLocalGatewayNATRules sets up iptables rules for interfaces
Expand Down

0 comments on commit d89b8e9

Please sign in to comment.