Search in sources :

Example 1 with Endpoint

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

the class KubernetesApiEndpointProvider 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"))) {
        String service = convertToString(item.asObject().get("metadata").asObject().get("name"));
        List<Endpoint> endpoints = parseEndpoints(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 2 with Endpoint

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

the class KubernetesApiEndpointSlicesProvider method parseEndpointSlices.

private List<Endpoint> parseEndpointSlices(JsonValue jsonValue) {
    List<KubernetesClient.Endpoint> addresses = new ArrayList<>();
    Integer endpointPort = extractPort(jsonValue);
    for (JsonValue endpoint : toJsonArray(jsonValue.asObject().get("endpoints"))) {
        JsonValue ready = endpoint.asObject().get("conditions").asObject().get("ready");
        Map<String, String> additionalProperties = extractAdditionalPropertiesFrom(endpoint);
        for (JsonValue address : toJsonArray(endpoint.asObject().get("addresses"))) {
            addresses.add(new Endpoint(new EndpointAddress(address.asString(), endpointPort), ready.asBoolean(), additionalProperties));
        }
    }
    return addresses;
}
Also used : Endpoint(com.hazelcast.kubernetes.KubernetesClient.Endpoint) ArrayList(java.util.ArrayList) JsonValue(com.hazelcast.internal.json.JsonValue) KubernetesApiProvider.convertToString(com.hazelcast.kubernetes.KubernetesApiProvider.convertToString) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress)

Example 3 with Endpoint

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

the class KubernetesApiProviderTest method format.

private static List<String> format(List<Endpoint> addresses) {
    List<String> result = new ArrayList<>();
    for (Endpoint address : addresses) {
        String ip = address.getPrivateAddress().getIp();
        Integer port = address.getPrivateAddress().getPort();
        boolean isReady = address.isReady();
        result.add(toString(ip, port, isReady));
    }
    return result;
}
Also used : Endpoint(com.hazelcast.kubernetes.KubernetesClient.Endpoint) ArrayList(java.util.ArrayList)

Example 4 with Endpoint

use of com.hazelcast.kubernetes.KubernetesClient.Endpoint 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 5 with Endpoint

use of com.hazelcast.kubernetes.KubernetesClient.Endpoint 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)

Aggregations

Endpoint (com.hazelcast.kubernetes.KubernetesClient.Endpoint)9 KubernetesApiProvider.convertToString (com.hazelcast.kubernetes.KubernetesApiProvider.convertToString)4 EndpointAddress (com.hazelcast.kubernetes.KubernetesClient.EndpointAddress)4 ArrayList (java.util.ArrayList)4 JsonValue (com.hazelcast.internal.json.JsonValue)3 JsonObject (com.hazelcast.internal.json.JsonObject)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2