Skip to content

Commit

Permalink
skip endpoints that doesn't have targetRef and nodeNames
Browse files Browse the repository at this point in the history
  • Loading branch information
semihbkgr committed Oct 23, 2023
1 parent e5e13fd commit 90661a9
Show file tree
Hide file tree
Showing 6 changed files with 527 additions and 507 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import static com.hazelcast.kubernetes.KubernetesApiProvider.convertToString;
import static com.hazelcast.kubernetes.KubernetesApiProvider.toJsonArray;
import static com.hazelcast.kubernetes.KubernetesApiProvider.extractTargetRefName;
import static com.hazelcast.kubernetes.KubernetesClient.Endpoint;
import static com.hazelcast.kubernetes.KubernetesClient.EndpointAddress;

Expand Down Expand Up @@ -62,18 +61,18 @@ public List<Endpoint> parseEndpoints(JsonValue endpointItemJson) {
for (JsonValue subset : toJsonArray(endpointItemJson.asObject().get("subsets"))) {
Integer endpointPort = extractPort(subset);
for (JsonValue address : toJsonArray(subset.asObject().get("addresses"))) {
addresses.add(extractEntrypointAddress(address, endpointPort, true));
addresses.add(extractEndpointAddress(address, endpointPort, true));
}
for (JsonValue address : toJsonArray(subset.asObject().get("notReadyAddresses"))) {
addresses.add(extractEntrypointAddress(address, endpointPort, false));
addresses.add(extractEndpointAddress(address, endpointPort, false));
}
}
return addresses;
}

