Search in sources :

Example 11 with Cluster

use of io.envoyproxy.envoy.config.cluster.v3.Cluster 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 12 with Cluster

use of io.envoyproxy.envoy.config.cluster.v3.Cluster in project grpc-java by grpc.

the class MessagePrinterTest method printCdsResponse_v3.

@Test
public void printCdsResponse_v3() {
    Cluster cluster = Cluster.newBuilder().setName("cluster-foo.googleapis.com").setEdsClusterConfig(EdsClusterConfig.newBuilder().setServiceName("backend-service-foo.googleapis.com").setEdsConfig(ConfigSource.newBuilder().setAds(AggregatedConfigSource.getDefaultInstance()))).setLrsServer(ConfigSource.newBuilder().setSelf(SelfConfigSource.getDefaultInstance())).setLbPolicy(LbPolicy.ROUND_ROBIN).setTransportSocket(TransportSocket.newBuilder().setTypedConfig(Any.pack(UpstreamTlsContext.newBuilder().setCommonTlsContext(CommonTlsContext.newBuilder().addTlsCertificateSdsSecretConfigs(SdsSecretConfig.getDefaultInstance())).build()))).build();
    DiscoveryResponse response = DiscoveryResponse.newBuilder().setTypeUrl("type.googleapis.com/envoy.config.cluster.v3.Cluster").setVersionInfo("0").addResources(Any.pack(cluster)).setNonce("0000").build();
    String expectedString = "{\n" + "  \"versionInfo\": \"0\",\n" + "  \"resources\": [{\n" + "    \"@type\": \"type.googleapis.com/envoy.config.cluster.v3.Cluster\",\n" + "    \"name\": \"cluster-foo.googleapis.com\",\n" + "    \"edsClusterConfig\": {\n" + "      \"edsConfig\": {\n" + "        \"ads\": {\n" + "        }\n" + "      },\n" + "      \"serviceName\": \"backend-service-foo.googleapis.com\"\n" + "    },\n" + "    \"transportSocket\": {\n" + "      \"typedConfig\": {\n" + "        \"@type\": \"type.googleapis.com/envoy.extensions.transport_sockets.tls.v3" + ".UpstreamTlsContext\",\n" + "        \"commonTlsContext\": {\n" + "          \"tlsCertificateSdsSecretConfigs\": [{\n" + "          }]\n" + "        }\n" + "      }\n" + "    },\n" + "    \"lrsServer\": {\n" + "      \"self\": {\n" + "      }\n" + "    }\n" + "  }],\n" + "  \"typeUrl\": \"type.googleapis.com/envoy.config.cluster.v3.Cluster\",\n" + "  \"nonce\": \"0000\"\n" + "}";
    String res = MessagePrinter.print(response);
    assertThat(res).isEqualTo(expectedString);
}
Also used : DiscoveryResponse(io.envoyproxy.envoy.service.discovery.v3.DiscoveryResponse) Cluster(io.envoyproxy.envoy.config.cluster.v3.Cluster) Test(org.junit.Test)

Aggregations

Cluster (io.envoyproxy.envoy.config.cluster.v3.Cluster)10 Test (org.junit.Test)8 WeightedCluster (io.envoyproxy.envoy.config.route.v3.WeightedCluster)7 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 LbEndpoint (io.grpc.xds.Endpoints.LbEndpoint)3 CdsUpdate (io.grpc.xds.XdsClient.CdsUpdate)2 HashSet (java.util.HashSet)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Any (com.google.protobuf.Any)1 Thresholds (io.envoyproxy.envoy.config.cluster.v3.CircuitBreakers.Thresholds)1 CustomClusterType (io.envoyproxy.envoy.config.cluster.v3.Cluster.CustomClusterType)1 DiscoveryType (io.envoyproxy.envoy.config.cluster.v3.Cluster.DiscoveryType)1 LeastRequestLbConfig (io.envoyproxy.envoy.config.cluster.v3.Cluster.LeastRequestLbConfig)1 RingHashLbConfig (io.envoyproxy.envoy.config.cluster.v3.Cluster.RingHashLbConfig)1 SocketAddress (io.envoyproxy.envoy.config.core.v3.SocketAddress)1 ClusterLoadAssignment (io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment)1 DiscoveryResponse (io.envoyproxy.envoy.service.discovery.v3.DiscoveryResponse)1 ServerInfo (io.grpc.xds.Bootstrapper.ServerInfo)1 UpstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext)1 ClientInterceptorBuilder (io.grpc.xds.Filter.ClientInterceptorBuilder)1