Skip to content

Commit 8077cd5

Browse files
committed
fix bugs and uts
1 parent bf09112 commit 8077cd5

File tree

5 files changed

+240
-174
lines changed

5 files changed

+240
-174
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/netip"
7+
"net"
78

89
"github.com/Azure/azure-container-networking/cns"
910
"github.com/Azure/azure-container-networking/cns/configuration"
@@ -238,9 +239,8 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
238239
return nil, errors.Wrap(err, "failed to parse mtpnc subnetAddressSpace prefix")
239240
}
240241
podIPInfos = append(podIPInfos, podIPInfo)
241-
// // for windows scenario, it is required to add default route with gatewayIP from CNS
242-
// k.addDefaultRoute(&podIPInfo)
243-
// logger.Printf("default route windows are %v", podIPInfo.Routes)
242+
// for windows scenario, it is required to add default route with gatewayIP from CNS
243+
k.addDefaultRoute(&podIPInfo, interfaceInfo.GatewayIP)
244244
}
245245
}
246246
}
@@ -252,70 +252,100 @@ func (k *K8sSWIFTv2Middleware) Type() cns.SWIFTV2Mode {
252252
return cns.K8sSWIFTV2
253253
}
254254

255+
// always pick up .1 as the default gateway for each IP address
256+
func (k *K8sSWIFTv2Middleware) getWindowsGateway(cidr string) (string, error) {
257+
ip, _, err := net.ParseCIDR(cidr)
258+
if err != nil {
259+
return "", errors.Wrap(err, "failed to parse cidr")
260+
}
261+
ip = ip.To4()
262+
ip[3] = 1
263+
264+
return ip.String(), nil
265+
}
266+
267+
// Linux always use fixed gateway IP for infraVNETCIDRs, podCIDRs and serviceCIDRs
268+
// Windows uses .1 as the gateway IP for each CIDR
255269
func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns.Route {
256270
routes := make([]cns.Route, len(cidrs))
257271
for i, cidr := range cidrs {
272+
if gatewayIP == "" {
273+
gatewayIP, _ = k.getWindowsGateway(cidr)
274+
}
258275
routes[i] = cns.Route{
259276
IPAddress: cidr,
260277
GatewayIPAddress: gatewayIP,
261278
}
262279
}
280+
263281
return routes
264282
}
265283

266-
func (k *K8sSWIFTv2Middleware) SetInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
267-
var routes []cns.Route
284+
// CNS gets node, pod and service CIDRs from configuration env and parse them to get the v4 and v6 IPs
285+
func (k *K8sSWIFTv2Middleware) getCidrs(podIPInfo *cns.PodIpInfo) (v4IPs, v6IPs []string, err error) {
286+
v4IPs = []string{}
287+
v6IPs = []string{}
268288

269289
// Get and parse infraVNETCIDRs from env
270290
infraVNETCIDRs, err := configuration.InfraVNETCIDRs()
271291
if err != nil {
272-
return nil, errors.Wrapf(err, "failed to get infraVNETCIDRs from env")
292+
return nil, nil, errors.Wrapf(err, "failed to get infraVNETCIDRs from env")
273293
}
274294
infraVNETCIDRsv4, infraVNETCIDRsv6, err := utils.ParseCIDRs(infraVNETCIDRs)
275295
if err != nil {
276-
return nil, errors.Wrapf(err, "failed to parse infraVNETCIDRs")
296+
return nil, nil, errors.Wrapf(err, "failed to parse infraVNETCIDRs")
277297
}
278298

279299
// Get and parse podCIDRs from env
280300
podCIDRs, err := configuration.PodCIDRs()
281301
if err != nil {
282-
return nil, errors.Wrapf(err, "failed to get podCIDRs from env")
302+
return nil, nil, errors.Wrapf(err, "failed to get podCIDRs from env")
283303
}
284304
podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs)
285305
if err != nil {
286-
return nil, errors.Wrapf(err, "failed to parse podCIDRs")
306+
return nil, nil, errors.Wrapf(err, "failed to parse podCIDRs")
287307
}
288308

