Search in sources :

Example 1 with LoadBalancerTO

use of com.cloud.agent.api.to.LoadBalancerTO in project cloudstack by apache.

the class ExternalLoadBalancerDeviceManagerImpl method applyLoadBalancerRules.

@Override
public boolean applyLoadBalancerRules(Network network, List<LoadBalancingRule> loadBalancingRules) throws ResourceUnavailableException {
    // Find the external load balancer in this zone
    long zoneId = network.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    if (loadBalancingRules == null || loadBalancingRules.isEmpty()) {
        return true;
    }
    ExternalLoadBalancerDeviceVO lbDeviceVO = getExternalLoadBalancerForNetwork(network);
    if (lbDeviceVO == null) {
        s_logger.warn("There is no external load balancer device assigned to this network either network is not implement are already shutdown so just returning");
        return true;
    }
    HostVO externalLoadBalancer = _hostDao.findById(lbDeviceVO.getHostId());
    boolean externalLoadBalancerIsInline = _networkMgr.isNetworkInlineMode(network);
    if (network.getState() == Network.State.Allocated) {
        s_logger.debug("External load balancer was asked to apply LB rules for network with ID " + network.getId() + "; this network is not implemented. Skipping backend commands.");
        return true;
    }
    List<LoadBalancerTO> loadBalancersToApply = new ArrayList<LoadBalancerTO>();
    List<MappingState> mappingStates = new ArrayList<MappingState>();
    for (int i = 0; i < loadBalancingRules.size(); i++) {
        LoadBalancingRule rule = loadBalancingRules.get(i);
        boolean revoked = (rule.getState().equals(FirewallRule.State.Revoke));
        String protocol = rule.getProtocol();
        String algorithm = rule.getAlgorithm();
        String uuid = rule.getUuid();
        String srcIp = rule.getSourceIp().addr();
        int srcPort = rule.getSourcePortStart();
        List<LbDestination> destinations = rule.getDestinations();
        if (externalLoadBalancerIsInline) {
            long ipId = _networkModel.getPublicIpAddress(rule.getSourceIp().addr(), network.getDataCenterId()).getId();
            MappingNic nic = getLoadBalancingIpNic(zone, network, ipId, revoked, null);
            mappingStates.add(nic.getState());
            Nic loadBalancingIpNic = nic.getNic();
            if (loadBalancingIpNic == null) {
                continue;
            }
            // Change the source IP address for the load balancing rule to be the load balancing IP address
            srcIp = loadBalancingIpNic.getIPv4Address();
        }
        if ((destinations != null && !destinations.isEmpty()) || rule.isAutoScaleConfig()) {
            boolean inline = _networkMgr.isNetworkInlineMode(network);
            LoadBalancerTO loadBalancer = new LoadBalancerTO(uuid, srcIp, srcPort, protocol, algorithm, revoked, false, inline, destinations, rule.getStickinessPolicies(), rule.getHealthCheckPolicies(), rule.getLbSslCert(), rule.getLbProtocol());
            if (rule.isAutoScaleConfig()) {
                loadBalancer.setAutoScaleVmGroup(rule.getAutoScaleVmGroup());
            }
            loadBalancersToApply.add(loadBalancer);
        }
    }
    try {
        if (loadBalancersToApply.size() > 0) {
            int numLoadBalancersForCommand = loadBalancersToApply.size();
            LoadBalancerTO[] loadBalancersForCommand = loadBalancersToApply.toArray(new LoadBalancerTO[numLoadBalancersForCommand]);
            LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(loadBalancersForCommand, null);
            long guestVlanTag = Integer.parseInt(BroadcastDomainType.getValue(network.getBroadcastUri()));
            cmd.setAccessDetail(NetworkElementCommand.GUEST_VLAN_TAG, String.valueOf(guestVlanTag));
            Answer answer = _agentMgr.easySend(externalLoadBalancer.getId(), cmd);
            if (answer == null || !answer.getResult()) {
                String details = (answer != null) ? answer.getDetails() : "details unavailable";
                String msg = "Unable to apply load balancer rules to the external load balancer appliance in zone " + zone.getName() + " due to: " + details + ".";
                s_logger.error(msg);
                throw new ResourceUnavailableException(msg, DataCenter.class, network.getDataCenterId());
            }
        }
    } catch (Exception ex) {
        if (externalLoadBalancerIsInline) {
            s_logger.error("Rollbacking static nat operation of inline mode load balancing due to error on applying LB rules!");
            String existedGuestIp = loadBalancersToApply.get(0).getSrcIp();
            // Rollback static NAT operation in current session
            for (int i = 0; i < loadBalancingRules.size(); i++) {
                LoadBalancingRule rule = loadBalancingRules.get(i);
                MappingState state = mappingStates.get(i);
                boolean revoke;
                if (state == MappingState.Create) {
                    revoke = true;
                } else if (state == MappingState.Remove) {
                    revoke = false;
                } else {
                    continue;
                }
                long sourceIpId = _networkModel.getPublicIpAddress(rule.getSourceIp().addr(), network.getDataCenterId()).getId();
                getLoadBalancingIpNic(zone, network, sourceIpId, revoke, existedGuestIp);
            }
        }
        throw new ResourceUnavailableException(ex.getMessage(), DataCenter.class, network.getDataCenterId());
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) ArrayList(java.util.ArrayList) Nic(com.cloud.vm.Nic) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) HostVO(com.cloud.host.HostVO) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) DestroyLoadBalancerApplianceAnswer(com.cloud.network.resource.DestroyLoadBalancerApplianceAnswer) Answer(com.cloud.agent.api.Answer) CreateLoadBalancerApplianceAnswer(com.cloud.network.resource.CreateLoadBalancerApplianceAnswer) HealthCheckLBConfigAnswer(com.cloud.agent.api.routing.HealthCheckLBConfigAnswer) DataCenter(com.cloud.dc.DataCenter) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand)

