mirror of https://github.com/apache/cloudstack.git
CLOUDSTACK-4083 if a failure occurs while adding VM to another network (this should be the first vm in the subnet). The ip alias created as a part of this process is not removed.
Signed-off-by: Sheng Yang <sheng.yang@citrix.com>
This commit is contained in:
parent
1a194ef725
commit
5f2f5181e9
|
|
@ -386,4 +386,5 @@ public interface NetworkManager {
|
|||
|
||||
PublicIp assignPublicIpAddressFromVlans(long dcId, Long podId, Account owner, VlanType type, List<Long> vlanDbIds, Long networkId, String requestedIp, boolean isSystem) throws InsufficientAddressCapacityException;
|
||||
|
||||
void removeDhcpServiceInSubnet(NicVO nic);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,9 @@ import javax.ejb.Local;
|
|||
import javax.inject.Inject;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import com.cloud.vm.NicIpAlias;
|
||||
import com.cloud.vm.dao.NicIpAliasDao;
|
||||
import com.cloud.vm.dao.NicIpAliasVO;
|
||||
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
|
||||
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
|
||||
import org.apache.cloudstack.context.CallContext;
|
||||
|
|
@ -267,6 +270,10 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
|
|||
DataCenterVnetDao _datacenterVnetDao;
|
||||
@Inject
|
||||
NetworkAccountDao _networkAccountDao;
|
||||
@Inject
|
||||
protected NicIpAliasDao _nicIpAliasDao;
|
||||
@Inject
|
||||
protected IPAddressDao _publicIpAddressDao;
|
||||
|
||||
List<NetworkGuru> _networkGurus;
|
||||
public List<NetworkGuru> getNetworkGurus() {
|
||||
|
|
@ -2352,6 +2359,11 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
|
|||
}
|
||||
}
|
||||
|
||||
// remove the dhcpservice ip if this is the last nic in subnet.
|
||||
if (vm.getType() == Type.User && isDhcpAccrossMultipleSubnetsSupported(network) && isLastNicInSubnet(nic) &&
|
||||
network.getTrafficType() == TrafficType.Guest && network.getGuestType() == GuestType.Shared) {
|
||||
removeDhcpServiceInSubnet(nic);
|
||||
}
|
||||
NetworkGuru guru = AdapterBase.getAdapterByName(_networkGurus, network.getGuruName());
|
||||
guru.deallocate(network, profile, vm);
|
||||
_nicDao.remove(nic.getId());
|
||||
|
|
@ -2362,6 +2374,52 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isDhcpAccrossMultipleSubnetsSupported(Network network) {
|
||||
DhcpServiceProvider dhcpServiceProvider = getDhcpServiceProvider(network);
|
||||
Map <Network.Capability, String> capabilities = dhcpServiceProvider.getCapabilities().get(Network.Service.Dhcp);
|
||||
String supportsMultipleSubnets = capabilities.get(Network.Capability.DhcpAccrossMultipleSubnets);
|
||||
if (supportsMultipleSubnets != null && Boolean.valueOf(supportsMultipleSubnets)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isLastNicInSubnet(NicVO nic) {
|
||||
if (_nicDao.listByNetworkIdTypeAndGatewayAndBroadcastUri(nic.getNetworkId(), VirtualMachine.Type.User, nic.getGateway(), nic.getBroadcastUri()).size() > 1) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@DB
|
||||
@Override
|
||||
public void removeDhcpServiceInSubnet(NicVO nic) {
|
||||
Network network = _networksDao.findById(nic.getNetworkId());
|
||||
DhcpServiceProvider dhcpServiceProvider = getDhcpServiceProvider(network);
|
||||
try {
|
||||
NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(nic.getGateway(), network.getId(), NicIpAlias.state.active);
|
||||
if (ipAlias != null) {
|
||||
ipAlias.setState(NicIpAlias.state.revoked);
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
_nicIpAliasDao.update(ipAlias.getId(),ipAlias);
|
||||
IPAddressVO aliasIpaddressVo = _publicIpAddressDao.findByIpAndSourceNetworkId(ipAlias.getNetworkId(), ipAlias.getIp4Address());
|
||||
_publicIpAddressDao.unassignIpAddress(aliasIpaddressVo.getId());
|
||||
txn.commit();
|
||||
if (!dhcpServiceProvider.removeDhcpSupportForSubnet(network)) {
|
||||
s_logger.warn("Failed to remove the ip alias on the router, marking it as removed in db and freed the allocated ip " + ipAlias.getIp4Address());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ResourceUnavailableException e) {
|
||||
//failed to remove the dhcpconfig on the router.
|
||||
s_logger.info ("Unable to delete the ip alias due to unable to contact the virtualrouter.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void expungeNics(VirtualMachineProfile vm) {
|
||||
List<NicVO> nics = _nicDao.listByVmIdIncludingRemoved(vm.getId());
|
||||
|
|
|
|||
|
|
@ -441,10 +441,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
|
|||
s_logger.debug("Destroying vm " + vm);
|
||||
}
|
||||
|
||||
if (vm.getType() == VirtualMachine.Type.User) {
|
||||
removeDhcpServiceInsubnet(vm);
|
||||
}
|
||||
|
||||
VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
|
||||
|
||||
HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
|
||||
|
|
@ -493,40 +489,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
|
|||
|
||||
}
|
||||
|
||||
@DB
|
||||
private void removeDhcpServiceInsubnet(VirtualMachine vm) {
|
||||
//list all the nics which belong to this vm and are the last nics in the subnets.
|
||||
//we are using the info in these nics to remove the dhcp sercvice for these subnets.
|
||||
List<NicVO> nicList = listLastNicsInSubnet(vm);
|
||||
|
||||
if (nicList != null && nicList.size() != 0) {
|
||||
for (NicVO nic : nicList) {
|
||||
//free the ipalias on the routers corresponding to each of the nics.
|
||||
Network network = _networkDao.findById(nic.getNetworkId());
|
||||
DhcpServiceProvider dhcpServiceProvider = _networkMgr.getDhcpServiceProvider(network);
|
||||
try {
|
||||
NicIpAliasVO ipAlias = _nicIpAliasDao.findByGatewayAndNetworkIdAndState(nic.getGateway(), network.getId(), NicIpAlias.state.active);
|
||||
if (ipAlias != null) {
|
||||
ipAlias.setState(NicIpAlias.state.revoked);
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
txn.start();
|
||||
_nicIpAliasDao.update(ipAlias.getId(), ipAlias);
|
||||
IPAddressVO aliasIpaddressVo = _publicIpAddressDao.findByIpAndSourceNetworkId(ipAlias.getNetworkId(), ipAlias.getIp4Address());
|
||||
_publicIpAddressDao.unassignIpAddress(aliasIpaddressVo.getId());
|
||||
txn.commit();
|
||||
if (!dhcpServiceProvider.removeDhcpSupportForSubnet(network)) {
|
||||
s_logger.warn("Failed to remove the ip alias on the router, marking it as removed in db and freed the allocated ip " + ipAlias.getIp4Address());
|
||||
}
|
||||
}
|
||||
} catch (ResourceUnavailableException e) {
|
||||
//failed to remove the dhcpconfig on the router.
|
||||
s_logger.info("Unable to delete the ip alias due to unable to contact the virtualrouter.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
_executor.scheduleAtFixedRate(new CleanupTask(), _cleanupInterval, _cleanupInterval, TimeUnit.SECONDS);
|
||||
|
|
@ -1377,28 +1339,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
|
|||
}
|
||||
}
|
||||
|
||||
//list all the nics which belong to this vm and are the last nics in the subnets.
|
||||
//we are using the info in these nics to remove the dhcp sercvice for these subnets.
|
||||
private List<NicVO> listLastNicsInSubnet(VirtualMachine vm) {
|
||||
List<NicVO> nicList = _nicsDao.listByVmId(vm.getId());
|
||||
List<NicVO> copyOfnicList = new ArrayList<NicVO>(nicList);
|
||||
for (NicVO nic : nicList) {
|
||||
Network network = _networkDao.findById(nic.getNetworkId());
|
||||
DhcpServiceProvider dhcpServiceProvider = _networkMgr.getDhcpServiceProvider(network);
|
||||
Map<Network.Capability, String> capabilities = dhcpServiceProvider.getCapabilities().get(Network.Service.Dhcp);
|
||||
String supportsMultipleSubnets = capabilities.get(Network.Capability.DhcpAccrossMultipleSubnets);
|
||||
if ((supportsMultipleSubnets != null && Boolean.valueOf(supportsMultipleSubnets) && network.getTrafficType() == Networks.TrafficType.Guest && network.getGuestType() == Network.GuestType.Shared)) {
|
||||
//including the ip of the vm and the ipAlias
|
||||
if (_nicsDao.listByNetworkIdTypeAndGatewayAndBroadcastUri(nic.getNetworkId(), VirtualMachine.Type.User, nic.getGateway(), nic.getBroadcastUri()).size() > 1) {
|
||||
copyOfnicList.remove(nic);
|
||||
}
|
||||
} else {
|
||||
copyOfnicList.remove(nic);
|
||||
}
|
||||
}
|
||||
return copyOfnicList;
|
||||
}
|
||||
|
||||
protected boolean checkVmOnHost(VirtualMachine vm, long hostId) throws AgentUnavailableException, OperationTimedoutException {
|
||||
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer)_agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName()));
|
||||
if (!answer.getResult() || answer.getState() == State.Stopped) {
|
||||
|
|
@ -2916,11 +2856,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
|
|||
_networkModel.isSecurityGroupSupportedInNetwork(network),
|
||||
_networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network));
|
||||
|
||||
// Adding this to the dhcpservice config if this is the last nic in subnet.
|
||||
if (vm.getType() == VirtualMachine.Type.User) {
|
||||
removeDhcpServiceInsubnet(vm);
|
||||
}
|
||||
|
||||
//1) Unplug the nic
|
||||
if (vm.getState() == State.Running) {
|
||||
NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType());
|
||||
|
|
|
|||
|
|
@ -1413,11 +1413,13 @@ public class MockNetworkManagerImpl extends ManagerBase implements NetworkManage
|
|||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeDhcpServiceInSubnet(NicVO nic) {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public void prepareNicForMigration(
|
||||
VirtualMachineProfile vm,
|
||||
DeployDestination dest) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue