Search in sources :

Example 1 with TypedStruct

use of com.github.udpa.udpa.type.v1.TypedStruct in project grpc-java by grpc.

the class ClientXdsClient method parseHttpFilter.

@VisibleForTesting
// Returns null if the filter is optional but not supported.
@Nullable
static StructOrError<FilterConfig> parseHttpFilter(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter httpFilter, FilterRegistry filterRegistry, boolean isForClient) {
    String filterName = httpFilter.getName();
    boolean isOptional = httpFilter.getIsOptional();
    if (!httpFilter.hasTypedConfig()) {
        if (isOptional) {
            return null;
        } else {
            return StructOrError.fromError("HttpFilter [" + filterName + "] is not optional and has no typed config");
        }
    }
    Message rawConfig = httpFilter.getTypedConfig();
    String typeUrl = httpFilter.getTypedConfig().getTypeUrl();
    try {
        if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA)) {
            TypedStruct typedStruct = httpFilter.getTypedConfig().unpack(TypedStruct.class);
            typeUrl = typedStruct.getTypeUrl();
            rawConfig = typedStruct.getValue();
        } else if (typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
            com.github.xds.type.v3.TypedStruct newTypedStruct = httpFilter.getTypedConfig().unpack(com.github.xds.type.v3.TypedStruct.class);
            typeUrl = newTypedStruct.getTypeUrl();
            rawConfig = newTypedStruct.getValue();
        }
    } catch (InvalidProtocolBufferException e) {
        return StructOrError.fromError("HttpFilter [" + filterName + "] contains invalid proto: " + e);
    }
    Filter filter = filterRegistry.get(typeUrl);
    if ((isForClient && !(filter instanceof ClientInterceptorBuilder)) || (!isForClient && !(filter instanceof ServerInterceptorBuilder))) {
        if (isOptional) {
            return null;
        } else {
            return StructOrError.fromError("HttpFilter [" + filterName + "](" + typeUrl + ") is required but unsupported for " + (isForClient ? "client" : "server"));
        }
    }
    ConfigOrError<? extends FilterConfig> filterConfig = filter.parseFilterConfig(rawConfig);
    if (filterConfig.errorDetail != null) {
        return StructOrError.fromError("Invalid filter config for HttpFilter [" + filterName + "]: " + filterConfig.errorDetail);
    }
    return StructOrError.fromStruct(filterConfig.config);
}
Also used : Message(com.google.protobuf.Message) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TypedStruct(com.github.udpa.udpa.type.v1.TypedStruct) ServerInterceptorBuilder(io.grpc.xds.Filter.ServerInterceptorBuilder) ClientInterceptorBuilder(io.grpc.xds.Filter.ClientInterceptorBuilder) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 2 with TypedStruct

use of com.github.udpa.udpa.type.v1.TypedStruct in project grpc-java by grpc.

the class ClientXdsClient method parseOverrideFilterConfigs.

@VisibleForTesting
static StructOrError<Map<String, FilterConfig>> parseOverrideFilterConfigs(Map<String, Any> rawFilterConfigMap, FilterRegistry filterRegistry) {
    Map<String, FilterConfig> overrideConfigs = new HashMap<>();
    for (String name : rawFilterConfigMap.keySet()) {
        Any anyConfig = rawFilterConfigMap.get(name);
        String typeUrl = anyConfig.getTypeUrl();
        boolean isOptional = false;
        if (typeUrl.equals(TYPE_URL_FILTER_CONFIG)) {
            io.envoyproxy.envoy.config.route.v3.FilterConfig filterConfig;
            try {
                filterConfig = anyConfig.unpack(io.envoyproxy.envoy.config.route.v3.FilterConfig.class);
            } catch (InvalidProtocolBufferException e) {
                return StructOrError.fromError("FilterConfig [" + name + "] contains invalid proto: " + e);
            }
            isOptional = filterConfig.getIsOptional();
            anyConfig = filterConfig.getConfig();
            typeUrl = anyConfig.getTypeUrl();
        }
        Message rawConfig = anyConfig;
        try {
            if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA)) {
                TypedStruct typedStruct = anyConfig.unpack(TypedStruct.class);
                typeUrl = typedStruct.getTypeUrl();
                rawConfig = typedStruct.getValue();
            } else if (typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
                com.github.xds.type.v3.TypedStruct newTypedStruct = anyConfig.unpack(com.github.xds.type.v3.TypedStruct.class);
                typeUrl = newTypedStruct.getTypeUrl();
                rawConfig = newTypedStruct.getValue();
            }
        } catch (InvalidProtocolBufferException e) {
            return StructOrError.fromError("FilterConfig [" + name + "] contains invalid proto: " + e);
        }
        Filter filter = filterRegistry.get(typeUrl);
        if (filter == null) {
            if (isOptional) {
                continue;
            }
            return StructOrError.fromError("HttpFilter [" + name + "](" + typeUrl + ") is required but unsupported");
        }
        ConfigOrError<? extends FilterConfig> filterConfig = filter.parseFilterConfigOverride(rawConfig);
        if (filterConfig.errorDetail != null) {
            return StructOrError.fromError("Invalid filter config for HttpFilter [" + name + "]: " + filterConfig.errorDetail);
        }
        overrideConfigs.put(name, filterConfig.config);
    }
    return StructOrError.fromStruct(overrideConfigs);
}
Also used : Message(com.google.protobuf.Message) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Any(com.google.protobuf.Any) TypedStruct(com.github.udpa.udpa.type.v1.TypedStruct) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) FilterConfig(io.grpc.xds.Filter.FilterConfig) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with TypedStruct

