-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to auto add nodeCIDR & serviceCIDR route table
- Loading branch information
Showing
17 changed files
with
273 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package route | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/containernetworking/plugins/pkg/ns" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
func AddPodKubeCIDRRoutes(podNS ns.NetNS, cidr string) error { | ||
ip, network, err := net.ParseCIDR(cidr) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse CIDR %q: %w", cidr, err) | ||
} | ||
err = podNS.Do(func(_ ns.NetNS) error { | ||
defaultLinkSet, err := GetDefaultLinkIDSet() | ||
if err != nil { | ||
return fmt.Errorf("failed to get pod default link id: %w", err) | ||
} | ||
|
||
podDefaultRoutes, err := GetDefaultRoutes() | ||
if err != nil { | ||
return fmt.Errorf("failed to get pod default routes: %w", err) | ||
} | ||
if len(podDefaultRoutes) == 0 { | ||
return nil | ||
} | ||
var podDefaultGatewayV4 net.IP | ||
var podDefaultGatewayV6 net.IP | ||
for _, r := range podDefaultRoutes { | ||
switch r.Family { | ||
case netlink.FAMILY_V4: | ||
podDefaultGatewayV4 = r.Gw | ||
default: | ||
podDefaultGatewayV6 = r.Gw | ||
} | ||
} | ||
|
||
for id := range defaultLinkSet { | ||
r := netlink.Route{ | ||
LinkIndex: id, | ||
Dst: network, | ||
Family: netlink.FAMILY_V4, | ||
Gw: podDefaultGatewayV4, | ||
} | ||
if ip.To16() != nil && len(ip.To4()) == 0 { | ||
r.Family = netlink.FAMILY_V6 | ||
r.Gw = podDefaultGatewayV6 | ||
} | ||
return EnsureRouteExists(&r) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("addPodKubeCIDRRoutes: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package route | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
flv1 "github.com/cnrancher/rancher-flat-network/pkg/apis/flatnetwork.pandaria.io/v1" | ||
"github.com/cnrancher/rancher-flat-network/pkg/cni/common" | ||
"github.com/cnrancher/rancher-flat-network/pkg/utils" | ||
"github.com/containernetworking/plugins/pkg/ns" | ||
"github.com/sirupsen/logrus" | ||
"github.com/vishvananda/netlink" | ||
"github.com/vishvananda/netlink/nl" | ||
) | ||
|
||
// getHostCIDRCustomRoutes for adding host iface IP addr routes to pod | ||
func getHostCIDRCustomRoutes(linkID int, gwV4, gwV6 net.IP) ([]flv1.Route, error) { | ||
link, err := netlink.LinkByIndex(linkID) | ||
if err != nil { | ||
return nil, fmt.Errorf("getHostCIDRCustomRoutes: %w", err) | ||
} | ||
addrs, err := netlink.AddrList(link, netlink.FAMILY_ALL) | ||
if err != nil { | ||
return nil, fmt.Errorf("getHostCIDRCustomRoutes: %w", err) | ||
} | ||
if len(addrs) == 0 { | ||
return nil, nil | ||
} | ||
routes := []flv1.Route{} | ||
for _, a := range addrs { | ||
if a.IP.IsLinkLocalUnicast() { | ||
continue | ||
} | ||
r := flv1.Route{ | ||
Dev: common.PodIfaceEth0, | ||
Dst: a.IPNet.String(), | ||
Via: nil, | ||
} | ||
switch nl.GetIPFamily(a.IP) { | ||
case netlink.FAMILY_V4: | ||
r.Via = gwV4 | ||
default: | ||
r.Via = gwV6 | ||
} | ||
routes = append(routes, r) | ||
} | ||
logrus.Debugf("getHostCIDRCustomRoutes: %v", utils.Print(routes)) | ||
return routes, nil | ||
} | ||
|
||
func AddPodNodeCIDRRoutes(podNS ns.NetNS) error { | ||
// Add host iface IP addr routes and user custom routes to Pod | ||
customRoutes := []flv1.Route{} | ||
defaultLinkSet, err := GetDefaultLinkIDSet() | ||
if err != nil { | ||
return fmt.Errorf("failed to get pod default link id: %w", err) | ||
} | ||
|
||
var podDefaultGatewayV4 net.IP | ||
var podDefaultGatewayV6 net.IP | ||
if err := podNS.Do(func(_ ns.NetNS) error { | ||
podDefaultRoutes, err := GetDefaultRoutes() | ||
if err != nil { | ||
return fmt.Errorf("failed to get pod default routes: %w", err) | ||
} | ||
if len(podDefaultRoutes) == 0 { | ||
return nil | ||
} | ||
for _, r := range podDefaultRoutes { | ||
switch r.Family { | ||
case netlink.FAMILY_V4: | ||
podDefaultGatewayV4 = r.Gw | ||
default: | ||
podDefaultGatewayV6 = r.Gw | ||
} | ||
} | ||
return nil | ||
}); err != nil { | ||
return fmt.Errorf("addPodNodeCIDRRoutes: %w", err) | ||
} | ||
for id := range defaultLinkSet { | ||
results, err := getHostCIDRCustomRoutes(id, podDefaultGatewayV4, podDefaultGatewayV6) | ||
if err != nil { | ||
return fmt.Errorf("addPodNodeCIDRRoutes: %w", err) | ||
} | ||
if len(results) == 0 { | ||
continue | ||
} | ||
customRoutes = append(customRoutes, results...) | ||
} | ||
|
||
return AddPodFlatNetworkCustomRoutes(podNS, customRoutes) | ||
} |
Oops, something went wrong.