private Endpoint extractEntrypointAddress(JsonValue endpointAddressJson, Integer endpointPort, boolean isReady) {
private Endpoint extractEndpointAddress(JsonValue endpointAddressJson, Integer endpointPort, boolean isReady) {
String ip = endpointAddressJson.asObject().get("ip").asString();
String targetRefName = endpointAddressJson.asObject().get("targetRef").asObject().get("name").asString();
String targetRefName = extractTargetRefName(endpointAddressJson);
Map<String, String> additionalProperties = extractAdditionalPropertiesFrom(endpointAddressJson);
return new Endpoint(new EndpointAddress(ip, endpointPort, targetRefName), isReady, additionalProperties);
}
Expand All @@ -89,9 +88,13 @@ public Map<EndpointAddress, String> extractServices(JsonObject endpointsListJson
// Service must point to exactly one endpoint address, otherwise the public IP would be ambiguous.
if (endpoints.size() == 1) {
EndpointAddress address = endpoints.get(0).getPrivateAddress();
// Omit the endpoint if the targetRef name in its private address is null.
if (address.getTargetRefName() == null) {
continue;
}
if (privateAddresses.contains(address.getIp())) {
// If multiple services match the pod, then match service and pod names
if (!result.containsKey(address) || service.equals(extractTargetRefName(item))) {
if (!result.containsKey(address) || service.equals(address.getTargetRefName())) {
result.put(address, service);
}
left.remove(address.getIp());
Expand All @@ -105,20 +108,6 @@ public Map<EndpointAddress, String> extractServices(JsonObject endpointsListJson
return result;
}

private String extractTargetRefName(JsonValue endpointItemJson) {
return Optional.of(endpointItemJson)
.flatMap(e -> toJsonArray(e.asObject().get("subsets")).values().stream().findFirst())
.flatMap(e -> Stream.concat(
toJsonArray(e.asObject().get("addresses")).values().stream(),
toJsonArray(e.asObject().get("notReadyAddresses")).values().stream()
).findFirst()
)
.map(e -> e.asObject().get("targetRef"))
.map(e -> e.asObject().get("name"))
.map(KubernetesApiProvider::convertToString)
.orElse(null);
}

public Map<EndpointAddress, String> extractNodes(JsonObject endpointsListJson,
List<String> privateAddresses) {
Map<EndpointAddress, String> result = new HashMap<>();
Expand Down Expand Up @@ -154,10 +143,12 @@ private Map<EndpointAddress, String> extractNodes(JsonValue addressesJson, List<
Map<EndpointAddress, String> result = new HashMap<>();
for (JsonValue address : toJsonArray(addressesJson)) {
String ip = address.asObject().get("ip").asString();
String targetRefName = address.asObject().get("targetRef").asObject().get("name").asString();
String nodeName = KubernetesApiProvider.convertToString(address.asObject().get("nodeName"));
for (Integer port : ports) {
result.put(new EndpointAddress(ip, port, targetRefName), nodeName);
String targetRefName = extractTargetRefName(address);
JsonValue nodeName = address.asObject().get("nodeName");
if (nodeName != null && !nodeName.isNull()) {
for (Integer port : ports) {
result.put(new EndpointAddress(ip, port, targetRefName), nodeName.asString());
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static com.hazelcast.kubernetes.KubernetesApiProvider.toJsonArray;
import static com.hazelcast.kubernetes.KubernetesApiProvider.extractTargetRefName;
import static com.hazelcast.kubernetes.KubernetesClient.Endpoint;
import static com.hazelcast.kubernetes.KubernetesClient.EndpointAddress;

Expand Down Expand Up @@ -74,9 +74,13 @@ public Map<EndpointAddress, String> extractServices(JsonObject endpointsListJson
// Service must point to exactly one endpoint address, otherwise the public IP would be ambiguous.
if (endpoints.size() == 1) {
EndpointAddress address = endpoints.get(0).getPrivateAddress();
// Omit the endpoint if the targetRef name in its private address is null.
if (address.getTargetRefName() == null) {
continue;
}
if (privateAddresses.contains(address.getIp())) {
// If multiple services match the pod, then match service and pod names
if (!result.containsKey(address) || service.equals(extractTargetRefName(item))) {
if (!result.containsKey(address) || service.equals(address.getTargetRefName())) {
result.put(address, service);
}
left.remove(address.getIp());
Expand All @@ -97,7 +101,7 @@ private List<Endpoint> parseEndpointSlices(JsonValue jsonValue) {
for (JsonValue endpoint : toJsonArray(jsonValue.asObject().get("endpoints"))) {
JsonValue ready = endpoint.asObject().get("conditions").asObject().get("ready");
Map<String, String> additionalProperties = extractAdditionalPropertiesFrom(endpoint);
String targetRefName = endpoint.asObject().get("targetRef").asObject().get("name").asString();
String targetRefName = extractTargetRefName(endpoint);
for (JsonValue address : toJsonArray(endpoint.asObject().get("addresses"))) {
addresses.add(new Endpoint(new EndpointAddress(address.asString(), endpointPort, targetRefName),
ready.asBoolean(), additionalProperties));
Expand All @@ -106,15 +110,6 @@ private List<Endpoint> parseEndpointSlices(JsonValue jsonValue) {
return addresses;
}

private String extractTargetRefName(JsonValue endpointItemJson) {
return Optional.of(endpointItemJson)
.flatMap(e -> toJsonArray(e.asObject().get("endpoints")).values().stream().findFirst())
.map(e -> e.asObject().get("targetRef"))
.map(e -> e.asObject().get("name"))
.map(KubernetesApiProvider::convertToString)
.orElse(null);
}

@Override
public Map<EndpointAddress, String> extractNodes(JsonObject jsonObject,
List<String> privateAddresses) {
Expand All @@ -127,16 +122,17 @@ public Map<EndpointAddress, String> extractNodes(JsonObject jsonObject,
}
for (JsonValue endpoint : toJsonArray(item.asObject().get("endpoints"))) {
JsonObject endpointObject = endpoint.asObject();
String targetRefName = endpointObject.get("targetRef").asObject().get("name").asString();
String nodeName = KubernetesApiProvider.convertToString(endpointObject.get("nodeName"));

Map<EndpointAddress, String> nodes = extractNodes(
endpointObject.get("addresses"), ports, nodeName, targetRefName);
for (Map.Entry<EndpointAddress, String> nodeEntry : nodes.entrySet()) {
EndpointAddress address = nodeEntry.getKey();
if (privateAddresses.contains(address.getIp())) {
result.put(address, nodes.get(address));
left.remove(address.getIp());
String targetRefName = extractTargetRefName(endpointObject);
JsonValue nodeName = endpointObject.asObject().get("nodeName");
if (nodeName != null && !nodeName.isNull()) {
Map<EndpointAddress, String> nodes = extractNodes(
endpointObject.get("addresses"), ports, nodeName.asString(), targetRefName);
for (Map.Entry<EndpointAddress, String> nodeEntry : nodes.entrySet()) {
EndpointAddress address = nodeEntry.getKey();
if (privateAddresses.contains(address.getIp())) {
result.put(address, nodes.get(address));
left.remove(address.getIp());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,12 @@ static String convertToString(JsonValue jsonValue) {
return jsonValue.toString();
}
}

static String extractTargetRefName(JsonValue endpointAddressJson) {
JsonValue targetRef = endpointAddressJson.asObject().get("targetRef");
if (targetRef == null || targetRef.isNull()) {
return null;
}
return targetRef.asObject().get("name").asString();
}
}
Loading

0 comments on commit 90661a9

Please sign in to comment.