Search in sources :

Example 1 with KubernetesClusterVmMap

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;
}
Also used : KubernetesClusterDetailsVO(com.cloud.kubernetes.cluster.KubernetesClusterDetailsVO) NetworkVO(com.cloud.network.dao.NetworkVO) ManagementServerException(com.cloud.exception.ManagementServerException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMInstanceVO(com.cloud.vm.VMInstanceVO) KubernetesClusterVmMap(com.cloud.kubernetes.cluster.KubernetesClusterVmMap)

Example 2 with KubernetesClusterVmMap

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++;
        }
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) KubernetesClusterVmMap(com.cloud.kubernetes.cluster.KubernetesClusterVmMap)

Aggregations

KubernetesClusterVmMap (com.cloud.kubernetes.cluster.KubernetesClusterVmMap)2 ManagementServerException (com.cloud.exception.ManagementServerException)1 KubernetesClusterDetailsVO (com.cloud.kubernetes.cluster.KubernetesClusterDetailsVO)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 UserVmVO (com.cloud.vm.UserVmVO)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1