Skip to content

Commit f471fbf

Browse files
committed
revert changes back
1 parent 0598af5 commit f471fbf

File tree

4 files changed

+68
-58
lines changed

4 files changed

+68
-58
lines changed

cns/middlewares/k8sSwiftV2_linux.go

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package middlewares
22

33
import (
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
48101
func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo, _ v1alpha1.InterfaceInfo, _ string) error {
49102
return nil

cns/middlewares/k8sSwiftV2_linux_test.go

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

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

cns/middlewares/k8sSwiftV2_windows.go

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

33
import (
44
"github.com/Azure/azure-container-networking/cns"
5+
"github.com/Azure/azure-container-networking/cns/logger"
56
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
67
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
78
"github.com/pkg/errors"
@@ -11,6 +12,8 @@ import (
1112
// default route is set for secondary interface NIC(i.e,delegatedNIC)
1213
func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
1314
if podIPInfo.NICType == cns.InfraNIC {
15+
logger.Printf("[SWIFTv2Middleware] skip setting default route on InfraNIC interface")
16+
1417
// as a workaround, set a default route with gw 0.0.0.0 to avoid HNS setting default route to infraNIC interface
1518
// TODO: remove this once HNS supports custom routes adding to the pod
1619
route := cns.Route{
@@ -19,12 +22,6 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
1922
}
2023
podIPInfo.Routes = append(podIPInfo.Routes, route)
2124

22-
// add routes for infraNIC
23-
routes, err := k.SetInfraRoutes(podIPInfo)
24-
if err != nil {
25-
return errors.Wrap(err, "failed to set routes for infraNIC interface")
26-
}
27-
podIPInfo.Routes = routes
2825
podIPInfo.SkipDefaultRoutes = true
2926
}
3027
return nil
@@ -51,13 +48,13 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(podIPInfo *cns.Pod
5148
PrefixLength: uint8(subnetPrefix),
5249
},
5350
GatewayIPAddress: interfaceInfo.GatewayIP,
54-
}
55-
// assign default route
56-
route := cns.Route{
57-
IPAddress: "0.0.0.0/0",
58-
GatewayIPAddress: interfaceInfo.GatewayIP,
59-
}
60-
podIPInfo.Routes = append(podIPInfo.Routes, route)
6151

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)
58+
}
6259
return nil
6360
}
Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
package middlewares
22

33
import (
4-
"reflect"
54
"testing"
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/middlewares/mock"
108
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
119
"gotest.tools/v3/assert"
1210
)
1311

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

2015
podIPInfo := []cns.PodIpInfo{
2116
{
@@ -46,27 +41,6 @@ func TestSetRoutesSuccess(t *testing.T) {
4641
}
4742
}
4843

49-
func TestSetRoutesFailure(t *testing.T) {
50-
// Failure due to env var not set
51-
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
52-
podIPInfo := []cns.PodIpInfo{
53-
{
54-
PodIPConfig: cns.IPSubnet{
55-
IPAddress: "10.0.1.10",
56-
PrefixLength: 32,
57-
},
58-
NICType: cns.InfraNIC,
59-
},
60-
}
61-
for i := range podIPInfo {
62-
ipInfo := &podIPInfo[i]
63-
err := middleware.setRoutes(ipInfo)
64-
if err == nil {
65-
t.Errorf("SetRoutes should fail due to env var not set")
66-
}
67-
}
68-
}
69-
7044
func TestAssignSubnetPrefixSuccess(t *testing.T) {
7145
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
7246

@@ -79,29 +53,16 @@ func TestAssignSubnetPrefixSuccess(t *testing.T) {
7953
MacAddress: "12:34:56:78:9a:bc",
8054
}
8155

82-
gatewayIP := "20.240.1.1"
8356
intInfo := v1alpha1.InterfaceInfo{
84-
GatewayIP: gatewayIP,
57+
GatewayIP: "20.240.1.1",
8558
SubnetAddressSpace: "20.240.1.0/16",
8659
}
8760

88-
routes := []cns.Route{
89-
{
90-
IPAddress: "0.0.0.0/0",
91-
GatewayIPAddress: gatewayIP,
92-
},
93-
}
94-
9561
ipInfo := podIPInfo
9662
err := middleware.assignSubnetPrefixLengthFields(&ipInfo, intInfo, ipInfo.PodIPConfig.IPAddress)
9763
assert.Equal(t, err, nil)
9864
// assert that the function for windows modifies all the expected fields with prefix-length
9965
assert.Equal(t, ipInfo.PodIPConfig.PrefixLength, uint8(16))
10066
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Gateway, intInfo.GatewayIP)
10167
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Subnet, intInfo.SubnetAddressSpace)
102-
103-
// compare two slices of routes
104-
if !reflect.DeepEqual(ipInfo.Routes, routes) {
105-
t.Errorf("got '%+v', expected '%+v'", ipInfo.Routes, routes)
106-
}
10768
}

0 commit comments

Comments
 (0)