use of com.cloud.legacymodel.to.LoadBalancerTO.DestinationTO in project cosmic by MissionCriticalCloud.
the class HAProxyConfigurator method getRulesForPool.
private List<String> getRulesForPool(final LoadBalancerTO lbTO, final boolean keepAliveEnabled) {
StringBuilder sb = new StringBuilder();
final String poolName = sb.append(lbTO.getSrcIp().replace(".", "_")).append('-').append(lbTO.getSrcPort()).toString();
final String publicIP = lbTO.getSrcIp();
final int publicPort = lbTO.getSrcPort();
final String algorithm = lbTO.getAlgorithm();
final List<String> result = new ArrayList<>();
// Add line like this: "listen 65_37_141_30-80 65.37.141.30:80"
sb = new StringBuilder();
sb.append("listen ").append(poolName).append(" ").append(publicIP).append(":").append(publicPort);
result.add(sb.toString());
sb = new StringBuilder();
sb.append("\t").append("balance ").append(algorithm);
sb.append("\n\t").append("timeout client ").append(lbTO.getClientTimeout());
sb.append("\n\t").append("timeout server ").append(lbTO.getServerTimeout());
result.add(sb.toString());
int i = 0;
Boolean destsAvailable = false;
final String stickinessSubRule = getLbSubRuleForStickiness(lbTO);
final List<String> dstSubRule = new ArrayList<>();
final List<String> dstWithCookieSubRule = new ArrayList<>();
for (final DestinationTO dest : lbTO.getDestinations()) {
// Add line like this: "server 65_37_141_30-80_3 10.1.1.4:80 check"
if (dest.isRevoked()) {
continue;
}
sb = new StringBuilder();
sb.append("\t").append("server ").append(poolName).append("_").append(Integer.toString(i++)).append(" ").append(dest.getDestIp()).append(":").append(dest.getDestPort()).append(" check");
if (lbTO.getLbProtocol() != null && lbTO.getLbProtocol().equals("tcp-proxy")) {
sb.append(" send-proxy");
}
dstSubRule.add(sb.toString());
if (stickinessSubRule != null) {
sb.append(" cookie ").append(dest.getDestIp().replace(".", "_")).append('-').append(dest.getDestPort()).toString();
dstWithCookieSubRule.add(sb.toString());
}
destsAvailable = true;
}
Boolean httpbasedStickiness = false;
// Attach stickiness sub rule only if the destinations are available
if (stickinessSubRule != null && destsAvailable == true) {
for (final StickinessPolicyTO stickinessPolicy : lbTO.getStickinessPolicies()) {
if (stickinessPolicy == null) {
continue;
}
if (StickinessMethodType.LBCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName()) || StickinessMethodType.AppCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
httpbasedStickiness = true;
}
}
if (httpbasedStickiness) {
result.addAll(dstWithCookieSubRule);
} else {
result.addAll(dstSubRule);
}
result.add(stickinessSubRule);
} else {
result.addAll(dstSubRule);
}
if (stickinessSubRule != null && !destsAvailable) {
s_logger.warn("Haproxy stickiness policy for lb rule: " + lbTO.getSrcIp() + ":" + lbTO.getSrcPort() + ": Not Applied, cause: backends are unavailable");
}
if (publicPort == NetUtils.HTTP_PORT && !keepAliveEnabled || httpbasedStickiness) {
sb = new StringBuilder();
sb.append("\t").append("mode http");
result.add(sb.toString());
sb = new StringBuilder();
sb.append("\t").append("option httpclose");
result.add(sb.toString());
}
result.add(blankLine);
return result;
}
use of com.cloud.legacymodel.to.LoadBalancerTO.DestinationTO in project cosmic by MissionCriticalCloud.
the class HAProxyConfigurator method generateFwRules.
@Override
public String[][] generateFwRules(final LoadBalancerConfigCommand lbCmd) {
final String[][] result = new String[3][];
final Set<String> toAdd = new HashSet<>();
final Set<String> toRemove = new HashSet<>();
final Set<String> toStats = new HashSet<>();
for (final LoadBalancerTO lbTO : lbCmd.getLoadBalancers()) {
final StringBuilder sb = new StringBuilder();
sb.append(lbTO.getSrcIp()).append(":");
sb.append(lbTO.getSrcPort()).append(":");
sb.append(lbTO.getProtocol()).append(":");
for (final DestinationTO dest : lbTO.getDestinations()) {
// Add dst entry like this: "10.1.1.4:80"
if (dest.isRevoked()) {
continue;
}
sb.append(dest.getDestIp()).append(":");
sb.append(dest.getDestPort()).append(":");
}
final String lbRuleEntry = sb.toString();
if (!lbTO.isRevoked()) {
toAdd.add(lbRuleEntry);
} else {
toRemove.add(lbRuleEntry);
}
}
StringBuilder sb = new StringBuilder("");
if (lbCmd.lbStatsVisibility.equals("guest-network")) {
sb = new StringBuilder(lbCmd.lbStatsGuestIP).append(":").append(lbCmd.lbStatsPort).append(":").append(lbCmd.lbStatsSrcCidrs).append(":,");
} else if (lbCmd.lbStatsVisibility.equals("link-local")) {
sb = new StringBuilder(lbCmd.lbStatsPrivateIP).append(":").append(lbCmd.lbStatsPort).append(":").append(lbCmd.lbStatsSrcCidrs).append(":,");
} else if (lbCmd.lbStatsVisibility.equals("global")) {
sb = new StringBuilder(lbCmd.lbStatsPublicIP).append(":").append(lbCmd.lbStatsPort).append(":").append(lbCmd.lbStatsSrcCidrs).append(":,");
} else if (lbCmd.lbStatsVisibility.equals("all")) {
sb = new StringBuilder("0.0.0.0/0").append(":").append(lbCmd.lbStatsPort).append(":").append(lbCmd.lbStatsSrcCidrs).append(":,");
}
toStats.add(sb.toString());
toRemove.removeAll(toAdd);
result[ADD] = toAdd.toArray(new String[toAdd.size()]);
result[REMOVE] = toRemove.toArray(new String[toRemove.size()]);
result[STATS] = toStats.toArray(new String[toStats.size()]);
return result;
}
Aggregations