@@ -32,50 +32,12 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
3232 routes = append (routes , virtualGWRoute , route )
3333
3434 case cns .InfraNIC :
35- // Get and parse infraVNETCIDRs from env
36- infraVNETCIDRs , err := configuration . InfraVNETCIDRs ( )
35+ // get service and infravnet routes
36+ infraRoutes , err := k . getInfraRoutes ( podIPInfo )
3737 if err != nil {
38- return errors .Wrapf (err , "failed to get infraVNETCIDRs from env" )
39- }
40- infraVNETCIDRsv4 , infraVNETCIDRsv6 , err := utils .ParseCIDRs (infraVNETCIDRs )
41- if err != nil {
42- return errors .Wrapf (err , "failed to parse infraVNETCIDRs" )
43- }
44-
45- // Get and parse podCIDRs from env
46- podCIDRs , err := configuration .PodCIDRs ()
47- if err != nil {
48- return errors .Wrapf (err , "failed to get podCIDRs from env" )
49- }
50- podCIDRsV4 , podCIDRv6 , err := utils .ParseCIDRs (podCIDRs )
51- if err != nil {
52- return errors .Wrapf (err , "failed to parse podCIDRs" )
53- }
54-
55- // Get and parse serviceCIDRs from env
56- serviceCIDRs , err := configuration .ServiceCIDRs ()
57- if err != nil {
58- return errors .Wrapf (err , "failed to get serviceCIDRs from env" )
59- }
60- serviceCIDRsV4 , serviceCIDRsV6 , err := utils .ParseCIDRs (serviceCIDRs )
61- if err != nil {
62- return errors .Wrapf (err , "failed to parse serviceCIDRs" )
63- }
64-
65- ip , err := netip .ParseAddr (podIPInfo .PodIPConfig .IPAddress )
66- if err != nil {
67- return errors .Wrapf (err , "failed to parse podIPConfig IP address %s" , podIPInfo .PodIPConfig .IPAddress )
68- }
69-
70- if ip .Is4 () {
71- routes = append (routes , addRoutes (podCIDRsV4 , overlayGatewayv4 )... )
72- routes = append (routes , addRoutes (serviceCIDRsV4 , overlayGatewayv4 )... )
73- routes = append (routes , addRoutes (infraVNETCIDRsv4 , overlayGatewayv4 )... )
74- } else {
75- routes = append (routes , addRoutes (podCIDRv6 , overlayGatewayV6 )... )
76- routes = append (routes , addRoutes (serviceCIDRsV6 , overlayGatewayV6 )... )
77- routes = append (routes , addRoutes (infraVNETCIDRsv6 , overlayGatewayV6 )... )
38+ return errors .Wrap (err , "failed to get infra routes for infraNIC interface" )
7839 }
40+ routes = infraRoutes
7941 podIPInfo .SkipDefaultRoutes = true
8042
8143 case cns .NodeNetworkInterfaceBackendNIC : //nolint:exhaustive // ignore exhaustive types check
@@ -88,15 +50,57 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
8850 return nil
8951}
9052
91- func addRoutes (cidrs []string , gatewayIP string ) []cns.Route {
92- routes := make ([]cns.Route , len (cidrs ))
93- for i , cidr := range cidrs {
94- routes [i ] = cns.Route {
95- IPAddress : cidr ,
96- GatewayIPAddress : gatewayIP ,
97- }
53+ // CNS gets pod CIDRs from configuration env and parse them to get the v4 and v6 IPs
54+ // Containerd reassigns the IP to the adapter and kernel configures the pod cidr route by default, so windows swiftv2 does not require pod cidr
55+ func (k * K8sSWIFTv2Middleware ) GetPodCidrs () (v4IPs , v6IPs []string , err error ) {
56+ v4PodCidrs := []string {}
57+ v6PodCidrs := []string {}
58+
59+ // Get and parse podCIDRs from env
60+ podCIDRs , err := configuration .PodCIDRs ()
61+ if err != nil {
62+ return nil , nil , errors .Wrapf (err , "failed to get podCIDRs from env" )
63+ }
64+ podCIDRsV4 , podCIDRv6 , err := utils .ParseCIDRs (podCIDRs )
65+ if err != nil {
66+ return nil , nil , errors .Wrapf (err , "failed to parse podCIDRs" )
67+ }
68+
69+ v4PodCidrs = append (v4PodCidrs , podCIDRsV4 ... )
70+ v6PodCidrs = append (v6PodCidrs , podCIDRv6 ... )
71+
72+ return v4PodCidrs , v6PodCidrs , nil
73+ }
74+
75+ func (k * K8sSWIFTv2Middleware ) getInfraRoutes (podIPInfo * cns.PodIpInfo ) ([]cns.Route , error ) {
76+ var routes []cns.Route
77+
78+ ip , err := netip .ParseAddr (podIPInfo .PodIPConfig .IPAddress )
79+ if err != nil {
80+ return nil , errors .Wrapf (err , "failed to parse podIPConfig IP address %s" , podIPInfo .PodIPConfig .IPAddress )
9881 }
99- return routes
82+
83+ v4IPs , v6IPs , err := k .GetCidrs ()
84+ if err != nil {
85+ return nil , errors .Wrap (err , "failed to get node and service CIDRs" )
86+ }
87+
88+ v4PodIPs , v6PodIPs , err := k .GetPodCidrs ()
89+ if err != nil {
90+ return nil , errors .Wrap (err , "failed to get pod CIDRs" )
91+ }
92+
93+ v4IPs = append (v4IPs , v4PodIPs ... )
94+ v6IPs = append (v6IPs , v6PodIPs ... )
95+
96+ // Linux uses 169.254.1.1 as the default ipv4 gateway and fe80::1234:5678:9abc as the default ipv6 gateway
97+ if ip .Is4 () {
98+ routes = append (routes , k .AddRoutes (v4IPs , overlayGatewayv4 )... )
99+ } else {
100+ routes = append (routes , k .AddRoutes (v6IPs , overlayGatewayV6 )... )
101+ }
102+
103+ return routes , nil
100104}
101105
102106// assignSubnetPrefixLengthFields is a no-op for linux swiftv2 as the default prefix-length is sufficient
0 commit comments