Search in sources :

Example 1 with ClusterLoadAssignment

use of io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment in project grpc-java by grpc.

the class ClientXdsClient method handleEdsResponse.

@Override
public void handleEdsResponse(ServerInfo serverInfo, String versionInfo, List<Any> resources, String nonce) {
    syncContext.throwIfNotInThisSynchronizationContext();
    Map<String, ParsedResource> parsedResources = new HashMap<>(resources.size());
    Set<String> unpackedResources = new HashSet<>(resources.size());
    Set<String> invalidResources = new HashSet<>();
    List<String> errors = new ArrayList<>();
    for (int i = 0; i < resources.size(); i++) {
        Any resource = resources.get(i);
        // Unpack the ClusterLoadAssignment.
        ClusterLoadAssignment assignment;
        try {
            assignment = unpackCompatibleType(resource, ClusterLoadAssignment.class, ResourceType.EDS.typeUrl(), ResourceType.EDS.typeUrlV2());
        } catch (InvalidProtocolBufferException e) {
            errors.add("EDS response Resource index " + i + " - can't decode ClusterLoadAssignment: " + e);
            continue;
        }
        if (!isResourceNameValid(assignment.getClusterName(), resource.getTypeUrl())) {
            errors.add("Unsupported resource name: " + assignment.getClusterName() + " for type: " + ResourceType.EDS);
            continue;
        }
        String clusterName = canonifyResourceName(assignment.getClusterName());
        // unrequested resources.
        if (!edsResourceSubscribers.containsKey(clusterName)) {
            continue;
        }
        unpackedResources.add(clusterName);
        // Process ClusterLoadAssignment into EdsUpdate.
        EdsUpdate edsUpdate;
        try {
            edsUpdate = processClusterLoadAssignment(assignment);
        } catch (ResourceInvalidException e) {
            errors.add("EDS response ClusterLoadAssignment '" + clusterName + "' validation error: " + e.getMessage());
            invalidResources.add(clusterName);
            continue;
        }
        parsedResources.put(clusterName, new ParsedResource(edsUpdate, resource));
    }
    logger.log(XdsLogLevel.INFO, "Received EDS Response version {0} nonce {1}. Parsed resources: {2}", versionInfo, nonce, unpackedResources);
    handleResourceUpdate(serverInfo, ResourceType.EDS, parsedResources, invalidResources, Collections.<String>emptySet(), versionInfo, nonce, errors);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Any(com.google.protobuf.Any) LbEndpoint(io.grpc.xds.Endpoints.LbEndpoint) ClusterLoadAssignment(io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment) HashSet(java.util.HashSet)

Example 2 with ClusterLoadAssignment

use of io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment in project grpc-java by grpc.

the class ClientXdsClient method parseNonAggregateCluster.

private static StructOrError<CdsUpdate.Builder> parseNonAggregateCluster(Cluster cluster, Set<String> edsResources, Set<String> certProviderInstances, ServerInfo serverInfo) {
    String clusterName = cluster.getName();
    ServerInfo lrsServerInfo = null;
    Long maxConcurrentRequests = null;
    UpstreamTlsContext upstreamTlsContext = null;
    if (cluster.hasLrsServer()) {
        if (!cluster.getLrsServer().hasSelf()) {
            return StructOrError.fromError("Cluster " + clusterName + ": only support LRS for the same management server");
        }
        lrsServerInfo = serverInfo;
    }
    if (cluster.hasCircuitBreakers()) {
        List<Thresholds> thresholds = cluster.getCircuitBreakers().getThresholdsList();
        for (Thresholds threshold : thresholds) {
            if (threshold.getPriority() != RoutingPriority.DEFAULT) {
                continue;
            }
            if (threshold.hasMaxRequests()) {
                maxConcurrentRequests = (long) threshold.getMaxRequests().getValue();
            }
        }
    }
    if (cluster.getTransportSocketMatchesCount() > 0) {
        return StructOrError.fromError("Cluster " + clusterName + ": transport-socket-matches not supported.");
    }
    if (cluster.hasTransportSocket()) {
        if (!TRANSPORT_SOCKET_NAME_TLS.equals(cluster.getTransportSocket().getName())) {
            return StructOrError.fromError("transport-socket with name " + cluster.getTransportSocket().getName() + " not supported.");
        }
        try {
            upstreamTlsContext = UpstreamTlsContext.fromEnvoyProtoUpstreamTlsContext(validateUpstreamTlsContext(unpackCompatibleType(cluster.getTransportSocket().getTypedConfig(), io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext.class, TYPE_URL_UPSTREAM_TLS_CONTEXT, TYPE_URL_UPSTREAM_TLS_CONTEXT_V2), certProviderInstances));
        } catch (InvalidProtocolBufferException | ResourceInvalidException e) {
            return StructOrError.fromError("Cluster " + clusterName + ": malformed UpstreamTlsContext: " + e);
        }
    }
    DiscoveryType type = cluster.getType();
    if (type == DiscoveryType.EDS) {
        String edsServiceName = null;
        io.envoyproxy.envoy.config.cluster.v3.Cluster.EdsClusterConfig edsClusterConfig = cluster.getEdsClusterConfig();
        if (!edsClusterConfig.getEdsConfig().hasAds() && !edsClusterConfig.getEdsConfig().hasSelf()) {
            return StructOrError.fromError("Cluster " + clusterName + ": field eds_cluster_config must be set to indicate to use" + " EDS over ADS or self ConfigSource");
        }
        // If the service_name field is set, that value will be used for the EDS request.
        if (!edsClusterConfig.getServiceName().isEmpty()) {
            edsServiceName = edsClusterConfig.getServiceName();
            edsResources.add(edsServiceName);
        } else {
            edsResources.add(clusterName);
        }
        return StructOrError.fromStruct(CdsUpdate.forEds(clusterName, edsServiceName, lrsServerInfo, maxConcurrentRequests, upstreamTlsContext));
    } else if (type.equals(DiscoveryType.LOGICAL_DNS)) {
        if (!cluster.hasLoadAssignment()) {
            return StructOrError.fromError("Cluster " + clusterName + ": LOGICAL_DNS clusters must have a single host");
        }
        ClusterLoadAssignment assignment = cluster.getLoadAssignment();
        if (assignment.getEndpointsCount() != 1 || assignment.getEndpoints(0).getLbEndpointsCount() != 1) {
            return StructOrError.fromError("Cluster " + clusterName + ": LOGICAL_DNS clusters must have a single " + "locality_lb_endpoint and a single lb_endpoint");
        }
        io.envoyproxy.envoy.config.endpoint.v3.LbEndpoint lbEndpoint = assignment.getEndpoints(0).getLbEndpoints(0);
        if (!lbEndpoint.hasEndpoint() || !lbEndpoint.getEndpoint().hasAddress() || !lbEndpoint.getEndpoint().getAddress().hasSocketAddress()) {
            return StructOrError.fromError("Cluster " + clusterName + ": LOGICAL_DNS clusters must have an endpoint with address and socket_address");
        }
        SocketAddress socketAddress = lbEndpoint.getEndpoint().getAddress().getSocketAddress();
        if (!socketAddress.getResolverName().isEmpty()) {
            return StructOrError.fromError("Cluster " + clusterName + ": LOGICAL DNS clusters must NOT have a custom resolver name set");
        }
        if (socketAddress.getPortSpecifierCase() != PortSpecifierCase.PORT_VALUE) {
            return StructOrError.fromError("Cluster " + clusterName + ": LOGICAL DNS clusters socket_address must have port_value");
        }
        String dnsHostName = String.format("%s:%d", socketAddress.getAddress(), socketAddress.getPortValue());
        return StructOrError.fromStruct(CdsUpdate.forLogicalDns(clusterName, dnsHostName, lrsServerInfo, maxConcurrentRequests, upstreamTlsContext));
    }
    return StructOrError.fromError("Cluster " + clusterName + ": unsupported built-in discovery type: " + type);
}
Also used : Thresholds(io.envoyproxy.envoy.config.cluster.v3.CircuitBreakers.Thresholds) ServerInfo(io.grpc.xds.Bootstrapper.ServerInfo) UpstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Cluster(io.envoyproxy.envoy.config.cluster.v3.Cluster) LbEndpoint(io.grpc.xds.Endpoints.LbEndpoint) ClusterLoadAssignment(io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment) DiscoveryType(io.envoyproxy.envoy.config.cluster.v3.Cluster.DiscoveryType) SocketAddress(io.envoyproxy.envoy.config.core.v3.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with ClusterLoadAssignment

use of io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment in project grpc-java by grpc.

the class FakeControlPlaneXdsIntegrationTest method eds.

private static ClusterLoadAssignment eds(String hostName, int port) {
    Address address = Address.newBuilder().setSocketAddress(SocketAddress.newBuilder().setAddress(hostName).setPortValue(port).build()).build();
    LocalityLbEndpoints endpoints = LocalityLbEndpoints.newBuilder().setLoadBalancingWeight(UInt32Value.of(10)).setPriority(0).addLbEndpoints(LbEndpoint.newBuilder().setEndpoint(Endpoint.newBuilder().setAddress(address).build()).setHealthStatus(HealthStatus.HEALTHY).build()).build();
    return ClusterLoadAssignment.newBuilder().setClusterName(edsName).addEndpoints(endpoints).build();
}
Also used : Address(io.envoyproxy.envoy.config.core.v3.Address) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(io.envoyproxy.envoy.config.core.v3.SocketAddress) LocalityLbEndpoints(io.envoyproxy.envoy.config.endpoint.v3.LocalityLbEndpoints)

Example 4 with ClusterLoadAssignment

use of io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment in project grpc-java by grpc.

the class MessagePrinterTest method printEdsResponse_v3.

@Test
public void printEdsResponse_v3() {
    ClusterLoadAssignment clusterLoadAssignment = ClusterLoadAssignment.newBuilder().setClusterName("cluster-foo.googleapis.com").setPolicy(Policy.newBuilder().addDropOverloads(DropOverload.newBuilder().setCategory("throttle").setDropPercentage(FractionalPercent.newBuilder().setNumerator(80).setDenominator(DenominatorType.HUNDRED)))).addEndpoints(LocalityLbEndpoints.newBuilder().setLocality(Locality.newBuilder().setRegion("region").setZone("zone").setSubZone("subzone")).setPriority(1).setLoadBalancingWeight(UInt32Value.newBuilder().setValue(20)).addLbEndpoints(LbEndpoint.newBuilder().setLoadBalancingWeight(UInt32Value.newBuilder().setValue(100)).setHealthStatus(HealthStatus.UNHEALTHY).setEndpoint(Endpoint.newBuilder().setAddress(Address.newBuilder().setSocketAddress(SocketAddress.newBuilder().setAddress("10.0.0.1").setPortValue(8001)))))).build();
    DiscoveryResponse response = DiscoveryResponse.newBuilder().setTypeUrl("type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment").setVersionInfo("0").addResources(Any.pack(clusterLoadAssignment)).setNonce("0000").build();
    String expectedString = "{\n" + "  \"versionInfo\": \"0\",\n" + "  \"resources\": [{\n" + "    \"@type\": \"type.googleapis.com/envoy.config.endpoint.v3" + ".ClusterLoadAssignment\",\n" + "    \"clusterName\": \"cluster-foo.googleapis.com\",\n" + "    \"endpoints\": [{\n" + "      \"locality\": {\n" + "        \"region\": \"region\",\n" + "        \"zone\": \"zone\",\n" + "        \"subZone\": \"subzone\"\n" + "      },\n" + "      \"lbEndpoints\": [{\n" + "        \"endpoint\": {\n" + "          \"address\": {\n" + "            \"socketAddress\": {\n" + "              \"address\": \"10.0.0.1\",\n" + "              \"portValue\": 8001\n" + "            }\n" + "          }\n" + "        },\n" + "        \"healthStatus\": \"UNHEALTHY\",\n" + "        \"loadBalancingWeight\": 100\n" + "      }],\n" + "      \"loadBalancingWeight\": 20,\n" + "      \"priority\": 1\n" + "    }],\n" + "    \"policy\": {\n" + "      \"dropOverloads\": [{\n" + "        \"category\": \"throttle\",\n" + "        \"dropPercentage\": {\n" + "          \"numerator\": 80\n" + "        }\n" + "      }]\n" + "    }\n" + "  }],\n" + "  \"typeUrl\": \"type.googleapis.com/envoy.config.endpoint.v3" + ".ClusterLoadAssignment\",\n" + "  \"nonce\": \"0000\"\n" + "}";
    String res = MessagePrinter.print(response);
    assertThat(res).isEqualTo(expectedString);
}
Also used : DiscoveryResponse(io.envoyproxy.envoy.service.discovery.v3.DiscoveryResponse) ClusterLoadAssignment(io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment) Test(org.junit.Test)

Aggregations

ClusterLoadAssignment (io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 SocketAddress (io.envoyproxy.envoy.config.core.v3.SocketAddress)2 LbEndpoint (io.grpc.xds.Endpoints.LbEndpoint)2 InetSocketAddress (java.net.InetSocketAddress)2 Any (com.google.protobuf.Any)1 Thresholds (io.envoyproxy.envoy.config.cluster.v3.CircuitBreakers.Thresholds)1 Cluster (io.envoyproxy.envoy.config.cluster.v3.Cluster)1 DiscoveryType (io.envoyproxy.envoy.config.cluster.v3.Cluster.DiscoveryType)1 Address (io.envoyproxy.envoy.config.core.v3.Address)1 LocalityLbEndpoints (io.envoyproxy.envoy.config.endpoint.v3.LocalityLbEndpoints)1 DiscoveryResponse (io.envoyproxy.envoy.service.discovery.v3.DiscoveryResponse)1 ServerInfo (io.grpc.xds.Bootstrapper.ServerInfo)1 UpstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1