diff --git a/cns/middlewares/k8sSwiftV2.go b/cns/middlewares/k8sSwiftV2.go index b51eeca333..4cb3a8f2e0 100644 --- a/cns/middlewares/k8sSwiftV2.go +++ b/cns/middlewares/k8sSwiftV2.go @@ -250,7 +250,29 @@ func (k *K8sSWIFTv2Middleware) Type() cns.SWIFTV2Mode { return cns.K8sSWIFTV2 } -// CNS gets node, pod and service CIDRs from configuration env and parse them to get the v4 and v6 IPs +// CNS gets pod CIDRs from configuration env and parse them to get the v4 and v6 IPs +// Containerd reassigns the IP to the adapter and kernel configures the pod cidr route by default, so windows swiftv2 does not require pod cidr +func (k *K8sSWIFTv2Middleware) GetPodCidrs() (v4IPs, v6IPs []string, err error) { + v4IPs = []string{} + v6IPs = []string{} + + // Get and parse podCIDRs from env + podCIDRs, err := configuration.PodCIDRs() + if err != nil { + return nil, nil, errors.Wrapf(err, "failed to get podCIDRs from env") + } + podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs) + if err != nil { + return nil, nil, errors.Wrapf(err, "failed to parse podCIDRs") + } + + v4IPs = append(v4IPs, podCIDRsV4...) + v6IPs = append(v6IPs, podCIDRv6...) + + return v4IPs, v6IPs, nil +} + +// CNS gets node 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) { v4IPs = []string{} v6IPs = []string{} @@ -265,16 +287,6 @@ func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) { return nil, nil, errors.Wrapf(err, "failed to parse infraVNETCIDRs") } - // Get and parse podCIDRs from env - podCIDRs, err := configuration.PodCIDRs() - if err != nil { - return nil, nil, errors.Wrapf(err, "failed to get podCIDRs from env") - } - podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs) - if err != nil { - return nil, nil, errors.Wrapf(err, "failed to parse podCIDRs") - } - // Get and parse serviceCIDRs from env serviceCIDRs, err := configuration.ServiceCIDRs() if err != nil { @@ -286,11 +298,9 @@ func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) { } v4IPs = append(v4IPs, infraVNETCIDRsv4...) - v4IPs = append(v4IPs, podCIDRsV4...) v4IPs = append(v4IPs, serviceCIDRsV4...) v6IPs = append(v6IPs, infraVNETCIDRsv6...) - v6IPs = append(v6IPs, podCIDRv6...) v6IPs = append(v6IPs, serviceCIDRsV6...) return v4IPs, v6IPs, nil diff --git a/cns/middlewares/k8sSwiftV2_linux.go b/cns/middlewares/k8sSwiftV2_linux.go index 1f4c3de9eb..23dff175b3 100644 --- a/cns/middlewares/k8sSwiftV2_linux.go +++ b/cns/middlewares/k8sSwiftV2_linux.go @@ -29,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) + infraRoutes, err := k.getInfraRoutes(podIPInfo) if err != nil { return errors.Wrap(err, "failed to set routes for infraNIC interface") } @@ -64,7 +64,7 @@ func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns return routes } -func (k *K8sSWIFTv2Middleware) setInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) { +func (k *K8sSWIFTv2Middleware) getInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) { var routes []cns.Route ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress) @@ -74,9 +74,17 @@ func (k *K8sSWIFTv2Middleware) setInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.R v4IPs, v6IPs, err := k.GetCidrs() if err != nil { - return nil, errors.Wrap(err, "failed to get CIDRs") + return nil, errors.Wrap(err, "failed to get node and service CIDRs") } + v4PodIPs, v6PodIPs, err := k.GetPodCidrs() + if err != nil { + return nil, errors.Wrap(err, "failed to get pod CIDRs") + } + + v4IPs = append(v4IPs, v4PodIPs...) + v6IPs = append(v6IPs, v6PodIPs...) + if ip.Is4() { routes = append(routes, k.addRoutes(v4IPs, overlayGatewayv4)...) } else { diff --git a/cns/middlewares/k8sSwiftV2_windows.go b/cns/middlewares/k8sSwiftV2_windows.go index eb435f7d5c..0e29a1f352 100644 --- a/cns/middlewares/k8sSwiftV2_windows.go +++ b/cns/middlewares/k8sSwiftV2_windows.go @@ -25,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.getInfraRoutes(podIPInfo) if err != nil { return errors.Wrap(err, "failed to set routes for infraNIC interface") } @@ -94,7 +94,7 @@ func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string) []cns.Route { return routes } -func (k *K8sSWIFTv2Middleware) setInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) { +func (k *K8sSWIFTv2Middleware) getInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) { var routes []cns.Route ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress) diff --git a/cns/middlewares/k8sSwiftV2_windows_test.go b/cns/middlewares/k8sSwiftV2_windows_test.go index 69cf67fc2e..28f7ad2061 100644 --- a/cns/middlewares/k8sSwiftV2_windows_test.go +++ b/cns/middlewares/k8sSwiftV2_windows_test.go @@ -13,7 +13,6 @@ import ( func TestSetRoutesSuccess(t *testing.T) { middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()} - t.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24") t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16") t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.10/16") @@ -42,10 +41,6 @@ func TestSetRoutesSuccess(t *testing.T) { }, NICType: cns.InfraNIC, Routes: []cns.Route{ - { - IPAddress: "10.0.1.10/24", - GatewayIPAddress: "10.0.1.1", - }, { IPAddress: "10.0.0.0/16", GatewayIPAddress: "10.0.0.1",