-
Notifications
You must be signed in to change notification settings - Fork 1k
Implement xDS virtual host routing #6322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 0 additions & 79 deletions
79
it/xds-client/src/test/java/com/linecorp/armeria/xds/it/RoutingTest.java
This file was deleted.
Oops, something went wrong.
283 changes: 283 additions & 0 deletions
283
it/xds-client/src/test/java/com/linecorp/armeria/xds/it/VirtualHostRoutingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,283 @@ | ||
| /* | ||
| * Copyright 2025 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.armeria.xds.it; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.util.stream.Stream; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import com.linecorp.armeria.client.Endpoint; | ||
| import com.linecorp.armeria.client.UnprocessedRequestException; | ||
| import com.linecorp.armeria.client.WebClient; | ||
| import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
| import com.linecorp.armeria.common.HttpHeaderNames; | ||
| import com.linecorp.armeria.common.HttpMethod; | ||
| import com.linecorp.armeria.common.HttpRequest; | ||
| import com.linecorp.armeria.common.RequestHeaders; | ||
| import com.linecorp.armeria.common.annotation.Nullable; | ||
| import com.linecorp.armeria.xds.XdsBootstrap; | ||
| import com.linecorp.armeria.xds.client.endpoint.XdsHttpPreprocessor; | ||
|
|
||
| class VirtualHostRoutingTest { | ||
|
|
||
| private static final String virtualHostRoutingBootstrap = | ||
| """ | ||
| static_resources: | ||
| listeners: | ||
| - name: my-listener | ||
| api_listener: | ||
| api_listener: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager\ | ||
| .v3.HttpConnectionManager | ||
| route_config: | ||
| name: local_route | ||
| virtual_hosts: | ||
| - name: local_service1 | ||
| domains: %s | ||
| routes: | ||
| - match: | ||
| prefix: "/" | ||
| route: | ||
| cluster: my-cluster1 | ||
| - name: local_service2 | ||
| domains: %s | ||
| routes: | ||
| - match: | ||
| prefix: "/" | ||
| route: | ||
| cluster: my-cluster2 | ||
| http_filters: | ||
| - name: envoy.filters.http.router | ||
| clusters: | ||
| - name: my-cluster1 | ||
| type: STATIC | ||
| load_assignment: | ||
| endpoints: | ||
| - lb_endpoints: | ||
| - endpoint: | ||
| address: | ||
| socket_address: | ||
| address: 127.0.0.1 | ||
| port_value: 8081 | ||
| - name: my-cluster2 | ||
| type: STATIC | ||
| load_assignment: | ||
| endpoints: | ||
| - lb_endpoints: | ||
| - endpoint: | ||
| address: | ||
| socket_address: | ||
| address: 127.0.0.1 | ||
| port_value: 8082 | ||
| """; | ||
|
|
||
| static Stream<Arguments> vHostRouting_args() { | ||
| return Stream.of( | ||
| // exact match | ||
| Arguments.of("[ 'foo.com' ]", "[ 'bar.com' ]", "foo.com", 8081), | ||
| Arguments.of("[ 'foo.com' ]", "[ 'bar.com' ]", "bar.com", 8082), | ||
| Arguments.of("[ '*' ]", "[ 'bar.com' ]", "bar.com", 8082), | ||
| Arguments.of("[ 'foo.com', '*' ]", "[ 'bar.com' ]", "bar.com", 8082), | ||
| // exact preferred over prefix/suffix match | ||
| Arguments.of("[ '*ar.com', '*' ]", "[ 'bar.com' ]", "bar.com", 8082), | ||
| Arguments.of("[ 'bar.co*', '*' ]", "[ 'bar.com' ]", "bar.com", 8082), | ||
| // suffix match | ||
| Arguments.of("[ '*' ]", "[ '*foo.com' ]", "afoo.com", 8082), | ||
| Arguments.of("[ '*' ]", "[ '*foo.com' ]", "foo.com", 8081), | ||
| Arguments.of("[ '*' ]", "[ '*foo.com' ]", "oo.com", 8081), | ||
| // longer suffix is matched | ||
| Arguments.of("[ '*o.com' ]", "[ '*oo.com' ]", "foo.com", 8082), | ||
| // prefix match | ||
| Arguments.of("[ '*' ]", "[ 'foo.com*' ]", "foo.coma", 8082), | ||
| Arguments.of("[ '*' ]", "[ 'foo.com*' ]", "foo.com", 8081), | ||
| Arguments.of("[ '*' ]", "[ 'foo.com*' ]", "foo.co", 8081), | ||
| // longer prefix is matched | ||
| Arguments.of("[ 'foo.c*' ]", "[ 'foo.co*' ]", "foo.com", 8082), | ||
| // default vhost | ||
| Arguments.of("[ 'foo.com' ]", "[ '*' ]", "bar.com", 8082), | ||
| Arguments.of("[ 'foo.com' ]", "[ '*' ]", null, 8082), | ||
| // port ignore disabled by default | ||
| Arguments.of("[ '*' ]", "[ 'foo.com:8082' ]", "foo.com:8082", 8082), | ||
| Arguments.of("[ '*' ]", "[ 'foo.com' ]", "foo.com:8082", 8081), | ||
| Arguments.of("[ '*' ]", "[ '[::]:8082' ]", "[::]:8082", 8082), | ||
| Arguments.of("[ '*' ]", "[ '[::]' ]", "[::]:8082", 8081) | ||
| ); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("vHostRouting_args") | ||
| void vHostRouting(String vhostPattern1, String vhostPattern2, | ||
| @Nullable String hostHeader, int expectedPort) { | ||
| final String bootstrap = virtualHostRoutingBootstrap.formatted(vhostPattern1, vhostPattern2); | ||
| final EndpointCollectingDecorator collector = new EndpointCollectingDecorator(); | ||
| try (XdsBootstrap xdsBootstrap = XdsBootstrap.of(XdsResourceReader.fromYaml(bootstrap)); | ||
| XdsHttpPreprocessor preprocessor = XdsHttpPreprocessor.ofListener("my-listener", xdsBootstrap)) { | ||
| final RequestHeaders requestHeaders; | ||
| if (hostHeader == null) { | ||
| requestHeaders = RequestHeaders.of(HttpMethod.GET, "/"); | ||
| } else { | ||
| requestHeaders = RequestHeaders.of(HttpMethod.GET, "/", HttpHeaderNames.AUTHORITY, hostHeader); | ||
| } | ||
| final AggregatedHttpResponse res = WebClient.builder(preprocessor).decorator(collector) | ||
| .build().blocking() | ||
| .execute(HttpRequest.of(requestHeaders)); | ||
| assertThat(res.status().code()).isEqualTo(200); | ||
| assertThat(collector.endpointsQueue()).hasSize(1); | ||
| final Endpoint endpoint = collector.endpointsQueue().poll(); | ||
| assertThat(endpoint.port()).isEqualTo(expectedPort); | ||
| } | ||
| } | ||
|
|
||
| private static final String ignorePortBootstrap = | ||
| """ | ||
| static_resources: | ||
| listeners: | ||
| - name: my-listener | ||
| api_listener: | ||
| api_listener: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager\ | ||
| .v3.HttpConnectionManager | ||
| route_config: | ||
| name: local_route | ||
| ignore_port_in_host_matching: true | ||
| virtual_hosts: | ||
| - name: local_service1 | ||
| domains: %s | ||
| routes: | ||
| - match: | ||
| prefix: "/" | ||
| route: | ||
| cluster: my-cluster1 | ||
| - name: local_service2 | ||
| domains: %s | ||
| routes: | ||
| - match: | ||
| prefix: "/" | ||
| route: | ||
| cluster: my-cluster2 | ||
| http_filters: | ||
| - name: envoy.filters.http.router | ||
| clusters: | ||
| - name: my-cluster1 | ||
| type: STATIC | ||
| load_assignment: | ||
| endpoints: | ||
| - lb_endpoints: | ||
| - endpoint: | ||
| address: | ||
| socket_address: | ||
| address: 127.0.0.1 | ||
| port_value: 8081 | ||
| - name: my-cluster2 | ||
| type: STATIC | ||
| load_assignment: | ||
| endpoints: | ||
| - lb_endpoints: | ||
| - endpoint: | ||
| address: | ||
| socket_address: | ||
| address: 127.0.0.1 | ||
| port_value: 8082 | ||
| """; | ||
|
|
||
| static Stream<Arguments> ignorePortRouting_args() { | ||
| return Stream.of( | ||
| Arguments.of("[ '*' ]", "[ 'foo.com:8082' ]", "foo.com:8082", 8081), | ||
| Arguments.of("[ '*' ]", "[ 'foo.com' ]", "foo.com:8082", 8082), | ||
| Arguments.of("[ '*' ]", "[ '[::]:8082' ]", "[::]:8082", 8081), | ||
| Arguments.of("[ '*' ]", "[ '[::]' ]", "[::]:8082", 8082) | ||
| ); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("ignorePortRouting_args") | ||
| void ignorePortRouting(String vhostPattern1, String vhostPattern2, | ||
| @Nullable String hostHeader, int expectedPort) { | ||
| final String bootstrap = ignorePortBootstrap.formatted(vhostPattern1, vhostPattern2); | ||
| final EndpointCollectingDecorator collector = new EndpointCollectingDecorator(); | ||
| try (XdsBootstrap xdsBootstrap = XdsBootstrap.of(XdsResourceReader.fromYaml(bootstrap)); | ||
| XdsHttpPreprocessor preprocessor = XdsHttpPreprocessor.ofListener("my-listener", xdsBootstrap)) { | ||
| final RequestHeaders requestHeaders; | ||
| if (hostHeader == null) { | ||
| requestHeaders = RequestHeaders.of(HttpMethod.GET, "/"); | ||
| } else { | ||
| requestHeaders = RequestHeaders.of(HttpMethod.GET, "/", HttpHeaderNames.AUTHORITY, hostHeader); | ||
| } | ||
| final AggregatedHttpResponse res = WebClient.builder(preprocessor).decorator(collector) | ||
| .build().blocking() | ||
| .execute(HttpRequest.of(requestHeaders)); | ||
| assertThat(res.status().code()).isEqualTo(200); | ||
| assertThat(collector.endpointsQueue()).hasSize(1); | ||
| final Endpoint endpoint = collector.endpointsQueue().poll(); | ||
| assertThat(endpoint.port()).isEqualTo(expectedPort); | ||
| } | ||
| } | ||
|
|
||
| private static final String noMatchBootstrap = | ||
| """ | ||
| static_resources: | ||
| listeners: | ||
| - name: my-listener | ||
| api_listener: | ||
| api_listener: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager\ | ||
| .v3.HttpConnectionManager | ||
| route_config: | ||
| name: local_route | ||
| virtual_hosts: | ||
| - name: local_service1 | ||
| domains: [ "foo.com" ] | ||
| routes: | ||
| - match: | ||
| prefix: "/" | ||
| route: | ||
| cluster: my-cluster | ||
| http_filters: | ||
| - name: envoy.filters.http.router | ||
| clusters: | ||
| - name: my-cluster | ||
| type: STATIC | ||
| load_assignment: | ||
| cluster_name: my-cluster | ||
| endpoints: | ||
| - lb_endpoints: | ||
| - endpoint: | ||
| address: | ||
| socket_address: | ||
| address: 127.0.0.1 | ||
| port_value: 8081 | ||
| """; | ||
|
|
||
| @Test | ||
| void noMatchTest() { | ||
| try (XdsBootstrap xdsBootstrap = XdsBootstrap.of(XdsResourceReader.fromYaml(noMatchBootstrap)); | ||
| XdsHttpPreprocessor preprocessor = | ||
| XdsHttpPreprocessor.ofListener("my-listener", xdsBootstrap)) { | ||
| assertThatThrownBy(() -> WebClient.of(preprocessor).blocking().get("/")) | ||
| .isInstanceOf(UnprocessedRequestException.class) | ||
| .cause() | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessageStartingWith("No route has been selected for listener"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.