Skip to content

Commit 5fda0c7

Browse files
authored
xds: allow wildcard listener port in LDS
1 parent e9a8c2b commit 5fda0c7

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎xds/src/main/java/io/grpc/xds/XdsServerWrapper.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,11 @@ public void onAmbientError(final Status error) {
663663
private boolean ipAddressesMatch(String ldsAddress) {
664664
HostAndPort ldsAddressHnP = HostAndPort.fromString(ldsAddress);
665665
HostAndPort listenerAddressHnP = HostAndPort.fromString(listenerAddress);
666+
// A port value of 0 in the xDS Listener matches any listening port, but the
667+
// listener address itself must still match exactly.
666668
if (!ldsAddressHnP.hasPort() || !listenerAddressHnP.hasPort()
667-
|| ldsAddressHnP.getPort() != listenerAddressHnP.getPort()) {
669+
|| (ldsAddressHnP.getPort() != 0
670+
&& ldsAddressHnP.getPort() != listenerAddressHnP.getPort())) {
668671
return false;
669672
}
670673
InetAddress listenerIp = InetAddresses.forString(listenerAddressHnP.getHost());

‎xds/src/test/java/io/grpc/xds/XdsServerWrapperTest.java‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,9 @@ public void run() {
748748
EnvoyServerProtoData.FilterChain filterChain = EnvoyServerProtoData.FilterChain.create(
749749
"filter-chain-foo", createMatch(), httpConnectionManager, createTls(),
750750
mock(TlsContextManager.class));
751+
// A wildcard port must not make a mismatched listener address valid.
751752
LdsUpdate listenerUpdate = LdsUpdate.forTcpListener(
752-
Listener.create("listener", "20.3.4.5:1",
753+
Listener.create("listener", "20.3.4.5:0",
753754
ImmutableList.copyOf(Collections.singletonList(filterChain)), null, Protocol.TCP));
754755

755756
xdsClient.deliverLdsUpdate(listenerUpdate);
@@ -795,6 +796,44 @@ public void run() {
795796
verify(listener, timeout(10000)).onNotServing(any());
796797
}
797798

799+
@Test
800+
public void onChanged_listenerAddressWildcardPort()
801+
throws ExecutionException, InterruptedException, TimeoutException {
802+
xdsServerWrapper = new XdsServerWrapper("10.1.2.3:1", mockBuilder, listener,
803+
selectorManager, new FakeXdsClientPoolFactory(xdsClient),
804+
XdsServerTestHelper.RAW_BOOTSTRAP,
805+
filterRegistry, executor.getScheduledExecutorService());
806+
final SettableFuture<Server> start = SettableFuture.create();
807+
Executors.newSingleThreadExecutor().execute(new Runnable() {
808+
@Override
809+
public void run() {
810+
try {
811+
start.set(xdsServerWrapper.start());
812+
} catch (Exception ex) {
813+
start.setException(ex);
814+
}
815+
}
816+
});
817+
String ldsResource = xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
818+
assertThat(ldsResource).isEqualTo("grpc/server?udpa.resource.listening_address=10.1.2.3:1");
819+
VirtualHost virtualHost =
820+
VirtualHost.create(
821+
"virtual-host", Collections.singletonList("auth"), new ArrayList<Route>(),
822+
ImmutableMap.<String, FilterConfig>of());
823+
HttpConnectionManager httpConnectionManager = HttpConnectionManager.forVirtualHosts(
824+
0L, Collections.singletonList(virtualHost), new ArrayList<NamedFilterConfig>());
825+
EnvoyServerProtoData.FilterChain filterChain = EnvoyServerProtoData.FilterChain.create(
826+
"filter-chain-foo", createMatch(), httpConnectionManager, createTls(),
827+
mock(TlsContextManager.class));
828+
LdsUpdate listenerUpdate = LdsUpdate.forTcpListener(
829+
Listener.create("listener", "10.1.2.3:0",
830+
ImmutableList.copyOf(Collections.singletonList(filterChain)), null, Protocol.TCP));
831+
832+
xdsClient.deliverLdsUpdate(listenerUpdate);
833+
834+
verify(listener, timeout(10000)).onServing();
835+
}
836+
798837
@Test
799838
public void discoverState_rds() throws Exception {
800839
final SettableFuture<Server> start = SettableFuture.create();

0 commit comments

Comments
 (0)