Skip to content

Commit e3b25da

Browse files
kmurudipaulyufan2
andauthored
add prefix length from mtpnc to podipinfo & gatewayip (#2828)
* add prefix length from mtpnc to podipinfo & gatewayip * separate getIpConfig based on OS for HNS prefix changes, add subnet in UTs * import packages * refactor middleware into os files after rebase w/ master * split only the subnet-prefix assign part * fix lint errors * add UTs to test the function's behavior for linux vs windows --------- Signed-off-by: Kshitija Murudi <[email protected]> Co-authored-by: paulyufan2 <[email protected]>
1 parent dc0c474 commit e3b25da

File tree

6 files changed

+102
-3
lines changed

6 files changed

+102
-3
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
221221
if prefixSize != prefixLength {
222222
return nil, errors.Wrapf(errInvalidMTPNCPrefixLength, "mtpnc primaryIP prefix length is %d", prefixSize)
223223
}
224-
podIPInfos = append(podIPInfos, cns.PodIpInfo{
224+
225+
podIPInfo := cns.PodIpInfo{
225226
PodIPConfig: cns.IPSubnet{
226227
IPAddress: ip,
227228
PrefixLength: uint8(prefixSize),
@@ -230,9 +231,15 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
230231
NICType: cns.DelegatedVMNIC,
231232
SkipDefaultRoutes: false,
232233
// InterfaceName is empty for DelegatedVMNIC
233-
})
234+
}
235+
// for windows scenario, it is required to add additional fields with the exact subnetAddressSpace
236+
// received from MTPNC, this function assigns them for windows while linux is a no-op
237+
err = k.assignSubnetPrefixLengthFields(&podIPInfo, interfaceInfo, ip)
238+
if err != nil {
239+
return nil, errors.Wrap(err, "failed to parse mtpnc subnetAddressSpace prefix")
240+
}
241+
podIPInfos = append(podIPInfos, podIPInfo)
234242
}
235-
236243
}
237244
}
238245

cns/middlewares/k8sSwiftV2_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/Azure/azure-container-networking/cns/configuration"
99
"github.com/Azure/azure-container-networking/cns/logger"
1010
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
11+
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
1112
"github.com/pkg/errors"
1213
)
1314

@@ -95,3 +96,8 @@ func addRoutes(cidrs []string, gatewayIP string) []cns.Route {
9596
}
9697
return routes
9798
}
99+
100+
// assignSubnetPrefixLengthFields is a no-op for linux swiftv2 as the default prefix-length is sufficient
101+
func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo, _ v1alpha1.InterfaceInfo, _ string) error {
102+
return nil
103+
}

cns/middlewares/k8sSwiftV2_linux_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/Azure/azure-container-networking/cns/logger"
1111
"github.com/Azure/azure-container-networking/cns/middlewares/mock"
1212
"github.com/Azure/azure-container-networking/cns/types"
13+
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
1314
"gotest.tools/v3/assert"
1415
)
1516

@@ -455,3 +456,29 @@ func TestGetSWIFTv2IPConfigMultiInterfaceSuccess(t *testing.T) {
455456
assert.Equal(t, ipInfo.SkipDefaultRoutes, false)
456457
}
457458
}
459+
460+
func TestAssignSubnetPrefixSuccess(t *testing.T) {
461+
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
462+
463+
podIPInfo := cns.PodIpInfo{
464+
PodIPConfig: cns.IPSubnet{
465+
IPAddress: "20.240.1.242",
466+
PrefixLength: 32,
467+
},
468+
NICType: cns.DelegatedVMNIC,
469+
MacAddress: "12:34:56:78:9a:bc",
470+
}
471+
472+
intInfo := v1alpha1.InterfaceInfo{
473+
GatewayIP: "20.240.1.1",
474+
SubnetAddressSpace: "20.240.1.0/16",
475+
}
476+
477+
ipInfo := podIPInfo
478+
err := middleware.assignSubnetPrefixLengthFields(&ipInfo, intInfo, ipInfo.PodIPConfig.IPAddress)
479+
assert.Equal(t, err, nil)
480+
// assert that the function for linux does not modify any fields
481+
assert.Equal(t, ipInfo.PodIPConfig.PrefixLength, uint8(32))
482+
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Gateway, "")
483+
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Subnet, "")
484+
}

cns/middlewares/k8sSwiftV2_windows.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package middlewares
33
import (
44
"github.com/Azure/azure-container-networking/cns"
55
"github.com/Azure/azure-container-networking/cns/logger"
6+
"github.com/Azure/azure-container-networking/cns/middlewares/utils"
7+
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
8+
"github.com/pkg/errors"
69
)
710

