Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import com.vmware.vapi.internal.protocol.client.rest.authn.BasicAuthenticationAppender;
import com.vmware.vapi.protocol.HttpConfiguration;
import com.vmware.vapi.std.errors.Error;
import com.vmware.vapi.std.errors.NotFound;
import org.apache.cloudstack.resource.NsxLoadBalancerMember;
import org.apache.cloudstack.resource.NsxNetworkRule;
import org.apache.cloudstack.utils.NsxControllerUtils;
Expand All @@ -96,9 +97,11 @@
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toSet;
import static org.apache.cloudstack.utils.NsxControllerUtils.getServerPoolMemberName;
import static org.apache.cloudstack.utils.NsxControllerUtils.getServerPoolName;
import static org.apache.cloudstack.utils.NsxControllerUtils.getServiceName;
Expand Down Expand Up @@ -656,9 +659,19 @@ List<LBPoolMember> getLbPoolMembers(List<NsxLoadBalancerMember> memberList, Stri
public void createNsxLbServerPool(List<NsxLoadBalancerMember> memberList, String tier1GatewayName, String lbServerPoolName,
String algorithm, String privatePort, String protocol) {
try {
String activeMonitorPath = getLbActiveMonitorPath(lbServerPoolName, privatePort, protocol);
List<LBPoolMember> members = getLbPoolMembers(memberList, tier1GatewayName);
LbPools lbPools = (LbPools) nsxService.apply(LbPools.class);
Optional<LBPool> nsxLbServerPool = getNsxLbServerPool(lbPools, lbServerPoolName);
// Skip if pool exists and members unchanged
if (nsxLbServerPool.isPresent()) {
List<LBPoolMember> existingMembers = nsxLbServerPool
.map(LBPool::getMembers)
.orElseGet(List::of);
if (hasSamePoolMembers(existingMembers, members)) {
return;
}
}
String activeMonitorPath = getLbActiveMonitorPath(lbServerPoolName, privatePort, protocol);
LBPool lbPool = new LBPool.Builder()
.setId(lbServerPoolName)
.setDisplayName(lbServerPoolName)
Expand All @@ -676,27 +689,62 @@ public void createNsxLbServerPool(List<NsxLoadBalancerMember> memberList, String
}
}

private Optional<LBPool> getNsxLbServerPool(LbPools lbPools, String lbServerPoolName) {
try {
return Optional.ofNullable(lbPools.get(lbServerPoolName));
} catch (NotFound e) {
logger.debug("Server Pool not found: {}", lbServerPoolName);
return Optional.empty();
}
}

private boolean hasSamePoolMembers(List<LBPoolMember> existingMembers, List<LBPoolMember> membersUpdate) {
Set<String> existingMembersSet = existingMembers.stream()
.map(this::buildPoolMemberKey)
.collect(toSet());
Set<String> updateMembersSet = membersUpdate.stream()
.map(this::buildPoolMemberKey)
.collect(toSet());

return existingMembersSet.size() == updateMembersSet.size()
&& existingMembersSet.containsAll(updateMembersSet);
}

private String buildPoolMemberKey(LBPoolMember member) {
return member.getIpAddress() + ':' + member.getPort() + ':' + member.getDisplayName();
}

private String getLbActiveMonitorPath(String lbServerPoolName, String port, String protocol) {
LbMonitorProfiles lbActiveMonitor = (LbMonitorProfiles) nsxService.apply(LbMonitorProfiles.class);
String lbMonitorProfileId = getActiveMonitorProfileName(lbServerPoolName, port, protocol);
if ("TCP".equals(protocol.toUpperCase(Locale.ROOT))) {
LBTcpMonitorProfile lbTcpMonitorProfile = new LBTcpMonitorProfile.Builder(TCP_MONITOR_PROFILE)
.setDisplayName(lbMonitorProfileId)
.setMonitorPort(Long.parseLong(port))
.build();
lbActiveMonitor.patch(lbMonitorProfileId, lbTcpMonitorProfile);
} else if ("UDP".equals(protocol.toUpperCase(Locale.ROOT))) {
LBIcmpMonitorProfile icmpMonitorProfile = new LBIcmpMonitorProfile.Builder(ICMP_MONITOR_PROFILE)
.setDisplayName(lbMonitorProfileId)
.build();
lbActiveMonitor.patch(lbMonitorProfileId, icmpMonitorProfile);
Optional<Structure> monitorProfile = getMonitorProfile(lbActiveMonitor, lbMonitorProfileId);
if (monitorProfile.isEmpty()) {
if ("TCP".equals(protocol.toUpperCase(Locale.ROOT))) {
LBTcpMonitorProfile lbTcpMonitorProfile = new LBTcpMonitorProfile.Builder(TCP_MONITOR_PROFILE)
.setDisplayName(lbMonitorProfileId)
.setMonitorPort(Long.parseLong(port))
.build();
lbActiveMonitor.patch(lbMonitorProfileId, lbTcpMonitorProfile);
} else if ("UDP".equals(protocol.toUpperCase(Locale.ROOT))) {
LBIcmpMonitorProfile icmpMonitorProfile = new LBIcmpMonitorProfile.Builder(ICMP_MONITOR_PROFILE)
.setDisplayName(lbMonitorProfileId)
.build();
lbActiveMonitor.patch(lbMonitorProfileId, icmpMonitorProfile);
}
monitorProfile = getMonitorProfile(lbActiveMonitor, lbMonitorProfileId);
}

LBMonitorProfileListResult listResult = listLBActiveMonitors(lbActiveMonitor);
Optional<Structure> monitorProfile = listResult.getResults().stream().filter(profile -> profile._getDataValue().getField("id").toString().equals(lbMonitorProfileId)).findFirst();
return monitorProfile.map(structure -> structure._getDataValue().getField("path").toString()).orElse(null);
}

private Optional<Structure> getMonitorProfile(LbMonitorProfiles lbActiveMonitor, String lbMonitorProfileId) {
try {
return Optional.ofNullable(lbActiveMonitor.get(lbMonitorProfileId));
} catch (NotFound e) {
logger.debug("LB Monitor Profile not found: {}", lbMonitorProfileId);
return Optional.empty();
}
}

LBMonitorProfileListResult listLBActiveMonitors(LbMonitorProfiles lbActiveMonitor) {
return lbActiveMonitor.list(null, false, null, null, null, null);
}
Expand Down Expand Up @@ -735,7 +783,7 @@ public void createAndAddNsxLbVirtualServer(String tier1GatewayName, long lbId, S
String lbVirtualServerName = getVirtualServerName(tier1GatewayName, lbId);
String lbServiceName = getLoadBalancerName(tier1GatewayName);
LbVirtualServers lbVirtualServers = (LbVirtualServers) nsxService.apply(LbVirtualServers.class);
if (Objects.nonNull(getLbVirtualServerService(lbVirtualServers, lbServiceName))) {
if (Objects.nonNull(getLbVirtualServerService(lbVirtualServers, lbVirtualServerName))) {
return;
}
LBVirtualServer lbVirtualServer = new LBVirtualServer.Builder()
Expand Down
Loading
Loading