Search in sources :

Example 11 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cloudstack by apache.

the class KubernetesClusterStartWorker method setupKubernetesClusterNetworkRules.

/**
 * Setup network rules for Kubernetes cluster
 * Open up firewall port CLUSTER_API_PORT, secure port on which Kubernetes
 * API server is running. Also create load balancing rule to forward public
 * IP traffic to control VMs' private IP.
 * Open up  firewall ports NODES_DEFAULT_START_SSH_PORT to NODES_DEFAULT_START_SSH_PORT+n
 * for SSH access. Also create port-forwarding rule to forward public IP traffic to all
 * @param network
 * @param clusterVMs
 * @throws ManagementServerException
 */
private void setupKubernetesClusterNetworkRules(Network network, List<UserVm> clusterVMs) throws ManagementServerException {
    if (!Network.GuestType.Isolated.equals(network.getGuestType())) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(String.format("Network : %s for Kubernetes cluster : %s is not an isolated network, therefore, no need for network rules", network.getName(), kubernetesCluster.getName()));
        }
        return;
    }
    List<Long> clusterVMIds = new ArrayList<>();
    for (UserVm vm : clusterVMs) {
        clusterVMIds.add(vm.getId());
    }
    IpAddress publicIp = getSourceNatIp(network);
    if (publicIp == null) {
        throw new ManagementServerException(String.format("No source NAT IP addresses found for network : %s, Kubernetes cluster : %s", network.getName(), kubernetesCluster.getName()));
    }
    createFirewallRules(publicIp, clusterVMIds);
    // Load balancer rule fo API access for control node VMs
    try {
        provisionLoadBalancerRule(publicIp, network, owner, clusterVMIds, CLUSTER_API_PORT);
    } catch (NetworkRuleConflictException | InsufficientAddressCapacityException e) {
        throw new ManagementServerException(String.format("Failed to provision load balancer rule for API access for the Kubernetes cluster : %s", kubernetesCluster.getName()), e);
    }
    // Port forwarding rule fo SSH access on each node VM
    try {
        provisionSshPortForwardingRules(publicIp, network, owner, clusterVMIds, CLUSTER_NODES_DEFAULT_START_SSH_PORT);
    } catch (ResourceUnavailableException | NetworkRuleConflictException e) {
        throw new ManagementServerException(String.format("Failed to activate SSH port forwarding rules for the Kubernetes cluster : %s", kubernetesCluster.getName()), e);
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ManagementServerException(com.cloud.exception.ManagementServerException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ArrayList(java.util.ArrayList) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IpAddress(com.cloud.network.IpAddress) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException)

Example 12 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cloudstack by apache.

the class KubernetesClusterDestroyWorker method deleteKubernetesClusterNetworkRules.

private void deleteKubernetesClusterNetworkRules() throws ManagementServerException {
    NetworkVO network = networkDao.findById(kubernetesCluster.getNetworkId());
    if (network == null) {
        return;
    }
    List<Long> removedVmIds = new ArrayList<>();
    if (!CollectionUtils.isEmpty(clusterVMs)) {
        for (KubernetesClusterVmMapVO clusterVM : clusterVMs) {
            removedVmIds.add(clusterVM.getVmId());
        }
    }
    IpAddress publicIp = getSourceNatIp(network);
    if (publicIp == null) {
        throw new ManagementServerException(String.format("No source NAT IP addresses found for network : %s", network.getName()));
    }
    try {
        removeLoadBalancingRule(publicIp, network, owner, CLUSTER_API_PORT);
    } catch (ResourceUnavailableException e) {
        throw new ManagementServerException(String.format("Failed to KubernetesCluster load balancing rule for network : %s", network.getName()));
    }
    FirewallRule firewallRule = removeApiFirewallRule(publicIp);
    if (firewallRule == null) {
        logMessage(Level.WARN, "Firewall rule for API access can't be removed", null);
    }
    firewallRule = removeSshFirewallRule(publicIp);
    if (firewallRule == null) {
        logMessage(Level.WARN, "Firewall rule for SSH access can't be removed", null);
    }
    try {
        removePortForwardingRules(publicIp, network, owner, removedVmIds);
    } catch (ResourceUnavailableException e) {
        throw new ManagementServerException(String.format("Failed to KubernetesCluster port forwarding rules for network : %s", network.getName()));
    }
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) ManagementServerException(com.cloud.exception.ManagementServerException) ArrayList(java.util.ArrayList) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IpAddress(com.cloud.network.IpAddress) FirewallRule(com.cloud.network.rules.FirewallRule) KubernetesClusterVmMapVO(com.cloud.kubernetes.cluster.KubernetesClusterVmMapVO)

Example 13 with ManagementServerException

use of com.cloud.exception.ManagementServerException 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 14 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cloudstack by apache.

the class ScaleSystemVMCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("SystemVm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId()));
    ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
    if (serviceOffering == null) {
        throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId);
    }
    VirtualMachine result = null;
    try {
        result = _mgr.upgradeSystemVM(this);
    } catch (ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (ManagementServerException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (VirtualMachineMigrationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
    if (result != null) {
        SystemVmResponse response = _responseGenerator.createSystemVmResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to upgrade system vm");
    }
}
Also used : SystemVmResponse(org.apache.cloudstack.api.response.SystemVmResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ManagementServerException(com.cloud.exception.ManagementServerException) ServiceOffering(com.cloud.offering.ServiceOffering) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 15 with ManagementServerException

use of com.cloud.exception.ManagementServerException in project cloudstack by apache.

the class ScaleVMCmdByAdmin method execute.

@Override
public void execute() {
    UserVm result;
    try {
        result = _userVmService.upgradeVirtualMachine(this);
    } catch (ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (ManagementServerException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (VirtualMachineMigrationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
    if (result != null) {
        List<UserVmResponse> responseList = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", result);
        UserVmResponse response = responseList.get(0);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to scale vm");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(org.apache.cloudstack.api.ServerApiException) ManagementServerException(com.cloud.exception.ManagementServerException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse)

Aggregations

ManagementServerException (com.cloud.exception.ManagementServerException)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)25 UserVm (com.cloud.uservm.UserVm)18 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)17 VirtualMachineMigrationException (com.cloud.exception.VirtualMachineMigrationException)16 VirtualMachine (com.cloud.vm.VirtualMachine)10 ServerApiException (com.cloud.api.ServerApiException)8 Host (com.cloud.host.Host)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)6 ServerApiException (org.apache.cloudstack.api.ServerApiException)6 UserVmResponse (com.cloud.api.response.UserVmResponse)5 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)5 ArrayList (java.util.ArrayList)5 StoragePool (com.cloud.storage.StoragePool)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)4 UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)4 NetworkRuleConflictException (com.cloud.exception.NetworkRuleConflictException)3 KubernetesClusterVmMapVO (com.cloud.kubernetes.cluster.KubernetesClusterVmMapVO)3 IpAddress (com.cloud.network.IpAddress)3