From 4ca17f6e03c675c7c48573f5b72c7fc191a0658b Mon Sep 17 00:00:00 2001 From: rootnomad Date: Wed, 2 Dec 2026 11:20:16 -0500 Subject: [PATCH 1/3] network: name the network when its IP ranges have no free address left A network with its own IP ranges (shared, or isolated with SpecifyIpRanges) allocates guest IPs only from those ranges. When they are all in use the allocation failed with a bare "Insufficient address capacity" scoped to the zone, which reads as the zone running out of public IPs. Say which network ran out, or that the requested IP is not available in it. The scope stays on the zone so the deployment retry logic is unchanged. --- .../cloud/network/IpAddressManagerImpl.java | 12 ++++++- .../cloud/network/IpAddressManagerTest.java | 35 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/cloud/network/IpAddressManagerImpl.java b/server/src/main/java/com/cloud/network/IpAddressManagerImpl.java index b7a6846422fb..88e178bc25a6 100644 --- a/server/src/main/java/com/cloud/network/IpAddressManagerImpl.java +++ b/server/src/main/java/com/cloud/network/IpAddressManagerImpl.java @@ -963,6 +963,16 @@ public PublicIp fetchNewPublicIp(final long dcId, final Long podId, final List listAvailablePublicIps(final long dcId, final Long podId, final List vlanDbIds, final Account owner, final VlanType vlanUse, final Long guestNetworkId, final boolean sourceNat, final boolean assign, final boolean allocate, final String requestedIp, final String requestedGateway, final boolean isSystem, @@ -1114,7 +1124,7 @@ public List listAvailablePublicIps(final long dcId, final Long podI throw ex; } logger.warn(errorMessage.toString()); - InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException("Insufficient address capacity", DataCenter.class, dcId); + InsufficientAddressCapacityException ex = new InsufficientAddressCapacityException(getInsufficientAddressCapacityMessage(vlanUse, network, requestedIp), DataCenter.class, dcId); ex.addProxyObject(ApiDBUtils.findZoneById(dcId).getUuid()); throw ex; } diff --git a/server/src/test/java/com/cloud/network/IpAddressManagerTest.java b/server/src/test/java/com/cloud/network/IpAddressManagerTest.java index d12419b66e0a..059c7415dd07 100644 --- a/server/src/test/java/com/cloud/network/IpAddressManagerTest.java +++ b/server/src/test/java/com/cloud/network/IpAddressManagerTest.java @@ -63,6 +63,7 @@ import com.cloud.network.vpc.dao.VpcDao; import com.cloud.network.vpc.VpcVO; import com.cloud.network.vpc.VpcManager; +import com.cloud.dc.Vlan.VlanType; import com.cloud.dc.dao.VlanDao; import com.cloud.offerings.NetworkOfferingVO; import com.cloud.offerings.dao.NetworkOfferingDao; @@ -640,4 +641,38 @@ public void applyRulesVpcRuleErrorHandlingWithContinueOnErrorTrue() throws Resou assertFalse(result); } + + private Network networkNamed(String name) { + Network network = mock(Network.class); + when(network.getName()).thenReturn(name); + return network; + } + + @Test + public void insufficientAddressCapacityMessageNamesTheNetworkWhoseIpRangesAreFull() { + String message = ipAddressManager.getInsufficientAddressCapacityMessage(VlanType.DirectAttached, networkNamed("shared-net"), null); + + Assert.assertEquals("Insufficient address capacity: no free IP address left in the IP ranges of network shared-net", message); + } + + @Test + public void insufficientAddressCapacityMessageNamesTheRequestedIpAndNetwork() { + String message = ipAddressManager.getInsufficientAddressCapacityMessage(VlanType.DirectAttached, networkNamed("shared-net"), "10.1.1.20"); + + Assert.assertEquals("Insufficient address capacity: the requested IP address 10.1.1.20 is not available in the IP ranges of network shared-net", message); + } + + @Test + public void insufficientAddressCapacityMessageIsUnchangedForPublicIps() { + String message = ipAddressManager.getInsufficientAddressCapacityMessage(VlanType.VirtualNetwork, networkNamed("isolated-net"), null); + + Assert.assertEquals("Insufficient address capacity", message); + } + + @Test + public void insufficientAddressCapacityMessageIsUnchangedWithoutANetwork() { + String message = ipAddressManager.getInsufficientAddressCapacityMessage(VlanType.DirectAttached, null, null); + + Assert.assertEquals("Insufficient address capacity", message); + } } From 203f28a81e0c91afcf0656dbd22dff898f6ee762 Mon Sep 17 00:00:00 2001 From: rootnomad Date: Wed, 2 Dec 2026 11:20:17 -0500 Subject: [PATCH 2/3] vm: return a capacity error when deploy cannot allocate an address The resource reservation wrapper in deployVirtualMachine caught every exception other than ResourceAllocationException and CloudRuntimeException, logged it at ERROR with a stack trace and rethrew it as a CloudRuntimeException. An InsufficientCapacityException from NIC allocation therefore reached the API as an internal error with the raw exception text. Let it through so DeployVMCmd returns INSUFFICIENT_CAPACITY_ERROR with the exception message. --- server/src/main/java/com/cloud/vm/UserVmManagerImpl.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index b2e20256cc53..9edc935651a0 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -4563,7 +4563,7 @@ private UserVm getCheckedUserVmResource(DataCenter zone, String hostName, String Map> dhcpOptionMap, Map datadiskTemplateToDiskOfferringMap, Map userVmOVFPropertiesMap, boolean dynamicScalingEnabled, String vmType, VMTemplateVO template, HypervisorType hypervisorType, long accountId, ServiceOfferingVO offering, boolean isIso, - Long rootDiskOfferingId, Long rootDiskKmsKeyId, long volumesSize, Volume volume, Snapshot snapshot) throws ResourceAllocationException { + Long rootDiskOfferingId, Long rootDiskKmsKeyId, long volumesSize, Volume volume, Snapshot snapshot) throws ResourceAllocationException, InsufficientCapacityException { if (!VirtualMachineManager.ResourceCountRunningVMsonly.value() && !(VALIDATION_VM.equals(vmType) && !EnforceResourceLimitOnValidationVm.valueIn(accountId))) { List resourceLimitHostTags = resourceLimitService.getResourceLimitHostTags(offering, template); try (CheckedReservation vmReservation = new CheckedReservation(owner, ResourceType.user_vm, resourceLimitHostTags, 1l, reservationDao, resourceLimitService); @@ -4573,7 +4573,7 @@ private UserVm getCheckedUserVmResource(DataCenter zone, String hostName, String new CheckedReservation(owner, ResourceType.gpu, resourceLimitHostTags, Long.valueOf(offering.getGpuCount()), reservationDao, resourceLimitService) : null; ) { return getUncheckedUserVmResource(zone, hostName, displayName, owner, diskOfferingId, diskSize, dataDiskInfoList, networkList, securityGroupIdList, group, httpmethod, userData, userDataId, userDataDetails, sshKeyPairs, caller, requestedIps, defaultIps, isDisplayVm, keyboard, affinityGroupIdList, customParameters, customId, dhcpOptionMap, datadiskTemplateToDiskOfferringMap, userVmOVFPropertiesMap, dynamicScalingEnabled, vmType, template, hypervisorType, accountId, offering, isIso, rootDiskOfferingId, rootDiskKmsKeyId, volumesSize, volume, snapshot); - } catch (ResourceAllocationException | CloudRuntimeException e) { + } catch (ResourceAllocationException | InsufficientCapacityException | CloudRuntimeException e) { throw e; } catch (Exception e) { logger.error("error during resource reservation and allocation", e); @@ -4635,7 +4635,7 @@ private UserVm getUncheckedUserVmResource(DataCenter zone, String hostName, Stri Map> dhcpOptionMap, Map datadiskTemplateToDiskOfferringMap, Map userVmOVFPropertiesMap, boolean dynamicScalingEnabled, String vmType, VMTemplateVO template, HypervisorType hypervisorType, long accountId, ServiceOfferingVO offering, boolean isIso, - Long rootDiskOfferingId, Long rootDiskKmsKeyId, long volumesSize, Volume volume, Snapshot snapshot) throws ResourceAllocationException { + Long rootDiskOfferingId, Long rootDiskKmsKeyId, long volumesSize, Volume volume, Snapshot snapshot) throws ResourceAllocationException, InsufficientCapacityException { List checkedReservations = new ArrayList<>(); @@ -4918,7 +4918,7 @@ private UserVm getUncheckedUserVmResource(DataCenter zone, String hostName, Stri assignInstanceToGroup(group, id); return vm; - } catch (ResourceAllocationException | CloudRuntimeException e) { + } catch (ResourceAllocationException | InsufficientCapacityException | CloudRuntimeException e) { throw e; } catch (Exception e) { logger.error("error during resource reservation and allocation", e); From 2a39afb8f684edd9e1929848f778811a9425956d Mon Sep 17 00:00:00 2001 From: rootnomad Date: Wed, 2 Dec 2026 11:20:17 -0500 Subject: [PATCH 3/3] test: networks with SpecifyIpRanges allocate guest IPs from their IP ranges Cover GuestNetworkGuru.allocate() for isolated and shared networks with SpecifyIpRanges: both take the address from the network's IP ranges through allocateDirectIp, not from the whole CIDR. --- .../guru/GuestNetworkGuruAllocateTest.java | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 server/src/test/java/com/cloud/network/guru/GuestNetworkGuruAllocateTest.java diff --git a/server/src/test/java/com/cloud/network/guru/GuestNetworkGuruAllocateTest.java b/server/src/test/java/com/cloud/network/guru/GuestNetworkGuruAllocateTest.java new file mode 100644 index 000000000000..34e7f0e8368a --- /dev/null +++ b/server/src/test/java/com/cloud/network/guru/GuestNetworkGuruAllocateTest.java @@ -0,0 +1,134 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.network.guru; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.cloud.dc.DataCenter; +import com.cloud.dc.DataCenterVO; +import com.cloud.dc.dao.DataCenterDao; +import com.cloud.exception.InsufficientAddressCapacityException; +import com.cloud.exception.InsufficientVirtualNetworkCapacityException; +import com.cloud.network.IpAddressManager; +import com.cloud.network.Network; +import com.cloud.network.Network.GuestType; +import com.cloud.network.NetworkModel; +import com.cloud.network.Networks.TrafficType; +import com.cloud.network.PhysicalNetwork; +import com.cloud.offering.NetworkOffering; +import com.cloud.offerings.dao.NetworkOfferingDao; +import com.cloud.utils.Pair; +import com.cloud.vm.VirtualMachine; +import com.cloud.vm.VirtualMachineProfile; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.Silent.class) +public class GuestNetworkGuruAllocateTest { + + public static class TestGuestNetworkGuru extends GuestNetworkGuru { + @Override + protected boolean canHandle(NetworkOffering offering, DataCenter.NetworkType networkType, PhysicalNetwork physicalNetwork) { + return false; + } + } + + @Mock + private DataCenterDao dcDao; + @Mock + private IpAddressManager ipAddrMgr; + @Mock + private NetworkModel networkModel; + @Mock + private NetworkOfferingDao networkOfferingDao; + + @InjectMocks + private TestGuestNetworkGuru guru = new TestGuestNetworkGuru(); + + private static final long ZONE_ID = 1L; + private static final long NETWORK_ID = 100L; + private static final long OFFERING_ID = 11L; + + private VirtualMachineProfile userVmProfile() { + VirtualMachine vmInstance = Mockito.mock(VirtualMachine.class); + when(vmInstance.getType()).thenReturn(VirtualMachine.Type.User); + VirtualMachineProfile vm = Mockito.mock(VirtualMachineProfile.class); + when(vm.getVirtualMachine()).thenReturn(vmInstance); + when(vm.getType()).thenReturn(VirtualMachine.Type.User); + return vm; + } + + private Network guestNetwork(GuestType guestType) { + Network network = Mockito.mock(Network.class); + when(network.getTrafficType()).thenReturn(TrafficType.Guest); + when(network.getGuestType()).thenReturn(guestType); + when(network.getSpecifyIpRanges()).thenReturn(true); + when(network.getDataCenterId()).thenReturn(ZONE_ID); + when(network.getId()).thenReturn(NETWORK_ID); + when(network.getNetworkOfferingId()).thenReturn(OFFERING_ID); + when(network.getGateway()).thenReturn("10.0.0.1"); + return network; + } + + @Test + public void testAllocateIsolatedWithSpecifyIpRangesAllocatesFromTheNetworkIpRanges() + throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException { + Network network = guestNetwork(GuestType.Isolated); + when(network.getCidr()).thenReturn("10.0.0.0/24"); + VirtualMachineProfile vm = userVmProfile(); + + DataCenterVO dc = Mockito.mock(DataCenterVO.class); + when(dcDao.findById(ZONE_ID)).thenReturn(dc); + when(ipAddrMgr.acquireGuestIpAddress(eq(network), isNull())).thenReturn("10.0.0.200"); + when(networkModel.getValidNetworkCidr(network)).thenReturn("10.0.0.0/24"); + when(networkModel.getNetworkIp4Dns(eq(network), eq(dc))).thenReturn(new Pair<>("8.8.8.8", null)); + when(networkModel.getNextAvailableMacAddressInNetwork(NETWORK_ID)).thenReturn("02:00:00:aa:bb:cc"); + when(networkOfferingDao.isIpv6Supported(OFFERING_ID)).thenReturn(false); + + guru.allocate(network, null, vm); + + verify(ipAddrMgr, times(1)).allocateDirectIp(any(), eq(dc), eq(vm), eq(network), isNull(), isNull()); + verify(ipAddrMgr, never()).acquireGuestIpAddress(any(), any()); + } + + @Test + public void testAllocateSharedWithSpecifyIpRangesUsesDirectIp() + throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException { + Network network = guestNetwork(GuestType.Shared); + VirtualMachineProfile vm = userVmProfile(); + + DataCenterVO dc = Mockito.mock(DataCenterVO.class); + when(dcDao.findById(ZONE_ID)).thenReturn(dc); + when(networkModel.getNextAvailableMacAddressInNetwork(NETWORK_ID)).thenReturn("02:00:00:aa:bb:cc"); + when(networkOfferingDao.isIpv6Supported(OFFERING_ID)).thenReturn(false); + + guru.allocate(network, null, vm); + + verify(ipAddrMgr, times(1)).allocateDirectIp(any(), eq(dc), eq(vm), eq(network), isNull(), isNull()); + verify(ipAddrMgr, never()).acquireGuestIpAddress(any(), any()); + } +}