Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import com.cloud.hypervisor.Hypervisor;
import com.cloud.hypervisor.HypervisorGuru;
import com.cloud.hypervisor.HypervisorGuruManager;
import com.cloud.kubernetes.cluster.KubernetesServiceHelper;
import com.cloud.network.Network;
import com.cloud.network.NetworkModel;
import com.cloud.network.Networks;
Expand Down Expand Up @@ -313,6 +314,8 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
private DataStoreManager dataStoreManager;
@Inject
private ImportVmTasksManager importVmTasksManager;
@Inject
private KubernetesServiceHelper kubernetesServiceHelper;

protected Gson gson;

Expand Down Expand Up @@ -2365,6 +2368,8 @@ public List<Class<?>> getCommands() {
* Perform validations before attempting to unmanage a VM from CloudStack:
* - VM must not have any associated volume snapshot
* - VM must not have an attached ISO
* - VM must not belong to any CKS cluster
* @throws UnsupportedServiceException in case any of the validations above fail
*/
void performUnmanageVMInstancePrechecks(VMInstanceVO vmVO) {
if (hasVolumeSnapshotsPriorToUnmanageVM(vmVO)) {
Expand All @@ -2376,6 +2381,15 @@ void performUnmanageVMInstancePrechecks(VMInstanceVO vmVO) {
throw new UnsupportedServiceException("Cannot unmanage VM with id = " + vmVO.getUuid() +
" as there is an ISO attached. Please detach ISO before unmanaging.");
}

if (isVmPartOfCKSCluster(vmVO)) {
throw new UnsupportedServiceException("Cannot unmanage VM with id = " + vmVO.getUuid() +
" as it belongs to a CKS cluster. Please remove the VM from the CKS cluster before unmanaging.");
}
}

private boolean isVmPartOfCKSCluster(VMInstanceVO vmVO) {
return kubernetesServiceHelper.findByVmId(vmVO.getId()) != null;
}

private boolean hasVolumeSnapshotsPriorToUnmanageVM(VMInstanceVO vmVO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.cloud.kubernetes.cluster.KubernetesServiceHelper;
import com.cloud.offering.DiskOffering;
import com.cloud.storage.Snapshot;
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.dao.SnapshotDao;
import com.cloud.vm.ImportVMTaskVO;
import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.ResponseObject;
Expand Down Expand Up @@ -241,6 +247,10 @@ public class UnmanagedVMsManagerImplTest {
private StoragePoolHostDao storagePoolHostDao;
@Mock
private ImportVmTasksManager importVmTasksManager;
@Mock
private KubernetesServiceHelper kubernetesServiceHelper;
@Mock
private SnapshotDao snapshotDao;

@Mock
private VMInstanceVO virtualMachine;
Expand Down Expand Up @@ -568,6 +578,53 @@ public void unmanageVMInstanceStoppedInstanceTest() {
unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false);
}

@Test(expected = UnsupportedServiceException.class)
public void testUnmanageVMInstanceWithVolumeSnapshotsFail() {
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User);
when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM);
when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped);
when(virtualMachine.getId()).thenReturn(virtualMachineId);
UserVmVO userVmVO = mock(UserVmVO.class);
when(userVmDao.findById(anyLong())).thenReturn(userVmVO);
when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine);
VolumeVO volumeVO = mock(VolumeVO.class);
long volumeId = 20L;
when(volumeVO.getId()).thenReturn(volumeId);
SnapshotVO snapshotVO = mock(SnapshotVO.class);
when(snapshotVO.getState()).thenReturn(Snapshot.State.BackedUp);
when(snapshotDao.listByVolumeId(volumeId)).thenReturn(Collections.singletonList(snapshotVO));
when(volumeDao.findByInstance(virtualMachineId)).thenReturn(Collections.singletonList(volumeVO));
unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false);
}

@Test(expected = UnsupportedServiceException.class)
public void testUnmanageVMInstanceWithAssociatedIsoFail() {
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User);
when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM);
when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped);
when(virtualMachine.getId()).thenReturn(virtualMachineId);
UserVmVO userVmVO = mock(UserVmVO.class);
when(userVmVO.getIsoId()).thenReturn(null);
when(userVmDao.findById(anyLong())).thenReturn(userVmVO);
when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine);
when(userVmVO.getIsoId()).thenReturn(1L);
unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false);
}

@Test(expected = UnsupportedServiceException.class)
public void testUnmanageVMInstanceBelongingToCksClusterFail() {
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User);
when(virtualMachine.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM);
when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Stopped);
when(virtualMachine.getId()).thenReturn(virtualMachineId);
UserVmVO userVmVO = mock(UserVmVO.class);
when(userVmVO.getIsoId()).thenReturn(null);
when(userVmDao.findById(anyLong())).thenReturn(userVmVO);
when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine);
when(kubernetesServiceHelper.findByVmId(virtualMachineId)).thenReturn(mock(ControlledEntity.class));
unmanagedVMsManager.unmanageVMInstance(virtualMachineId, null, false);
}

@Test
public void testListRemoteInstancesTest() {
ListVmsForImportCmd cmd = Mockito.mock(ListVmsForImportCmd.class);
Expand Down
Loading