Example 2 with LoadBalancerTO

use of com.cloud.agent.api.to.LoadBalancerTO in project cloudstack by apache.

the class HAProxyConfigurator method generateConfiguration.

@Override
public String[] generateConfiguration(final LoadBalancerConfigCommand lbCmd) {
    final List<String> result = new ArrayList<String>();
    final List<String> gSection = Arrays.asList(globalSection);
    //        note that this is overwritten on the String in the static ArrayList<String>
    gSection.set(2, "\tmaxconn " + lbCmd.maxconn);
    // TODO DH: write test for this function
    final String pipesLine = "\tmaxpipes " + Long.toString(Long.parseLong(lbCmd.maxconn) / 4);
    gSection.set(3, pipesLine);
    if (s_logger.isDebugEnabled()) {
        for (final String s : gSection) {
            s_logger.debug("global section: " + s);
        }
    }
    result.addAll(gSection);
    // TODO decide under what circumstances these options are needed
    //        result.add("\tnokqueue");
    //        result.add("\tnopoll");
    result.add(blankLine);
    final List<String> dSection = Arrays.asList(defaultsSection);
    if (lbCmd.keepAliveEnabled) {
        dSection.set(7, "\tno option forceclose");
    }
    if (s_logger.isDebugEnabled()) {
        for (final String s : dSection) {
            s_logger.debug("default section: " + s);
        }
    }
    result.addAll(dSection);
    if (!lbCmd.lbStatsVisibility.equals("disabled")) {
        /* new rule : listen admin_page guestip/link-local:8081 */
        if (lbCmd.lbStatsVisibility.equals("global")) {
            result.add(generateStatsRule(lbCmd, "stats_on_public", lbCmd.lbStatsPublicIP));
        } else if (lbCmd.lbStatsVisibility.equals("guest-network")) {
            result.add(generateStatsRule(lbCmd, "stats_on_guest", lbCmd.lbStatsGuestIP));
        } else if (lbCmd.lbStatsVisibility.equals("link-local")) {
            result.add(generateStatsRule(lbCmd, "stats_on_private", lbCmd.lbStatsPrivateIP));
        } else if (lbCmd.lbStatsVisibility.equals("all")) {
            result.add(generateStatsRule(lbCmd, "stats_on_public", lbCmd.lbStatsPublicIP));
            result.add(generateStatsRule(lbCmd, "stats_on_guest", lbCmd.lbStatsGuestIP));
            result.add(generateStatsRule(lbCmd, "stats_on_private", lbCmd.lbStatsPrivateIP));
        } else {
            /*
                 * stats will be available on the default http serving port, no
                 * special stats port
                 */
            final StringBuilder subRule = new StringBuilder("\tstats enable\n\tstats uri     ").append(lbCmd.lbStatsUri).append("\n\tstats realm   Haproxy\\ Statistics\n\tstats auth    ").append(lbCmd.lbStatsAuth);
            result.add(subRule.toString());
        }
    }
    result.add(blankLine);
    boolean has_listener = false;
    for (final LoadBalancerTO lbTO : lbCmd.getLoadBalancers()) {
        if (lbTO.isRevoked()) {
            continue;
        }
        final List<String> poolRules = getRulesForPool(lbTO, lbCmd.keepAliveEnabled);
        result.addAll(poolRules);
        has_listener = true;
    }
    result.add(blankLine);
    if (!has_listener) {
        // haproxy cannot handle empty listen / frontend or backend, so add
        // a dummy listener
        // on port 9
        result.addAll(Arrays.asList(defaultListen));
    }
    return result.toArray(new String[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO)

Example 3 with LoadBalancerTO

use of com.cloud.agent.api.to.LoadBalancerTO in project cloudstack by apache.

the class InternalLbElementTest method verifyUpdateHealthChecks.

//TEST FOR updateHealthChecks METHOD
@Test
public void verifyUpdateHealthChecks() throws ResourceUnavailableException {
    List<LoadBalancerTO> check = _lbEl.updateHealthChecks(new NetworkVO(), new ArrayList<LoadBalancingRule>());
    assertNull("Wrong value is returned by updateHealthChecks method", check);
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) LoadBalancingRule(com.cloud.network.lb.LoadBalancingRule) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) Test(org.junit.Test)

Example 4 with LoadBalancerTO

use of com.cloud.agent.api.to.LoadBalancerTO in project cloudstack by apache.

the class ConfigHelperTest method generateLoadBalancerConfigCommand.

protected LoadBalancerConfigCommand generateLoadBalancerConfigCommand() {
    final List<LoadBalancerTO> lbs = new ArrayList<>();
    final List<LbDestination> dests = new ArrayList<>();
    dests.add(new LbDestination(80, 8080, "10.1.10.2", false));
    dests.add(new LbDestination(80, 8080, "10.1.10.2", true));
    lbs.add(new LoadBalancerTO(UUID.randomUUID().toString(), "64.10.1.10", 80, "tcp", "algo", false, false, false, dests));
    final LoadBalancerTO[] arrayLbs = new LoadBalancerTO[lbs.size()];
    lbs.toArray(arrayLbs);
    final NicTO nic = new NicTO();
    final LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(arrayLbs, "64.10.2.10", "10.1.10.2", "192.168.1.2", nic, null, "1000", false);
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, "10.1.10.2");
    cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, ROUTERNAME);
    return cmd;
}
Also used : ArrayList(java.util.ArrayList) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) NicTO(com.cloud.agent.api.to.NicTO) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand)

