Skip to content

Commit

Permalink
fix: ipt wrong order and add cluster route
Browse files Browse the repository at this point in the history
(cherry picked from commit a27e176)
  • Loading branch information
oilbeater committed Oct 26, 2020
1 parent 33afdd1 commit df8530a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions dist/images/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ iptables -t nat -D POSTROUTING -m set --match-set ovn40local-pod-ip-nat src -m s
iptables -t nat -D POSTROUTING -m set ! --match-set ovn40subnets src -m set --match-set ovn40subnets-nat dst -j RETURN
iptables -t nat -D POSTROUTING -m set ! --match-set ovn40subnets src -m set --match-set ovn40local-pod-ip-nat dst -j RETURN
iptables -t nat -D POSTROUTING -m set --match-set ovn40subnets src -m set --match-set ovn40subnets dst -j MASQUERADE
iptables -t nat -D POSTROUTING -m set --match-set ovn40subnets src -m set --match-set ovn40subnets dst -j RETURN
iptables -t filter -D INPUT -m set --match-set ovn40subnets dst -j ACCEPT
iptables -t filter -D INPUT -m set --match-set ovn40subnets src -j ACCEPT
iptables -t filter -D FORWARD -m set --match-set ovn40subnets dst -j ACCEPT
Expand All @@ -21,6 +22,7 @@ ip6tables -t nat -D POSTROUTING -m set --match-set ovn60local-pod-ip-nat src -m
ip6tables -t nat -D POSTROUTING -m set ! --match-set ovn60subnets src -m set --match-set ovn60subnets-nat dst -j RETURN
ip6tables -t nat -D POSTROUTING -m set ! --match-set ovn60subnets src -m set --match-set ovn60local-pod-ip-nat dst -j RETURN
ip6tables -t nat -D POSTROUTING -m set --match-set ovn60subnets src -m set --match-set ovn60subnets dst -j MASQUERADE
ip6tables -t nat -D POSTROUTING -m set --match-set ovn60subnets src -m set --match-set ovn60subnets dst -j RETURN
ip6tables -t filter -D INPUT -m set --match-set ovn60subnets dst -j ACCEPT
ip6tables -t filter -D INPUT -m set --match-set ovn60subnets src -j ACCEPT
ip6tables -t filter -D FORWARD -m set --match-set ovn60subnets dst -j ACCEPT
Expand Down
13 changes: 10 additions & 3 deletions pkg/daemon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func (c *Controller) reconcileRouters() error {
klog.Errorf("failed to list namespace %v", err)
return err
}
cidrs := make([]string, 0, len(subnets))
cidrs := make([]string, 0, len(subnets)+1)
cidrs = append(cidrs, c.config.ServiceClusterIPRange)
for _, subnet := range subnets {
if !subnet.Status.IsReady() || subnet.Spec.UnderlayGateway {
continue
Expand Down Expand Up @@ -234,8 +235,14 @@ func (c *Controller) reconcileRouters() error {
_, cidr, _ := net.ParseCIDR(r)
gw := net.ParseIP(gateway)
src := net.ParseIP(c.internalIP)
if err = netlink.RouteReplace(&netlink.Route{Dst: cidr, LinkIndex: nic.Attrs().Index, Scope: netlink.SCOPE_UNIVERSE, Gw: gw, Src: src}); err != nil {
klog.Errorf("failed to add route %v", err)
if r == c.config.ServiceClusterIPRange {
if err = netlink.RouteReplace(&netlink.Route{Dst: cidr, LinkIndex: nic.Attrs().Index, Scope: netlink.SCOPE_UNIVERSE, Gw: gw}); err != nil {
klog.Errorf("failed to add route %v", err)
}
} else {
if err = netlink.RouteReplace(&netlink.Route{Dst: cidr, LinkIndex: nic.Attrs().Index, Scope: netlink.SCOPE_UNIVERSE, Gw: gw, Src: src}); err != nil {
klog.Errorf("failed to add route %v", err)
}
}
}
return err
Expand Down
6 changes: 4 additions & 2 deletions pkg/daemon/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
var (
v4Rules = []util.IPTableRule{
// This rule makes sure we don't NAT traffic within overlay network
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set --match-set ovn40subnets src -m set --match-set ovn40subnets dst -j MASQUERADE`, " ")},
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set --match-set ovn40subnets src -m set --match-set ovn40subnets dst -j RETURN`, " ")},
// Prevent performing Masquerade on external traffic which arrives from a Node that owns the Pod/Subnet IP
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set ! --match-set ovn40subnets src -m set --match-set ovn40local-pod-ip-nat dst -j RETURN`, " ")},
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set ! --match-set ovn40subnets src -m set --match-set ovn40subnets-nat dst -j RETURN`, " ")},
Expand All @@ -42,7 +42,7 @@ var (
}
v6Rules = []util.IPTableRule{
// This rule makes sure we don't NAT traffic within overlay network
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set --match-set ovn60subnets src -m set --match-set ovn60subnets dst -j MASQUERADE`, " ")},
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set --match-set ovn60subnets src -m set --match-set ovn60subnets dst -j RETURN`, " ")},
// Prevent performing Masquerade on external traffic which arrives from a Node that owns the Pod/Subnet IP
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set ! --match-set ovn40subnets src -m set --match-set ovn60local-pod-ip-nat dst -j RETURN`, " ")},
{Table: "nat", Chain: "POSTROUTING", Rule: strings.Split(`-m set ! --match-set ovn40subnets src -m set --match-set ovn60subnets-nat dst -j RETURN`, " ")},
Expand Down Expand Up @@ -97,6 +97,8 @@ func (c *Controller) runGateway() {
} else {
iptableRules = v6Rules
}
iptableRules[0], iptableRules[1], iptableRules[3], iptableRules[4] =
iptableRules[4], iptableRules[3], iptableRules[1], iptableRules[0]
for _, iptRule := range iptableRules {
exists, err := c.iptable.Exists(iptRule.Table, iptRule.Chain, iptRule.Rule...)
if err != nil {
Expand Down

0 comments on commit df8530a

Please sign in to comment.