From edfbed34adb978e3798e2b84163d2a5b9b5e959e Mon Sep 17 00:00:00 2001 From: nvazquez Date: Mon, 21 Sep 2020 00:12:28 -0300 Subject: [PATCH] Use network adapter from OVF on deploy-as-is --- .../cloud/agent/api/to/DeployAsIsInfoTO.java | 8 +++++++- .../image/deployasis/DeployAsIsHelper.java | 3 +++ .../deployasis/DeployAsIsHelperImpl.java | 20 +++++++++++++++++++ .../hypervisor/guru/VmwareVmImplementer.java | 3 ++- .../vmware/resource/VmwareResource.java | 12 ++++++++--- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/com/cloud/agent/api/to/DeployAsIsInfoTO.java b/api/src/main/java/com/cloud/agent/api/to/DeployAsIsInfoTO.java index cc0e1d01d03..d82a2975eaf 100644 --- a/api/src/main/java/com/cloud/agent/api/to/DeployAsIsInfoTO.java +++ b/api/src/main/java/com/cloud/agent/api/to/DeployAsIsInfoTO.java @@ -27,14 +27,17 @@ public class DeployAsIsInfoTO { private String destStoragePool; @LogLevel(LogLevel.Log4jLevel.Off) private Map properties = new HashMap<>(); + private Map nicAdapterMap = new HashMap(); public DeployAsIsInfoTO() { } - public DeployAsIsInfoTO(String templatePath, String destStoragePool, Map properties) { + public DeployAsIsInfoTO(String templatePath, String destStoragePool, Map properties, + Map nicAdapterMap) { this.templatePath = templatePath; this.destStoragePool = destStoragePool; this.properties = properties; + this.nicAdapterMap = nicAdapterMap; } public String getTemplatePath() { @@ -49,4 +52,7 @@ public class DeployAsIsInfoTO { return destStoragePool; } + public Map getNicAdapterMap() { + return nicAdapterMap; + } } diff --git a/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelper.java b/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelper.java index 574e8cdf7bd..303161c0c59 100644 --- a/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelper.java +++ b/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelper.java @@ -17,6 +17,7 @@ package org.apache.cloudstack.storage.image.deployasis; import com.cloud.agent.api.storage.DownloadAnswer; +import com.cloud.agent.api.to.NicTO; import com.cloud.vm.VirtualMachineProfile; import java.util.Map; @@ -28,4 +29,6 @@ public interface DeployAsIsHelper { String getAllocatedVirtualMachineTemplatePath(VirtualMachineProfile vm, String configuration, String destStoragePool); String getAllocatedVirtualMachineDestinationStoragePool(VirtualMachineProfile vm); + + Map getAllocatedVirtualMachineNicsAdapterMapping(VirtualMachineProfile vm, NicTO[] nics); } diff --git a/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelperImpl.java b/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelperImpl.java index fa3f0cbebc4..7e431b1a729 100644 --- a/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelperImpl.java +++ b/engine/storage/src/main/java/org/apache/cloudstack/storage/image/deployasis/DeployAsIsHelperImpl.java @@ -22,6 +22,7 @@ import com.cloud.agent.api.storage.DownloadAnswer; import com.cloud.agent.api.to.DataStoreTO; import com.cloud.agent.api.to.DataTO; import com.cloud.agent.api.to.DiskTO; +import com.cloud.agent.api.to.NicTO; import com.cloud.agent.api.to.deployasis.OVFConfigurationTO; import com.cloud.agent.api.to.deployasis.OVFEulaSectionTO; import com.cloud.agent.api.to.deployasis.OVFPropertyTO; @@ -45,6 +46,7 @@ import com.cloud.agent.api.to.deployasis.OVFNetworkTO; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.ArrayUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @@ -146,6 +148,24 @@ public class DeployAsIsHelperImpl implements DeployAsIsHelper { return null; } + @Override + public Map getAllocatedVirtualMachineNicsAdapterMapping(VirtualMachineProfile vm, NicTO[] nics) { + Map map = new HashMap<>(); + List networks = templateDeployAsIsDetailsDao.listNetworkRequirementsByTemplateId(vm.getTemplateId()); + if (ArrayUtils.isNotEmpty(nics)) { + if (nics.length != networks.size()) { + String msg = "Different number of networks provided vs networks defined in deploy-as-is template"; + LOGGER.error(msg); + return map; + } + for (int i = 0; i < nics.length; i++) { + // The nic Adapter is defined on the resource sub type + map.put(nics[i].getDeviceId(), networks.get(i).getResourceSubType()); + } + } + return map; + } + private void persistTemplateDeployAsIsInformationTOList(long templateId, List informationTOList) { for (TemplateDeployAsIsInformationTO informationTO : informationTOList) { diff --git a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/guru/VmwareVmImplementer.java b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/guru/VmwareVmImplementer.java index 56a875d8ab2..5c61887176c 100644 --- a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/guru/VmwareVmImplementer.java +++ b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/guru/VmwareVmImplementer.java @@ -190,7 +190,8 @@ class VmwareVmImplementer { Map properties = deployAsIsHelper.getVirtualMachineDeployAsIsProperties(vm); String destStoragePool = deployAsIsHelper.getAllocatedVirtualMachineDestinationStoragePool(vm); String templatePath = deployAsIsHelper.getAllocatedVirtualMachineTemplatePath(vm, configuration, destStoragePool); - DeployAsIsInfoTO info = new DeployAsIsInfoTO(templatePath, destStoragePool, properties); + Map nicsAdapterMapping = deployAsIsHelper.getAllocatedVirtualMachineNicsAdapterMapping(vm, to.getNics()); + DeployAsIsInfoTO info = new DeployAsIsInfoTO(templatePath, destStoragePool, properties, nicsAdapterMapping); to.setDeployAsIsInfo(info); } diff --git a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java index e2db99fea4b..b07e7714646 100644 --- a/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -2223,9 +2223,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } } - VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.valueOf(vmSpec.getDetails().get(VmDetailConstants.NIC_ADAPTER)); - if (s_logger.isDebugEnabled()) - s_logger.debug("VM " + vmInternalCSName + " will be started with NIC device type: " + nicDeviceType); + VirtualEthernetCardType nicDeviceType; NiciraNvpApiVersion.logNiciraApiVersion(); @@ -2233,6 +2231,14 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa for (NicTO nicTo : sortNicsByDeviceId(nics)) { s_logger.info("Prepare NIC device based on NicTO: " + _gson.toJson(nicTo)); + String adapterTypeStr = deployAsIs ? + deployAsIsInfo.getNicAdapterMap().get(nicTo.getDeviceId()) : + vmSpec.getDetails().get(VmDetailConstants.NIC_ADAPTER); + nicDeviceType = VirtualEthernetCardType.valueOf(adapterTypeStr); + + if (s_logger.isDebugEnabled()) { + s_logger.debug("VM " + vmInternalCSName + " will be started with NIC device type: " + nicDeviceType + " on NIC device " + nicTo.getDeviceId()); + } boolean configureVServiceInNexus = (nicTo.getType() == TrafficType.Guest) && (vmSpec.getDetails().containsKey("ConfigureVServiceInNexus")); VirtualMachine.Type vmType = cmd.getVirtualMachine().getType(); Pair networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, configureVServiceInNexus, vmType);