From 06cfca3245a353864cd86d79804684d9830f3fb4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 2 Jul 2026 15:04:22 +0000 Subject: [PATCH 01/12] Initial plan From b35f3de8c607d79f0f53ab72b57cce4a9158f292 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 2 Jul 2026 15:20:54 +0000 Subject: [PATCH 02/12] Fix domain-dedicated resource eligibility in ImplicitDedicationPlanner (issue #5803) - Fix findAvoidSetForNonExplicitUserVM to check domain/account dedication before adding pods/clusters/hosts to the avoid list - Domain-dedicated resources are now accessible to users in the same domain - Add 4 unit tests covering same-domain, different-domain, same-account, different-account scenarios - Use mutable ArrayList in test return values to support list.clear() calls in production code --- .../deploy/DeploymentPlanningManagerImpl.java | 68 +++++- .../DeploymentPlanningManagerImplTest.java | 207 ++++++++++++++++++ 2 files changed, 274 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index b686f351adfc..2477b2e2ea15 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -1083,8 +1083,74 @@ private void findAvoiSetForRouterVM(ExcludeList avoids, VirtualMachine vm, List< } private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine vm, List allPodsInDc, List allClustersInDc, List allHostsInDc) { + long vmAccountId = vm.getAccountId(); + long vmDomainId = vm.getDomainId(); + + List allPodsFromDedicatedID = new ArrayList<>(); + List allClustersFromDedicatedID = new ArrayList<>(); + List allHostsFromDedicatedID = new ArrayList<>(); + + // Check if the VM owner's domain has explicit dedication affinity group mappings. + // If so, resources dedicated to that domain are accessible to the VM owner (fixes + // issue where users in a domain-dedicated pod could not deploy with ImplicitDedicationPlanner). + List domainGroupMappings = _affinityGroupDomainMapDao.listByDomain(vmDomainId); + + List tempStorage; + + if (domainGroupMappings == null || domainGroupMappings.isEmpty()) { + // No domain-level affinity groups: search for resources dedicated to this specific account + tempStorage = _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, + new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); + for (DedicatedResourceVO vo : tempStorage) { + allPodsFromDedicatedID.add(vo.getPodId()); + } + + tempStorage.clear(); + tempStorage = _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, + new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); + for (DedicatedResourceVO vo : tempStorage) { + allClustersFromDedicatedID.add(vo.getClusterId()); + } + + tempStorage.clear(); + tempStorage = _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, + new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); + for (DedicatedResourceVO vo : tempStorage) { + allHostsFromDedicatedID.add(vo.getHostId()); + } + + allPodsInDc.removeAll(allPodsFromDedicatedID); + allClustersInDc.removeAll(allClustersFromDedicatedID); + allHostsInDc.removeAll(allHostsFromDedicatedID); + } else { + // Domain has explicit dedication affinity groups: search for resources dedicated to this domain + tempStorage = _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, + new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); + for (DedicatedResourceVO vo : tempStorage) { + allPodsFromDedicatedID.add(vo.getPodId()); + } + + tempStorage.clear(); + tempStorage = _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null, + new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); + for (DedicatedResourceVO vo : tempStorage) { + allClustersFromDedicatedID.add(vo.getClusterId()); + } + + tempStorage.clear(); + tempStorage = _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null, + new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); + for (DedicatedResourceVO vo : tempStorage) { + allHostsFromDedicatedID.add(vo.getHostId()); + } + + allPodsInDc.removeAll(allPodsFromDedicatedID); + allClustersInDc.removeAll(allClustersFromDedicatedID); + allHostsInDc.removeAll(allHostsFromDedicatedID); + } + logger.debug(() -> LogUtils.logGsonWithoutException("Adding pods [%s], clusters [%s] and hosts [%s] to the avoid list in the deploy process of user VM [%s], " - + "because this VM is not explicitly dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm)); + + "because this VM is not dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm)); avoids.addPodList(allPodsInDc); avoids.addClusterList(allClustersInDc); avoids.addHostList(allHostsInDc); diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 978cbfd98af0..dfa032fea3a5 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -27,6 +27,7 @@ import com.cloud.dc.ClusterVO; import com.cloud.dc.DataCenter; import com.cloud.dc.DataCenterVO; +import com.cloud.dc.DedicatedResourceVO; import com.cloud.dc.HostPodVO; import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; @@ -74,6 +75,7 @@ import com.cloud.user.dao.AccountDao; import com.cloud.utils.Pair; import com.cloud.utils.component.ComponentContext; +import com.cloud.utils.db.Filter; import com.cloud.vm.DiskProfile; import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine; @@ -88,6 +90,7 @@ import com.cloud.vm.dao.UserVmDao; import com.cloud.vm.dao.VMInstanceDetailsDao; import com.cloud.vm.dao.VMInstanceDao; +import org.apache.cloudstack.affinity.AffinityGroupDomainMapVO; import org.apache.cloudstack.affinity.AffinityGroupProcessor; import org.apache.cloudstack.affinity.AffinityGroupService; import org.apache.cloudstack.affinity.dao.AffinityGroupDao; @@ -243,6 +246,9 @@ public class DeploymentPlanningManagerImplTest { @Inject HostPodDao _podDao; + @Inject + AffinityGroupDomainMapDao _affinityGroupDomainMapDao; + private static final long dataCenterId = 1L; private static final long instanceId = 123L; private static final long hostId = 0L; @@ -1105,6 +1111,207 @@ private void assertAvoidIsEmpty(ExcludeList avoids, boolean isDcEmpty, boolean i Assert.assertEquals(isHostsEmpty, CollectionUtils.isEmpty(avoids.getHostsToAvoid())); } + /** + * Helper: set up mocks for checkForNonDedicatedResources tests. + * Sets up a non-root, non-explicit user VM with the given domain and account. + */ + private VMInstanceVO setupNonExplicitUserVmMocks(long vmDomainId, long vmAccountId, DataCenter mockDc) { + // Zone is not dedicated + Mockito.when(_dedicatedDao.findByZoneId(Mockito.anyLong())).thenReturn(null); + // Not root admin + Mockito.when(_accountMgr.isRootAdmin(Mockito.anyLong())).thenReturn(false); + // No explicit dedication affinity group for the VM + Mockito.when(_affinityGroupVMMapDao.findByVmIdType(Mockito.anyLong(), Mockito.eq("ExplicitDedication"))) + .thenReturn(new ArrayList<>()); + + VMInstanceVO mockVm = Mockito.mock(VMInstanceVO.class); + Mockito.when(mockVm.getType()).thenReturn(VirtualMachine.Type.User); + Mockito.when(mockVm.getDomainId()).thenReturn(vmDomainId); + Mockito.when(mockVm.getAccountId()).thenReturn(vmAccountId); + Mockito.when(mockVm.getId()).thenReturn(instanceId); + + // No dedicated clusters or hosts in DC + Mockito.when(_clusterDao.listAllClusterIds(Mockito.anyLong())).thenReturn(new ArrayList<>()); + Mockito.when(_dedicatedDao.listAllClusters()).thenReturn(new ArrayList<>()); + Mockito.when(_hostDao.listAllHosts(Mockito.anyLong())).thenReturn(new ArrayList<>()); + Mockito.when(_dedicatedDao.listAllHosts()).thenReturn(new ArrayList<>()); + Mockito.when(_dedicatedDao.searchDedicatedClusters(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + Mockito.when(_dedicatedDao.searchDedicatedHosts(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + + return mockVm; + } + + /** + * Issue #5803: A regular user in the same domain as a domain-dedicated pod + * must be allowed to deploy on that pod (i.e., the pod must NOT appear in the avoid list). + */ + @Test + public void checkForNonDedicatedResources_domainDedicatedPod_sameDomainUser_podNotInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 200L; + long dedicatedPodId = 42L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId, mockDc); + + // Pod in DC and it is dedicated + List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // Domain has affinity group mappings (pod dedicated to this domain) + AffinityGroupDomainMapVO domainMap = Mockito.mock(AffinityGroupDomainMapVO.class); + Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) + .thenReturn(Arrays.asList(domainMap)); + + // The pod is found when searching by domain (accountId = null) + DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedPod.getPodId()).thenReturn(dedicatedPodId); + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.isNull(), Mockito.isNull(), Mockito.any(Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedPod)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + // The dedicated pod belongs to the same domain as the VM owner: must NOT be in avoid list + assertTrue("Domain-dedicated pod should not be in avoid list for same-domain user", + CollectionUtils.isEmpty(avoids.getPodsToAvoid()) || !avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * A user from a different domain must NOT be allowed to deploy on a pod dedicated to another domain. + */ + @Test + public void checkForNonDedicatedResources_domainDedicatedPod_differentDomainUser_podInAvoidList() { + long podDomainId = 10L; // domain the pod is dedicated to + long vmDomainId = 20L; // VM owner belongs to a DIFFERENT domain + long vmAccountId = 300L; + long dedicatedPodId = 43L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId, mockDc); + + // Pod in DC and it is dedicated (to podDomainId, NOT to vmDomainId) + List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // VM owner's domain has NO affinity group mappings (no dedicated resources for vmDomainId) + Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) + .thenReturn(new ArrayList<>()); + + // No pods dedicated to the VM owner's account + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + // The pod is dedicated to a different domain: must be in avoid list + assertTrue("Domain-dedicated pod should be in avoid list for different-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * An account-dedicated pod should be accessible to the owning account + * (no domain-level affinity groups, but account-level dedicated resource exists). + */ + @Test + public void checkForNonDedicatedResources_accountDedicatedPod_sameAccount_podNotInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 200L; + long dedicatedPodId = 44L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId, mockDc); + + // Pod in DC and it is dedicated + List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // Domain has NO affinity group mappings (account-level dedication, not domain-level) + Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) + .thenReturn(new ArrayList<>()); + + // The pod IS found when searching by account + DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedPod.getPodId()).thenReturn(dedicatedPodId); + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedPod)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + // The pod is dedicated to this account: must NOT be in avoid list + assertTrue("Account-dedicated pod should not be in avoid list for same-account user", + CollectionUtils.isEmpty(avoids.getPodsToAvoid()) || !avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * A user whose account is different from the account a pod is dedicated to + * must NOT be able to deploy on that pod. + */ + @Test + public void checkForNonDedicatedResources_accountDedicatedPod_differentAccount_podInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 300L; // different account + long dedicatedPodId = 45L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId, mockDc); + + // Pod in DC and it is dedicated (to a different account) + List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // Domain has NO affinity group mappings + Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) + .thenReturn(new ArrayList<>()); + + // No pods dedicated to this account + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + // The pod is dedicated to a different account: must be in avoid list + assertTrue("Account-dedicated pod should be in avoid list for different-account user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + @Configuration @ComponentScan(basePackageClasses = {DeploymentPlanningManagerImpl.class}, includeFilters = {@Filter(value = TestConfiguration.Library.class, From 63cb87dd7e08313f58013dae2d7911082bb7916c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 2 Jul 2026 15:23:05 +0000 Subject: [PATCH 03/12] Refactor findAvoidSetForNonExplicitUserVM: remove pagination limit and tempStorage.clear() - Use Filter without offset/limit to retrieve all dedicated resources (not just the first) - Eliminate tempStorage variable and clear() calls to avoid potential side effects - Code is now cleaner and consistent with correct behavior --- .../deploy/DeploymentPlanningManagerImpl.java | 46 +++++-------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index 2477b2e2ea15..59c12c8eb6e3 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -1095,60 +1095,36 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine // issue where users in a domain-dedicated pod could not deploy with ImplicitDedicationPlanner). List domainGroupMappings = _affinityGroupDomainMapDao.listByDomain(vmDomainId); - List tempStorage; + Filter filter = new Filter(DedicatedResourceVO.class, "id", true); if (domainGroupMappings == null || domainGroupMappings.isEmpty()) { // No domain-level affinity groups: search for resources dedicated to this specific account - tempStorage = _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, - new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); - for (DedicatedResourceVO vo : tempStorage) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, filter).first()) { allPodsFromDedicatedID.add(vo.getPodId()); } - - tempStorage.clear(); - tempStorage = _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, - new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); - for (DedicatedResourceVO vo : tempStorage) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, filter).first()) { allClustersFromDedicatedID.add(vo.getClusterId()); } - - tempStorage.clear(); - tempStorage = _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, - new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); - for (DedicatedResourceVO vo : tempStorage) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, filter).first()) { allHostsFromDedicatedID.add(vo.getHostId()); } - - allPodsInDc.removeAll(allPodsFromDedicatedID); - allClustersInDc.removeAll(allClustersFromDedicatedID); - allHostsInDc.removeAll(allHostsFromDedicatedID); } else { // Domain has explicit dedication affinity groups: search for resources dedicated to this domain - tempStorage = _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, - new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); - for (DedicatedResourceVO vo : tempStorage) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, filter).first()) { allPodsFromDedicatedID.add(vo.getPodId()); } - - tempStorage.clear(); - tempStorage = _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null, - new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); - for (DedicatedResourceVO vo : tempStorage) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null, filter).first()) { allClustersFromDedicatedID.add(vo.getClusterId()); } - - tempStorage.clear(); - tempStorage = _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null, - new Filter(DedicatedResourceVO.class, "id", true, 0L, 1L)).first(); - for (DedicatedResourceVO vo : tempStorage) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null, filter).first()) { allHostsFromDedicatedID.add(vo.getHostId()); } - - allPodsInDc.removeAll(allPodsFromDedicatedID); - allClustersInDc.removeAll(allClustersFromDedicatedID); - allHostsInDc.removeAll(allHostsFromDedicatedID); } + allPodsInDc.removeAll(allPodsFromDedicatedID); + allClustersInDc.removeAll(allClustersFromDedicatedID); + allHostsInDc.removeAll(allHostsFromDedicatedID); + logger.debug(() -> LogUtils.logGsonWithoutException("Adding pods [%s], clusters [%s] and hosts [%s] to the avoid list in the deploy process of user VM [%s], " + "because this VM is not dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm)); avoids.addPodList(allPodsInDc); From 5f778e3295dd7aef470cde0769fdc32e797fa9bd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 2 Jul 2026 15:24:00 +0000 Subject: [PATCH 04/12] Address code review: document filter purpose and clarify test mock intent --- .../java/com/cloud/deploy/DeploymentPlanningManagerImpl.java | 1 + .../com/cloud/deploy/DeploymentPlanningManagerImplTest.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index 59c12c8eb6e3..f0fc3b711b15 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -1095,6 +1095,7 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine // issue where users in a domain-dedicated pod could not deploy with ImplicitDedicationPlanner). List domainGroupMappings = _affinityGroupDomainMapDao.listByDomain(vmDomainId); + // Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account Filter filter = new Filter(DedicatedResourceVO.class, "id", true); if (domainGroupMappings == null || domainGroupMappings.isEmpty()) { diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index dfa032fea3a5..7d4efce95aaa 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -1163,7 +1163,8 @@ public void checkForNonDedicatedResources_domainDedicatedPod_sameDomainUser_podN Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); - // Domain has affinity group mappings (pod dedicated to this domain) + // Domain has affinity group mappings (pod dedicated to this domain). + // The content of the list entries does not matter; only emptiness is checked. AffinityGroupDomainMapVO domainMap = Mockito.mock(AffinityGroupDomainMapVO.class); Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) .thenReturn(Arrays.asList(domainMap)); From a4f2e7a85fd2a33ba24fe58cfbd2f354b5220c42 Mon Sep 17 00:00:00 2001 From: dahn Date: Mon, 6 Jul 2026 16:04:41 +0200 Subject: [PATCH 05/12] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../deploy/DeploymentPlanningManagerImpl.java | 26 +++++++++---------- .../DeploymentPlanningManagerImplTest.java | 9 +++---- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index f0fc3b711b15..7ce1e61c589c 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -1098,19 +1098,19 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine // Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account Filter filter = new Filter(DedicatedResourceVO.class, "id", true); - if (domainGroupMappings == null || domainGroupMappings.isEmpty()) { - // No domain-level affinity groups: search for resources dedicated to this specific account - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, filter).first()) { - allPodsFromDedicatedID.add(vo.getPodId()); - } - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, filter).first()) { - allClustersFromDedicatedID.add(vo.getClusterId()); - } - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, filter).first()) { - allHostsFromDedicatedID.add(vo.getHostId()); - } - } else { - // Domain has explicit dedication affinity groups: search for resources dedicated to this domain + // Always allow resources dedicated to the VM owner's account. + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, filter).first()) { + allPodsFromDedicatedID.add(vo.getPodId()); + } + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, filter).first()) { + allClustersFromDedicatedID.add(vo.getClusterId()); + } + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, filter).first()) { + allHostsFromDedicatedID.add(vo.getHostId()); + } + + if (domainGroupMappings != null && !domainGroupMappings.isEmpty()) { + // Domain has explicit dedication affinity groups: also allow resources dedicated to this domain for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, filter).first()) { allPodsFromDedicatedID.add(vo.getPodId()); } diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 7d4efce95aaa..2e6ceb849519 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -75,7 +75,6 @@ import com.cloud.user.dao.AccountDao; import com.cloud.utils.Pair; import com.cloud.utils.component.ComponentContext; -import com.cloud.utils.db.Filter; import com.cloud.vm.DiskProfile; import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine; @@ -1173,7 +1172,7 @@ public void checkForNonDedicatedResources_domainDedicatedPod_sameDomainUser_podN DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); Mockito.when(dedicatedPod.getPodId()).thenReturn(dedicatedPodId); Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), - Mockito.isNull(), Mockito.isNull(), Mockito.any(Filter.class))) + Mockito.isNull(), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedPod)), 1)); VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); @@ -1214,7 +1213,7 @@ public void checkForNonDedicatedResources_domainDedicatedPod_differentDomainUser // No pods dedicated to the VM owner's account Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), - Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(Filter.class))) + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) .thenReturn(new Pair<>(new ArrayList<>(), 0)); VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); @@ -1257,7 +1256,7 @@ public void checkForNonDedicatedResources_accountDedicatedPod_sameAccount_podNot DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); Mockito.when(dedicatedPod.getPodId()).thenReturn(dedicatedPodId); Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), - Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(Filter.class))) + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedPod)), 1)); VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); @@ -1298,7 +1297,7 @@ public void checkForNonDedicatedResources_accountDedicatedPod_differentAccount_p // No pods dedicated to this account Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), - Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(Filter.class))) + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) .thenReturn(new Pair<>(new ArrayList<>(), 0)); VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); From ac4b37a42b665e44213dcf795373714d01d48486 Mon Sep 17 00:00:00 2001 From: dahn Date: Mon, 6 Jul 2026 16:24:30 +0200 Subject: [PATCH 06/12] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: dahn --- .../cloud/deploy/DeploymentPlanningManagerImpl.java | 10 +++++----- .../deploy/DeploymentPlanningManagerImplTest.java | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index 7ce1e61c589c..e46bb605e2e3 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -1090,10 +1090,10 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine List allClustersFromDedicatedID = new ArrayList<>(); List allHostsFromDedicatedID = new ArrayList<>(); - // Check if the VM owner's domain has explicit dedication affinity group mappings. - // If so, resources dedicated to that domain are accessible to the VM owner (fixes - // issue where users in a domain-dedicated pod could not deploy with ImplicitDedicationPlanner). - List domainGroupMappings = _affinityGroupDomainMapDao.listByDomain(vmDomainId); + // If the VM owner's domain has an ExplicitDedication domain-level affinity group, + // resources dedicated to that domain are accessible to the VM owner (fixes issue #5803). + boolean hasDomainExplicitDedicationGroup = + _affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication") != null; // Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account Filter filter = new Filter(DedicatedResourceVO.class, "id", true); @@ -1109,7 +1109,7 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine allHostsFromDedicatedID.add(vo.getHostId()); } - if (domainGroupMappings != null && !domainGroupMappings.isEmpty()) { + if (hasDomainExplicitDedicationGroup) { // Domain has explicit dedication affinity groups: also allow resources dedicated to this domain for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, filter).first()) { allPodsFromDedicatedID.add(vo.getPodId()); diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 2e6ceb849519..0b498ffba83a 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -245,6 +245,9 @@ public class DeploymentPlanningManagerImplTest { @Inject HostPodDao _podDao; + @Inject + HostDao _hostDao; + @Inject AffinityGroupDomainMapDao _affinityGroupDomainMapDao; From 26b45246df70d45e5bfb1e11619ff6885ff810b2 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Mon, 20 Jul 2026 17:26:45 +0200 Subject: [PATCH 07/12] Fix NPE in ImplicitDedicationPlanner unit tests (issue #5803) findAvoidSetForNonExplicitUserVM referenced _affinityGroupDao, which has no corresponding mock in DeploymentPlanningManagerImplTest, so it was always null via Mockito's @InjectMocks and every call NPE'd. Use _affinityGroupDomainMapDao.listByDomain, already injected and used by the analogous findAvoiSetForRouterVM method, to detect a domain-level ExplicitDedication affinity group. Also add a default empty stub for searchDedicatedPods, mirroring the existing cluster/host stubs, since the fix now queries pods for both the account and the domain. Co-Authored-By: Claude Sonnet 5 --- .../java/com/cloud/deploy/DeploymentPlanningManagerImpl.java | 4 ++-- .../com/cloud/deploy/DeploymentPlanningManagerImplTest.java | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index e46bb605e2e3..5a4d95f9cc3a 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -1092,8 +1092,8 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine // If the VM owner's domain has an ExplicitDedication domain-level affinity group, // resources dedicated to that domain are accessible to the VM owner (fixes issue #5803). - boolean hasDomainExplicitDedicationGroup = - _affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication") != null; + List domainGroupMappings = _affinityGroupDomainMapDao.listByDomain(vmDomainId); + boolean hasDomainExplicitDedicationGroup = domainGroupMappings != null && !domainGroupMappings.isEmpty(); // Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account Filter filter = new Filter(DedicatedResourceVO.class, "id", true); diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 0b498ffba83a..2fce1f8715e4 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -1137,6 +1137,8 @@ private VMInstanceVO setupNonExplicitUserVmMocks(long vmDomainId, long vmAccount Mockito.when(_dedicatedDao.listAllClusters()).thenReturn(new ArrayList<>()); Mockito.when(_hostDao.listAllHosts(Mockito.anyLong())).thenReturn(new ArrayList<>()); Mockito.when(_dedicatedDao.listAllHosts()).thenReturn(new ArrayList<>()); + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); Mockito.when(_dedicatedDao.searchDedicatedClusters(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(new Pair<>(new ArrayList<>(), 0)); Mockito.when(_dedicatedDao.searchDedicatedHosts(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) From e5f74f6a12b17a4fc1dd6fac3b3ad0a097cf751b Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Mon, 20 Jul 2026 18:03:19 +0200 Subject: [PATCH 08/12] Apply PR #13529 review hardening: type-specific check + early return Restore the type-specific AffinityGroupDao.findDomainLevelGroupByType check (as Copilot's review on PR #13529 requested), instead of the looser AffinityGroupDomainMapDao.listByDomain used in the previous fix commit, which matches any domain-level affinity group rather than specifically ExplicitDedication. Add the missing @Mock for AffinityGroupDao and update the new tests to stub the type-specific call, so this no longer NPEs the way the last "apply suggestions" commit did. Also add an early return in findAvoidSetForNonExplicitUserVM when nothing in the DC is dedicated, per another unaddressed review comment, to avoid unnecessary DAO calls on the common case. Co-Authored-By: Claude Sonnet 5 --- .../deploy/DeploymentPlanningManagerImpl.java | 9 ++++-- .../DeploymentPlanningManagerImplTest.java | 32 +++++++++---------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index 5a4d95f9cc3a..cbfaddda152d 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -1083,6 +1083,11 @@ private void findAvoiSetForRouterVM(ExcludeList avoids, VirtualMachine vm, List< } private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine vm, List allPodsInDc, List allClustersInDc, List allHostsInDc) { + if (allPodsInDc.isEmpty() && allClustersInDc.isEmpty() && allHostsInDc.isEmpty()) { + // Nothing in this DC is dedicated to anyone, so there is nothing to avoid or to check dedication for. + return; + } + long vmAccountId = vm.getAccountId(); long vmDomainId = vm.getDomainId(); @@ -1092,8 +1097,8 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine // If the VM owner's domain has an ExplicitDedication domain-level affinity group, // resources dedicated to that domain are accessible to the VM owner (fixes issue #5803). - List domainGroupMappings = _affinityGroupDomainMapDao.listByDomain(vmDomainId); - boolean hasDomainExplicitDedicationGroup = domainGroupMappings != null && !domainGroupMappings.isEmpty(); + boolean hasDomainExplicitDedicationGroup = + _affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication") != null; // Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account Filter filter = new Filter(DedicatedResourceVO.class, "id", true); diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 2fce1f8715e4..739b0e9d5412 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -89,9 +89,9 @@ import com.cloud.vm.dao.UserVmDao; import com.cloud.vm.dao.VMInstanceDetailsDao; import com.cloud.vm.dao.VMInstanceDao; -import org.apache.cloudstack.affinity.AffinityGroupDomainMapVO; import org.apache.cloudstack.affinity.AffinityGroupProcessor; import org.apache.cloudstack.affinity.AffinityGroupService; +import org.apache.cloudstack.affinity.AffinityGroupVO; import org.apache.cloudstack.affinity.dao.AffinityGroupDao; import org.apache.cloudstack.affinity.dao.AffinityGroupDomainMapDao; import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; @@ -248,8 +248,8 @@ public class DeploymentPlanningManagerImplTest { @Inject HostDao _hostDao; - @Inject - AffinityGroupDomainMapDao _affinityGroupDomainMapDao; + @Mock + AffinityGroupDao _affinityGroupDao; private static final long dataCenterId = 1L; private static final long instanceId = 123L; @@ -1167,11 +1167,9 @@ public void checkForNonDedicatedResources_domainDedicatedPod_sameDomainUser_podN Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); - // Domain has affinity group mappings (pod dedicated to this domain). - // The content of the list entries does not matter; only emptiness is checked. - AffinityGroupDomainMapVO domainMap = Mockito.mock(AffinityGroupDomainMapVO.class); - Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) - .thenReturn(Arrays.asList(domainMap)); + // Domain has an ExplicitDedication domain-level affinity group (pod dedicated to this domain). + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(Mockito.mock(AffinityGroupVO.class)); // The pod is found when searching by domain (accountId = null) DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); @@ -1212,9 +1210,9 @@ public void checkForNonDedicatedResources_domainDedicatedPod_differentDomainUser Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); - // VM owner's domain has NO affinity group mappings (no dedicated resources for vmDomainId) - Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) - .thenReturn(new ArrayList<>()); + // VM owner's domain has no ExplicitDedication domain-level affinity group + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(null); // No pods dedicated to the VM owner's account Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), @@ -1253,9 +1251,9 @@ public void checkForNonDedicatedResources_accountDedicatedPod_sameAccount_podNot Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); - // Domain has NO affinity group mappings (account-level dedication, not domain-level) - Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) - .thenReturn(new ArrayList<>()); + // Domain has no ExplicitDedication domain-level affinity group (account-level dedication, not domain-level) + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(null); // The pod IS found when searching by account DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); @@ -1296,9 +1294,9 @@ public void checkForNonDedicatedResources_accountDedicatedPod_differentAccount_p Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); - // Domain has NO affinity group mappings - Mockito.when(_affinityGroupDomainMapDao.listByDomain(vmDomainId)) - .thenReturn(new ArrayList<>()); + // Domain has no ExplicitDedication domain-level affinity group + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(null); // No pods dedicated to this account Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), From b7bcdab63785991f67929803537b5aca1deaf690 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Thu, 30 Jul 2026 16:10:19 +0200 Subject: [PATCH 09/12] minor improvements DeploymentPlanningManagerImpl.java: renamed allPodsFromDedicatedID/allClustersFromDedicatedID/allHostsFromDedicatedID to allowedDedicatedPodIds/allowedDedicatedClusterIds/allowedDedicatedHostIds for clarity. DeploymentPlanningManagerImplTest.java: removed the unused mockDc parameter from setupNonExplicitUserVmMocks; simplified two assertions to assertFalse instead of the isEmpty || !contains pattern; added --- .../deploy/DeploymentPlanningManagerImpl.java | 24 ++-- .../DeploymentPlanningManagerImplTest.java | 122 ++++++++++++++++-- 2 files changed, 125 insertions(+), 21 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index cbfaddda152d..6312563ec91f 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -1091,9 +1091,9 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine long vmAccountId = vm.getAccountId(); long vmDomainId = vm.getDomainId(); - List allPodsFromDedicatedID = new ArrayList<>(); - List allClustersFromDedicatedID = new ArrayList<>(); - List allHostsFromDedicatedID = new ArrayList<>(); + List allowedDedicatedPodIds = new ArrayList<>(); + List allowedDedicatedClusterIds = new ArrayList<>(); + List allowedDedicatedHostIds = new ArrayList<>(); // If the VM owner's domain has an ExplicitDedication domain-level affinity group, // resources dedicated to that domain are accessible to the VM owner (fixes issue #5803). @@ -1105,31 +1105,31 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine // Always allow resources dedicated to the VM owner's account. for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, filter).first()) { - allPodsFromDedicatedID.add(vo.getPodId()); + allowedDedicatedPodIds.add(vo.getPodId()); } for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, filter).first()) { - allClustersFromDedicatedID.add(vo.getClusterId()); + allowedDedicatedClusterIds.add(vo.getClusterId()); } for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, filter).first()) { - allHostsFromDedicatedID.add(vo.getHostId()); + allowedDedicatedHostIds.add(vo.getHostId()); } if (hasDomainExplicitDedicationGroup) { // Domain has explicit dedication affinity groups: also allow resources dedicated to this domain for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, filter).first()) { - allPodsFromDedicatedID.add(vo.getPodId()); + allowedDedicatedPodIds.add(vo.getPodId()); } for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null, filter).first()) { - allClustersFromDedicatedID.add(vo.getClusterId()); + allowedDedicatedClusterIds.add(vo.getClusterId()); } for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null, filter).first()) { - allHostsFromDedicatedID.add(vo.getHostId()); + allowedDedicatedHostIds.add(vo.getHostId()); } } - allPodsInDc.removeAll(allPodsFromDedicatedID); - allClustersInDc.removeAll(allClustersFromDedicatedID); - allHostsInDc.removeAll(allHostsFromDedicatedID); + allPodsInDc.removeAll(allowedDedicatedPodIds); + allClustersInDc.removeAll(allowedDedicatedClusterIds); + allHostsInDc.removeAll(allowedDedicatedHostIds); logger.debug(() -> LogUtils.logGsonWithoutException("Adding pods [%s], clusters [%s] and hosts [%s] to the avoid list in the deploy process of user VM [%s], " + "because this VM is not dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm)); diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 739b0e9d5412..5ac2dbc3bce6 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -1117,7 +1117,7 @@ private void assertAvoidIsEmpty(ExcludeList avoids, boolean isDcEmpty, boolean i * Helper: set up mocks for checkForNonDedicatedResources tests. * Sets up a non-root, non-explicit user VM with the given domain and account. */ - private VMInstanceVO setupNonExplicitUserVmMocks(long vmDomainId, long vmAccountId, DataCenter mockDc) { + private VMInstanceVO setupNonExplicitUserVmMocks(long vmDomainId, long vmAccountId) { // Zone is not dedicated Mockito.when(_dedicatedDao.findByZoneId(Mockito.anyLong())).thenReturn(null); // Not root admin @@ -1160,7 +1160,7 @@ public void checkForNonDedicatedResources_domainDedicatedPod_sameDomainUser_podN DataCenter mockDc = Mockito.mock(DataCenter.class); Mockito.when(mockDc.getId()).thenReturn(dataCenterId); - VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId, mockDc); + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); // Pod in DC and it is dedicated List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); @@ -1186,8 +1186,8 @@ public void checkForNonDedicatedResources_domainDedicatedPod_sameDomainUser_podN _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); // The dedicated pod belongs to the same domain as the VM owner: must NOT be in avoid list - assertTrue("Domain-dedicated pod should not be in avoid list for same-domain user", - CollectionUtils.isEmpty(avoids.getPodsToAvoid()) || !avoids.getPodsToAvoid().contains(dedicatedPodId)); + assertFalse("Domain-dedicated pod should not be in avoid list for same-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); } /** @@ -1203,7 +1203,7 @@ public void checkForNonDedicatedResources_domainDedicatedPod_differentDomainUser DataCenter mockDc = Mockito.mock(DataCenter.class); Mockito.when(mockDc.getId()).thenReturn(dataCenterId); - VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId, mockDc); + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); // Pod in DC and it is dedicated (to podDomainId, NOT to vmDomainId) List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); @@ -1244,7 +1244,7 @@ public void checkForNonDedicatedResources_accountDedicatedPod_sameAccount_podNot DataCenter mockDc = Mockito.mock(DataCenter.class); Mockito.when(mockDc.getId()).thenReturn(dataCenterId); - VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId, mockDc); + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); // Pod in DC and it is dedicated List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); @@ -1270,8 +1270,8 @@ public void checkForNonDedicatedResources_accountDedicatedPod_sameAccount_podNot _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); // The pod is dedicated to this account: must NOT be in avoid list - assertTrue("Account-dedicated pod should not be in avoid list for same-account user", - CollectionUtils.isEmpty(avoids.getPodsToAvoid()) || !avoids.getPodsToAvoid().contains(dedicatedPodId)); + assertFalse("Account-dedicated pod should not be in avoid list for same-account user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); } /** @@ -1287,7 +1287,7 @@ public void checkForNonDedicatedResources_accountDedicatedPod_differentAccount_p DataCenter mockDc = Mockito.mock(DataCenter.class); Mockito.when(mockDc.getId()).thenReturn(dataCenterId); - VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId, mockDc); + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); // Pod in DC and it is dedicated (to a different account) List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); @@ -1315,6 +1315,110 @@ public void checkForNonDedicatedResources_accountDedicatedPod_differentAccount_p avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); } + /** + * Issue #5803: the same domain-dedication rule applied to pods above must also apply to + * clusters and hosts, since findAvoidSetForNonExplicitUserVM handles all three resource types. + */ + @Test + public void checkForNonDedicatedResources_domainDedicatedClusterAndHost_sameDomainUser_notInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 200L; + long dedicatedClusterId = 52L; + long dedicatedHostId = 62L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); + + // Cluster and host in DC are dedicated + Mockito.when(_clusterDao.listAllClusterIds(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedClusterId))); + Mockito.when(_dedicatedDao.listAllClusters()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedClusterId))); + Mockito.when(_hostDao.listAllHosts(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedHostId))); + Mockito.when(_dedicatedDao.listAllHosts()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedHostId))); + + // Domain has an ExplicitDedication domain-level affinity group + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(Mockito.mock(AffinityGroupVO.class)); + + // The cluster and host are found when searching by domain (accountId = null) + DedicatedResourceVO dedicatedCluster = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedCluster.getClusterId()).thenReturn(dedicatedClusterId); + Mockito.when(_dedicatedDao.searchDedicatedClusters(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.isNull(), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedCluster)), 1)); + + DedicatedResourceVO dedicatedHost = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedHost.getHostId()).thenReturn(dedicatedHostId); + Mockito.when(_dedicatedDao.searchDedicatedHosts(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.isNull(), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedHost)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + assertFalse("Domain-dedicated cluster should not be in avoid list for same-domain user", + avoids.getClustersToAvoid() != null && avoids.getClustersToAvoid().contains(dedicatedClusterId)); + assertFalse("Domain-dedicated host should not be in avoid list for same-domain user", + avoids.getHostsToAvoid() != null && avoids.getHostsToAvoid().contains(dedicatedHostId)); + } + + /** + * Issue #5803: the same account-dedication rule applied to pods above must also apply to + * clusters and hosts, since findAvoidSetForNonExplicitUserVM handles all three resource types. + */ + @Test + public void checkForNonDedicatedResources_accountDedicatedClusterAndHost_sameAccount_notInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 200L; + long dedicatedClusterId = 53L; + long dedicatedHostId = 63L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); + + // Cluster and host in DC are dedicated + Mockito.when(_clusterDao.listAllClusterIds(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedClusterId))); + Mockito.when(_dedicatedDao.listAllClusters()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedClusterId))); + Mockito.when(_hostDao.listAllHosts(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedHostId))); + Mockito.when(_dedicatedDao.listAllHosts()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedHostId))); + + // Domain has no ExplicitDedication domain-level affinity group (account-level dedication, not domain-level) + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(null); + + // The cluster and host ARE found when searching by account + DedicatedResourceVO dedicatedCluster = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedCluster.getClusterId()).thenReturn(dedicatedClusterId); + Mockito.when(_dedicatedDao.searchDedicatedClusters(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedCluster)), 1)); + + DedicatedResourceVO dedicatedHost = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedHost.getHostId()).thenReturn(dedicatedHostId); + Mockito.when(_dedicatedDao.searchDedicatedHosts(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedHost)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + assertFalse("Account-dedicated cluster should not be in avoid list for same-account user", + avoids.getClustersToAvoid() != null && avoids.getClustersToAvoid().contains(dedicatedClusterId)); + assertFalse("Account-dedicated host should not be in avoid list for same-account user", + avoids.getHostsToAvoid() != null && avoids.getHostsToAvoid().contains(dedicatedHostId)); + } + @Configuration @ComponentScan(basePackageClasses = {DeploymentPlanningManagerImpl.class}, includeFilters = {@Filter(value = TestConfiguration.Library.class, From f80d9aa7abea5208da6442975837e757fae8102a Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Fri, 13 Nov 2026 10:11:43 +0100 Subject: [PATCH 10/12] per-type dedicated lookups, constant for affinity type, drop issue refs --- .../deploy/DeploymentPlanningManagerImpl.java | 61 +++++++++++-------- .../DeploymentPlanningManagerImplTest.java | 6 +- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index 6312563ec91f..8de5c7768a1e 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -199,6 +199,7 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy private static final long ADMIN_ACCOUNT_ROLE_ID = 1l; private static final long INITIAL_RESERVATION_RELEASE_CHECKER_DELAY = 30L * 1000L; // thirty seconds expressed in milliseconds + private static final String EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE = "ExplicitDedication"; protected long _nodeId = -1; protected static final List DEPLOYMENT_PLANNING_SKIP_HYPERVISORS = Arrays.asList( HypervisorType.BareMetal, HypervisorType.External); @@ -977,7 +978,7 @@ public void checkForNonDedicatedResources(VirtualMachineProfile vmProfile, DataC // check affinity group of type Explicit dedication exists. If No put // dedicated pod/cluster/host in avoid list - List vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), "ExplicitDedication"); + List vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE); if (vmGroupMappings != null && !vmGroupMappings.isEmpty()) { isExplicit = true; @@ -1091,46 +1092,52 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine long vmAccountId = vm.getAccountId(); long vmDomainId = vm.getDomainId(); - List allowedDedicatedPodIds = new ArrayList<>(); - List allowedDedicatedClusterIds = new ArrayList<>(); - List allowedDedicatedHostIds = new ArrayList<>(); - // If the VM owner's domain has an ExplicitDedication domain-level affinity group, - // resources dedicated to that domain are accessible to the VM owner (fixes issue #5803). + // resources dedicated to that domain are accessible to the VM owner as well. boolean hasDomainExplicitDedicationGroup = - _affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication") != null; + _affinityGroupDao.findDomainLevelGroupByType(vmDomainId, EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE) != null; // Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account Filter filter = new Filter(DedicatedResourceVO.class, "id", true); - // Always allow resources dedicated to the VM owner's account. - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, filter).first()) { - allowedDedicatedPodIds.add(vo.getPodId()); - } - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, filter).first()) { - allowedDedicatedClusterIds.add(vo.getClusterId()); - } - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, filter).first()) { - allowedDedicatedHostIds.add(vo.getHostId()); - } - - if (hasDomainExplicitDedicationGroup) { - // Domain has explicit dedication affinity groups: also allow resources dedicated to this domain - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, filter).first()) { + // Only query the resource types that have dedicated entries in this DC. + if (!allPodsInDc.isEmpty()) { + Set allowedDedicatedPodIds = new HashSet<>(); + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, filter).first()) { allowedDedicatedPodIds.add(vo.getPodId()); } - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null, filter).first()) { + if (hasDomainExplicitDedicationGroup) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, filter).first()) { + allowedDedicatedPodIds.add(vo.getPodId()); + } + } + allPodsInDc.removeAll(allowedDedicatedPodIds); + } + if (!allClustersInDc.isEmpty()) { + Set allowedDedicatedClusterIds = new HashSet<>(); + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, filter).first()) { allowedDedicatedClusterIds.add(vo.getClusterId()); } - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null, filter).first()) { + if (hasDomainExplicitDedicationGroup) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null, filter).first()) { + allowedDedicatedClusterIds.add(vo.getClusterId()); + } + } + allClustersInDc.removeAll(allowedDedicatedClusterIds); + } + if (!allHostsInDc.isEmpty()) { + Set allowedDedicatedHostIds = new HashSet<>(); + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, filter).first()) { allowedDedicatedHostIds.add(vo.getHostId()); } + if (hasDomainExplicitDedicationGroup) { + for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null, filter).first()) { + allowedDedicatedHostIds.add(vo.getHostId()); + } + } + allHostsInDc.removeAll(allowedDedicatedHostIds); } - allPodsInDc.removeAll(allowedDedicatedPodIds); - allClustersInDc.removeAll(allowedDedicatedClusterIds); - allHostsInDc.removeAll(allowedDedicatedHostIds); - logger.debug(() -> LogUtils.logGsonWithoutException("Adding pods [%s], clusters [%s] and hosts [%s] to the avoid list in the deploy process of user VM [%s], " + "because this VM is not dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm)); avoids.addPodList(allPodsInDc); diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 5ac2dbc3bce6..36e2565be407 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -1148,7 +1148,7 @@ private VMInstanceVO setupNonExplicitUserVmMocks(long vmDomainId, long vmAccount } /** - * Issue #5803: A regular user in the same domain as a domain-dedicated pod + * A regular user in the same domain as a domain-dedicated pod * must be allowed to deploy on that pod (i.e., the pod must NOT appear in the avoid list). */ @Test @@ -1316,7 +1316,7 @@ public void checkForNonDedicatedResources_accountDedicatedPod_differentAccount_p } /** - * Issue #5803: the same domain-dedication rule applied to pods above must also apply to + * The same domain-dedication rule applied to pods above must also apply to * clusters and hosts, since findAvoidSetForNonExplicitUserVM handles all three resource types. */ @Test @@ -1368,7 +1368,7 @@ public void checkForNonDedicatedResources_domainDedicatedClusterAndHost_sameDoma } /** - * Issue #5803: the same account-dedication rule applied to pods above must also apply to + * The same account-dedication rule applied to pods above must also apply to * clusters and hosts, since findAvoidSetForNonExplicitUserVM handles all three resource types. */ @Test From 6c5fae276c053d0fcbba244cd8fa5924491fabb1 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Tue, 1 Dec 2026 10:14:50 +0100 Subject: [PATCH 11/12] extract remove dedicated-ids into one filtering method for pods, clusters and hosts --- .../deploy/DeploymentPlanningManagerImpl.java | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index 8de5c7768a1e..231fde0c97e6 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -31,6 +31,8 @@ import java.util.Set; import java.util.Timer; import java.util.TreeSet; +import java.util.function.BiFunction; +import java.util.function.Function; import java.util.stream.Collectors; import javax.inject.Inject; @@ -1101,42 +1103,15 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine Filter filter = new Filter(DedicatedResourceVO.class, "id", true); // Only query the resource types that have dedicated entries in this DC. - if (!allPodsInDc.isEmpty()) { - Set allowedDedicatedPodIds = new HashSet<>(); - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, vmAccountId, null, filter).first()) { - allowedDedicatedPodIds.add(vo.getPodId()); - } - if (hasDomainExplicitDedicationGroup) { - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedPods(null, vmDomainId, null, null, filter).first()) { - allowedDedicatedPodIds.add(vo.getPodId()); - } - } - allPodsInDc.removeAll(allowedDedicatedPodIds); - } - if (!allClustersInDc.isEmpty()) { - Set allowedDedicatedClusterIds = new HashSet<>(); - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, vmAccountId, null, filter).first()) { - allowedDedicatedClusterIds.add(vo.getClusterId()); - } - if (hasDomainExplicitDedicationGroup) { - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedClusters(null, vmDomainId, null, null, filter).first()) { - allowedDedicatedClusterIds.add(vo.getClusterId()); - } - } - allClustersInDc.removeAll(allowedDedicatedClusterIds); - } - if (!allHostsInDc.isEmpty()) { - Set allowedDedicatedHostIds = new HashSet<>(); - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, vmAccountId, null, filter).first()) { - allowedDedicatedHostIds.add(vo.getHostId()); - } - if (hasDomainExplicitDedicationGroup) { - for (DedicatedResourceVO vo : _dedicatedDao.searchDedicatedHosts(null, vmDomainId, null, null, filter).first()) { - allowedDedicatedHostIds.add(vo.getHostId()); - } - } - allHostsInDc.removeAll(allowedDedicatedHostIds); - } + removeDedicatedIdsAccessibleToVm(allPodsInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + (domainId, accountId) -> _dedicatedDao.searchDedicatedPods(null, domainId, accountId, null, filter), + DedicatedResourceVO::getPodId); + removeDedicatedIdsAccessibleToVm(allClustersInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + (domainId, accountId) -> _dedicatedDao.searchDedicatedClusters(null, domainId, accountId, null, filter), + DedicatedResourceVO::getClusterId); + removeDedicatedIdsAccessibleToVm(allHostsInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + (domainId, accountId) -> _dedicatedDao.searchDedicatedHosts(null, domainId, accountId, null, filter), + DedicatedResourceVO::getHostId); logger.debug(() -> LogUtils.logGsonWithoutException("Adding pods [%s], clusters [%s] and hosts [%s] to the avoid list in the deploy process of user VM [%s], " + "because this VM is not dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm)); @@ -1145,6 +1120,27 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine avoids.addHostList(allHostsInDc); } + /** + * Removes from {@code dedicatedIdsInDc} the ids of the resources the VM owner may use: those dedicated to the + * owner's account and, if {@code includeDomainDedications} is set, those dedicated to the owner's domain. + */ + private void removeDedicatedIdsAccessibleToVm(List dedicatedIdsInDc, long vmDomainId, long vmAccountId, boolean includeDomainDedications, + BiFunction, Integer>> searchDedicated, Function idGetter) { + if (dedicatedIdsInDc.isEmpty()) { + return; + } + Set allowedIds = new HashSet<>(); + for (DedicatedResourceVO vo : searchDedicated.apply(vmDomainId, vmAccountId).first()) { + allowedIds.add(idGetter.apply(vo)); + } + if (includeDomainDedications) { + for (DedicatedResourceVO vo : searchDedicated.apply(vmDomainId, null).first()) { + allowedIds.add(idGetter.apply(vo)); + } + } + dedicatedIdsInDc.removeAll(allowedIds); + } + private void resetAvoidSet(ExcludeList avoidSet, ExcludeList removeSet) { if (avoidSet.getDataCentersToAvoid() != null && removeSet.getDataCentersToAvoid() != null) { avoidSet.getDataCentersToAvoid().removeAll(removeSet.getDataCentersToAvoid()); From a9c6daf0376d674c8f5b8bbc62c935e2c4246ed4 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Tue, 1 Dec 2026 16:32:11 +0100 Subject: [PATCH 12/12] deploy: let non-explicit VMs use dedications inherited from parent domains --- .../deploy/DeploymentPlanningManagerImpl.java | 49 +++++-- .../DeploymentPlanningManagerImplTest.java | 121 ++++++++++++++++++ 2 files changed, 159 insertions(+), 11 deletions(-) diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index 231fde0c97e6..d97cd41283b9 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -96,6 +96,7 @@ import com.cloud.deploy.DeploymentPlanner.ExcludeList; import com.cloud.deploy.DeploymentPlanner.PlannerResourceUsage; import com.cloud.deploy.dao.PlannerHostReservationDao; +import com.cloud.domain.dao.DomainDao; import com.cloud.exception.AffinityConflictException; import com.cloud.exception.ConnectionException; import com.cloud.exception.InsufficientServerCapacityException; @@ -178,6 +179,8 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy @Inject protected AffinityGroupDomainMapDao _affinityGroupDomainMapDao; @Inject + protected DomainDao _domainDao; + @Inject AffinityGroupService _affinityGroupService; @Inject DataCenterDao _dcDao; @@ -1094,22 +1097,21 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine long vmAccountId = vm.getAccountId(); long vmDomainId = vm.getDomainId(); - // If the VM owner's domain has an ExplicitDedication domain-level affinity group, - // resources dedicated to that domain are accessible to the VM owner as well. - boolean hasDomainExplicitDedicationGroup = - _affinityGroupDao.findDomainLevelGroupByType(vmDomainId, EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE) != null; + // Domains whose domain-level dedications the VM owner may use: its own domain and the ancestors + // whose ExplicitDedication group grants subdomain access. + Set accessibleDedicationDomainIds = findDomainsWithAccessibleExplicitDedication(vmDomainId); // Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account Filter filter = new Filter(DedicatedResourceVO.class, "id", true); // Only query the resource types that have dedicated entries in this DC. - removeDedicatedIdsAccessibleToVm(allPodsInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + removeDedicatedIdsAccessibleToVm(allPodsInDc, vmDomainId, vmAccountId, accessibleDedicationDomainIds, (domainId, accountId) -> _dedicatedDao.searchDedicatedPods(null, domainId, accountId, null, filter), DedicatedResourceVO::getPodId); - removeDedicatedIdsAccessibleToVm(allClustersInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + removeDedicatedIdsAccessibleToVm(allClustersInDc, vmDomainId, vmAccountId, accessibleDedicationDomainIds, (domainId, accountId) -> _dedicatedDao.searchDedicatedClusters(null, domainId, accountId, null, filter), DedicatedResourceVO::getClusterId); - removeDedicatedIdsAccessibleToVm(allHostsInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + removeDedicatedIdsAccessibleToVm(allHostsInDc, vmDomainId, vmAccountId, accessibleDedicationDomainIds, (domainId, accountId) -> _dedicatedDao.searchDedicatedHosts(null, domainId, accountId, null, filter), DedicatedResourceVO::getHostId); @@ -1120,11 +1122,36 @@ private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine avoids.addHostList(allHostsInDc); } + /** + * Returns the domains whose domain-level dedications are available to a VM owned in {@code vmDomainId}: the + * VM's own domain if it has an ExplicitDedication domain-level affinity group, and each ancestor domain whose + * ExplicitDedication group grants subdomain access (as in {@code AffinityGroupService.isAffinityGroupAvailableInDomain}). + */ + protected Set findDomainsWithAccessibleExplicitDedication(long vmDomainId) { + Set accessibleDomainIds = new HashSet<>(); + for (Long domainId : _domainDao.getDomainParentIds(vmDomainId)) { + AffinityGroupVO group = _affinityGroupDao.findDomainLevelGroupByType(domainId, EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE); + if (group == null) { + continue; + } + if (domainId == vmDomainId) { + accessibleDomainIds.add(domainId); + continue; + } + AffinityGroupDomainMapVO domainMap = _affinityGroupDomainMapDao.findByAffinityGroup(group.getId()); + if (domainMap != null && Boolean.TRUE.equals(domainMap.isSubdomainAccess())) { + accessibleDomainIds.add(domainId); + } + } + return accessibleDomainIds; + } + /** * Removes from {@code dedicatedIdsInDc} the ids of the resources the VM owner may use: those dedicated to the - * owner's account and, if {@code includeDomainDedications} is set, those dedicated to the owner's domain. + * owner's account and those dedicated (without an account) to any of {@code accessibleDedicationDomainIds}. + * Resources dedicated to other accounts, in the VM's domain or an ancestor, stay in the list. */ - private void removeDedicatedIdsAccessibleToVm(List dedicatedIdsInDc, long vmDomainId, long vmAccountId, boolean includeDomainDedications, + private void removeDedicatedIdsAccessibleToVm(List dedicatedIdsInDc, long vmDomainId, long vmAccountId, Set accessibleDedicationDomainIds, BiFunction, Integer>> searchDedicated, Function idGetter) { if (dedicatedIdsInDc.isEmpty()) { return; @@ -1133,8 +1160,8 @@ private void removeDedicatedIdsAccessibleToVm(List dedicatedIdsInDc, long for (DedicatedResourceVO vo : searchDedicated.apply(vmDomainId, vmAccountId).first()) { allowedIds.add(idGetter.apply(vo)); } - if (includeDomainDedications) { - for (DedicatedResourceVO vo : searchDedicated.apply(vmDomainId, null).first()) { + for (Long domainId : accessibleDedicationDomainIds) { + for (DedicatedResourceVO vo : searchDedicated.apply(domainId, null).first()) { allowedIds.add(idGetter.apply(vo)); } } diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 36e2565be407..ed57768f19fc 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -36,6 +36,7 @@ import com.cloud.deploy.DeploymentPlanner.ExcludeList; import com.cloud.deploy.DeploymentPlanner.PlannerResourceUsage; import com.cloud.deploy.dao.PlannerHostReservationDao; +import com.cloud.domain.dao.DomainDao; import com.cloud.exception.AffinityConflictException; import com.cloud.exception.InsufficientServerCapacityException; import com.cloud.gpu.GPU; @@ -90,6 +91,7 @@ import com.cloud.vm.dao.VMInstanceDetailsDao; import com.cloud.vm.dao.VMInstanceDao; import org.apache.cloudstack.affinity.AffinityGroupProcessor; +import org.apache.cloudstack.affinity.AffinityGroupDomainMapVO; import org.apache.cloudstack.affinity.AffinityGroupService; import org.apache.cloudstack.affinity.AffinityGroupVO; import org.apache.cloudstack.affinity.dao.AffinityGroupDao; @@ -251,6 +253,12 @@ public class DeploymentPlanningManagerImplTest { @Mock AffinityGroupDao _affinityGroupDao; + @Mock + AffinityGroupDomainMapDao _affinityGroupDomainMapDao; + + @Mock + DomainDao _domainDao; + private static final long dataCenterId = 1L; private static final long instanceId = 123L; private static final long hostId = 0L; @@ -1132,6 +1140,9 @@ private VMInstanceVO setupNonExplicitUserVmMocks(long vmDomainId, long vmAccount Mockito.when(mockVm.getAccountId()).thenReturn(vmAccountId); Mockito.when(mockVm.getId()).thenReturn(instanceId); + // The VM's domain has no parent domain unless a test says otherwise + Mockito.when(_domainDao.getDomainParentIds(vmDomainId)).thenReturn(Set.of(vmDomainId)); + // No dedicated clusters or hosts in DC Mockito.when(_clusterDao.listAllClusterIds(Mockito.anyLong())).thenReturn(new ArrayList<>()); Mockito.when(_dedicatedDao.listAllClusters()).thenReturn(new ArrayList<>()); @@ -1419,6 +1430,111 @@ public void checkForNonDedicatedResources_accountDedicatedClusterAndHost_sameAcc avoids.getHostsToAvoid() != null && avoids.getHostsToAvoid().contains(dedicatedHostId)); } + /** + * Sets up a VM in {@code childDomainId} whose parent domain {@code parentDomainId} has a pod dedicated to it. + * The parent's ExplicitDedication group grants subdomain access as given; the child has no group of its own. + */ + private ExcludeList deployInChildDomainOfPodDedicatedToParent(long parentDomainId, long childDomainId, long vmAccountId, + long dedicatedPodId, boolean parentGroupSubdomainAccess) { + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(childDomainId, vmAccountId); + Mockito.when(_domainDao.getDomainParentIds(childDomainId)).thenReturn(Set.of(childDomainId, parentDomainId)); + + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + long parentGroupId = 77L; + AffinityGroupVO parentGroup = Mockito.mock(AffinityGroupVO.class); + Mockito.when(parentGroup.getId()).thenReturn(parentGroupId); + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(childDomainId, "ExplicitDedication")).thenReturn(null); + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(parentDomainId, "ExplicitDedication")).thenReturn(parentGroup); + Mockito.when(_affinityGroupDomainMapDao.findByAffinityGroup(parentGroupId)) + .thenReturn(new AffinityGroupDomainMapVO(parentGroupId, parentDomainId, parentGroupSubdomainAccess)); + + // The pod is dedicated to the parent domain as a whole (accountId = null) + DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedPod.getPodId()).thenReturn(dedicatedPodId); + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(parentDomainId), + Mockito.isNull(), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedPod)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + return avoids; + } + + /** + * A user in a child domain inherits a pod dedicated to its parent domain, as explicit dedication does + * through subdomain access, so the pod must not be in the avoid list. + */ + @Test + public void checkForNonDedicatedResources_podDedicatedToParentDomain_childDomainUser_podNotInAvoidList() { + long dedicatedPodId = 46L; + ExcludeList avoids = deployInChildDomainOfPodDedicatedToParent(10L, 11L, 210L, dedicatedPodId, true); + + assertFalse("Pod dedicated to the parent domain should not be in avoid list for a child-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * If the parent domain's ExplicitDedication group does not grant subdomain access, + * a child-domain user must stay off the pod dedicated to the parent domain. + */ + @Test + public void checkForNonDedicatedResources_podDedicatedToParentDomainWithoutSubdomainAccess_childDomainUser_podInAvoidList() { + long dedicatedPodId = 47L; + ExcludeList avoids = deployInChildDomainOfPodDedicatedToParent(10L, 11L, 210L, dedicatedPodId, false); + + assertTrue("Pod dedicated to the parent domain without subdomain access should be in avoid list for a child-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * A pod dedicated to an account in the parent domain is not inherited by a child-domain user: + * only domain-wide dedications of ancestor domains are. + */ + @Test + public void checkForNonDedicatedResources_podDedicatedToAccountInParentDomain_childDomainUser_podInAvoidList() { + long parentDomainId = 10L; + long childDomainId = 11L; + long vmAccountId = 210L; + long dedicatedPodId = 48L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(childDomainId, vmAccountId); + Mockito.when(_domainDao.getDomainParentIds(childDomainId)).thenReturn(Set.of(childDomainId, parentDomainId)); + + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // Parent domain has an ExplicitDedication group with subdomain access, but no domain-wide dedications: + // the pod is dedicated to another account in the parent domain, which the default search stub does not return. + long parentGroupId = 78L; + AffinityGroupVO parentGroup = Mockito.mock(AffinityGroupVO.class); + Mockito.when(parentGroup.getId()).thenReturn(parentGroupId); + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(parentDomainId, "ExplicitDedication")).thenReturn(parentGroup); + Mockito.when(_affinityGroupDomainMapDao.findByAffinityGroup(parentGroupId)) + .thenReturn(new AffinityGroupDomainMapVO(parentGroupId, parentDomainId, true)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + assertTrue("Pod dedicated to an account in the parent domain should be in avoid list for a child-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + @Configuration @ComponentScan(basePackageClasses = {DeploymentPlanningManagerImpl.class}, includeFilters = {@Filter(value = TestConfiguration.Library.class, @@ -1581,6 +1697,11 @@ public AffinityGroupDao affinityGroupDao() { return Mockito.mock(AffinityGroupDao.class); } + @Bean + public DomainDao domainDao() { + return Mockito.mock(DomainDao.class); + } + @Bean public AffinityGroupVMMapDao affinityGroupVMMapDao() { return Mockito.mock(AffinityGroupVMMapDao.class);