@@ -2,9 +2,12 @@ package middlewares
22
33import (
44 "fmt"
5+ "net/netip"
56
67 "github.com/Azure/azure-container-networking/cns"
8+ "github.com/Azure/azure-container-networking/cns/configuration"
79 "github.com/Azure/azure-container-networking/cns/logger"
10+ "github.com/Azure/azure-container-networking/cns/middlewares/utils"
811 "github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
912 "github.com/pkg/errors"
1013)
@@ -27,11 +30,50 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
2730 routes = append (routes , virtualGWRoute , route )
2831
2932 case cns .InfraNIC :
30- infraRoutes , err := k .SetInfraRoutes (podIPInfo )
33+ // Get and parse infraVNETCIDRs from env
34+ infraVNETCIDRs , err := configuration .InfraVNETCIDRs ()
3135 if err != nil {
32- return errors .Wrap (err , "failed to set routes for infraNIC interface" )
36+ return errors .Wrapf (err , "failed to get infraVNETCIDRs from env" )
37+ }
38+ infraVNETCIDRsv4 , infraVNETCIDRsv6 , err := utils .ParseCIDRs (infraVNETCIDRs )
39+ if err != nil {
40+ return errors .Wrapf (err , "failed to parse infraVNETCIDRs" )
41+ }
42+
43+ // Get and parse podCIDRs from env
44+ podCIDRs , err := configuration .PodCIDRs ()
45+ if err != nil {
46+ return errors .Wrapf (err , "failed to get podCIDRs from env" )
47+ }
48+ podCIDRsV4 , podCIDRv6 , err := utils .ParseCIDRs (podCIDRs )
49+ if err != nil {
50+ return errors .Wrapf (err , "failed to parse podCIDRs" )
51+ }
52+
53+ // Get and parse serviceCIDRs from env
54+ serviceCIDRs , err := configuration .ServiceCIDRs ()
55+ if err != nil {
56+ return errors .Wrapf (err , "failed to get serviceCIDRs from env" )
57+ }
58+ serviceCIDRsV4 , serviceCIDRsV6 , err := utils .ParseCIDRs (serviceCIDRs )
59+ if err != nil {
60+ return errors .Wrapf (err , "failed to parse serviceCIDRs" )
61+ }
62+
63+ ip , err := netip .ParseAddr (podIPInfo .PodIPConfig .IPAddress )
64+ if err != nil {
65+ return errors .Wrapf (err , "failed to parse podIPConfig IP address %s" , podIPInfo .PodIPConfig .IPAddress )
66+ }
67+
68+ if ip .Is4 () {
69+ routes = append (routes , addRoutes (podCIDRsV4 , overlayGatewayv4 )... )
70+ routes = append (routes , addRoutes (serviceCIDRsV4 , overlayGatewayv4 )... )
71+ routes = append (routes , addRoutes (infraVNETCIDRsv4 , overlayGatewayv4 )... )
72+ } else {
73+ routes = append (routes , addRoutes (podCIDRv6 , overlayGatewayV6 )... )
74+ routes = append (routes , addRoutes (serviceCIDRsV6 , overlayGatewayV6 )... )
75+ routes = append (routes , addRoutes (infraVNETCIDRsv6 , overlayGatewayV6 )... )
3376 }
34- routes = infraRoutes
3577 podIPInfo .SkipDefaultRoutes = true
3678
3779 case cns .NodeNetworkInterfaceBackendNIC : //nolint:exhaustive // ignore exhaustive types check
@@ -44,6 +86,17 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
4486 return nil
4587}
4688
89+ func addRoutes (cidrs []string , gatewayIP string ) []cns.Route {
90+ routes := make ([]cns.Route , len (cidrs ))
91+ for i , cidr := range cidrs {
92+ routes [i ] = cns.Route {
93+ IPAddress : cidr ,
94+ GatewayIPAddress : gatewayIP ,
95+ }
96+ }
97+ return routes
98+ }
99+
47100// assignSubnetPrefixLengthFields is a no-op for linux swiftv2 as the default prefix-length is sufficient
48101func (k * K8sSWIFTv2Middleware ) assignSubnetPrefixLengthFields (_ * cns.PodIpInfo , _ v1alpha1.InterfaceInfo , _ string ) error {
49102 return nil
0 commit comments