Skip to content

Commit cac0941

Browse files
paulyufan2rbtr
andauthored
Fix: Add default route on delegatedNIC interface (#3109)
* add default route on delegatedNIC interface * remove logger import * fix linter issue * fix comments * format codes * gofumpt file * enhance comment * Update cns/middlewares/k8sSwiftV2_linux.go Co-authored-by: Evan Baker <[email protected]> Signed-off-by: Paul Yu <[email protected]> --------- Signed-off-by: Paul Yu <[email protected]> Co-authored-by: Evan Baker <[email protected]>
1 parent 1f3f5c5 commit cac0941

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
237237
return nil, errors.Wrap(err, "failed to parse mtpnc subnetAddressSpace prefix")
238238
}
239239
podIPInfos = append(podIPInfos, podIPInfo)
240+
// for windows scenario, it is required to add default route with gatewayIP from CNS
241+
k.addDefaultRoute(&podIPInfo, interfaceInfo.GatewayIP)
240242
}
241243
}
242244
}

cns/middlewares/k8sSwiftV2_linux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@ func addRoutes(cidrs []string, gatewayIP string) []cns.Route {
101101
func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo, _ v1alpha1.InterfaceInfo, _ string) error {
102102
return nil
103103
}
104+
105+
func (k *K8sSWIFTv2Middleware) addDefaultRoute(*cns.PodIpInfo, string) {}

cns/middlewares/k8sSwiftV2_windows.go

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

33
import (
44
"github.com/Azure/azure-container-networking/cns"
5-
"github.com/Azure/azure-container-networking/cns/logger"
65
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
76
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
87
"github.com/pkg/errors"
@@ -12,7 +11,15 @@ import (
1211
// default route is set for secondary interface NIC(i.e,delegatedNIC)
1312
func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
1413
if podIPInfo.NICType == cns.InfraNIC {
15-
logger.Printf("[SWIFTv2Middleware] skip setting default route on InfraNIC interface")
14+
// as a workaround, HNS will not set this dummy default route(0.0.0.0/0, nexthop:0.0.0.0) on infraVnet interface eth0
15+
// the only usage for this dummy default is to bypass HNS setting default route on eth0
16+
// TODO: Remove this once HNS fix is ready
17+
route := cns.Route{
18+
IPAddress: "0.0.0.0/0",
19+
GatewayIPAddress: "0.0.0.0",
20+
}
21+
podIPInfo.Routes = append(podIPInfo.Routes, route)
22+
1623
podIPInfo.SkipDefaultRoutes = true
1724
}
1825
return nil
@@ -42,3 +49,12 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(podIPInfo *cns.Pod
4249
}
4350
return nil
4451
}
52+
53+
// add default route with gateway IP to podIPInfo
54+
func (k *K8sSWIFTv2Middleware) addDefaultRoute(podIPInfo *cns.PodIpInfo, gwIP string) {
55+
route := cns.Route{
56+
IPAddress: "0.0.0.0/0",
57+
GatewayIPAddress: gwIP,
58+
}
59+
podIPInfo.Routes = append(podIPInfo.Routes, route)
60+
}

cns/middlewares/k8sSwiftV2_windows_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package middlewares
22

33
import (
4+
"reflect"
45
"testing"
56

67
"github.com/Azure/azure-container-networking/cns"
@@ -66,3 +67,36 @@ func TestAssignSubnetPrefixSuccess(t *testing.T) {
6667
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Gateway, intInfo.GatewayIP)
6768
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Subnet, intInfo.SubnetAddressSpace)
6869
}
70+
71+
func TestAddDefaultRoute(t *testing.T) {
72+
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
73+
74+
podIPInfo := cns.PodIpInfo{
75+
PodIPConfig: cns.IPSubnet{
76+
IPAddress: "20.240.1.242",
77+
PrefixLength: 32,
78+
},
79+
NICType: cns.DelegatedVMNIC,
80+
MacAddress: "12:34:56:78:9a:bc",
81+
}
82+
83+
gatewayIP := "20.240.1.1"
84+
intInfo := v1alpha1.InterfaceInfo{
85+
GatewayIP: gatewayIP,
86+
SubnetAddressSpace: "20.240.1.0/16",
87+
}
88+
89+
ipInfo := podIPInfo
90+
middleware.addDefaultRoute(&ipInfo, intInfo.GatewayIP)
91+
92+
expectedRoutes := []cns.Route{
93+
{
94+
IPAddress: "0.0.0.0/0",
95+
GatewayIPAddress: gatewayIP,
96+
},
97+
}
98+
99+
if !reflect.DeepEqual(ipInfo.Routes, expectedRoutes) {
100+
t.Errorf("got '%+v', expected '%+v'", ipInfo.Routes, expectedRoutes)
101+
}
102+
}

0 commit comments

Comments
 (0)