use of com.cloud.kubernetes.cluster.KubernetesClusterVmMap in project cloudstack by apache.
the class KubernetesClusterDestroyWorker method destroy.
public boolean destroy() throws CloudRuntimeException {
init();
validateClusterSate();
this.clusterVMs = kubernetesClusterVmMapDao.listByClusterId(kubernetesCluster.getId());
boolean cleanupNetwork = true;
final KubernetesClusterDetailsVO clusterDetails = kubernetesClusterDetailsDao.findDetail(kubernetesCluster.getId(), "networkCleanup");
if (clusterDetails != null) {
cleanupNetwork = Boolean.parseBoolean(clusterDetails.getValue());
}
if (cleanupNetwork) {
// if network has additional VM, cannot proceed with cluster destroy
NetworkVO network = networkDao.findById(kubernetesCluster.getNetworkId());
if (network != null) {
List<VMInstanceVO> networkVMs = vmInstanceDao.listNonRemovedVmsByTypeAndNetwork(network.getId(), VirtualMachine.Type.User);
if (networkVMs.size() > clusterVMs.size()) {
logAndThrow(Level.ERROR, String.format("Network : %s for Kubernetes cluster : %s has instances using it which are not part of the Kubernetes cluster", network.getName(), kubernetesCluster.getName()));
}
for (VMInstanceVO vm : networkVMs) {
boolean vmFoundInKubernetesCluster = false;
for (KubernetesClusterVmMap clusterVM : clusterVMs) {
if (vm.getId() == clusterVM.getVmId()) {
vmFoundInKubernetesCluster = true;
break;
}
}
if (!vmFoundInKubernetesCluster) {
logAndThrow(Level.ERROR, String.format("VM : %s which is not a part of Kubernetes cluster : %s is using Kubernetes cluster network : %s", vm.getUuid(), kubernetesCluster.getName(), network.getName()));
}
}
} else {
LOGGER.error(String.format("Failed to find network for Kubernetes cluster : %s", kubernetesCluster.getName()));
}
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info(String.format("Destroying Kubernetes cluster : %s", kubernetesCluster.getName()));
}
stateTransitTo(kubernetesCluster.getId(), KubernetesCluster.Event.DestroyRequested);
boolean vmsDestroyed = destroyClusterVMs();
// if there are VM's that were not expunged, we can not delete the network
if (vmsDestroyed) {
if (cleanupNetwork) {
validateClusterVMsDestroyed();
try {
destroyKubernetesClusterNetwork();
} catch (ManagementServerException e) {
String msg = String.format("Failed to destroy network of Kubernetes cluster : %s cleanup", kubernetesCluster.getName());
LOGGER.warn(msg, e);
updateKubernetesClusterEntryForGC();
throw new CloudRuntimeException(msg, e);
}
} else {
try {
checkForRulesToDelete();
} catch (ManagementServerException e) {
String msg = String.format("Failed to remove network rules of Kubernetes cluster : %s", kubernetesCluster.getName());
LOGGER.warn(msg, e);
updateKubernetesClusterEntryForGC();
throw new CloudRuntimeException(msg, e);
}
}
} else {
String msg = String.format("Failed to destroy one or more VMs as part of Kubernetes cluster : %s cleanup", kubernetesCluster.getName());
LOGGER.warn(msg);
updateKubernetesClusterEntryForGC();
throw new CloudRuntimeException(msg);
}
stateTransitTo(kubernetesCluster.getId(), KubernetesCluster.Event.OperationSucceeded);
annotationDao.removeByEntityType(AnnotationService.EntityType.KUBERNETES_CLUSTER.name(), kubernetesCluster.getUuid());
boolean deleted = kubernetesClusterDao.remove(kubernetesCluster.getId());
if (!deleted) {
logMessage(Level.WARN, String.format("Failed to delete Kubernetes cluster : %s", kubernetesCluster.getName()), null);
updateKubernetesClusterEntryForGC();
return false;
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info(String.format("Kubernetes cluster : %s is successfully deleted", kubernetesCluster.getName()));
}
return true;
}
use of com.cloud.kubernetes.cluster.KubernetesClusterVmMap in project cloudstack by apache.
the class KubernetesClusterDestroyWorker method validateClusterVMsDestroyed.
private void validateClusterVMsDestroyed() {
if (clusterVMs != null && !clusterVMs.isEmpty()) {
// Wait for few seconds to get all VMs really expunged
final int maxRetries = 3;
int retryCounter = 0;
while (retryCounter < maxRetries) {
boolean allVMsRemoved = true;
for (KubernetesClusterVmMap clusterVM : clusterVMs) {
UserVmVO userVM = userVmDao.findById(clusterVM.getVmId());
if (userVM != null && !userVM.isRemoved()) {
allVMsRemoved = false;
break;
}
}
if (allVMsRemoved) {
break;
}
try {
Thread.sleep(10000);
} catch (InterruptedException ie) {
}
retryCounter++;
}
}
}
Aggregations