Skip to content

Commit 67f46ab

Browse files
committed
optimize codes to add routes
1 parent 7022446 commit 67f46ab

File tree

4 files changed

+57
-98
lines changed

4 files changed

+57
-98
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (k *K8sSWIFTv2Middleware) Type() cns.SWIFTV2Mode {
248248
return cns.K8sSWIFTV2
249249
}
250250

251-
func (k *K8sSWIFTv2Middleware) AddRoutes(cidrs []string, gatewayIP string) []cns.Route {
251+
func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns.Route {
252252
routes := make([]cns.Route, len(cidrs))
253253
for i, cidr := range cidrs {
254254
routes[i] = cns.Route{
@@ -258,3 +258,54 @@ func (k *K8sSWIFTv2Middleware) AddRoutes(cidrs []string, gatewayIP string) []cns
258258
}
259259
return routes
260260
}
261+
262+
func (k *K8sSWIFTv2Middleware) SetInfraRoutes() []cns.Route {
263+
var routes []cns.Route
264+
265+
// Get and parse infraVNETCIDRs from env
266+
infraVNETCIDRs, err := configuration.InfraVNETCIDRs()
267+
if err != nil {
268+
return errors.Wrapf(err, "failed to get infraVNETCIDRs from env")
269+
}
270+
infraVNETCIDRsv4, infraVNETCIDRsv6, err := utils.ParseCIDRs(infraVNETCIDRs)
271+
if err != nil {
272+
return errors.Wrapf(err, "failed to parse infraVNETCIDRs")
273+
}
274+
275+
// Get and parse podCIDRs from env
276+
podCIDRs, err := configuration.PodCIDRs()
277+
if err != nil {
278+
return errors.Wrapf(err, "failed to get podCIDRs from env")
279+
}
280+
podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs)
281+
if err != nil {
282+
return errors.Wrapf(err, "failed to parse podCIDRs")
283+
}
284+
285+
// Get and parse serviceCIDRs from env
286+
serviceCIDRs, err := configuration.ServiceCIDRs()
287+
if err != nil {
288+
return errors.Wrapf(err, "failed to get serviceCIDRs from env")
289+
}
290+
serviceCIDRsV4, serviceCIDRsV6, err := utils.ParseCIDRs(serviceCIDRs)
291+
if err != nil {
292+
return errors.Wrapf(err, "failed to parse serviceCIDRs")
293+
}
294+
295+
ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
296+
if err != nil {
297+
return errors.Wrapf(err, "failed to parse podIPConfig IP address %s", podIPInfo.PodIPConfig.IPAddress)
298+
}
299+
300+
if ip.Is4() {
301+
routes = append(routes, k.addRoutes(podCIDRsV4, overlayGatewayv4)...)
302+
routes = append(routes, k.addRoutes(serviceCIDRsV4, overlayGatewayv4)...)
303+
routes = append(routes, k.addRoutes(infraVNETCIDRsv4, overlayGatewayv4)...)
304+
} else {
305+
routes = append(routes, k.addRoutes(podCIDRv6, overlayGatewayV6)...)
306+
routes = append(routes, k.addRoutes(serviceCIDRsV6, overlayGatewayV6)...)
307+
routes = append(routes, k.addRoutes(infraVNETCIDRsv6, overlayGatewayV6)...)
308+
}
309+
310+
return routes
311+
}

cns/middlewares/k8sSwiftV2_linux.go

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ package middlewares
22

33
import (
44
"fmt"
5-
"net/netip"
65

76
"github.com/Azure/azure-container-networking/cns"
8-
"github.com/Azure/azure-container-networking/cns/configuration"
97
"github.com/Azure/azure-container-networking/cns/logger"
10-
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
118
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
12-
"github.com/pkg/errors"
139
)
1410

1511
// setRoutes sets the routes for podIPInfo used in SWIFT V2 scenario.
@@ -30,50 +26,7 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
3026
routes = append(routes, virtualGWRoute, route)
3127

3228
case cns.InfraNIC:
33-
// Get and parse infraVNETCIDRs from env
34-
infraVNETCIDRs, err := configuration.InfraVNETCIDRs()
35-
if err != nil {
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, k.AddRoutes(podCIDRsV4, overlayGatewayv4)...)
70-
routes = append(routes, k.AddRoutes(serviceCIDRsV4, overlayGatewayv4)...)
71-
routes = append(routes, k.AddRoutes(infraVNETCIDRsv4, overlayGatewayv4)...)
72-
} else {
73-
routes = append(routes, k.AddRoutes(podCIDRv6, overlayGatewayV6)...)
74-
routes = append(routes, k.AddRoutes(serviceCIDRsV6, overlayGatewayV6)...)
75-
routes = append(routes, k.AddRoutes(infraVNETCIDRsv6, overlayGatewayV6)...)
76-
}
29+
routes = k.SetInfraRoutes()
7730
podIPInfo.SkipDefaultRoutes = true
7831

7932
case cns.NodeNetworkInterfaceBackendNIC: //nolint:exhaustive // ignore exhaustive types check

cns/middlewares/k8sSwiftV2_linux_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,10 @@ func TestSetRoutesFailure(t *testing.T) {
378378
}
379379

380380
func TestAddRoutes(t *testing.T) {
381+
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
381382
cidrs := []string{"10.0.0.0/24", "20.0.0.0/24"}
382383
gatewayIP := "192.168.1.1"
383-
routes := addRoutes(cidrs, gatewayIP)
384+
routes := middleware.addRoutes(cidrs, gatewayIP)
384385
expectedRoutes := []cns.Route{
385386
{
386387
IPAddress: "10.0.0.0/24",

cns/middlewares/k8sSwiftV2_windows.go

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package middlewares
22

33
import (
4-
"net/netip"
5-
64
"github.com/Azure/azure-container-networking/cns"
7-
"github.com/Azure/azure-container-networking/cns/configuration"
85
"github.com/Azure/azure-container-networking/cns/logger"
96
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
107
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
@@ -25,51 +22,8 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
2522
}
2623
podIPInfo.Routes = append(podIPInfo.Routes, route)
2724

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, k.AddRoutes(podCIDRsV4, overlayGatewayv4)...)
65-
podIPInfo.Routes = append(podIPInfo.Routes, k.AddRoutes(serviceCIDRsV4, overlayGatewayv4)...)
66-
podIPInfo.Routes = append(podIPInfo.Routes, k.AddRoutes(infraVNETCIDRsv4, overlayGatewayv4)...)
67-
} else {
68-
podIPInfo.Routes = append(podIPInfo.Routes, k.AddRoutes(podCIDRv6, overlayGatewayV6)...)
69-
podIPInfo.Routes = append(podIPInfo.Routes, k.AddRoutes(serviceCIDRsV6, overlayGatewayV6)...)
70-
podIPInfo.Routes = append(podIPInfo.Routes, k.AddRoutes(infraVNETCIDRsv6, overlayGatewayV6)...)
71-
}
72-
25+
// add routes for infraNIC
26+
podIPInfo.Routes = append(podIPInfo.Routes, k.SetInfraRoutes())
7327
podIPInfo.SkipDefaultRoutes = true
7428
}
7529
return nil

0 commit comments

Comments
 (0)