11package middlewares
22
33import (
4+ "net/netip"
5+
46 "github.com/Azure/azure-container-networking/cns"
7+ "github.com/Azure/azure-container-networking/cns/configuration"
58 "github.com/Azure/azure-container-networking/cns/logger"
69 "github.com/Azure/azure-container-networking/cns/middlewares/utils"
710 "github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
@@ -13,7 +16,6 @@ import (
1316func (k * K8sSWIFTv2Middleware ) setRoutes (podIPInfo * cns.PodIpInfo ) error {
1417 if podIPInfo .NICType == cns .InfraNIC {
1518 logger .Printf ("[SWIFTv2Middleware] skip setting default route on InfraNIC interface" )
16- podIPInfo .SkipDefaultRoutes = true
1719
1820 // as a workaround, set a default route with gw 0.0.0.0 to avoid HNS setting default route to infraNIC interface
1921 // TODO: remove this once HNS supports custom routes adding to the pod
@@ -22,6 +24,53 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
2224 GatewayIPAddress : "0.0.0.0" ,
2325 }
2426 podIPInfo .Routes = append (podIPInfo .Routes , route )
27+
28+ // Get and parse infraVNETCIDRs from env
29+ infraVNETCIDRs , err := configuration .InfraVNETCIDRs ()
30+ if err != nil {
31+ return errors .Wrapf (err , "failed to get infraVNETCIDRs from env" )
32+ }
33+ infraVNETCIDRsv4 , infraVNETCIDRsv6 , err := utils .ParseCIDRs (infraVNETCIDRs )
34+ if err != nil {
35+ return errors .Wrapf (err , "failed to parse infraVNETCIDRs" )
36+ }
37+
38+ // Get and parse podCIDRs from env
39+ podCIDRs , err := configuration .PodCIDRs ()
40+ if err != nil {
41+ return errors .Wrapf (err , "failed to get podCIDRs from env" )
42+ }
43+ podCIDRsV4 , podCIDRv6 , err := utils .ParseCIDRs (podCIDRs )
44+ if err != nil {
45+ return errors .Wrapf (err , "failed to parse podCIDRs" )
46+ }
47+
48+ // Get and parse serviceCIDRs from env
49+ serviceCIDRs , err := configuration .ServiceCIDRs ()
50+ if err != nil {
51+ return errors .Wrapf (err , "failed to get serviceCIDRs from env" )
52+ }
53+ serviceCIDRsV4 , serviceCIDRsV6 , err := utils .ParseCIDRs (serviceCIDRs )
54+ if err != nil {
55+ return errors .Wrapf (err , "failed to parse serviceCIDRs" )
56+ }
57+
58+ ip , err := netip .ParseAddr (podIPInfo .PodIPConfig .IPAddress )
59+ if err != nil {
60+ return errors .Wrapf (err , "failed to parse podIPConfig IP address %s" , podIPInfo .PodIPConfig .IPAddress )
61+ }
62+
63+ if ip .Is4 () {
64+ podIPInfo .Routes = append (podIPInfo .Routes , addRoutes (podCIDRsV4 , overlayGatewayv4 )... )
65+ podIPInfo .Routes = append (podIPInfo .Routes , addRoutes (serviceCIDRsV4 , overlayGatewayv4 )... )
66+ podIPInfo .Routes = append (podIPInfo .Routes , addRoutes (infraVNETCIDRsv4 , overlayGatewayv4 )... )
67+ } else {
68+ podIPInfo .Routes = append (podIPInfo .Routes , addRoutes (podCIDRv6 , overlayGatewayV6 )... )
69+ podIPInfo .Routes = append (podIPInfo .Routes , addRoutes (serviceCIDRsV6 , overlayGatewayV6 )... )
70+ podIPInfo .Routes = append (podIPInfo .Routes , addRoutes (infraVNETCIDRsv6 , overlayGatewayV6 )... )
71+ }
72+
73+ podIPInfo .SkipDefaultRoutes = true
2574 }
2675 return nil
2776}
0 commit comments