use of com.github.udpa.udpa.type.v1.TypedStruct 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 4 with TypedStruct

use of com.github.udpa.udpa.type.v1.TypedStruct in project grpc-java by grpc.

the class ClientXdsClient method parseClusterSpecifierPlugin.

@VisibleForTesting
static PluginConfig parseClusterSpecifierPlugin(ClusterSpecifierPlugin pluginProto, ClusterSpecifierPluginRegistry registry) throws ResourceInvalidException {
    TypedExtensionConfig extension = pluginProto.getExtension();
    String pluginName = extension.getName();
    Any anyConfig = extension.getTypedConfig();
    String typeUrl = anyConfig.getTypeUrl();
    Message rawConfig = anyConfig;
    if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA) || typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
        try {
            TypedStruct typedStruct = unpackCompatibleType(anyConfig, TypedStruct.class, TYPE_URL_TYPED_STRUCT_UDPA, TYPE_URL_TYPED_STRUCT);
            typeUrl = typedStruct.getTypeUrl();
            rawConfig = typedStruct.getValue();
        } catch (InvalidProtocolBufferException e) {
            throw new ResourceInvalidException("ClusterSpecifierPlugin [" + pluginName + "] contains invalid proto", e);
        }
    }
    io.grpc.xds.ClusterSpecifierPlugin plugin = registry.get(typeUrl);
    if (plugin == null) {
        throw new ResourceInvalidException("Unsupported ClusterSpecifierPlugin type: " + typeUrl);
    }
    ConfigOrError<? extends PluginConfig> pluginConfigOrError = plugin.parsePlugin(rawConfig);
    if (pluginConfigOrError.errorDetail != null) {
        throw new ResourceInvalidException(pluginConfigOrError.errorDetail);
    }
    return pluginConfigOrError.config;
}
Also used : Message(com.google.protobuf.Message) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TypedExtensionConfig(io.envoyproxy.envoy.config.core.v3.TypedExtensionConfig) Any(com.google.protobuf.Any) TypedStruct(com.github.udpa.udpa.type.v1.TypedStruct) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

TypedStruct (com.github.udpa.udpa.type.v1.TypedStruct)4 Message (com.google.protobuf.Message)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 Any (com.google.protobuf.Any)2 TypedExtensionConfig (io.envoyproxy.envoy.config.core.v3.TypedExtensionConfig)1 NamedPluginConfig (io.grpc.xds.ClusterSpecifierPlugin.NamedPluginConfig)1 PluginConfig (io.grpc.xds.ClusterSpecifierPlugin.PluginConfig)1 ClientInterceptorBuilder (io.grpc.xds.Filter.ClientInterceptorBuilder)1 FilterConfig (io.grpc.xds.Filter.FilterConfig)1 NamedFilterConfig (io.grpc.xds.Filter.NamedFilterConfig)1 ServerInterceptorBuilder (io.grpc.xds.Filter.ServerInterceptorBuilder)1 RlsPluginConfig (io.grpc.xds.RouteLookupServiceClusterSpecifierPlugin.RlsPluginConfig)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1