vmware: search unmanaged instances using hypervisor name (#4328)

VMware code keeps a cache of existing VMs on a hypervisor host using cloud.vm.internal.name property of the VM. Searching for unmanaged instances/VMs on a host might not return an expected result when this property differs from the actual name of the VM.

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
Abhishek Kumar 2020-09-24 11:01:33 +05:30 committed by GitHub
parent 534dd475d8
commit ca1e02fab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 9 deletions

View File

@ -316,8 +316,8 @@ import com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo;
import com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo;
import com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo;
import com.vmware.vim25.VirtualIDEController;
import com.vmware.vim25.VirtualMachineConfigSpec;
import com.vmware.vim25.VirtualMachineBootOptions;
import com.vmware.vim25.VirtualMachineConfigSpec;
import com.vmware.vim25.VirtualMachineFileInfo;
import com.vmware.vim25.VirtualMachineFileLayoutEx;
import com.vmware.vim25.VirtualMachineFileLayoutExFileInfo;
@ -7069,7 +7069,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
VmwareHypervisorHost hyperHost = getHyperHost(context);
String vmName = cmd.getInstanceName();
List<VirtualMachineMO> vmMos = hyperHost.listVmsOnHyperHost(vmName);
List<VirtualMachineMO> vmMos = hyperHost.listVmsOnHyperHostWithHypervisorName(vmName);
for (VirtualMachineMO vmMo : vmMos) {
if (vmMo == null) {

View File

@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
@ -217,13 +218,13 @@ public class ClusterMO extends BaseMO implements VmwareHypervisorHost {
}
@Override
public synchronized List<VirtualMachineMO> listVmsOnHyperHost(String vmName) throws Exception {
public synchronized List<VirtualMachineMO> listVmsOnHyperHostWithHypervisorName(String vmName) throws Exception {
List<VirtualMachineMO> vms = new ArrayList<>();
List<ManagedObjectReference> hosts = _context.getVimClient().getDynamicProperty(_mor, "host");
if (hosts != null && hosts.size() > 0) {
if (CollectionUtils.isNotEmpty(hosts)) {
for (ManagedObjectReference morHost : hosts) {
HostMO hostMo = new HostMO(_context, morHost);
vms.addAll(hostMo.listVmsOnHyperHost(vmName));
vms.addAll(hostMo.listVmsOnHyperHostWithHypervisorName(vmName));
}
}
return vms;

View File

@ -18,11 +18,14 @@ package com.cloud.hypervisor.vmware.mo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
@ -497,10 +500,10 @@ public class HostMO extends BaseMO implements VmwareHypervisorHost {
}
@Override
public synchronized List<VirtualMachineMO> listVmsOnHyperHost(String vmName) throws Exception {
public synchronized List<VirtualMachineMO> listVmsOnHyperHostWithHypervisorName(String vmName) throws Exception {
List<VirtualMachineMO> vms = new ArrayList<>();
if (vmName != null && !vmName.isEmpty()) {
vms.add(findVmOnHyperHost(vmName));
if (StringUtils.isNotEmpty(vmName)) {
vms.add(findVmOnHyperHostWithHypervisorName(vmName));
} else {
loadVmCache();
vms.addAll(_vmCache.values());
@ -1209,4 +1212,36 @@ public class HostMO extends BaseMO implements VmwareHypervisorHost {
return false;
}
private synchronized VirtualMachineMO findVmOnHyperHostWithHypervisorName(String vmName) throws Exception {
if (s_logger.isDebugEnabled())
s_logger.debug("find VM hypervisor name: " + vmName + " on host");
VirtualMachineMO vmMo = getVmWithHypervisorName(_vmCache.values(), vmName);
if (vmMo != null) {
if (s_logger.isDebugEnabled())
s_logger.debug("VM hypervisor name: " + vmName + " found in host cache");
return vmMo;
}
s_logger.info("VM hypervisor name: " + vmName + " not found in host cache");
loadVmCache();
return getVmWithHypervisorName(_vmCache.values(), vmName);
}
private VirtualMachineMO getVmWithHypervisorName(Collection<VirtualMachineMO> vmList, String vmName) {
if (CollectionUtils.isNotEmpty(vmList)) {
for (VirtualMachineMO vm : vmList) {
try {
if (StringUtils.isNotEmpty(vm.getVmName()) && vm.getVmName().equals(vmName)) {
return vm;
}
} catch (Exception e) {
s_logger.debug("Failed to get VM name, ignoring exception", e);
}
}
}
return null;
}
}

View File

@ -53,7 +53,7 @@ public interface VmwareHypervisorHost {
String getHyperHostDefaultGateway() throws Exception;
List<VirtualMachineMO> listVmsOnHyperHost(String name) throws Exception;
List<VirtualMachineMO> listVmsOnHyperHostWithHypervisorName(String name) throws Exception;
VirtualMachineMO findVmOnHyperHost(String name) throws Exception;