Search in sources :

Example 1 with Route

use of io.grpc.xds.VirtualHost.Route in project grpc-java by grpc.

the class ClientXdsClientDataTest method parseHttpConnectionManager_clusterSpecifierPlugin.

@Test
public void parseHttpConnectionManager_clusterSpecifierPlugin() throws Exception {
    ClientXdsClient.enableRouteLookup = true;
    RouteLookupConfig routeLookupConfig = RouteLookupConfig.newBuilder().addGrpcKeybuilders(GrpcKeyBuilder.newBuilder().addNames(Name.newBuilder().setService("service1")).addNames(Name.newBuilder().setService("service2")).addHeaders(NameMatcher.newBuilder().setKey("key1").addNames("v1").setRequiredMatch(true))).setLookupService("rls-cbt.googleapis.com").setLookupServiceTimeout(Durations.fromMillis(1234)).setCacheSizeBytes(5000).addValidTargets("valid-target").build();
    RouteLookupClusterSpecifier specifier = RouteLookupClusterSpecifier.newBuilder().setRouteLookupConfig(routeLookupConfig).build();
    TypedExtensionConfig typedExtensionConfig = TypedExtensionConfig.newBuilder().setName("rls-plugin-1").setTypedConfig(Any.pack(specifier)).build();
    io.envoyproxy.envoy.config.route.v3.Route route = io.envoyproxy.envoy.config.route.v3.Route.newBuilder().setName("route-1").setMatch(io.envoyproxy.envoy.config.route.v3.RouteMatch.newBuilder().setPrefix("")).setRoute(io.envoyproxy.envoy.config.route.v3.RouteAction.newBuilder().setClusterSpecifierPlugin("rls-plugin-1")).build();
    HttpConnectionManager hcm = HttpConnectionManager.newBuilder().setRouteConfig(RouteConfiguration.newBuilder().addClusterSpecifierPlugins(io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin.newBuilder().setExtension(typedExtensionConfig).build()).addVirtualHosts(io.envoyproxy.envoy.config.route.v3.VirtualHost.newBuilder().setName("virtual-host-1").addRoutes(route))).build();
    io.grpc.xds.HttpConnectionManager parsedHcm = ClientXdsClient.parseHttpConnectionManager(hcm, new HashSet<String>(), filterRegistry, false, /* parseHttpFilter */
    true);
    VirtualHost virtualHost = Iterables.getOnlyElement(parsedHcm.virtualHosts());
    Route parsedRoute = Iterables.getOnlyElement(virtualHost.routes());
    NamedPluginConfig namedPluginConfig = parsedRoute.routeAction().namedClusterSpecifierPluginConfig();
    assertThat(namedPluginConfig.name()).isEqualTo("rls-plugin-1");
    assertThat(namedPluginConfig.config()).isInstanceOf(RlsPluginConfig.class);
}
Also used : RouteLookupClusterSpecifier(io.grpc.lookup.v1.RouteLookupClusterSpecifier) RouteLookupConfig(io.grpc.lookup.v1.RouteLookupConfig) NamedPluginConfig(io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig) HttpConnectionManager(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager) TypedExtensionConfig(io.envoyproxy.envoy.config.core.v3.TypedExtensionConfig) RBACPerRoute(io.envoyproxy.envoy.extensions.filters.http.rbac.v3.RBACPerRoute) Route(io.grpc.xds.VirtualHost.Route) Test(org.junit.Test)

Example 2 with Route

use of io.grpc.xds.VirtualHost.Route in project grpc-java by grpc.

the class XdsServerWrapperTest method interceptor_invalidRouteAction.

