Skip to content

Commit a14aeff

Browse files
committed
add support for custom routes
1 parent 24107b5 commit a14aeff

File tree

4 files changed

+102
-67
lines changed

4 files changed

+102
-67
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middlewares
33
import (
44
"context"
55
"fmt"
6+
"net/netip"
67

78
"github.com/Azure/azure-container-networking/cns"
89
"github.com/Azure/azure-container-networking/cns/configuration"
@@ -247,3 +248,65 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
247248
func (k *K8sSWIFTv2Middleware) Type() cns.SWIFTV2Mode {
248249
return cns.K8sSWIFTV2
249250
}
251+
252+
func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns.Route {
253+
routes := make([]cns.Route, len(cidrs))
254+
for i, cidr := range cidrs {
255+
routes[i] = cns.Route{
256+
IPAddress: cidr,
257+
GatewayIPAddress: gatewayIP,
258+
}
259+
}
260+
return routes
261+
}
262+
263+
func (k *K8sSWIFTv2Middleware) SetInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
264+
var routes []cns.Route
265+
266+
// Get and parse infraVNETCIDRs from env
267+
infraVNETCIDRs, err := configuration.InfraVNETCIDRs()
268+
if err != nil {
269+
return nil, errors.Wrapf(err, "failed to get infraVNETCIDRs from env")
270+
}
271+
infraVNETCIDRsv4, infraVNETCIDRsv6, err := utils.ParseCIDRs(infraVNETCIDRs)
272+
if err != nil {
273+
return nil, errors.Wrapf(err, "failed to parse infraVNETCIDRs")
274+
}
275+
276+
// Get and parse podCIDRs from env
277+
podCIDRs, err := configuration.PodCIDRs()
278+
if err != nil {
279+
return nil, errors.Wrapf(err, "failed to get podCIDRs from env")
280+
}
281+
podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs)
282+
if err != nil {
283+
return nil, errors.Wrapf(err, "failed to parse podCIDRs")
284+
}
285+
286+
// Get and parse serviceCIDRs from env
287+
serviceCIDRs, err := configuration.ServiceCIDRs()
288+
if err != nil {
289+
return nil, errors.Wrapf(err, "failed to get serviceCIDRs from env")
290+
}
291+
serviceCIDRsV4, serviceCIDRsV6, err := utils.ParseCIDRs(serviceCIDRs)
292+
if err != nil {
293+
return nil, errors.Wrapf(err, "failed to parse serviceCIDRs")
294+
}
295+
296+
ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
297+
if err != nil {
298+
return nil, errors.Wrapf(err, "failed to parse podIPConfig IP address %s", podIPInfo.PodIPConfig.IPAddress)
299+
}
300+
301+
if ip.Is4() {
302+
routes = append(routes, k.addRoutes(podCIDRsV4, overlayGatewayv4)...)
303+
routes = append(routes, k.addRoutes(serviceCIDRsV4, overlayGatewayv4)...)
304+
routes = append(routes, k.addRoutes(infraVNETCIDRsv4, overlayGatewayv4)...)
305+
} else {
306+
routes = append(routes, k.addRoutes(podCIDRv6, overlayGatewayV6)...)
307+
routes = append(routes, k.addRoutes(serviceCIDRsV6, overlayGatewayV6)...)
308+
routes = append(routes, k.addRoutes(infraVNETCIDRsv6, overlayGatewayV6)...)
309+
}
310+
311+
return routes, nil
312+
}

cns/middlewares/k8sSwiftV2_linux.go

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ 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"
129
"github.com/pkg/errors"
1310
)
@@ -30,50 +27,11 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
3027
routes = append(routes, virtualGWRoute, route)
3128

3229
case cns.InfraNIC:
33-
// Get and parse infraVNETCIDRs from env
34-
infraVNETCIDRs, err := configuration.InfraVNETCIDRs()
30+
infraRoutes, err := k.SetInfraRoutes(podIPInfo)
3531
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, 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)...)
32+
return errors.Wrap(err, "failed to set routes for infraNIC interface")
7633
}
34+
routes = infraRoutes
7735
podIPInfo.SkipDefaultRoutes = true
7836

