Skip to content

Commit

Permalink
fix addRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyufan2 committed Jan 10, 2025
1 parent 0869c8c commit 85972eb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 56 deletions.
55 changes: 1 addition & 54 deletions cns/middlewares/k8sSwiftV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package middlewares
import (
"context"
"fmt"
"net"
"net/netip"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/configuration"
Expand Down Expand Up @@ -252,37 +250,8 @@ func (k *K8sSWIFTv2Middleware) Type() cns.SWIFTV2Mode {
return cns.K8sSWIFTV2
}

// always pick up .1 as the default ipv4 gateway for each IP address
func (k *K8sSWIFTv2Middleware) getWindowsIPv4Gateway(cidr string) (string, error) {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return "", errors.Wrap(err, "failed to parse cidr")
}
ip = ip.To4()
ip[3] = 1

return ip.String(), nil
}

// Linux always use fixed gateway IP for infraVNETCIDRs, podCIDRs and serviceCIDRs
// Windows uses .1 as the gateway IP for each CIDR
func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns.Route {
routes := make([]cns.Route, len(cidrs))
for i, cidr := range cidrs {
if gatewayIP == "" {
gatewayIP, _ = k.getWindowsIPv4Gateway(cidr)
}
routes[i] = cns.Route{
IPAddress: cidr,
GatewayIPAddress: gatewayIP,
}
}

return routes
}

// CNS gets node, pod and service CIDRs from configuration env and parse them to get the v4 and v6 IPs
func (k *K8sSWIFTv2Middleware) getCidrs() (v4IPs, v6IPs []string, err error) {
func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) {
v4IPs = []string{}
v6IPs = []string{}

Expand Down Expand Up @@ -326,25 +295,3 @@ func (k *K8sSWIFTv2Middleware) getCidrs() (v4IPs, v6IPs []string, err error) {

return v4IPs, v6IPs, nil
}

func (k *K8sSWIFTv2Middleware) SetInfraRoutes(podIPInfo *cns.PodIpInfo, gwv4, gwv6 string) ([]cns.Route, error) {
var routes []cns.Route

ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse podIPConfig IP address %s", podIPInfo.PodIPConfig.IPAddress)
}

v4IPs, v6IPs, err := k.getCidrs()
if err != nil {
return nil, errors.Wrap(err, "failed to get CIDRs")
}

if ip.Is4() {
routes = append(routes, k.addRoutes(v4IPs, gwv4)...)
} else {
routes = append(routes, k.addRoutes(v6IPs, gwv6)...)
}

return routes, nil
}
36 changes: 35 additions & 1 deletion cns/middlewares/k8sSwiftV2_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middlewares

import (
"fmt"
"net/netip"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/logger"
Expand All @@ -28,7 +29,7 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {

case cns.InfraNIC:
// Linux uses 169.254.1.1 as the default ipv4 gateway and fe80::1234:5678:9abc as the default ipv6 gateway
infraRoutes, err := k.SetInfraRoutes(podIPInfo, overlayGatewayv4, overlayGatewayV6)
infraRoutes, err := k.setInfraRoutes(podIPInfo)
if err != nil {
return errors.Wrap(err, "failed to set routes for infraNIC interface")
}
Expand All @@ -51,3 +52,36 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo,
}

func (k *K8sSWIFTv2Middleware) addDefaultRoute(*cns.PodIpInfo, string) {}

func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns.Route {
routes := make([]cns.Route, len(cidrs))
for i, cidr := range cidrs {
routes[i] = cns.Route{
IPAddress: cidr,
GatewayIPAddress: gatewayIP,
}
}
return routes
}

func (k *K8sSWIFTv2Middleware) setInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
var routes []cns.Route

ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse podIPConfig IP address %s", podIPInfo.PodIPConfig.IPAddress)
}

v4IPs, v6IPs, err := k.GetCidrs()
if err != nil {
return nil, errors.Wrap(err, "failed to get CIDRs")
}

if ip.Is4() {
routes = append(routes, k.addRoutes(v4IPs, overlayGatewayv4)...)
} else {
routes = append(routes, k.addRoutes(v6IPs, overlayGatewayV6)...)
}

return routes, nil
}
52 changes: 51 additions & 1 deletion cns/middlewares/k8sSwiftV2_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package middlewares

import (
"net"
"net/netip"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
Expand All @@ -22,7 +25,7 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {

// set routes(pod/node/service cidrs) for infraNIC interface
// Swiftv2 Windows does not support IPv6
infraRoutes, err := k.SetInfraRoutes(podIPInfo, "", "")
infraRoutes, err := k.setInfraRoutes(podIPInfo)
if err != nil {
return errors.Wrap(err, "failed to set routes for infraNIC interface")
}
Expand Down Expand Up @@ -65,3 +68,50 @@ func (k *K8sSWIFTv2Middleware) addDefaultRoute(podIPInfo *cns.PodIpInfo, gateway
}
podIPInfo.Routes = append(podIPInfo.Routes, route)
}

// always pick up .1 as the default ipv4 gateway for each IP address
func (k *K8sSWIFTv2Middleware) getIPv4Gateway(cidr string) (string, error) {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return "", errors.Wrap(err, "failed to parse cidr")
}
ip = ip.To4()
ip[3] = 1

return ip.String(), nil
}

// Windows uses .1 as the gateway IP for each CIDR
func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string) []cns.Route {
routes := make([]cns.Route, len(cidrs))
for i, cidr := range cidrs {
gatewayIP, _ := k.getIPv4Gateway(cidr)
routes[i] = cns.Route{
IPAddress: cidr,
GatewayIPAddress: gatewayIP,
}
}
return routes
}

func (k *K8sSWIFTv2Middleware) setInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
var routes []cns.Route

ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse podIPConfig IP address %s", podIPInfo.PodIPConfig.IPAddress)
}

v4IPs, v6IPs, err := k.GetCidrs()
if err != nil {
return nil, errors.Wrap(err, "failed to get CIDRs")
}

if ip.Is4() {
routes = append(routes, k.addRoutes(v4IPs)...)
} else {
routes = append(routes, k.addRoutes(v6IPs)...)
}

return routes, nil
}

0 comments on commit 85972eb

Please sign in to comment.