Search in sources :

Example 21 with ManagementServerException

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

the class KubernetesClusterStartWorker method createFirewallRules.

private void createFirewallRules(IpAddress publicIp, List<Long> clusterVMIds) throws ManagementServerException {
    // Firewall rule fo API access for control node VMs
    try {
        provisionFirewallRules(publicIp, owner, CLUSTER_API_PORT, CLUSTER_API_PORT);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("Provisioned firewall rule to open up port %d on %s for Kubernetes cluster %s", CLUSTER_API_PORT, publicIp.getAddress().addr(), kubernetesCluster.getName()));
        }
    } catch (NoSuchFieldException | IllegalAccessException | ResourceUnavailableException | NetworkRuleConflictException e) {
        throw new ManagementServerException(String.format("Failed to provision firewall rules for API access for the Kubernetes cluster : %s", kubernetesCluster.getName()), e);
    }
    // Firewall rule fo SSH access on each node VM
    try {
        int endPort = CLUSTER_NODES_DEFAULT_START_SSH_PORT + clusterVMIds.size() - 1;
        provisionFirewallRules(publicIp, owner, CLUSTER_NODES_DEFAULT_START_SSH_PORT, endPort);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("Provisioned firewall rule to open up port %d to %d on %s for Kubernetes cluster : %s", CLUSTER_NODES_DEFAULT_START_SSH_PORT, endPort, publicIp.getAddress().addr(), kubernetesCluster.getName()));
        }
    } catch (NoSuchFieldException | IllegalAccessException | ResourceUnavailableException | NetworkRuleConflictException e) {
        throw new ManagementServerException(String.format("Failed to provision firewall rules for SSH access for the Kubernetes cluster : %s", kubernetesCluster.getName()), e);
    }
}
Also used : ManagementServerException(com.cloud.exception.ManagementServerException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException)

Example 22 with ManagementServerException

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

the class KubernetesClusterDestroyWorker method destroyKubernetesClusterNetwork.

private void destroyKubernetesClusterNetwork() throws ManagementServerException {
    NetworkVO network = networkDao.findById(kubernetesCluster.getNetworkId());
    if (network != null && network.getRemoved() == null) {
        Account owner = accountManager.getAccount(network.getAccountId());
        User callerUser = accountManager.getActiveUser(CallContext.current().getCallingUserId());
        ReservationContext context = new ReservationContextImpl(null, null, callerUser, owner);
        boolean networkDestroyed = networkMgr.destroyNetwork(kubernetesCluster.getNetworkId(), context, true);
        if (!networkDestroyed) {
            String msg = String.format("Failed to destroy network : %s as part of Kubernetes cluster : %s cleanup", network.getName(), kubernetesCluster.getName());
            LOGGER.warn(msg);
            throw new ManagementServerException(msg);
        }
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("Destroyed network : %s as part of Kubernetes cluster : %s cleanup", network.getName(), kubernetesCluster.getName()));
        }
    }
}
Also used : Account(com.cloud.user.Account) NetworkVO(com.cloud.network.dao.NetworkVO) User(com.cloud.user.User) ManagementServerException(com.cloud.exception.ManagementServerException) ReservationContextImpl(com.cloud.vm.ReservationContextImpl) ReservationContext(com.cloud.vm.ReservationContext)

Example 23 with ManagementServerException

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

the class KubernetesClusterScaleWorker method removeNodesFromCluster.