@Test
@SuppressWarnings("unchecked")
public void interceptor_invalidRouteAction() throws Exception {
    ArgumentCaptor<ConfigApplyingInterceptor> interceptorCaptor = ArgumentCaptor.forClass(ConfigApplyingInterceptor.class);
    final SettableFuture<Server> start = SettableFuture.create();
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            try {
                start.set(xdsServerWrapper.start());
            } catch (Exception ex) {
                start.setException(ex);
            }
        }
    });
    xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
    verify(mockBuilder).intercept(interceptorCaptor.capture());
    ConfigApplyingInterceptor interceptor = interceptorCaptor.getValue();
    ServerRoutingConfig routingConfig = createRoutingConfig("/FooService/barMethod", "foo.google.com", "filter-type-url", Route.RouteAction.forCluster("cluster", Collections.<Route.RouteAction.HashPolicy>emptyList(), null, null));
    ServerCall<Void, Void> serverCall = mock(ServerCall.class);
    when(serverCall.getAttributes()).thenReturn(Attributes.newBuilder().set(ATTR_SERVER_ROUTING_CONFIG, new AtomicReference<>(routingConfig)).build());
    when(serverCall.getMethodDescriptor()).thenReturn(createMethod("FooService/barMethod"));
    when(serverCall.getAuthority()).thenReturn("foo.google.com");
    Filter filter = mock(Filter.class);
    when(filter.typeUrls()).thenReturn(new String[] { "filter-type-url" });
    filterRegistry.register(filter);
    ServerCallHandler<Void, Void> next = mock(ServerCallHandler.class);
    interceptor.interceptCall(serverCall, new Metadata(), next);
    verify(next, never()).startCall(any(ServerCall.class), any(Metadata.class));
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(serverCall).close(statusCaptor.capture(), any(Metadata.class));
    Status status = statusCaptor.getValue();
    assertThat(status.getCode()).isEqualTo(Status.UNAVAILABLE.getCode());
    assertThat(status.getDescription()).isEqualTo("Invalid xDS route action for matching " + "route: only Route.non_forwarding_action should be allowed.");
}
Also used : Status(io.grpc.Status) ServerRoutingConfig(io.grpc.xds.XdsServerWrapper.ServerRoutingConfig) Server(io.grpc.Server) Metadata(io.grpc.Metadata) TimeoutException(java.util.concurrent.TimeoutException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConfigApplyingInterceptor(io.grpc.xds.XdsServerWrapper.ConfigApplyingInterceptor) ServerCall(io.grpc.ServerCall) Route(io.grpc.xds.VirtualHost.Route) Test(org.junit.Test)

Example 3 with Route

use of io.grpc.xds.VirtualHost.Route in project grpc-java by grpc.

the class XdsServerWrapperTest method interceptor_success.

@Test
@SuppressWarnings("unchecked")
public void interceptor_success() throws Exception {
    ArgumentCaptor<ConfigApplyingInterceptor> interceptorCaptor = ArgumentCaptor.forClass(ConfigApplyingInterceptor.class);
    final SettableFuture<Server> start = SettableFuture.create();
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            try {
                start.set(xdsServerWrapper.start());
            } catch (Exception ex) {
                start.setException(ex);
            }
        }
    });
    xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
    verify(mockBuilder).intercept(interceptorCaptor.capture());
    ConfigApplyingInterceptor interceptor = interceptorCaptor.getValue();
    RouteMatch routeMatch = RouteMatch.create(PathMatcher.fromPath("/FooService/barMethod", true), Collections.<HeaderMatcher>emptyList(), null);
    Route route = Route.forAction(routeMatch, null, ImmutableMap.<String, FilterConfig>of());
    VirtualHost virtualHost = VirtualHost.create("v1", Collections.singletonList("foo.google.com"), Arrays.asList(route), ImmutableMap.<String, FilterConfig>of());
    final List<Integer> interceptorTrace = new ArrayList<>();
    ServerInterceptor interceptor0 = new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            interceptorTrace.add(0);
            return next.startCall(call, headers);
        }
    };
    ServerRoutingConfig realConfig = ServerRoutingConfig.create(ImmutableList.of(virtualHost), ImmutableMap.of(route, interceptor0));
    ServerCall<Void, Void> serverCall = mock(ServerCall.class);
    when(serverCall.getMethodDescriptor()).thenReturn(createMethod("FooService/barMethod"));
    when(serverCall.getAttributes()).thenReturn(Attributes.newBuilder().set(ATTR_SERVER_ROUTING_CONFIG, new AtomicReference<>(realConfig)).build());
    when(serverCall.getAuthority()).thenReturn("foo.google.com");
    ServerCallHandler<Void, Void> next = mock(ServerCallHandler.class);
    interceptor.interceptCall(serverCall, new Metadata(), next);
    verify(next).startCall(eq(serverCall), any(Metadata.class));
    assertThat(interceptorTrace).isEqualTo(Arrays.asList(0));
}
Also used : ServerRoutingConfig(io.grpc.xds.XdsServerWrapper.ServerRoutingConfig) Server(io.grpc.Server) ServerCallHandler(io.grpc.ServerCallHandler) ArrayList(java.util.ArrayList) Metadata(io.grpc.Metadata) TimeoutException(java.util.concurrent.TimeoutException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RouteMatch(io.grpc.xds.VirtualHost.Route.RouteMatch) ConfigApplyingInterceptor(io.grpc.xds.XdsServerWrapper.ConfigApplyingInterceptor) ServerCall(io.grpc.ServerCall) ServerInterceptor(io.grpc.ServerInterceptor) Route(io.grpc.xds.VirtualHost.Route) Test(org.junit.Test)

Example 4 with Route

use of io.grpc.xds.VirtualHost.Route in project grpc-java by grpc.

the class XdsServerWrapperTest method buildInterceptor_rds.

@Test
@SuppressWarnings("unchecked")
public void buildInterceptor_rds() throws Exception {
    final SettableFuture<Server> start = SettableFuture.create();
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            try {
                start.set(xdsServerWrapper.start());
            } catch (Exception ex) {
                start.setException(ex);
            }
        }
    });
    xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
    Filter filter = mock(Filter.class, withSettings().extraInterfaces(ServerInterceptorBuilder.class));
    when(filter.typeUrls()).thenReturn(new String[] { "filter-type-url" });
    filterRegistry.register(filter);
    FilterConfig f0 = mock(FilterConfig.class);
    FilterConfig f0Override = mock(FilterConfig.class);
    when(f0.typeUrl()).thenReturn("filter-type-url");
    final List<Integer> interceptorTrace = new ArrayList<>();
    ServerInterceptor interceptor0 = new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            interceptorTrace.add(0);
            return next.startCall(call, headers);
        }
    };
    ServerInterceptor interceptor1 = new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            interceptorTrace.add(1);
            return next.startCall(call, headers);
        }
    };
    when(((ServerInterceptorBuilder) filter).buildServerInterceptor(f0, null)).thenReturn(interceptor0);
    when(((ServerInterceptorBuilder) filter).buildServerInterceptor(f0, f0Override)).thenReturn(interceptor1);
    RouteMatch routeMatch = RouteMatch.create(PathMatcher.fromPath("/FooService/barMethod", true), Collections.<HeaderMatcher>emptyList(), null);
    HttpConnectionManager rdsHcm = HttpConnectionManager.forRdsName(0L, "r0", Arrays.asList(new NamedFilterConfig("filter-config-name-0", f0), new NamedFilterConfig("filter-config-name-1", f0)));
    EnvoyServerProtoData.FilterChain filterChain = createFilterChain("filter-chain-0", rdsHcm);
    xdsClient.deliverLdsUpdate(Collections.singletonList(filterChain), null);
    Route route = Route.forAction(routeMatch, null, ImmutableMap.<String, FilterConfig>of());
    VirtualHost virtualHost = VirtualHost.create("v1", Collections.singletonList("foo.google.com"), Arrays.asList(route), ImmutableMap.of("filter-config-name-0", f0Override));
    xdsClient.rdsCount.await(5, TimeUnit.SECONDS);
    xdsClient.deliverRdsUpdate("r0", Collections.singletonList(virtualHost));
    start.get(5000, TimeUnit.MILLISECONDS);
    verify(mockServer).start();
    assertThat(selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().size()).isEqualTo(1);
    ServerInterceptor realInterceptor = selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().get(filterChain).get().interceptors().get(route);
    assertThat(realInterceptor).isNotNull();
    ServerCall<Void, Void> serverCall = mock(ServerCall.class);
    ServerCallHandler<Void, Void> mockNext = mock(ServerCallHandler.class);
    final ServerCall.Listener<Void> listener = new ServerCall.Listener<Void>() {
    };
    when(mockNext.startCall(any(ServerCall.class), any(Metadata.class))).thenReturn(listener);
    realInterceptor.interceptCall(serverCall, new Metadata(), mockNext);
    assertThat(interceptorTrace).isEqualTo(Arrays.asList(1, 0));
    verify(mockNext).startCall(eq(serverCall), any(Metadata.class));
    virtualHost = VirtualHost.create("v1", Collections.singletonList("foo.google.com"), Arrays.asList(route), ImmutableMap.<String, FilterConfig>of());
    xdsClient.deliverRdsUpdate("r0", Collections.singletonList(virtualHost));
    realInterceptor = selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().get(filterChain).get().interceptors().get(route);
    assertThat(realInterceptor).isNotNull();
    interceptorTrace.clear();
    realInterceptor.interceptCall(serverCall, new Metadata(), mockNext);
    assertThat(interceptorTrace).isEqualTo(Arrays.asList(0, 0));
    verify(mockNext, times(2)).startCall(eq(serverCall), any(Metadata.class));
    xdsClient.rdsWatchers.get("r0").onResourceDoesNotExist("r0");
    assertThat(selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().get(filterChain).get()).isEqualTo(noopConfig);
}
Also used : XdsServingStatusListener(io.grpc.xds.XdsServerBuilder.XdsServingStatusListener) Server(io.grpc.Server) ServerCallHandler(io.grpc.ServerCallHandler) ArrayList(java.util.ArrayList) Metadata(io.grpc.Metadata) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) ServerCall(io.grpc.ServerCall) FilterConfig(io.grpc.xds.Filter.FilterConfig) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) ServerInterceptorBuilder(io.grpc.xds.Filter.ServerInterceptorBuilder) Route(io.grpc.xds.VirtualHost.Route) TimeoutException(java.util.concurrent.TimeoutException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RouteMatch(io.grpc.xds.VirtualHost.Route.RouteMatch) ServerInterceptor(io.grpc.ServerInterceptor) FilterChain(io.grpc.xds.EnvoyServerProtoData.FilterChain) Test(org.junit.Test)

Example 5 with Route

use of io.grpc.xds.VirtualHost.Route in project grpc-java by grpc.

the class XdsServerWrapperTest method discoverState_virtualhost.

@Test
public void discoverState_virtualhost() throws Exception {
    final SettableFuture<Server> start = SettableFuture.create();
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            try {
                start.set(xdsServerWrapper.start());
            } catch (Exception ex) {
                start.setException(ex);
            }
        }
    });
    String ldsWatched = xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
    assertThat(ldsWatched).isEqualTo("grpc/server?udpa.resource.listening_address=0.0.0.0:1");
    VirtualHost virtualHost = VirtualHost.create("virtual-host", Collections.singletonList("auth"), new ArrayList<Route>(), ImmutableMap.<String, FilterConfig>of());
    HttpConnectionManager httpConnectionManager = HttpConnectionManager.forVirtualHosts(0L, Collections.singletonList(virtualHost), new ArrayList<NamedFilterConfig>());
    EnvoyServerProtoData.FilterChain filterChain = EnvoyServerProtoData.FilterChain.create("filter-chain-foo", createMatch(), httpConnectionManager, createTls(), mock(TlsContextManager.class));
    xdsClient.deliverLdsUpdate(Collections.singletonList(filterChain), null);
    start.get(5000, TimeUnit.MILLISECONDS);
    assertThat(ldsWatched).isEqualTo("grpc/server?udpa.resource.listening_address=0.0.0.0:1");
    assertThat(selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().size()).isEqualTo(1);
    ServerRoutingConfig realConfig = selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().get(filterChain).get();
    assertThat(realConfig.virtualHosts()).isEqualTo(httpConnectionManager.virtualHosts());
    assertThat(realConfig.interceptors()).isEqualTo(ImmutableMap.of());
    verify(listener).onServing();
    verify(mockServer).start();
}
Also used : ServerRoutingConfig(io.grpc.xds.XdsServerWrapper.ServerRoutingConfig) Server(io.grpc.Server) TimeoutException(java.util.concurrent.TimeoutException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) FilterChain(io.grpc.xds.EnvoyServerProtoData.FilterChain) Route(io.grpc.xds.VirtualHost.Route) Test(org.junit.Test)

Aggregations

Route (io.grpc.xds.VirtualHost.Route)14 Test (org.junit.Test)12 NamedFilterConfig (io.grpc.xds.Filter.NamedFilterConfig)6 HashPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy)6 Server (io.grpc.Server)5 StatusException (io.grpc.StatusException)5 IOException (java.io.IOException)5 ExecutionException (java.util.concurrent.ExecutionException)5 TimeoutException (java.util.concurrent.TimeoutException)5 Metadata (io.grpc.Metadata)4 ServerCall (io.grpc.ServerCall)4 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)4 FilterConfig (io.grpc.xds.Filter.FilterConfig)4 ResolutionResult (io.grpc.NameResolver.ResolutionResult)3 ServerCallHandler (io.grpc.ServerCallHandler)3 ServerInterceptor (io.grpc.ServerInterceptor)3 RouteMatch (io.grpc.xds.VirtualHost.Route.RouteMatch)3 ServerRoutingConfig (io.grpc.xds.XdsServerWrapper.ServerRoutingConfig)3 ArrayList (java.util.ArrayList)3 InternalConfigSelector (io.grpc.InternalConfigSelector)2