Search in sources :

Example 1 with PluginConfig

use of io.grpc.xds.ClusterSpecifierPlugin.PluginConfig in project grpc-java by grpc.

the class ClientXdsClientDataTest method parseClusterSpecifierPlugin_typedStructInTypedExtension.

@Test
public void parseClusterSpecifierPlugin_typedStructInTypedExtension() throws Exception {
    class TestPluginConfig implements PluginConfig {

        @Override
        public String typeUrl() {
            return "type.googleapis.com/google.protobuf.Empty";
        }
    }
    ClusterSpecifierPluginRegistry registry = ClusterSpecifierPluginRegistry.newRegistry();
    registry.register(new ClusterSpecifierPlugin() {

        @Override
        public String[] typeUrls() {
            return new String[] { "type.googleapis.com/google.protobuf.Empty" };
        }

        @Override
        public ConfigOrError<? extends PluginConfig> parsePlugin(Message rawProtoMessage) {
            return ConfigOrError.fromConfig(new TestPluginConfig());
        }
    });
    TypedStruct typedStruct = TypedStruct.newBuilder().setTypeUrl("type.googleapis.com/google.protobuf.Empty").setValue(Struct.newBuilder()).build();
    io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin pluginProto = io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin.newBuilder().setExtension(TypedExtensionConfig.newBuilder().setTypedConfig(Any.pack(typedStruct))).build();
    PluginConfig pluginConfig = ClientXdsClient.parseClusterSpecifierPlugin(pluginProto, registry);
    assertThat(pluginConfig).isInstanceOf(TestPluginConfig.class);
}
Also used : NamedPluginConfig(io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig) PluginConfig(io.grpc.xds.ClusterSpecifierPlugin.PluginConfig) RlsPluginConfig(io.grpc.xds.RouteLookupServiceClusterSpecifierPlugin.RlsPluginConfig) Message(com.google.protobuf.Message) TypedStruct(com.github.udpa.udpa.type.v1.TypedStruct) Test(org.junit.Test)

Example 2 with PluginConfig

use of io.grpc.xds.ClusterSpecifierPlugin.PluginConfig in project grpc-java by grpc.

the class ClientXdsClientDataTest method parseClusterSpecifierPlugin_v3TypedStructInTypedExtension.

@Test
public void parseClusterSpecifierPlugin_v3TypedStructInTypedExtension() throws Exception {
    class TestPluginConfig implements PluginConfig {

        @Override
        public String typeUrl() {
            return "type.googleapis.com/google.protobuf.Empty";
        }
    }
    ClusterSpecifierPluginRegistry registry = ClusterSpecifierPluginRegistry.newRegistry();
    registry.register(new ClusterSpecifierPlugin() {

        @Override
        public String[] typeUrls() {
            return new String[] { "type.googleapis.com/google.protobuf.Empty" };
        }

        @Override
        public ConfigOrError<? extends PluginConfig> parsePlugin(Message rawProtoMessage) {
            return ConfigOrError.fromConfig(new TestPluginConfig());
        }
    });
    com.github.xds.type.v3.TypedStruct typedStruct = com.github.xds.type.v3.TypedStruct.newBuilder().setTypeUrl("type.googleapis.com/google.protobuf.Empty").setValue(Struct.newBuilder()).build();
    io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin pluginProto = io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin.newBuilder().setExtension(TypedExtensionConfig.newBuilder().setTypedConfig(Any.pack(typedStruct))).build();
    PluginConfig pluginConfig = ClientXdsClient.parseClusterSpecifierPlugin(pluginProto, registry);
    assertThat(pluginConfig).isInstanceOf(TestPluginConfig.class);
}
Also used : Message(com.google.protobuf.Message) NamedPluginConfig(io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig) PluginConfig(io.grpc.xds.ClusterSpecifierPlugin.PluginConfig) RlsPluginConfig(io.grpc.xds.RouteLookupServiceClusterSpecifierPlugin.RlsPluginConfig) Test(org.junit.Test)

Example 3 with PluginConfig

use of io.grpc.xds.ClusterSpecifierPlugin.PluginConfig in project grpc-java by grpc.

the class ClientXdsClient method extractVirtualHosts.

private static List<VirtualHost> extractVirtualHosts(RouteConfiguration routeConfig, FilterRegistry filterRegistry, boolean parseHttpFilter) throws ResourceInvalidException {
    Map<String, PluginConfig> pluginConfigMap = new HashMap<>();
    if (enableRouteLookup) {
        List<ClusterSpecifierPlugin> plugins = routeConfig.getClusterSpecifierPluginsList();
        for (ClusterSpecifierPlugin plugin : plugins) {
            PluginConfig existing = pluginConfigMap.put(plugin.getExtension().getName(), parseClusterSpecifierPlugin(plugin));
            if (existing != null) {
                throw new ResourceInvalidException("Multiple ClusterSpecifierPlugins with the same name: " + plugin.getExtension().getName());
            }
        }
    }
    List<VirtualHost> virtualHosts = new ArrayList<>(routeConfig.getVirtualHostsCount());
    for (io.envoyproxy.envoy.config.route.v3.VirtualHost virtualHostProto : routeConfig.getVirtualHostsList()) {
        StructOrError<VirtualHost> virtualHost = parseVirtualHost(virtualHostProto, filterRegistry, parseHttpFilter, pluginConfigMap);
        if (virtualHost.getErrorDetail() != null) {
            throw new ResourceInvalidException("RouteConfiguration contains invalid virtual host: " + virtualHost.getErrorDetail());
        }
        virtualHosts.add(virtualHost.getStruct());
    }
    return virtualHosts;
}
Also used : ClusterSpecifierPlugin(io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NamedPluginConfig(io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig) PluginConfig(io.grpc.xds.ClusterSpecifierPlugin.PluginConfig)

Example 4 with PluginConfig

use of io.grpc.xds.ClusterSpecifierPlugin.PluginConfig 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());
    }
}
Also used : ArrayList(java.util.ArrayList) HashPolicy(io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy) Pattern(com.google.re2j.Pattern) ClusterWeight(io.grpc.xds.VirtualHost.Route.RouteAction.ClusterWeight) NamedPluginConfig(io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig) RouteAction(io.grpc.xds.VirtualHost.Route.RouteAction) NamedPluginConfig(io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig) PluginConfig(io.grpc.xds.ClusterSpecifierPlugin.PluginConfig) RetryPolicy(io.grpc.xds.VirtualHost.Route.RouteAction.RetryPolicy) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Aggregations

NamedPluginConfig (io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig)4 PluginConfig (io.grpc.xds.ClusterSpecifierPlugin.PluginConfig)4 Message (com.google.protobuf.Message)2 RlsPluginConfig (io.grpc.xds.RouteLookupServiceClusterSpecifierPlugin.RlsPluginConfig)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 TypedStruct (com.github.udpa.udpa.type.v1.TypedStruct)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Pattern (com.google.re2j.Pattern)1 ClusterSpecifierPlugin (io.envoyproxy.envoy.config.route.v3.ClusterSpecifierPlugin)1 RouteAction (io.grpc.xds.VirtualHost.Route.RouteAction)1 ClusterWeight (io.grpc.xds.VirtualHost.Route.RouteAction.ClusterWeight)1 HashPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy)1 RetryPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.RetryPolicy)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Nullable (javax.annotation.Nullable)1