private void removeNodesFromCluster(List<KubernetesClusterVmMapVO> vmMaps) throws CloudRuntimeException {
    for (KubernetesClusterVmMapVO vmMapVO : vmMaps) {
        UserVmVO userVM = userVmDao.findById(vmMapVO.getVmId());
        LOGGER.info(String.format("Removing vm : %s from cluster %s", userVM.getDisplayName(), kubernetesCluster.getName()));
        if (!removeKubernetesClusterNode(publicIpAddress, sshPort, userVM, 3, 30000)) {
            logTransitStateAndThrow(Level.ERROR, String.format("Scaling failed for Kubernetes cluster : %s, failed to remove Kubernetes node: %s running on VM : %s", kubernetesCluster.getName(), userVM.getHostName(), userVM.getDisplayName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed);
        }
        try {
            UserVm vm = userVmService.destroyVm(userVM.getId(), true);
            if (!userVmManager.expunge(userVM, CallContext.current().getCallingUserId(), CallContext.current().getCallingAccount())) {
                logTransitStateAndThrow(Level.ERROR, String.format("Scaling Kubernetes cluster %s failed, unable to expunge VM '%s'.", kubernetesCluster.getName(), vm.getDisplayName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed);
            }
        } catch (ResourceUnavailableException e) {
            logTransitStateAndThrow(Level.ERROR, String.format("Scaling Kubernetes cluster %s failed, unable to remove VM ID: %s", kubernetesCluster.getName(), userVM.getDisplayName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed, e);
        }
        kubernetesClusterVmMapDao.expunge(vmMapVO.getId());
        if (System.currentTimeMillis() > scaleTimeoutTime) {
            logTransitStateAndThrow(Level.WARN, String.format("Scaling Kubernetes cluster %s failed, scaling action timed out", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed);
        }
    }
    // Scale network rules to update firewall rule
    try {
        List<Long> clusterVMIds = getKubernetesClusterVMMaps().stream().map(KubernetesClusterVmMapVO::getVmId).collect(Collectors.toList());
        scaleKubernetesClusterNetworkRules(clusterVMIds);
    } catch (ManagementServerException e) {
        logTransitStateAndThrow(Level.ERROR, String.format("Scaling failed for Kubernetes cluster : %s, unable to update network rules", kubernetesCluster.getName()), kubernetesCluster.getId(), KubernetesCluster.Event.OperationFailed, e);
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) UserVm(com.cloud.uservm.UserVm) ManagementServerException(com.cloud.exception.ManagementServerException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) KubernetesClusterVmMapVO(com.cloud.kubernetes.cluster.KubernetesClusterVmMapVO)

Example 24 with ManagementServerException

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

the class KubernetesClusterScaleWorker method scaleKubernetesClusterNetworkRules.

/**
 * Scale network rules for an existing Kubernetes cluster while scaling it
 * Open up firewall for SSH access from port NODES_DEFAULT_START_SSH_PORT to NODES_DEFAULT_START_SSH_PORT+n.
 * Also remove port forwarding rules for all virtual machines and re-create port-forwarding rule
 * to forward public IP traffic to all node VMs' private IP.
 * @param clusterVMIds
 * @throws ManagementServerException
 */
private void scaleKubernetesClusterNetworkRules(final List<Long> clusterVMIds) 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;
    }
    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()));
    }
    // Remove existing SSH firewall rules
    FirewallRule firewallRule = removeSshFirewallRule(publicIp);
    if (firewallRule == null) {
        throw new ManagementServerException("Firewall rule for node SSH access can't be provisioned");
    }
    int existingFirewallRuleSourcePortEnd = firewallRule.getSourcePortEnd();
    int endPort = CLUSTER_NODES_DEFAULT_START_SSH_PORT + clusterVMIds.size() - 1;
    // Provision new SSH firewall rules
    try {
        provisionFirewallRules(publicIp, owner, CLUSTER_NODES_DEFAULT_START_SSH_PORT, endPort);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(String.format("Provisioned  firewall rule to open up port %d to %d on %s in Kubernetes cluster %s", CLUSTER_NODES_DEFAULT_START_SSH_PORT, endPort, publicIp.getAddress().addr(), kubernetesCluster.getName()));
        }
    } catch (NoSuchFieldException | IllegalAccessException | ResourceUnavailableException e) {
        throw new ManagementServerException(String.format("Failed to activate SSH firewall rules for the Kubernetes cluster : %s", kubernetesCluster.getName()), e);
    }
    try {
        removePortForwardingRules(publicIp, network, owner, CLUSTER_NODES_DEFAULT_START_SSH_PORT, existingFirewallRuleSourcePortEnd);
    } catch (ResourceUnavailableException e) {
        throw new ManagementServerException(String.format("Failed to remove SSH port forwarding rules for removed VMs for the Kubernetes cluster : %s", kubernetesCluster.getName()), e);
    }
    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 : ManagementServerException(com.cloud.exception.ManagementServerException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IpAddress(com.cloud.network.IpAddress) FirewallRule(com.cloud.network.rules.FirewallRule) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException)

