Skip to content

Commit 48073b3

Browse files
committed
Include recoverVirtualMachine API validation
1 parent 27101f6 commit 48073b3

File tree

5 files changed

+18
-22
lines changed

5 files changed

+18
-22
lines changed

server/src/main/java/com/cloud/vm/UserVmManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,9 @@ static Set<String> getStrictHostTags() {
199199

200200
Boolean getDestroyRootVolumeOnVmDestruction(Long domainId);
201201

202+
/**
203+
* @return true if the VM is part of a CKS cluster, false otherwise.
204+
*/
205+
boolean isVMPartOfAnyCKSCluster(VMInstanceVO vm);
206+
202207
}

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,11 @@ public UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationE
23522352
throw new InvalidParameterValueException("Vm with id " + vm.getUuid() + " is not in the right state");
23532353
}
23542354

2355+
if (isVMPartOfAnyCKSCluster(vm)) {
2356+
throw new UnsupportedServiceException("Cannot recover VM with id = " + vm.getUuid() +
2357+
" as it belongs to a CKS cluster. Please remove the VM from the CKS cluster before recovering.");
2358+
}
2359+
23552360
if (logger.isDebugEnabled()) {
23562361
logger.debug("Recovering vm {}", vm);
23572362
}
@@ -9955,6 +9960,11 @@ public Boolean getDestroyRootVolumeOnVmDestruction(Long domainId){
99559960
return DestroyRootVolumeOnVmDestruction.valueIn(domainId);
99569961
}
99579962

9963+
@Override
9964+
public boolean isVMPartOfAnyCKSCluster(VMInstanceVO vm) {
9965+
return kubernetesServiceHelpers.get(0).findByVmId(vm.getId()) != null;
9966+
}
9967+
99589968
private void setVncPasswordForKvmIfAvailable(Map<String, String> customParameters, UserVmVO vm) {
99599969
if (customParameters.containsKey(VmDetailConstants.KVM_VNC_PASSWORD)
99609970
&& StringUtils.isNotEmpty(customParameters.get(VmDetailConstants.KVM_VNC_PASSWORD))) {

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import com.cloud.hypervisor.Hypervisor;
7272
import com.cloud.hypervisor.HypervisorGuru;
7373
import com.cloud.hypervisor.HypervisorGuruManager;
74-
import com.cloud.kubernetes.cluster.KubernetesServiceHelper;
7574
import com.cloud.network.Network;
7675
import com.cloud.network.NetworkModel;
7776
import com.cloud.network.Networks;
@@ -315,12 +314,6 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
315314
@Inject
316315
private ImportVmTasksManager importVmTasksManager;
317316

318-
private List<KubernetesServiceHelper> kubernetesServiceHelpers;
319-
320-
public void setKubernetesServiceHelpers(final List<KubernetesServiceHelper> kubernetesServiceHelpers) {
321-
this.kubernetesServiceHelpers = kubernetesServiceHelpers;
322-
}
323-
324317
protected Gson gson;
325318

326319
public UnmanagedVMsManagerImpl() {
@@ -2386,16 +2379,12 @@ void performUnmanageVMInstancePrechecks(VMInstanceVO vmVO) {
23862379
" as there is an ISO attached. Please detach ISO before unmanaging.");
23872380
}
23882381

2389-
if (isVmPartOfCKSCluster(vmVO)) {
2382+
if (userVmManager.isVMPartOfAnyCKSCluster(vmVO)) {
23902383
throw new UnsupportedServiceException("Cannot unmanage VM with id = " + vmVO.getUuid() +
23912384
" as it belongs to a CKS cluster. Please remove the VM from the CKS cluster before unmanaging.");
23922385
}
23932386
}
23942387

2395-
private boolean isVmPartOfCKSCluster(VMInstanceVO vmVO) {
2396-
return kubernetesServiceHelpers.get(0).findByVmId(vmVO.getId()) != null;
2397-
}
2398-
23992388
private boolean hasVolumeSnapshotsPriorToUnmanageVM(VMInstanceVO vmVO) {
24002389
List<VolumeVO> volumes = volumeDao.findByInstance(vmVO.getId());
24012390
for (VolumeVO volume : volumes) {

server/src/main/resources/META-INF/cloudstack/server-compute/spring-server-compute-context.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
<property name="name" value="LXCGuru" />
3636
</bean>
3737

38-
<bean id="vmImportService" class="org.apache.cloudstack.vm.UnmanagedVMsManagerImpl">
39-
<property name="kubernetesServiceHelpers" value="#{kubernetesServiceHelperRegistry.registered}" />
40-
</bean>
38+
<bean id="vmImportService" class="org.apache.cloudstack.vm.UnmanagedVMsManagerImpl" />
4139

4240
<bean id="importVmTasksManager" class="org.apache.cloudstack.vm.ImportVmTasksManagerImpl" />
4341

server/src/test/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImplTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@
3939
import java.util.Map;
4040
import java.util.UUID;
4141

42-
import com.cloud.kubernetes.cluster.KubernetesServiceHelper;
4342
import com.cloud.offering.DiskOffering;
4443
import com.cloud.storage.Snapshot;
4544
import com.cloud.storage.SnapshotVO;
4645
import com.cloud.storage.dao.SnapshotDao;
4746
import com.cloud.vm.ImportVMTaskVO;
48-
import org.apache.cloudstack.acl.ControlledEntity;
4947
import org.apache.cloudstack.api.ApiConstants;
5048
import org.apache.cloudstack.api.ResponseGenerator;
5149
import org.apache.cloudstack.api.ResponseObject;
@@ -248,8 +246,6 @@ public class UnmanagedVMsManagerImplTest {
248246
@Mock
249247
private ImportVmTasksManager importVmTasksManager;
250248
@Mock
251-
private KubernetesServiceHelper kubernetesServiceHelper;
252-
@Mock
253249
private SnapshotDao snapshotDao;
254250

255251
@Mock
@@ -289,8 +285,6 @@ public void setUp() throws Exception {
289285
UserVO user = new UserVO(1, "adminuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
290286
CallContext.register(user, account);
291287

292-
unmanagedVMsManager.setKubernetesServiceHelpers(List.of(kubernetesServiceHelper));
293-
294288
instance = new UnmanagedInstanceTO();
295289
instance.setName("TestInstance");
296290
instance.setCpuCores(2);
@@ -623,7 +617,7 @@ public void testUnmanageVMInstanceBelongingToCksClusterFail() {
623617
when(userVmVO.getIsoId()).thenReturn(null);
624618
when(userVmDao.findById(anyLong())).thenReturn(userVmVO);
625619
when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine);
626-
when(kubernetesServiceHelper.findByVmId(virtualMachineId)).thenReturn(mock(ControlledEntity.class));
620+
when(userVmManager.isVMPartOfAnyCKSCluster(virtualMachine)).thenReturn(true);
627621
unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false);
628622
}
629623

0 commit comments

Comments
 (0)