Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion server/src/main/java/com/cloud/network/IpAddressManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,16 @@ public PublicIp fetchNewPublicIp(final long dcId, final Long podId, final List<L
return PublicIp.createFromAddrAndVlan(addr, _vlanDao.findById(addr.getVlanId()));
}

protected String getInsufficientAddressCapacityMessage(VlanType vlanUse, Network network, String requestedIp) {
if (vlanUse != VlanType.DirectAttached || network == null) {
return "Insufficient address capacity";
}
if (requestedIp != null) {
return String.format("Insufficient address capacity: the requested IP address %s is not available in the IP ranges of network %s", requestedIp, network.getName());
}
return String.format("Insufficient address capacity: no free IP address left in the IP ranges of network %s", network.getName());
}

@Override
public List<IPAddressVO> listAvailablePublicIps(final long dcId, final Long podId, final List<Long> 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,
Expand Down Expand Up @@ -1114,7 +1124,7 @@ public List<IPAddressVO> 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;
}
Expand Down
8 changes: 4 additions & 4 deletions server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4563,7 +4563,7 @@ private UserVm getCheckedUserVmResource(DataCenter zone, String hostName, String
Map<String, Map<Integer, String>> dhcpOptionMap, Map<Long, DiskOffering> datadiskTemplateToDiskOfferringMap,
Map<String, String> 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<String> resourceLimitHostTags = resourceLimitService.getResourceLimitHostTags(offering, template);
try (CheckedReservation vmReservation = new CheckedReservation(owner, ResourceType.user_vm, resourceLimitHostTags, 1l, reservationDao, resourceLimitService);
Expand All @@ -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);
Expand Down Expand Up @@ -4635,7 +4635,7 @@ private UserVm getUncheckedUserVmResource(DataCenter zone, String hostName, Stri
Map<String, Map<Integer, String>> dhcpOptionMap, Map<Long, DiskOffering> datadiskTemplateToDiskOfferringMap,
Map<String, String> 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<Reserver> checkedReservations = new ArrayList<>();


Expand Down Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions server/src/test/java/com/cloud/network/IpAddressManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading