Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow egress-router to connect to its own node IP, for DNS, etc #19885

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion images/egress/router/egress-router.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function gen_iptables_rules() {
fi
fi
done <<< "${EGRESS_DESTINATION}"
echo -A POSTROUTING -j SNAT --to-source "${EGRESS_SOURCE}"
echo -A POSTROUTING -o macvlan0 -j SNAT --to-source "${EGRESS_SOURCE}"
}

function setup_iptables() {
Expand Down
18 changes: 9 additions & 9 deletions images/egress/router/egress_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestEgressRouter(t *testing.T) {
dest: "10.1.2.3",
output: `
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
Expand All @@ -29,7 +29,7 @@ func TestEgressRouter(t *testing.T) {
dest: "10.1.2.3",
output: `
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
Expand All @@ -38,7 +38,7 @@ func TestEgressRouter(t *testing.T) {
dest: "10.1.2.3",
output: `
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
Expand All @@ -47,7 +47,7 @@ func TestEgressRouter(t *testing.T) {
dest: "10.1.2.3\n",
output: `
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
Expand All @@ -56,7 +56,7 @@ func TestEgressRouter(t *testing.T) {
dest: "80 tcp 10.4.5.6",
output: `
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.4.5.6
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
Expand All @@ -65,7 +65,7 @@ func TestEgressRouter(t *testing.T) {
dest: "8080 tcp 10.7.8.9 80",
output: `
-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.7.8.9:80
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
Expand All @@ -75,7 +75,7 @@ func TestEgressRouter(t *testing.T) {
output: `
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.4.5.6
-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.7.8.9:80
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
Expand All @@ -86,7 +86,7 @@ func TestEgressRouter(t *testing.T) {
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.4.5.6
-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.7.8.9:80
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
{
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestEgressRouter(t *testing.T) {
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.4.5.6
-A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.7.8.9:80
-A PREROUTING -i eth0 -j DNAT --to-destination 10.1.2.3
-A POSTROUTING -j SNAT --to-source 1.2.3.4
-A POSTROUTING -o macvlan0 -j SNAT --to-source 1.2.3.4
`,
},
}
Expand Down
31 changes: 30 additions & 1 deletion pkg/network/sdn-cni-plugin/openshift-sdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (p *cniPlugin) CmdAdd(args *skel.CmdArgs) error {
if err != nil {
return fmt.Errorf("failed to convert IPAM result: %v", err)
}
defaultGW := result020.IP4.Gateway
result020.IP4.Gateway = nil

result030, err := current.NewResultFromResult(result020)
Expand All @@ -164,7 +165,7 @@ func (p *cniPlugin) CmdAdd(args *skel.CmdArgs) error {
}
result030.IPs[0].Interface = current.Int(0)

err = ns.WithNetNSPath(args.Netns, func(ns.NetNS) error {
err = ns.WithNetNSPath(args.Netns, func(hostNS ns.NetNS) error {
// Set up eth0
if err := ip.SetHWAddrByIP(args.IfName, result030.IPs[0].Address.IP, nil); err != nil {
return fmt.Errorf("failed to set pod interface MAC address: %v", err)
Expand All @@ -186,9 +187,37 @@ func (p *cniPlugin) CmdAdd(args *skel.CmdArgs) error {
link, err = netlink.LinkByName("macvlan0")
if err == nil {
err = netlink.LinkSetUp(link)
if err != nil {
return fmt.Errorf("failed to enable macvlan device: %v", err)
}

// A macvlan can't reach its parent interface's IP, so we need to
// add a route to that via the SDN
var addrs []netlink.Addr
err = hostNS.Do(func(ns.NetNS) error {
parent, err := netlink.LinkByIndex(link.Attrs().ParentIndex)
if err != nil {
return err
}
addrs, err = netlink.AddrList(parent, netlink.FAMILY_V4)
return err
})
if err != nil {
return fmt.Errorf("failed to configure macvlan device: %v", err)
}
for _, addr := range addrs {
route := &netlink.Route{
Dst: &net.IPNet{
IP: addr.IP,
Mask: net.CIDRMask(32, 32),
},
Gw: defaultGW,
}
err = netlink.RouteAdd(route)
if err != nil {
return fmt.Errorf("failed to configure macvlan device: %v", err)
}
}
}

return nil
Expand Down