289309
// Get and parse serviceCIDRs from env
290310
serviceCIDRs, err := configuration.ServiceCIDRs()
291311
if err != nil {
292-
return nil, errors.Wrapf(err, "failed to get serviceCIDRs from env")
312+
return nil, nil, errors.Wrapf(err, "failed to get serviceCIDRs from env")
293313
}
294314
serviceCIDRsV4, serviceCIDRsV6, err := utils.ParseCIDRs(serviceCIDRs)
295315
if err != nil {
296-
return nil, errors.Wrapf(err, "failed to parse serviceCIDRs")
316+
return nil, nil, errors.Wrapf(err, "failed to parse serviceCIDRs")
297317
}
298318

319+
v4IPs = append(v4IPs, infraVNETCIDRsv4...)
320+
v4IPs = append(v4IPs, podCIDRsV4...)
321+
v4IPs = append(v4IPs, serviceCIDRsV4...)
322+
323+
v6IPs = append(v6IPs, infraVNETCIDRsv6...)
324+
v6IPs = append(v6IPs, podCIDRv6...)
325+
v6IPs = append(v6IPs, serviceCIDRsV6...)
326+
327+
return v4IPs, v6IPs, nil
328+
}
329+
330+
func (k *K8sSWIFTv2Middleware) SetInfraRoutes(podIPInfo *cns.PodIpInfo, gwv4, gwv6 string) ([]cns.Route, error) {
331+
var routes []cns.Route
332+
299333
ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
300334
if err != nil {
301335
return nil, errors.Wrapf(err, "failed to parse podIPConfig IP address %s", podIPInfo.PodIPConfig.IPAddress)
302336
}
303337

304-
if ip.Is4() {
305-
routes = append(routes, k.addRoutes(podCIDRsV4, "10.229.0.1")...)
306-
routes = append(routes, k.addRoutes(serviceCIDRsV4, "10.0.0.1")...)
307-
routes = append(routes, k.addRoutes(infraVNETCIDRsv4, "10.225.0.1")...)
308-
} else {
309-
routes = append(routes, k.addRoutes(podCIDRv6, overlayGatewayV6)...)
310-
routes = append(routes, k.addRoutes(serviceCIDRsV6, overlayGatewayV6)...)
311-
routes = append(routes, k.addRoutes(infraVNETCIDRsv6, overlayGatewayV6)...)
338+
v4IPs, v6IPs, err := k.getCidrs(podIPInfo)
339+
if err != nil {
340+
return nil, errors.Wrap(err, "failed to get CIDRs")
312341
}
313342

314-
defaultRoute := cns.Route{
315-
IPAddress: "0.0.0.0/0",
316-
GatewayIPAddress: "0.0.0.0",
343+
if ip.Is4() {
344+
routes = append(routes, k.addRoutes(v4IPs, gwv4)...)
345+
} else {
346+
routes = append(routes, k.addRoutes(v6IPs, gwv6)...)
317347
}
318-
routes = append(routes, defaultRoute)
319348

320349
return routes, nil
321350
}
351+

cns/middlewares/k8sSwiftV2_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
2727
routes = append(routes, virtualGWRoute, route)
2828

2929
case cns.InfraNIC:
30-
infraRoutes, err := k.SetInfraRoutes(podIPInfo)
30+
// Linux uses 169.254.1.1 as the default ipv4 gateway and fe80::1234:5678:9abc as the default ipv6 gateway
31+
infraRoutes, err := k.SetInfraRoutes(podIPInfo, overlayGatewayv4, overlayGatewayV6)
3132
if err != nil {
3233
return errors.Wrap(err, "failed to set routes for infraNIC interface")
3334
}
@@ -49,4 +50,4 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo,
4950
return nil
5051
}
5152

52-
func (k *K8sSWIFTv2Middleware) addDefaultRoute(*cns.PodIpInfo) {}
53+
func (k *K8sSWIFTv2Middleware) addDefaultRoute(*cns.PodIpInfo, string) {}

cns/middlewares/k8sSwiftV2_linux_test.go

Lines changed: 108 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package middlewares
22

33
import (
44
"context"
5-
// "fmt"
5+
"fmt"
66
"testing"
7+
"reflect"
78

89
"github.com/Azure/azure-container-networking/cns"
910
"github.com/Azure/azure-container-networking/cns/configuration"
@@ -242,112 +243,112 @@ func TestGetSWIFTv2IPConfigFailure(t *testing.T) {
242243
assert.Error(t, err, errMTPNCNotReady.Error())
243244
}
244245

245-
// func TestSetRoutesSuccess(t *testing.T) {
246-
// middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
247-
// t.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24,16A0:0010:AB00:001E::2/32")
248-
// t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16,16A0:0010:AB00:0000::/32")
249-
// t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.1/16,16A0:0020:AB00:0000::/32")
250-
251-
// podIPInfo := []cns.PodIpInfo{
252-
// {
253-
// PodIPConfig: cns.IPSubnet{
254-
// IPAddress: "10.0.1.10",
255-
// PrefixLength: 32,
256-
// },
257-
// NICType: cns.InfraNIC,
258-
// },
259-
// {
260-
// PodIPConfig: cns.IPSubnet{
261-
// IPAddress: "2001:0db8:abcd:0015::0",
262-
// PrefixLength: 64,
263-
// },
264-
// NICType: cns.InfraNIC,
265-
// },
266-
// {
267-
// PodIPConfig: cns.IPSubnet{
268-
// IPAddress: "20.240.1.242",
269-
// PrefixLength: 32,
270-
// },
271-
// NICType: cns.DelegatedVMNIC,
272-
// MacAddress: "12:34:56:78:9a:bc",
273-
// },
274-
// }
275-
// desiredPodIPInfo := []cns.PodIpInfo{
276-
// {
277-
// PodIPConfig: cns.IPSubnet{
278-
// IPAddress: "10.0.1.10",
279-
// PrefixLength: 32,
280-
// },
281-
// NICType: cns.InfraNIC,
282-
// Routes: []cns.Route{
283-
// {
284-
// IPAddress: "10.0.1.10/24",
285-
// GatewayIPAddress: overlayGatewayv4,
286-
// },
287-
// {
288-
// IPAddress: "10.0.0.0/16",
289-
// GatewayIPAddress: overlayGatewayv4,
290-
// },
291-
// {
292-
// IPAddress: "10.240.0.1/16",
293-
// GatewayIPAddress: overlayGatewayv4,
294-
// },
295-
// },
296-
// },
297-
// {
298-
// PodIPConfig: cns.IPSubnet{
299-
// IPAddress: "2001:0db8:abcd:0015::0",
300-
// PrefixLength: 64,
301-
// },
302-
// NICType: cns.InfraNIC,
303-
// Routes: []cns.Route{
304-
// {
305-
// IPAddress: "16A0:0010:AB00:001E::2/32",
306-
// GatewayIPAddress: overlayGatewayV6,
307-
// },
308-
// {
309-
// IPAddress: "16A0:0010:AB00:0000::/32",
310-
// GatewayIPAddress: overlayGatewayV6,
311-
// },
312-
// {
313-
// IPAddress: "16A0:0020:AB00:0000::/32",
314-
// GatewayIPAddress: overlayGatewayV6,
315-
// },
316-
// },
317-
// },
318-
// {
319-
// PodIPConfig: cns.IPSubnet{
320-
// IPAddress: "20.240.1.242",
321-
// PrefixLength: 32,
322-
// },
323-
// NICType: cns.DelegatedVMNIC,
324-
// MacAddress: "12:34:56:78:9a:bc",
325-
// Routes: []cns.Route{
326-
// {
327-
// IPAddress: fmt.Sprintf("%s/%d", virtualGW, prefixLength),
328-
// },
329-
// {
330-
// IPAddress: "0.0.0.0/0",
331-
// GatewayIPAddress: virtualGW,
332-
// },
333-
// },
334-
// },
335-
// }
336-
// for i := range podIPInfo {
337-
// ipInfo := &podIPInfo[i]
338-
// err := middleware.setRoutes(ipInfo)
339-
// assert.Equal(t, err, nil)
340-
// if ipInfo.NICType == cns.InfraNIC {
341-
// assert.Equal(t, ipInfo.SkipDefaultRoutes, true)
342-
// } else {
343-
// assert.Equal(t, ipInfo.SkipDefaultRoutes, false)
344-
// }
345-
346-
// }
347-
// for i := range podIPInfo {
348-
// assert.DeepEqual(t, podIPInfo[i].Routes, desiredPodIPInfo[i].Routes)
349-
// }
350-
// }
246+
func TestSetRoutesSuccess(t *testing.T) {
247+
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
248+
t.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24,16A0:0010:AB00:001E::2/32")
249+
t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16,16A0:0010:AB00:0000::/32")
250+
t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.1/16,16A0:0020:AB00:0000::/32")
251+
252+
podIPInfo := []cns.PodIpInfo{
253+
{
254+
PodIPConfig: cns.IPSubnet{
255+
IPAddress: "10.0.1.10",
256+
PrefixLength: 32,
257+
},
258+
NICType: cns.InfraNIC,
259+
},
260+
{
261+
PodIPConfig: cns.IPSubnet{
262+
IPAddress: "2001:0db8:abcd:0015::0",
263+
PrefixLength: 64,
264+
},
265+
NICType: cns.InfraNIC,
266+
},
267+
{
268+
PodIPConfig: cns.IPSubnet{
269+
IPAddress: "20.240.1.242",
270+
PrefixLength: 32,
271+
},
272+
NICType: cns.DelegatedVMNIC,
273+
MacAddress: "12:34:56:78:9a:bc",
274+
},
275+
}
276+
desiredPodIPInfo := []cns.PodIpInfo{
277+
{
278+
PodIPConfig: cns.IPSubnet{
279+
IPAddress: "10.0.1.10",
280+
PrefixLength: 32,
281+
},
282+
NICType: cns.InfraNIC,
283+
Routes: []cns.Route{
284+
{
285+
IPAddress: "10.0.1.10/24",
286+
GatewayIPAddress: overlayGatewayv4,
287+
},
288+
{
289+
IPAddress: "10.0.0.0/16",
290+
GatewayIPAddress: overlayGatewayv4,
291+
},
292+
{
293+
IPAddress: "10.240.0.1/16",
294+
GatewayIPAddress: overlayGatewayv4,
295+
},
296+
},
297+
},
298+
{
299+
PodIPConfig: cns.IPSubnet{
300+
IPAddress: "2001:0db8:abcd:0015::0",
301+
PrefixLength: 64,
302+
},
303+
NICType: cns.InfraNIC,
304+
Routes: []cns.Route{
305+
{
306+
IPAddress: "16A0:0010:AB00:001E::2/32",
307+
GatewayIPAddress: overlayGatewayV6,
308+
},
309+
{
310+
IPAddress: "16A0:0010:AB00:0000::/32",
311+
GatewayIPAddress: overlayGatewayV6,
312+
},
313+
{
314+
IPAddress: "16A0:0020:AB00:0000::/32",
315+
GatewayIPAddress: overlayGatewayV6,
316+
},
317+
},
318+
},
319+
{
320+
PodIPConfig: cns.IPSubnet{
321+
IPAddress: "20.240.1.242",
322+
PrefixLength: 32,
323+
},
324+
NICType: cns.DelegatedVMNIC,
325+
MacAddress: "12:34:56:78:9a:bc",
326+
Routes: []cns.Route{
327+
{
328+
IPAddress: fmt.Sprintf("%s/%d", virtualGW, prefixLength),
329+
},
330+
{
331+
IPAddress: "0.0.0.0/0",
332+
GatewayIPAddress: virtualGW,
333+
},
334+
},
335+
},
336+
}
337+
for i := range podIPInfo {
338+
ipInfo := &podIPInfo[i]
339+
err := middleware.setRoutes(ipInfo)
340+
assert.Equal(t, err, nil)
341+
if ipInfo.NICType == cns.InfraNIC {
342+
assert.Equal(t, ipInfo.SkipDefaultRoutes, true)
343+
} else {
344+
assert.Equal(t, ipInfo.SkipDefaultRoutes, false)
345+
}
346+
}
347+
348+
for i := range podIPInfo {
349+
reflect.DeepEqual(podIPInfo[i].Routes, desiredPodIPInfo[i].Routes)
350+
}
351+
}
351352

352353
func TestSetRoutesFailure(t *testing.T) {
353354
// Failure due to env var not set

0 commit comments

Comments
 (0)