use of io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig 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);
}
use of io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig in project grpc-java by grpc.
the class ClientXdsClient method parseRouteAction.
/**
* Parses the RouteAction config. The returned result may contain a (parsed form)
* {@link RouteAction} or an error message. Returns {@code null} if the RouteAction
* should be ignored.
*/
@VisibleForTesting
@Nullable
static StructOrError<RouteAction> parseRouteAction(io.envoyproxy.envoy.config.route.v3.RouteAction proto, FilterRegistry filterRegistry, boolean parseHttpFilter, Map<String, PluginConfig> pluginConfigMap) {
Long timeoutNano = null;
if (proto.hasMaxStreamDuration()) {
io.envoyproxy.envoy.config.route.v3.RouteAction.MaxStreamDuration maxStreamDuration = proto.getMaxStreamDuration();
if (maxStreamDuration.hasGrpcTimeoutHeaderMax()) {
timeoutNano = Durations.toNanos(maxStreamDuration.getGrpcTimeoutHeaderMax());
} else if (maxStreamDuration.hasMaxStreamDuration()) {
timeoutNano = Durations.toNanos(maxStreamDuration.getMaxStreamDuration());
}
}
RetryPolicy retryPolicy = null;
if (enableRetry && proto.hasRetryPolicy()) {
StructOrError<RetryPolicy> retryPolicyOrError = parseRetryPolicy(proto.getRetryPolicy());
if (retryPolicyOrError != null) {
if (retryPolicyOrError.errorDetail != null) {
return StructOrError.fromError(retryPolicyOrError.errorDetail);
}
retryPolicy = retryPolicyOrError.struct;
}
}
List<HashPolicy> hashPolicies = new ArrayList<>();
for (io.envoyproxy.envoy.config.route.v3.RouteAction.HashPolicy config : proto.getHashPolicyList()) {
HashPolicy policy = null;
boolean terminal = config.getTerminal();
switch(config.getPolicySpecifierCase()) {
case HEADER:
io.envoyproxy.envoy.config.route.v3.RouteAction.HashPolicy.Header headerCfg = config.getHeader();
Pattern regEx = null;
String regExSubstitute = null;
if (headerCfg.hasRegexRewrite() && headerCfg.getRegexRewrite().hasPattern() && headerCfg.getRegexRewrite().getPattern().hasGoogleRe2()) {
regEx = Pattern.compile(headerCfg.getRegexRewrite().getPattern().getRegex());
regExSubstitute = headerCfg.getRegexRewrite().getSubstitution();
}
policy = HashPolicy.forHeader(terminal, headerCfg.getHeaderName(), regEx, regExSubstitute);
break;
case FILTER_STATE:
if (config.getFilterState().getKey().equals(HASH_POLICY_FILTER_STATE_KEY)) {
policy = HashPolicy.forChannelId(terminal);
}
break;
default:
}
if (policy != null) {
hashPolicies.add(policy);
}
}
switch(proto.getClusterSpecifierCase()) {
case CLUSTER:
return StructOrError.fromStruct(RouteAction.forCluster(proto.getCluster(), hashPolicies, timeoutNano, retryPolicy));
case CLUSTER_HEADER:
return null;
case WEIGHTED_CLUSTERS:
List<io.envoyproxy.envoy.config.route.v3.WeightedCluster.ClusterWeight> clusterWeights = proto.getWeightedClusters().getClustersList();
if (clusterWeights.isEmpty()) {
return StructOrError.fromError("No cluster found in weighted cluster list");
}
List<ClusterWeight> weightedClusters = new ArrayList<>();
for (io.envoyproxy.envoy.config.route.v3.WeightedCluster.ClusterWeight clusterWeight : clusterWeights) {
StructOrError<ClusterWeight> clusterWeightOrError = parseClusterWeight(clusterWeight, filterRegistry, parseHttpFilter);
if (clusterWeightOrError.getErrorDetail() != null) {
return StructOrError.fromError("RouteAction contains invalid ClusterWeight: " + clusterWeightOrError.getErrorDetail());
}
weightedClusters.add(clusterWeightOrError.getStruct());
}
// TODO(chengyuanzhang): validate if the sum of weights equals to total weight.
return StructOrError.fromStruct(RouteAction.forWeightedClusters(weightedClusters, hashPolicies, timeoutNano, retryPolicy));
case CLUSTER_SPECIFIER_PLUGIN:
if (enableRouteLookup) {
String pluginName = proto.getClusterSpecifierPlugin();
PluginConfig pluginConfig = pluginConfigMap.get(pluginName);
if (pluginConfig == null) {
return StructOrError.fromError("ClusterSpecifierPlugin for [" + pluginName + "] not found");
}
NamedPluginConfig namedPluginConfig = NamedPluginConfig.create(pluginName, pluginConfig);
return StructOrError.fromStruct(RouteAction.forClusterSpecifierPlugin(namedPluginConfig, hashPolicies, timeoutNano, retryPolicy));
} else {
return StructOrError.fromError("Support for ClusterSpecifierPlugin not enabled");
}
case CLUSTERSPECIFIER_NOT_SET:
default:
return StructOrError.fromError("Unknown cluster specifier: " + proto.getClusterSpecifierCase());
}
}
Aggregations