@@ -2194,39 +2194,47 @@ protected Map<String, List<String>> buildSourceNicCidrMap(UnmanagedInstanceTO so
21942194 public Map <String , Network .IpAddresses > autoFillStaticNicIpAddresses (Map <String , Long > nicNetworkMap ,
21952195 Map <String , Network .IpAddresses > nicIpAddressMap , Map <String , List <String >> nicIdToIpv4Cidrs ) {
21962196 Map <String , Network .IpAddresses > effective = nicIpAddressMap == null ? new HashMap <>() : new HashMap <>(nicIpAddressMap );
2197- if (MapUtils .isEmpty (nicNetworkMap ) || MapUtils . isEmpty ( nicIdToIpv4Cidrs ) ) {
2197+ if (MapUtils .isEmpty (nicNetworkMap )) {
21982198 return effective ;
21992199 }
22002200 for (Map .Entry <String , Long > mapping : nicNetworkMap .entrySet ()) {
22012201 String nicId = mapping .getKey ();
22022202 if (effective .containsKey (nicId )) {
22032203 continue ;
22042204 }
2205- List <String > sourceCidrs = nicIdToIpv4Cidrs .get (nicId );
2206- if (CollectionUtils .isEmpty (sourceCidrs )) {
2207- continue ;
2208- }
22092205 NetworkVO network = networkDao .findById (mapping .getValue ());
2210- if (network == null || StringUtils .isBlank (network .getCidr ())) {
2206+ if (network == null || network . getGuestType () == Network . GuestType . L2 || StringUtils .isBlank (network .getCidr ())) {
22112207 continue ;
22122208 }
2213- for (String sourceCidr : sourceCidrs ) {
2214- String sourceIp = sourceCidr .contains ("/" ) ? sourceCidr .substring (0 , sourceCidr .indexOf ('/' )) : sourceCidr ;
2215- if (!NetUtils .isValidIp4 (sourceIp ) || !NetUtils .isIpWithInCidrRange (sourceIp , network .getCidr ())) {
2216- continue ;
2217- }
2218- if (sourceIp .equals (network .getGateway ())) {
2219- logger .warn ("Not preserving source IP {} for NIC {}: it is the gateway of network {}" , sourceIp , nicId , network .getUuid ());
2220- continue ;
2221- }
2222- if (nicDao .findByIp4AddressAndNetworkId (sourceIp , network .getId ()) != null ) {
2223- logger .warn ("Not preserving source IP {} for NIC {}: it is already in use in network {}; the NIC falls back to automatic allocation" ,
2224- sourceIp , nicId , network .getUuid ());
2225- continue ;
2209+ String preservedIp = null ;
2210+ List <String > sourceCidrs = nicIdToIpv4Cidrs == null ? null : nicIdToIpv4Cidrs .get (nicId );
2211+ if (CollectionUtils .isNotEmpty (sourceCidrs )) {
2212+ for (String sourceCidr : sourceCidrs ) {
2213+ String sourceIp = sourceCidr .contains ("/" ) ? sourceCidr .substring (0 , sourceCidr .indexOf ('/' )) : sourceCidr ;
2214+ if (!NetUtils .isValidIp4 (sourceIp ) || !NetUtils .isIpWithInCidrRange (sourceIp , network .getCidr ())) {
2215+ continue ;
2216+ }
2217+ if (sourceIp .equals (network .getGateway ())) {
2218+ logger .warn ("Not preserving source IP {} for NIC {}: it is the gateway of network {}" , sourceIp , nicId , network .getUuid ());
2219+ continue ;
2220+ }
2221+ if (nicDao .findByIp4AddressAndNetworkId (sourceIp , network .getId ()) != null ) {
2222+ logger .warn ("Not preserving source IP {} for NIC {}: it is already in use in network {}; the NIC falls back to automatic allocation" ,
2223+ sourceIp , nicId , network .getUuid ());
2224+ continue ;
2225+ }
2226+ preservedIp = sourceIp ;
2227+ break ;
22262228 }
2227- logger .info ("Preserving static source IP {} for NIC {} in network {}" , sourceIp , nicId , network .getUuid ());
2228- effective .put (nicId , new Network .IpAddresses (sourceIp , null ));
2229- break ;
2229+ }
2230+ if (preservedIp != null ) {
2231+ logger .info ("Preserving static source IP {} for NIC {} in network {}" , preservedIp , nicId , network .getUuid ());
2232+ effective .put (nicId , new Network .IpAddresses (preservedIp , null ));
2233+ } else {
2234+ // Isolated/shared networks reject import NICs without an IP; "auto" makes the
2235+ // network orchestrator pick a free address instead (see allocateNic import path).
2236+ logger .info ("No preservable source IP for NIC {}; falling back to automatic allocation in network {}" , nicId , network .getUuid ());
2237+ effective .put (nicId , new Network .IpAddresses ("auto" , null ));
22302238 }
22312239 }
22322240 return effective ;
0 commit comments