From b2895198c3992e668d9ba312fcdaaaf5b9e0d1f0 Mon Sep 17 00:00:00 2001 From: chenwei321 Date: Tue, 24 Jan 2023 00:49:56 +0800 Subject: [PATCH] xds: fixed RouteConfiguration not supporting contain and stringMatcher (#9845) --- .../grpc/xds/XdsRouteConfigureResource.java | 39 +++---------------- .../io/grpc/xds/XdsClientImplDataTest.java | 23 +++++++++++ 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/xds/src/main/java/io/grpc/xds/XdsRouteConfigureResource.java b/xds/src/main/java/io/grpc/xds/XdsRouteConfigureResource.java index 35d96be435..ed109fd694 100644 --- a/xds/src/main/java/io/grpc/xds/XdsRouteConfigureResource.java +++ b/xds/src/main/java/io/grpc/xds/XdsRouteConfigureResource.java @@ -50,6 +50,7 @@ import io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher; import io.grpc.xds.XdsClient.ResourceUpdate; import io.grpc.xds.XdsClientImpl.ResourceInvalidException; import io.grpc.xds.XdsRouteConfigureResource.RdsUpdate; +import io.grpc.xds.internal.MatcherParser; import io.grpc.xds.internal.Matchers; import io.grpc.xds.internal.Matchers.FractionMatcher; import io.grpc.xds.internal.Matchers.HeaderMatcher; @@ -392,39 +393,11 @@ class XdsRouteConfigureResource extends XdsResourceType { @VisibleForTesting static StructOrError parseHeaderMatcher( io.envoyproxy.envoy.config.route.v3.HeaderMatcher proto) { - switch (proto.getHeaderMatchSpecifierCase()) { - case EXACT_MATCH: - return StructOrError.fromStruct(HeaderMatcher.forExactValue( - proto.getName(), proto.getExactMatch(), proto.getInvertMatch())); - case SAFE_REGEX_MATCH: - String rawPattern = proto.getSafeRegexMatch().getRegex(); - Pattern safeRegExMatch; - try { - safeRegExMatch = Pattern.compile(rawPattern); - } catch (PatternSyntaxException e) { - return StructOrError.fromError( - "HeaderMatcher [" + proto.getName() + "] contains malformed safe regex pattern: " - + e.getMessage()); - } - return StructOrError.fromStruct(Matchers.HeaderMatcher.forSafeRegEx( - proto.getName(), safeRegExMatch, proto.getInvertMatch())); - case RANGE_MATCH: - Matchers.HeaderMatcher.Range rangeMatch = Matchers.HeaderMatcher.Range.create( - proto.getRangeMatch().getStart(), proto.getRangeMatch().getEnd()); - return StructOrError.fromStruct(Matchers.HeaderMatcher.forRange( - proto.getName(), rangeMatch, proto.getInvertMatch())); - case PRESENT_MATCH: - return StructOrError.fromStruct(Matchers.HeaderMatcher.forPresent( - proto.getName(), proto.getPresentMatch(), proto.getInvertMatch())); - case PREFIX_MATCH: - return StructOrError.fromStruct(Matchers.HeaderMatcher.forPrefix( - proto.getName(), proto.getPrefixMatch(), proto.getInvertMatch())); - case SUFFIX_MATCH: - return StructOrError.fromStruct(Matchers.HeaderMatcher.forSuffix( - proto.getName(), proto.getSuffixMatch(), proto.getInvertMatch())); - case HEADERMATCHSPECIFIER_NOT_SET: - default: - return StructOrError.fromError("Unknown header matcher type"); + try { + Matchers.HeaderMatcher headerMatcher = MatcherParser.parseHeaderMatcher(proto); + return StructOrError.fromStruct(headerMatcher); + } catch (IllegalArgumentException e) { + return StructOrError.fromError(e.getMessage()); } } diff --git a/xds/src/test/java/io/grpc/xds/XdsClientImplDataTest.java b/xds/src/test/java/io/grpc/xds/XdsClientImplDataTest.java index f15647a3ff..051d890aea 100644 --- a/xds/src/test/java/io/grpc/xds/XdsClientImplDataTest.java +++ b/xds/src/test/java/io/grpc/xds/XdsClientImplDataTest.java @@ -131,6 +131,7 @@ import io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher; import io.grpc.xds.XdsClientImpl.ResourceInvalidException; import io.grpc.xds.XdsClusterResource.CdsUpdate; import io.grpc.xds.XdsResourceType.StructOrError; +import io.grpc.xds.internal.Matchers; import io.grpc.xds.internal.Matchers.FractionMatcher; import io.grpc.xds.internal.Matchers.HeaderMatcher; import java.util.Arrays; @@ -481,6 +482,28 @@ public class XdsClientImplDataTest { assertThat(struct.getStruct()).isNull(); } + @Test + @SuppressWarnings("deprecation") + public void parseHeaderMatcher_withStringMatcher() { + io.envoyproxy.envoy.type.matcher.v3.StringMatcher stringMatcherProto = + io.envoyproxy.envoy.type.matcher.v3.StringMatcher.newBuilder() + .setPrefix("service-foo") + .setIgnoreCase(false) + .build(); + + io.envoyproxy.envoy.config.route.v3.HeaderMatcher proto = + io.envoyproxy.envoy.config.route.v3.HeaderMatcher.newBuilder() + .setName("authority") + .setStringMatch(stringMatcherProto) + .setInvertMatch(false) + .build(); + StructOrError struct = XdsRouteConfigureResource.parseHeaderMatcher(proto); + assertThat(struct.getErrorDetail()).isNull(); + assertThat(struct.getStruct()).isEqualTo( + HeaderMatcher.forString("authority", Matchers.StringMatcher + .forPrefix("service-foo", false), false)); + } + @Test public void parseRouteAction_withCluster() { io.envoyproxy.envoy.config.route.v3.RouteAction proto =