Skip to content

Commit

Permalink
xds: fixed RouteConfiguration not supporting contain and stringMatcher (
Browse files Browse the repository at this point in the history
  • Loading branch information
olderwei authored Jan 23, 2023
1 parent 706646f commit b289519
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
39 changes: 6 additions & 33 deletions xds/src/main/java/io/grpc/xds/XdsRouteConfigureResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
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;
Expand Down Expand Up @@ -392,39 +393,11 @@ private static StructOrError<FractionMatcher> parseFractionMatcher(FractionalPer
@VisibleForTesting
static StructOrError<HeaderMatcher> 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());
}
}

Expand Down
23 changes: 23 additions & 0 deletions xds/src/test/java/io/grpc/xds/XdsClientImplDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
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;
Expand Down Expand Up @@ -481,6 +482,28 @@ public void parseHeaderMatcher_malformedRegExPattern() {
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<HeaderMatcher> 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 =
Expand Down

0 comments on commit b289519

Please sign in to comment.