Skip to content

Commit 98c51a6

Browse files
authored
server: check guest os preference of last host when start a vm (#4338)
If vm has last host_id specified, cloudstack will try to start vm on it at first. However, host tag is checked, but guest os preference is not checked. for new vm, it will be deployed to the preferred host as we expect. Fixes: #3554 (comment)
1 parent 1efe6e2 commit 98c51a6

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@
8989
import com.cloud.exception.ConnectionException;
9090
import com.cloud.exception.InsufficientServerCapacityException;
9191
import com.cloud.gpu.GPU;
92+
import com.cloud.host.DetailVO;
9293
import com.cloud.host.Host;
9394
import com.cloud.host.HostVO;
9495
import com.cloud.host.Status;
9596
import com.cloud.host.dao.HostDao;
97+
import com.cloud.host.dao.HostDetailsDao;
9698
import com.cloud.hypervisor.Hypervisor.HypervisorType;
9799
import com.cloud.offering.ServiceOffering;
98100
import com.cloud.org.Cluster;
@@ -102,6 +104,7 @@
102104
import com.cloud.service.ServiceOfferingDetailsVO;
103105
import com.cloud.service.dao.ServiceOfferingDetailsDao;
104106
import com.cloud.storage.DiskOfferingVO;
107+
import com.cloud.storage.GuestOSVO;
105108
import com.cloud.storage.ScopeType;
106109
import com.cloud.storage.Storage;
107110
import com.cloud.storage.StorageManager;
@@ -165,6 +168,8 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy
165168
private long _hostReservationReleasePeriod = 60L * 60L * 1000L; // one hour by default
166169
@Inject
167170
protected VMReservationDao _reservationDao;
171+
@Inject
172+
HostDetailsDao _hostDetailsDao;
168173

169174
private static final long INITIAL_RESERVATION_RELEASE_CHECKER_DELAY = 30L * 1000L; // thirty seconds expressed in milliseconds
170175
protected long _nodeId = -1;
@@ -411,14 +416,7 @@ public DeployDestination planDeployment(VirtualMachineProfile vmProfile, Deploym
411416
}
412417
} else {
413418
if (host.getStatus() == Status.Up && host.getResourceState() == ResourceState.Enabled) {
414-
boolean hostTagsMatch = true;
415-
if(offering.getHostTag() != null){
416-
_hostDao.loadHostTags(host);
417-
if (!(host.getHostTags() != null && host.getHostTags().contains(offering.getHostTag()))) {
418-
hostTagsMatch = false;
419-
}
420-
}
421-
if (hostTagsMatch) {
419+
if (checkVmProfileAndHost(vmProfile, host)) {
422420
long cluster_id = host.getClusterId();
423421
ClusterDetailsVO cluster_detail_cpu = _clusterDetailsDao.findDetail(cluster_id,
424422
"cpuOvercommitRatio");
@@ -489,8 +487,6 @@ public DeployDestination planDeployment(VirtualMachineProfile vmProfile, Deploym
489487
} else {
490488
s_logger.debug("The last host of this VM does not have enough capacity");
491489
}
492-
} else {
493-
s_logger.debug("Service Offering host tag does not match the last host of this VM");
494490
}
495491
} else {
496492
s_logger.debug("The last host of this VM is not UP or is not enabled, host status is: " + host.getStatus().name() + ", host resource state is: " +
@@ -569,6 +565,31 @@ public DeploymentPlanner getDeploymentPlannerByName(String plannerName) {
569565
return null;
570566
}
571567

568+
private boolean checkVmProfileAndHost(final VirtualMachineProfile vmProfile, final HostVO host) {
569+
ServiceOffering offering = vmProfile.getServiceOffering();
570+
if (offering.getHostTag() != null) {
571+
_hostDao.loadHostTags(host);
572+
if (!(host.getHostTags() != null && host.getHostTags().contains(offering.getHostTag()))) {
573+
s_logger.debug("Service Offering host tag does not match the last host of this VM");
574+
return false;
575+
}
576+
}
577+
long guestOSId = vmProfile.getTemplate().getGuestOSId();
578+
GuestOSVO guestOS = _guestOSDao.findById(guestOSId);
579+
if (guestOS != null) {
580+
long guestOSCategoryId = guestOS.getCategoryId();
581+
DetailVO hostDetail = _hostDetailsDao.findDetail(host.getId(), "guest.os.category.id");
582+
if (hostDetail != null) {
583+
String guestOSCategoryIdString = hostDetail.getValue();
584+
if (String.valueOf(guestOSCategoryId) != guestOSCategoryIdString) {
585+
s_logger.debug("The last host has different guest.os.category.id than guest os category of VM, skipping");
586+
return false;
587+
}
588+
}
589+
}
590+
return true;
591+
}
592+
572593
@Override
573594
public void checkForNonDedicatedResources(VirtualMachineProfile vmProfile, DataCenter dc, ExcludeList avoids) {
574595
boolean isExplicit = false;

0 commit comments

Comments
 (0)