Search in sources :

Example 6 with EndpointAddress

use of com.hazelcast.kubernetes.KubernetesClient.EndpointAddress in project hazelcast by hazelcast.

the class KubernetesApiEndpointProvider method extractEntrypointAddress.

private Endpoint extractEntrypointAddress(JsonValue endpointAddressJson, Integer endpointPort, boolean isReady) {
    String ip = endpointAddressJson.asObject().get("ip").asString();
    Map<String, String> additionalProperties = extractAdditionalPropertiesFrom(endpointAddressJson);
    return new Endpoint(new EndpointAddress(ip, endpointPort), isReady, additionalProperties);
}
Also used : Endpoint(com.hazelcast.kubernetes.KubernetesClient.Endpoint) KubernetesApiProvider.convertToString(com.hazelcast.kubernetes.KubernetesApiProvider.convertToString) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress)

Example 7 with EndpointAddress

use of com.hazelcast.kubernetes.KubernetesClient.EndpointAddress in project hazelcast by hazelcast.

the class KubernetesApiEndpointProvider method extractNodes.

public Map<EndpointAddress, String> extractNodes(JsonObject endpointsListJson, List<EndpointAddress> privateAddresses) {
    Map<EndpointAddress, String> result = new HashMap<>();
    Set<EndpointAddress> left = new HashSet<>(privateAddresses);
    for (JsonValue item : toJsonArray(endpointsListJson.get("items"))) {
        for (JsonValue subset : toJsonArray(item.asObject().get("subsets"))) {
            JsonObject subsetObject = subset.asObject();
            List<Integer> ports = new ArrayList<>();
            for (JsonValue port : toJsonArray(subsetObject.get("ports"))) {
                ports.add(port.asObject().get("port").asInt());
            }
            Map<EndpointAddress, String> nodes = new HashMap<>();
            nodes.putAll(extractNodes(subsetObject.get("addresses"), ports));
            nodes.putAll(extractNodes(subsetObject.get("notReadyAddresses"), ports));
            for (Map.Entry<EndpointAddress, String> nodeEntry : nodes.entrySet()) {
                EndpointAddress address = nodeEntry.getKey();
                if (privateAddresses.contains(address)) {
                    result.put(address, nodes.get(address));
                    left.remove(address);
                }
            }
        }
    }
    if (!left.isEmpty()) {
        // At least one Hazelcast Member POD does not have 'nodeName' assigned.
        throw noNodeNameAssignedException(left);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) JsonValue(com.hazelcast.internal.json.JsonValue) ArrayList(java.util.ArrayList) JsonObject(com.hazelcast.internal.json.JsonObject) KubernetesApiProvider.convertToString(com.hazelcast.kubernetes.KubernetesApiProvider.convertToString) HashMap(java.util.HashMap) Map(java.util.Map) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress) HashSet(java.util.HashSet)

Example 8 with EndpointAddress

use of com.hazelcast.kubernetes.KubernetesClient.EndpointAddress in project hazelcast by hazelcast.

the class KubernetesApiEndpointSlicesProvider method extractServices.

public Map<EndpointAddress, String> extractServices(JsonObject endpointsListJson, List<EndpointAddress> privateAddresses) {
    Map<EndpointAddress, String> result = new HashMap<>();
    Set<EndpointAddress> left = new HashSet<>(privateAddresses);
    for (JsonValue item : toJsonArray(endpointsListJson.get("items"))) {
        JsonValue ownerRefsValue = item.asObject().get("metadata").asObject().get("ownerReferences");
        if (ownerRefsValue == null || ownerRefsValue.asArray().size() > 1 || !ownerRefsValue.asArray().get(0).asObject().get("kind").asString().equals("Service")) {
            continue;
        }
        String service = ownerRefsValue.asArray().get(0).asObject().get("name").asString();
        List<Endpoint> endpoints = parseEndpointSlices(item);
        // 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();
            if (privateAddresses.contains(address)) {
                // If multiple services match the pod, then match service and pod names
                if (!result.containsKey(address) || service.equals(extractTargetRefName(item))) {
                    result.put(address, service);
                }
                left.remove(address);
            }
        }
    }
    if (!left.isEmpty()) {
        // At least one Hazelcast Member POD does not have a corresponding service.
        throw noCorrespondingServicesException(left);
    }
    return result;
}
Also used : Endpoint(com.hazelcast.kubernetes.KubernetesClient.Endpoint) HashMap(java.util.HashMap) JsonValue(com.hazelcast.internal.json.JsonValue) KubernetesApiProvider.convertToString(com.hazelcast.kubernetes.KubernetesApiProvider.convertToString) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress) HashSet(java.util.HashSet)

Example 9 with EndpointAddress

use of com.hazelcast.kubernetes.KubernetesClient.EndpointAddress in project hazelcast by hazelcast.

the class KubernetesApiProviderTest method extractServices.

@Test
public void extractServices() {
    // given
    JsonObject endpointsJson = Json.parse(getEndpointsResponseWithServices()).asObject();
    ArrayList<EndpointAddress> privateAddresses = new ArrayList<>();
    privateAddresses.add(new EndpointAddress("192.168.0.25", 5701));
    privateAddresses.add(new EndpointAddress("172.17.0.5", 5701));
    // when
    Map<EndpointAddress, String> services = provider.extractServices(endpointsJson, privateAddresses);
    // then
    assertThat(format(services), containsInAnyOrder(toString("192.168.0.25", 5701, "hazelcast-0"), toString("172.17.0.5", 5701, "service-1")));
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.hazelcast.internal.json.JsonObject) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress) Test(org.junit.Test)

Example 10 with EndpointAddress

use of com.hazelcast.kubernetes.KubernetesClient.EndpointAddress in project hazelcast by hazelcast.

the class KubernetesApiProviderTest method extractNodes.

@Test
public void extractNodes() {
    // given
    JsonObject endpointsJson = Json.parse(getEndpointsResponseWithServices()).asObject();
    ArrayList<EndpointAddress> privateAddresses = new ArrayList<>();
    privateAddresses.add(new EndpointAddress("192.168.0.25", 5701));
    privateAddresses.add(new EndpointAddress("172.17.0.5", 5701));
    // when
    Map<EndpointAddress, String> nodes = provider.extractNodes(endpointsJson, privateAddresses);
    // then
    assertThat(format(nodes), containsInAnyOrder(toString("192.168.0.25", 5701, "node-name-1"), toString("172.17.0.5", 5701, "node-name-2")));
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.hazelcast.internal.json.JsonObject) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress) Test(org.junit.Test)

Aggregations

EndpointAddress (com.hazelcast.kubernetes.KubernetesClient.EndpointAddress)10 KubernetesApiProvider.convertToString (com.hazelcast.kubernetes.KubernetesApiProvider.convertToString)7 JsonValue (com.hazelcast.internal.json.JsonValue)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 JsonObject (com.hazelcast.internal.json.JsonObject)4 Endpoint (com.hazelcast.kubernetes.KubernetesClient.Endpoint)4 HashSet (java.util.HashSet)4 Map (java.util.Map)2 Test (org.junit.Test)2