811
// for AKS L1VH, do not set default route on infraNIC to avoid customer pod reaching all infra vnet services
@@ -14,3 +17,28 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
1417
}
1518
return nil
1619
}
20+
21+
// assignSubnetPrefixLengthFields will assign the subnet-prefix length to some fields of podipinfo
22+
// this is required for the windows scenario so that HNS programming is successful for pods
23+
func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(podIPInfo *cns.PodIpInfo, interfaceInfo v1alpha1.InterfaceInfo, ip string) error {
24+
// Parse MTPNC SubnetAddressSpace to get the subnet prefix length
25+
subnet, subnetPrefix, err := utils.ParseIPAndPrefix(interfaceInfo.SubnetAddressSpace)
26+
if err != nil {
27+
return errors.Wrap(err, "failed to parse mtpnc subnetAddressSpace prefix")
28+
}
29+
// assign the subnet-prefix length to all fields in podipinfo
30+
podIPInfo.PodIPConfig.PrefixLength = uint8(subnetPrefix)
31+
podIPInfo.HostPrimaryIPInfo = cns.HostIPInfo{
32+
Gateway: interfaceInfo.GatewayIP,
33+
PrimaryIP: ip,
34+
Subnet: interfaceInfo.SubnetAddressSpace,
35+
}
36+
podIPInfo.NetworkContainerPrimaryIPConfig = cns.IPConfiguration{
37+
IPSubnet: cns.IPSubnet{
38+
IPAddress: subnet,
39+
PrefixLength: uint8(subnetPrefix),
40+
},
41+
GatewayIPAddress: interfaceInfo.GatewayIP,
42+
}
43+
return nil
44+
}

cns/middlewares/k8sSwiftV2_windows_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/Azure/azure-container-networking/cns"
77
"github.com/Azure/azure-container-networking/cns/middlewares/mock"
8+
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
89
"gotest.tools/v3/assert"
910
)
1011

@@ -39,3 +40,29 @@ func TestSetRoutesSuccess(t *testing.T) {
3940
}
4041
}
4142
}
43+
44+
func TestAssignSubnetPrefixSuccess(t *testing.T) {
45+
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
46+
47+
podIPInfo := cns.PodIpInfo{
48+
PodIPConfig: cns.IPSubnet{
49+
IPAddress: "20.240.1.242",
50+
PrefixLength: 32,
51+
},
52+
NICType: cns.DelegatedVMNIC,
53+
MacAddress: "12:34:56:78:9a:bc",
54+
}
55+
56+
intInfo := v1alpha1.InterfaceInfo{
57+
GatewayIP: "20.240.1.1",
58+
SubnetAddressSpace: "20.240.1.0/16",
59+
}
60+
61+
ipInfo := podIPInfo
62+
err := middleware.assignSubnetPrefixLengthFields(&ipInfo, intInfo, ipInfo.PodIPConfig.IPAddress)
63+
assert.Equal(t, err, nil)
64+
// assert that the function for windows modifies all the expected fields with prefix-length
65+
assert.Equal(t, ipInfo.PodIPConfig.PrefixLength, uint8(16))
66+
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Gateway, intInfo.GatewayIP)
67+
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Subnet, intInfo.SubnetAddressSpace)
68+
}

cns/middlewares/mock/mockClient.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func NewClient() *Client {
7373
GatewayIP: "10.0.0.1",
7474
DeviceType: v1alpha1.DeviceTypeVnetNIC,
7575
AccelnetEnabled: false,
76+
SubnetAddressSpace: "192.168.0.0/24",
7677
}
7778
testInterfaceInfos3 := v1alpha1.InterfaceInfo{
7879
NCID: "testncid",
@@ -81,6 +82,7 @@ func NewClient() *Client {
8182
GatewayIP: "10.0.0.1",
8283
DeviceType: v1alpha1.DeviceTypeVnetNIC,
8384
AccelnetEnabled: false,
85+
SubnetAddressSpace: "192.168.0.0/24",
8486
}
8587
testInterfaceInfos5 := v1alpha1.InterfaceInfo{
8688
NCID: "testncid",
@@ -89,6 +91,7 @@ func NewClient() *Client {
8991
GatewayIP: "10.0.0.1",
9092
DeviceType: v1alpha1.DeviceTypeInfiniBandNIC,
9193
AccelnetEnabled: true,
94+
SubnetAddressSpace: "192.168.0.0/24",
9295
}
9396

9497
testMTPNC1 := v1alpha1.MultitenantPodNetworkConfig{
@@ -216,6 +219,7 @@ func (c *Client) SetMTPNCReady() {
216219
GatewayIP: "10.0.0.1",
217220
DeviceType: v1alpha1.DeviceTypeVnetNIC,
218221
AccelnetEnabled: false,
222+
SubnetAddressSpace: "192.168.0.0/24",
219223
}
220224

221225
testMTPNC1 := v1alpha1.MultitenantPodNetworkConfig{}

0 commit comments

Comments
 (0)