Example 5 with LoadBalancerTO

use of com.cloud.agent.api.to.LoadBalancerTO in project cloudstack by apache.

the class HAProxyConfiguratorTest method testGenerateConfigurationLoadBalancerProxyProtocolConfigCommand.

/**
     * Test method for {@link com.cloud.network.HAProxyConfigurator#generateConfiguration(com.cloud.agent.api.routing.LoadBalancerConfigCommand)}.
     */
@Test
public void testGenerateConfigurationLoadBalancerProxyProtocolConfigCommand() {
    final List<LbDestination> dests = new ArrayList<>();
    dests.add(new LbDestination(443, 8443, "10.1.10.2", false));
    dests.add(new LbDestination(443, 8443, "10.1.10.2", true));
    LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 443, "tcp", "http", false, false, false, dests);
    lb.setLbProtocol("tcp-proxy");
    LoadBalancerTO[] lba = new LoadBalancerTO[1];
    lba[0] = lb;
    HAProxyConfigurator hpg = new HAProxyConfigurator();
    LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", false);
    String result = genConfig(hpg, cmd);
    assertTrue("'send-proxy' should result if protocol is 'tcp-proxy'", result.contains("send-proxy"));
}
Also used : ArrayList(java.util.ArrayList) LoadBalancerTO(com.cloud.agent.api.to.LoadBalancerTO) LbDestination(com.cloud.network.lb.LoadBalancingRule.LbDestination) LoadBalancerConfigCommand(com.cloud.agent.api.routing.LoadBalancerConfigCommand) Test(org.junit.Test)

Aggregations

LoadBalancerTO (com.cloud.agent.api.to.LoadBalancerTO)19 LbDestination (com.cloud.network.lb.LoadBalancingRule.LbDestination)12 ArrayList (java.util.ArrayList)12 LoadBalancerConfigCommand (com.cloud.agent.api.routing.LoadBalancerConfigCommand)10 LoadBalancingRule (com.cloud.network.lb.LoadBalancingRule)7 HealthCheckLBConfigAnswer (com.cloud.agent.api.routing.HealthCheckLBConfigAnswer)6 Nic (com.cloud.vm.Nic)5 Answer (com.cloud.agent.api.Answer)4 DataCenterVO (com.cloud.dc.DataCenterVO)4 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 HostVO (com.cloud.host.HostVO)4 ExternalLoadBalancerDeviceVO (com.cloud.network.dao.ExternalLoadBalancerDeviceVO)4 ConfigurationException (javax.naming.ConfigurationException)4 DestinationTO (com.cloud.agent.api.to.LoadBalancerTO.DestinationTO)3 NicTO (com.cloud.agent.api.to.NicTO)3 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)3 InsufficientNetworkCapacityException (com.cloud.exception.InsufficientNetworkCapacityException)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)3 Network (com.cloud.network.Network)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3