Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyufan2 committed Nov 11, 2024
1 parent 633ba50 commit d669910
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
2 changes: 2 additions & 0 deletions cns/middlewares/k8sSwiftV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
return nil, errors.Wrap(err, "failed to parse mtpnc subnetAddressSpace prefix")
}
podIPInfos = append(podIPInfos, podIPInfo)
// for windows scenario, it is required to add default route with gatewayIP from CNS
k.addDefaultRoute(&podIPInfo, interfaceInfo.GatewayIP)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions cns/middlewares/k8sSwiftV2_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ func addRoutes(cidrs []string, gatewayIP string) []cns.Route {
func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo, _ v1alpha1.InterfaceInfo, _ string) error {
return nil
}

func (k *K8sSWIFTv2Middleware) addDefaultRoute(_ *cns.PodIpInfo, _ string) {}
11 changes: 7 additions & 4 deletions cns/middlewares/k8sSwiftV2_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(podIPInfo *cns.Pod
},
GatewayIPAddress: interfaceInfo.GatewayIP,
}
// assign the default route with gateway IP

return nil
}

// add default route with gateway IP to podIPInfo
func (k *K8sSWIFTv2Middleware) addDefaultRoute(podIPInfo *cns.PodIpInfo, gwIP string) {
route := cns.Route{
IPAddress: "0.0.0.0/0",
GatewayIPAddress: interfaceInfo.GatewayIP,
GatewayIPAddress: gwIP,
}
podIPInfo.Routes = append(podIPInfo.Routes, route)

return nil
}
41 changes: 31 additions & 10 deletions cns/middlewares/k8sSwiftV2_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,44 @@ func TestAssignSubnetPrefixSuccess(t *testing.T) {
SubnetAddressSpace: "20.240.1.0/16",
}

routes := []cns.Route{
{
IPAddress: "0.0.0.0/0",
GatewayIPAddress: gatewayIP,
},
}

ipInfo := podIPInfo
err := middleware.assignSubnetPrefixLengthFields(&ipInfo, intInfo, ipInfo.PodIPConfig.IPAddress)
assert.Equal(t, err, nil)
// assert that the function for windows modifies all the expected fields with prefix-length
assert.Equal(t, ipInfo.PodIPConfig.PrefixLength, uint8(16))
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Gateway, intInfo.GatewayIP)
assert.Equal(t, ipInfo.HostPrimaryIPInfo.Subnet, intInfo.SubnetAddressSpace)
}

func TestAddDefaultRoute(t *testing.T) {
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}

podIPInfo := cns.PodIpInfo{
PodIPConfig: cns.IPSubnet{
IPAddress: "20.240.1.242",
PrefixLength: 32,
},
NICType: cns.DelegatedVMNIC,
MacAddress: "12:34:56:78:9a:bc",
}

gatewayIP := "20.240.1.1"
intInfo := v1alpha1.InterfaceInfo{
GatewayIP: gatewayIP,
SubnetAddressSpace: "20.240.1.0/16",
}

ipInfo := podIPInfo
middleware.addDefaultRoute(&ipInfo, intInfo.GatewayIP)

expectedRoutes := []cns.Route{
{
IPAddress: "0.0.0.0/0",
GatewayIPAddress: gatewayIP,
},
}

// compare two slices of routes
if !reflect.DeepEqual(ipInfo.Routes, routes) {
t.Errorf("got '%+v', expected '%+v'", ipInfo.Routes, routes)
if !reflect.DeepEqual(ipInfo.Routes, expectedRoutes) {
t.Errorf("got '%+v', expected '%+v'", ipInfo.Routes, expectedRoutes)
}
}

0 comments on commit d669910

Please sign in to comment.