7937
case cns.NodeNetworkInterfaceBackendNIC: //nolint:exhaustive // ignore exhaustive types check
@@ -86,17 +44,6 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
8644
return nil
8745
}
8846

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-
10047
// assignSubnetPrefixLengthFields is a no-op for linux swiftv2 as the default prefix-length is sufficient
10148
func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo, _ v1alpha1.InterfaceInfo, _ string) error {
10249
return nil

cns/middlewares/k8sSwiftV2_windows.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package middlewares
22

33
import (
4+
"fmt"
5+
46
"github.com/Azure/azure-container-networking/cns"
5-
"github.com/Azure/azure-container-networking/cns/logger"
67
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
78
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
89
"github.com/pkg/errors"
@@ -12,8 +13,6 @@ import (
1213
// default route is set for secondary interface NIC(i.e,delegatedNIC)
1314
func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
1415
if podIPInfo.NICType == cns.InfraNIC {
15-
logger.Printf("[SWIFTv2Middleware] skip setting default route on InfraNIC interface")
16-
1716
// as a workaround, set a default route with gw 0.0.0.0 to avoid HNS setting default route to infraNIC interface
1817
// TODO: remove this once HNS supports custom routes adding to the pod
1918
route := cns.Route{
@@ -22,6 +21,13 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
2221
}
2322
podIPInfo.Routes = append(podIPInfo.Routes, route)
2423

24+
// add routes for infraNIC
25+
routes, err := k.SetInfraRoutes(podIPInfo)
26+
fmt.Printf("routes are %v", routes)
27+
if err != nil {
28+
return errors.Wrap(err, "failed to set routes for infraNIC interface")
29+
}
30+
podIPInfo.Routes = routes
2531
podIPInfo.SkipDefaultRoutes = true
2632
}
2733
return nil
@@ -48,13 +54,13 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(podIPInfo *cns.Pod
4854
PrefixLength: uint8(subnetPrefix),
4955
},
5056
GatewayIPAddress: interfaceInfo.GatewayIP,
51-
52-
// assign default route
53-
route := cns.Route{
54-
IPAddress: "0.0.0.0/0",
55-
GatewayIPAddress: interfaceInfo.GatewayIP,
56-
}
57-
podIPInfo.Routes = append(podIPInfo.Routes, route)
5857
}
58+
// assign default route
59+
route := cns.Route{
60+
IPAddress: "0.0.0.0/0",
61+
GatewayIPAddress: interfaceInfo.GatewayIP,
62+
}
63+
podIPInfo.Routes = append(podIPInfo.Routes, route)
64+
5965
return nil
6066
}

cns/middlewares/k8sSwiftV2_windows_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package middlewares
22

33
import (
4+
"fmt"
5+
"reflect"
46
"testing"
57

68
"github.com/Azure/azure-container-networking/cns"
9+
"github.com/Azure/azure-container-networking/cns/configuration"
710
"github.com/Azure/azure-container-networking/cns/middlewares/mock"
811
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
912
"gotest.tools/v3/assert"
1013
)
1114

1215
func TestSetRoutesSuccess(t *testing.T) {
1316
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
17+
t.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24,16A0:0010:AB00:001E::2/32")
18+
t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16,16A0:0010:AB00:0000::/32")
19+
t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.1/16,16A0:0020:AB00:0000::/32")
1420

1521
podIPInfo := []cns.PodIpInfo{
1622
{
@@ -53,16 +59,29 @@ func TestAssignSubnetPrefixSuccess(t *testing.T) {
5359
MacAddress: "12:34:56:78:9a:bc",
5460
}
5561

62+
gatewayIP := "20.240.1.1"
5663
intInfo := v1alpha1.InterfaceInfo{
57-
GatewayIP: "20.240.1.1",
64+
GatewayIP: gatewayIP,
5865
SubnetAddressSpace: "20.240.1.0/16",
5966
}
6067

68+
routes := []cns.Route{
69+
{
70+
IPAddress: "0.0.0.0/0",
71+
GatewayIPAddress: gatewayIP,
72+
},
73+
}
74+
6175
ipInfo := podIPInfo
6276
err := middleware.assignSubnetPrefixLengthFields(&ipInfo, intInfo, ipInfo.PodIPConfig.IPAddress)
6377
assert.Equal(t, err, nil)
6478
// assert that the function for windows modifies all the expected fields with prefix-length
6579
assert.Equal(t, ipInfo.PodIPConfig.PrefixLength, uint8(16))
6680
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Gateway, intInfo.GatewayIP)
6781
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Subnet, intInfo.SubnetAddressSpace)
82+
83+
// compare two slices of routes
84+
if !reflect.DeepEqual(ipInfo.Routes, routes) {
85+
t.Errorf("got '%+v', expected '%+v'", ipInfo.Routes, routes)
86+
}
6887
}

0 commit comments

Comments
 (0)