Example 25 with ManagementServerException

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

the class KubernetesClusterScaleWorker method scaleUpKubernetesClusterSize.

private void scaleUpKubernetesClusterSize(final long newVmCount) throws CloudRuntimeException {
    if (!kubernetesCluster.getState().equals(KubernetesCluster.State.Scaling)) {
        stateTransitTo(kubernetesCluster.getId(), KubernetesCluster.Event.ScaleUpRequested);
    }
    List<UserVm> clusterVMs = new ArrayList<>();
    LaunchPermissionVO launchPermission = new LaunchPermissionVO(clusterTemplate.getId(), owner.getId());
    launchPermissionDao.persist(launchPermission);
    try {
        clusterVMs = provisionKubernetesClusterNodeVms((int) (newVmCount + kubernetesCluster.getNodeCount()), (int) kubernetesCluster.getNodeCount(), publicIpAddress);
        updateLoginUserDetails(clusterVMs.stream().map(InternalIdentity::getId).collect(Collectors.toList()));
    } catch (CloudRuntimeException | ManagementServerException | ResourceUnavailableException | InsufficientCapacityException e) {
        logTransitStateToFailedIfNeededAndThrow(Level.ERROR, String.format("Scaling failed for Kubernetes cluster : %s, unable to provision node VM in the cluster", kubernetesCluster.getName()), e);
    }
    try {
        List<Long> clusterVMIds = getKubernetesClusterVMMaps().stream().map(KubernetesClusterVmMapVO::getVmId).collect(Collectors.toList());
        scaleKubernetesClusterNetworkRules(clusterVMIds);
    } catch (ManagementServerException e) {
        logTransitStateToFailedIfNeededAndThrow(Level.ERROR, String.format("Scaling failed for Kubernetes cluster : %s, unable to update network rules", kubernetesCluster.getName()), e);
    }
    attachIsoKubernetesVMs(clusterVMs);
    KubernetesClusterVO kubernetesClusterVO = kubernetesClusterDao.findById(kubernetesCluster.getId());
    kubernetesClusterVO.setNodeCount(clusterSize);
    boolean readyNodesCountValid = KubernetesClusterUtil.validateKubernetesClusterReadyNodesCount(kubernetesClusterVO, publicIpAddress, sshPort, getControlNodeLoginUser(), sshKeyFile, scaleTimeoutTime, 15000);
    detachIsoKubernetesVMs(clusterVMs);
    deleteTemplateLaunchPermission();
    if (!readyNodesCountValid) {
        // Scaling failed
        logTransitStateToFailedIfNeededAndThrow(Level.ERROR, String.format("Scaling unsuccessful for Kubernetes cluster : %s as it does not have desired number of nodes in ready state", kubernetesCluster.getName()));
    }
}
Also used : ArrayList(java.util.ArrayList) KubernetesClusterVO(com.cloud.kubernetes.cluster.KubernetesClusterVO) LaunchPermissionVO(com.cloud.storage.LaunchPermissionVO) UserVm(com.cloud.uservm.UserVm) ManagementServerException(com.cloud.exception.ManagementServerException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InternalIdentity(org.apache.cloudstack.api.InternalIdentity)

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