diff --git a/cns/middlewares/k8sSwiftV2.go b/cns/middlewares/k8sSwiftV2.go index 9fd6fa9d2e..b51eeca333 100644 --- a/cns/middlewares/k8sSwiftV2.go +++ b/cns/middlewares/k8sSwiftV2.go @@ -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" @@ -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{} @@ -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 -} diff --git a/cns/middlewares/k8sSwiftV2_linux.go b/cns/middlewares/k8sSwiftV2_linux.go index c4fc58d3b7..1f4c3de9eb 100644 --- a/cns/middlewares/k8sSwiftV2_linux.go +++ b/cns/middlewares/k8sSwiftV2_linux.go @@ -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" @@ -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") } @@ -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 +} diff --git a/cns/middlewares/k8sSwiftV2_windows.go b/cns/middlewares/k8sSwiftV2_windows.go index 9f990f83cb..eb435f7d5c 100644 --- a/cns/middlewares/k8sSwiftV2_windows.go +++ b/cns/middlewares/k8sSwiftV2_windows.go @@ -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" @@ -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") } @@ -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 +}