|
1 | 1 | package network |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "encoding/json" |
6 | 5 | "fmt" |
7 | 6 | "net" |
@@ -160,42 +159,45 @@ func updateSubnetPrefix(cnsNwConfig *cns.GetNetworkContainerResponse, subnetPref |
160 | 159 | return nil |
161 | 160 | } |
162 | 161 |
|
163 | | -func (plugin *NetPlugin) getNetworkName(podName, podNs, ifName string, nwCfg *cni.NetworkConfig) (string, error) { |
164 | | - var ( |
165 | | - networkName string |
166 | | - err error |
167 | | - cnsNetworkConfig *cns.GetNetworkContainerResponse |
168 | | - ) |
169 | | - |
170 | | - networkName = nwCfg.Name |
171 | | - err = nil |
172 | | - |
173 | | - if nwCfg.MultiTenancy { |
174 | | - determineWinVer() |
175 | | - if len(strings.TrimSpace(podName)) == 0 || len(strings.TrimSpace(podNs)) == 0 { |
176 | | - err = fmt.Errorf("POD info cannot be empty. PodName: %s, PodNamespace: %s", podName, podNs) |
177 | | - return networkName, err |
178 | | - } |
| 162 | +func (plugin *NetPlugin) getNetworkName(podName, podNs, ifName, netNs string, cnsResponse *cns.GetNetworkContainerResponse, nwCfg *cni.NetworkConfig) (string, error) { |
| 163 | + // For singletenancy, the network name is simply the nwCfg.Name |
| 164 | + if !nwCfg.MultiTenancy { |
| 165 | + return nwCfg.Name, nil |
| 166 | + } |
179 | 167 |
|
180 | | - _, cnsNetworkConfig, _, err = plugin.multitenancyClient.GetContainerNetworkConfiguration(context.TODO(), nwCfg, podName, podNs, ifName) |
181 | | - if err != nil { |
182 | | - log.Printf( |
183 | | - "GetContainerNetworkConfiguration failed for podname %v namespace %v with error %v", |
184 | | - podName, |
185 | | - podNs, |
186 | | - err) |
187 | | - } else { |
188 | | - var subnet net.IPNet |
189 | | - if err = updateSubnetPrefix(cnsNetworkConfig, &subnet); err == nil { |
190 | | - // networkName will look like ~ azure-vlan1-172-28-1-0_24 |
191 | | - networkName = strings.Replace(subnet.String(), ".", "-", -1) |
192 | | - networkName = strings.Replace(networkName, "/", "_", -1) |
193 | | - networkName = fmt.Sprintf("%s-vlan%v-%v", nwCfg.Name, cnsNetworkConfig.MultiTenancyInfo.ID, networkName) |
194 | | - } |
| 168 | + // in multitenancy case, the network name will be in the state file or can be built from cnsResponse |
| 169 | + determineWinVer() |
| 170 | + if len(strings.TrimSpace(netNs)) == 0 || len(strings.TrimSpace(podName)) == 0 || len(strings.TrimSpace(podNs)) == 0 { |
| 171 | + return "", fmt.Errorf("POD info cannot be empty. PodName: %s, PodNamespace: %s, NetNs: %s", podName, podNs, netNs) |
| 172 | + } |
| 173 | + |
| 174 | + // First try to build the network name from the cnsResponse if present |
| 175 | + // This will happen during ADD call |
| 176 | + if cnsResponse != nil { |
| 177 | + var subnet net.IPNet |
| 178 | + if err := updateSubnetPrefix(cnsResponse, &subnet); err != nil { |
| 179 | + log.Printf("Error updating subnet prefix: %v", err) |
| 180 | + return "", err |
195 | 181 | } |
| 182 | + |
| 183 | + // networkName will look like ~ azure-vlan1-172-28-1-0_24 |
| 184 | + networkSuffix := strings.Replace(subnet.String(), ".", "-", -1) |
| 185 | + networkSuffix = strings.Replace(networkSuffix, "/", "_", -1) |
| 186 | + networkName := fmt.Sprintf("%s-vlan%v-%v", nwCfg.Name, cnsResponse.MultiTenancyInfo.ID, networkSuffix) |
| 187 | + |
| 188 | + return networkName, nil |
| 189 | + } |
| 190 | + |
| 191 | + // If no cnsResponse was present, try to get the network name from the state file |
| 192 | + // This will happen during DEL call |
| 193 | + networkName, err := plugin.nm.FindNetworkIDFromNetNs(netNs) |
| 194 | + if err != nil { |
| 195 | + log.Printf("Error getting network name from state: %v.", err) |
| 196 | + return "", fmt.Errorf("error getting network name from state: %w", err) |
| 197 | + |
196 | 198 | } |
197 | 199 |
|
198 | | - return networkName, err |
| 200 | + return networkName, nil |
199 | 201 | } |
200 | 202 |
|
201 | 203 | func setupInfraVnetRoutingForMultitenancy( |
|
0 commit comments