Skip to content

Commit 9a48bc7

Browse files
authored
Divide hns network (#347)
* Renaming HnsNetwork based on subnet and prefix. * Addressing Ashvin's comments.
1 parent 313878e commit 9a48bc7

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

cni/network/network.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,10 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
288288
}
289289

290290
endpointId := GetEndpointID(args)
291-
292291
policies := cni.GetPoliciesFromNwCfg(nwCfg.AdditionalArgs)
293292

294293
// Check whether the network already exists.
295294
nwInfo, nwInfoErr := plugin.nm.GetNetworkInfo(networkId)
296-
297295
if nwInfoErr == nil {
298296
/* Handle consecutive ADD calls for infrastructure containers.
299297
* This is a temporary work around for issue #57253 of Kubernetes.
@@ -318,7 +316,6 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
318316

319317
if nwInfoErr != nil {
320318
// Network does not exist.
321-
322319
log.Printf("[cni-net] Creating network %v.", networkId)
323320

324321
if !nwCfg.MultiTenancy {
@@ -331,7 +328,6 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
331328

332329
// Derive the subnet prefix from allocated IP address.
333330
subnetPrefix = result.IPs[0].Address
334-
335331
iface := &cniTypesCurr.Interface{Name: args.IfName}
336332
result.Interfaces = append(result.Interfaces, iface)
337333
}
@@ -375,7 +371,10 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
375371

376372
log.Printf("[cni-net] nwDNSInfo: %v", nwDNSInfo)
377373
// Update subnet prefix for multi-tenant scenario
378-
updateSubnetPrefix(cnsNetworkConfig, &subnetPrefix)
374+
if err = updateSubnetPrefix(cnsNetworkConfig, &subnetPrefix); err != nil {
375+
err = plugin.Errorf("Failed to updateSubnetPrefix: %v", err)
376+
return err
377+
}
379378

380379
// Create the network.
381380
nwInfo := network.NetworkInfo{

cni/network/network_linux.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig) []policy.Policy {
109109
return nil
110110
}
111111

112-
func updateSubnetPrefix(cnsNetworkConfig *cns.GetNetworkContainerResponse, subnetPrefix *net.IPNet) {
112+
func updateSubnetPrefix(cnsNetworkConfig *cns.GetNetworkContainerResponse, subnetPrefix *net.IPNet) error {
113+
return nil
113114
}
114115

115116
func getNetworkName(podName, podNs, ifName string, nwCfg *cni.NetworkConfig) (string, error) {

cni/network/network_windows.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,44 @@ func setEndpointOptions(cnsNwConfig *cns.GetNetworkContainerResponse, epInfo *ne
9393
func addSnatInterface(nwCfg *cni.NetworkConfig, result *cniTypesCurr.Result) {
9494
}
9595

96-
func updateSubnetPrefix(cnsNwConfig *cns.GetNetworkContainerResponse, subnetPrefix *net.IPNet) {
96+
func updateSubnetPrefix(cnsNwConfig *cns.GetNetworkContainerResponse, subnetPrefix *net.IPNet) error {
9797
if cnsNwConfig != nil && cnsNwConfig.MultiTenancyInfo.ID != 0 {
9898
ipconfig := cnsNwConfig.IPConfiguration
9999
ipAddr := net.ParseIP(ipconfig.IPSubnet.IPAddress)
100-
101100
if ipAddr.To4() != nil {
102-
*subnetPrefix = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(int(ipconfig.IPSubnet.PrefixLength), 32)}
101+
*subnetPrefix = net.IPNet{Mask: net.CIDRMask(int(ipconfig.IPSubnet.PrefixLength), 32)}
102+
} else if ipAddr.To16() != nil {
103+
*subnetPrefix = net.IPNet{Mask: net.CIDRMask(int(ipconfig.IPSubnet.PrefixLength), 128)}
103104
} else {
104-
*subnetPrefix = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(int(ipconfig.IPSubnet.PrefixLength), 128)}
105+
return fmt.Errorf("[cni-net] Failed to get mask from CNS network configuration")
105106
}
106107

107-
subnetPrefix.IP = subnetPrefix.IP.Mask(subnetPrefix.Mask)
108+
subnetPrefix.IP = ipAddr.Mask(subnetPrefix.Mask)
108109
log.Printf("Updated subnetPrefix: %s", subnetPrefix.String())
109110
}
111+
112+
return nil
110113
}
111114

112-
func getNetworkName(podName, podNs, ifName string, nwCfg *cni.NetworkConfig) (string, error) {
115+
func getNetworkName(podName, podNs, ifName string, nwCfg *cni.NetworkConfig) (networkName string, err error) {
116+
networkName = nwCfg.Name
117+
err = nil
113118
if nwCfg.MultiTenancy {
114119
_, cnsNetworkConfig, _, err := getContainerNetworkConfiguration(nwCfg, podName, podNs, ifName)
115120
if err != nil {
116121
log.Printf("GetContainerNetworkConfiguration failed for podname %v namespace %v with error %v", podName, podNs, err)
117-
return "", err
122+
} else {
123+
var subnet net.IPNet
124+
if err = updateSubnetPrefix(cnsNetworkConfig, &subnet); err == nil {
125+
// networkName will look like ~ azure-vlan1-172-28-1-0_24
126+
networkName = strings.Replace(subnet.String(), ".", "-", -1)
127+
networkName = strings.Replace(networkName, "/", "_", -1)
128+
networkName = fmt.Sprintf("%s-vlan%v-%v", nwCfg.Name, cnsNetworkConfig.MultiTenancyInfo.ID, networkName)
129+
}
118130
}
119-
120-
networkName := fmt.Sprintf("%s-vlanid%v", nwCfg.Name, cnsNetworkConfig.MultiTenancyInfo.ID)
121-
return networkName, nil
122131
}
123132

124-
return nwCfg.Name, nil
133+
return
125134
}
126135

127136
func setupInfraVnetRoutingForMultitenancy(

0 commit comments

Comments
 (0)