Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typedMetadata for RouteEntry&ClusterInfo #4723

Merged
merged 11 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions api/envoy/api/v2/core/base.proto
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ message Node {
}

// Metadata provides additional inputs to filters based on matched listeners,
// filter chains, routes and endpoints. It is structured as a map from filter
// name (in reverse DNS format) to metadata specific to the filter. Metadata
// filter chains, routes and endpoints. It is structured as a map, usually from
// filter name (in reverse DNS format) to metadata specific to the filter. Metadata
// key-values for a filter are merged as connection and request handling occurs,
// with later values for the same key overriding earlier values.
//
// An example use of metadata is providing additional values to
// http_connection_manager in the envoy.http_connection_manager.access_log
// namespace.
//
// Another example use of metadata is to per service config info in cluster metadata, which may get
// consumed by multiple filters.
//
// For load balancing, Metadata provides a means to subset cluster endpoints.
// Endpoints have a Metadata object associated and routes contain a Metadata
// object to match against. There are some well defined metadata used today for
Expand Down
8 changes: 8 additions & 0 deletions include/envoy/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ envoy_cc_library(
"//source/common/protobuf",
],
)

envoy_cc_library(
name = "typed_metadata_interface",
hdrs = ["typed_metadata.h"],
deps = [
"//source/common/protobuf",
],
)
78 changes: 78 additions & 0 deletions include/envoy/config/typed_metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once

#include <memory>
#include <string>

#include "envoy/common/pure.h"

#include "common/protobuf/protobuf.h"

namespace Envoy {
namespace Config {

/**
* TypedMetadata interface.
*/
class TypedMetadata {
public:
class Object {
public:
virtual ~Object() {}
};

virtual ~TypedMetadata() {}

/**
* @return a T instance by key. If the conversion is not able to complete, or
* if the data is not in the store, returns a nullptr.
*/
template <typename T> const T* get(const std::string& key) const {
static_assert(std::is_base_of<Object, T>::value,
"Data type must be subclass of TypedMetadata::Object");
const Object* p = getData(key);
if (p != nullptr) {
return dynamic_cast<const T*>(p);
}
return nullptr;
}

protected:
/**
* Returns data associated with given 'key'.
* If there is no data associated with this key, a nullptr is returned.
* @param key the key (usually a reversed DNS) associated with the typed metadata.
* @return A TypedMetadata::Object pointer, nullptr if no data is associated with the key.
*/
virtual const Object* getData(const std::string& key) const PURE;
};

/**
* Typed metadata should implement this factory and register via Registry::registerFactory or the
* convenience class RegisterFactory.
*/
class TypedMetadataFactory {
public:
virtual ~TypedMetadataFactory() {}

/**
* Name of the factory, a reversed DNS name is encouraged to avoid cross-org conflict.
* It's used as key in the metadata map, as well as key in the factory registry.
* When building a TypedMetadata from envoy::api::v2::core::Metadata, if the key is not found, the
* parse will not be called and the corresponding typedMetadata entry will be set to nullptr.
* @return the name of the factory.
*/
virtual const std::string name() const PURE;

/**
* Convert the google.protobuf.Struct into an instance of TypedMetadata::Object.
* It should throw an EnvoyException in case the conversion can't be completed.
* @param data config data stored as a protobuf struct.
* @return a derived class object pointer of TypedMetadata.
* @throw EnvoyException if the parsing can't be done.
*/
virtual std::unique_ptr<const TypedMetadata::Object>
parse(const ProtobufWkt::Struct& data) const PURE;
};

} // namespace Config
} // namespace Envoy
15 changes: 7 additions & 8 deletions include/envoy/registry/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ template <class Base> class FactoryRegistry {

return absl::StrJoin(ret, ",");
}
/**
* Gets the current map of factory implementations. This is an ordered map for sorting reasons.
*/
static std::map<std::string, Base*>& factories() {
static std::map<std::string, Base*>* factories = new std::map<std::string, Base*>;
return *factories;
}

static void registerFactory(Base& factory) {
auto result = factories().emplace(std::make_pair(factory.name(), &factory));
Expand Down Expand Up @@ -94,14 +101,6 @@ template <class Base> class FactoryRegistry {
auto result = factories().erase(name);
RELEASE_ASSERT(result == 1, "");
}

/**
* Gets the current map of factory implementations. This is an ordered map for sorting reasons.
*/
static std::map<std::string, Base*>& factories() {
static std::map<std::string, Base*>* factories = new std::map<std::string, Base*>;
return *factories;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unnecessary diff..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change opens factories() from private to public.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But where are the new factories registered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see examples in router/config_impl_test and upstream_impl_test.
Customer provides the logic of de-serialization, so it's user's obligation to provide/register the factories (just like a filter's factory).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean where is this used, it's in source/common/config/metadata.h where all registered factories for a sort of metadata are iterated to instantiate typed data.

};

/**
Expand Down
1 change: 1 addition & 0 deletions include/envoy/router/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ envoy_cc_library(
external_deps = ["abseil_optional"],
deps = [
"//include/envoy/access_log:access_log_interface",
"//include/envoy/config:typed_metadata_interface",
"//include/envoy/http:codec_interface",
"//include/envoy/http:codes_interface",
"//include/envoy/http:header_map_interface",
Expand Down
12 changes: 12 additions & 0 deletions include/envoy/router/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "envoy/access_log/access_log.h"
#include "envoy/api/v2/core/base.pb.h"
#include "envoy/config/typed_metadata.h"
#include "envoy/http/codec.h"
#include "envoy/http/codes.h"
#include "envoy/http/header_map.h"
Expand Down Expand Up @@ -467,6 +468,11 @@ class PathMatchCriterion {
virtual const std::string& matcher() const PURE;
};

/**
* Base class for all route typed metadata factories.
*/
class HttpRouteTypedMetadataFactory : public Envoy::Config::TypedMetadataFactory {};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we please call this HttpTyped... / HttpRouteTyped... ?
router is for Http. Thrift also has a router.. Other protocols will also have similar..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And we have all sorts of "Instance" classes. :P
I don't have strong feeling about adding the http prefix, will add it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on HttpRouteTyped, thanks.

/**
* An individual resolved route entry.
*/
Expand Down Expand Up @@ -603,6 +609,12 @@ class RouteEntry : public ResponseEntry {
*/
virtual bool includeVirtualHostRateLimits() const PURE;

/**
* @return const Envoy::Config::TypedMetadata& return the typed metadata provided in the config
* for this route.
*/
virtual const Envoy::Config::TypedMetadata& typedMetadata() const PURE;
stevenzzzz marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return const envoy::api::v2::core::Metadata& return the metadata provided in the config for
* this route.
Expand Down
1 change: 1 addition & 0 deletions include/envoy/upstream/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ envoy_cc_library(
":locality_lib",
":resource_manager_interface",
"//include/envoy/common:callback",
"//include/envoy/config:typed_metadata_interface",
"//include/envoy/http:codec_interface",
"//include/envoy/network:connection_interface",
"//include/envoy/network:transport_socket_interface",
Expand Down
11 changes: 11 additions & 0 deletions include/envoy/upstream/upstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "envoy/api/v2/core/base.pb.h"
#include "envoy/common/callback.h"
#include "envoy/config/typed_metadata.h"
#include "envoy/http/codec.h"
#include "envoy/network/connection.h"
#include "envoy/network/transport_socket.h"
Expand Down Expand Up @@ -434,6 +435,11 @@ class ProtocolOptionsConfig {
};
typedef std::shared_ptr<const ProtocolOptionsConfig> ProtocolOptionsConfigConstSharedPtr;

/**
* Base class for all cluster typed metadata factory.
*/
class ClusterTypedMetadataFactory : public Envoy::Config::TypedMetadataFactory {};

/**
* Information about a given upstream cluster.
*/
Expand Down Expand Up @@ -590,6 +596,11 @@ class ClusterInfo {
*/
virtual const envoy::api::v2::core::Metadata& metadata() const PURE;

/**
* @return const Envoy::Config::TypedMetadata&& the typed metadata for this cluster.
*/
virtual const Envoy::Config::TypedMetadata& typedMetadata() const PURE;

/**
*
* @return const Network::ConnectionSocket::OptionsSharedPtr& socket options for all
Expand Down
3 changes: 3 additions & 0 deletions source/common/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ envoy_cc_library(
srcs = ["metadata.cc"],
hdrs = ["metadata.h"],
deps = [
"//include/envoy/config:typed_metadata_interface",
"//include/envoy/registry",
"//source/common/common:assert_lib",
"//source/common/protobuf",
"@envoy_api//envoy/api/v2/core:base_cc",
],
Expand Down
27 changes: 27 additions & 0 deletions source/common/config/metadata.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma once

#include <memory>
#include <string>
#include <unordered_map>

#include "envoy/api/v2/core/base.pb.h"
#include "envoy/config/typed_metadata.h"
#include "envoy/registry/registry.h"

#include "common/protobuf/protobuf.h"

Expand Down Expand Up @@ -46,5 +50,28 @@ class Metadata {
const std::string& key);
};

template <typename factoryClass> class TypedMetadataImpl : public TypedMetadata {
public:
static_assert(std::is_base_of<Config::TypedMetadataFactory, factoryClass>::value,
"Factory type must be inherited from Envoy::Config::TypedMetadataFactory.");
TypedMetadataImpl(const envoy::api::v2::core::Metadata& metadata) : data_() {
auto& data_by_key = metadata.filter_metadata();
for (const auto& it : Registry::FactoryRegistry<factoryClass>::factories()) {
const auto& meta_iter = data_by_key.find(it.first);
if (meta_iter != data_by_key.end()) {
data_[it.second->name()] = it.second->parse(meta_iter->second);
}
}
}

const TypedMetadata::Object* getData(const std::string& key) const override {
const auto& it = data_.find(key);
return it == data_.end() ? nullptr : it->second.get();
}

private:
std::unordered_map<std::string, std::unique_ptr<const TypedMetadata::Object>> data_;
};

} // namespace Config
} // namespace Envoy
1 change: 1 addition & 0 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ envoy_cc_library(
srcs = ["async_client_impl.cc"],
hdrs = ["async_client_impl.h"],
deps = [
"//include/envoy/config:typed_metadata_interface",
"//include/envoy/event:dispatcher_interface",
"//include/envoy/http:async_client_interface",
"//include/envoy/http:codec_interface",
Expand Down
2 changes: 2 additions & 0 deletions source/common/http/async_client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const AsyncStreamImpl::NullRateLimitPolicy AsyncStreamImpl::NullVirtualHost::rat
const AsyncStreamImpl::NullConfig AsyncStreamImpl::NullVirtualHost::route_configuration_;
const std::multimap<std::string, std::string> AsyncStreamImpl::RouteEntryImpl::opaque_config_;
const envoy::api::v2::core::Metadata AsyncStreamImpl::RouteEntryImpl::metadata_;
const Config::TypedMetadataImpl<Envoy::Config::TypedMetadataFactory>
AsyncStreamImpl::RouteEntryImpl::typed_metadata_({});
const AsyncStreamImpl::NullPathMatchCriterion
AsyncStreamImpl::RouteEntryImpl::path_match_criterion_;
const std::list<LowerCaseString> AsyncStreamImpl::NullConfig::internal_only_headers_;
Expand Down
5 changes: 5 additions & 0 deletions source/common/http/async_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>

#include "envoy/config/typed_metadata.h"
#include "envoy/event/dispatcher.h"
#include "envoy/http/async_client.h"
#include "envoy/http/codec.h"
Expand All @@ -17,6 +18,7 @@
#include "envoy/router/router.h"
#include "envoy/router/router_ratelimit.h"
#include "envoy/router/shadow_writer.h"
#include "envoy/server/filter_config.h"
#include "envoy/ssl/connection.h"
#include "envoy/tracing/http_tracer.h"
#include "envoy/upstream/upstream.h"
Expand Down Expand Up @@ -228,6 +230,7 @@ class AsyncStreamImpl : public AsyncClient::Stream,
}
bool includeVirtualHostRateLimits() const override { return true; }
const envoy::api::v2::core::Metadata& metadata() const override { return metadata_; }
const Config::TypedMetadata& typedMetadata() const override { return typed_metadata_; }
const Router::PathMatchCriterion& pathMatchCriterion() const override {
return path_match_criterion_;
}
Expand All @@ -244,6 +247,8 @@ class AsyncStreamImpl : public AsyncClient::Stream,
static const NullVirtualHost virtual_host_;
static const std::multimap<std::string, std::string> opaque_config_;
static const envoy::api::v2::core::Metadata metadata_;
// Async client doesn't require metadata.
static const Config::TypedMetadataImpl<Config::TypedMetadataFactory> typed_metadata_;
static const NullPathMatchCriterion path_match_criterion_;

const std::string& cluster_name_;
Expand Down
4 changes: 2 additions & 2 deletions source/common/network/connection_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class ConnectionImpl : public virtual Connection,
Event::FileEventPtr file_event_;

private:
friend class ::Envoy::RandomPauseFilter;
friend class ::Envoy::TestPauseFilter;
friend class Envoy::RandomPauseFilter;
friend class Envoy::TestPauseFilter;

void onFileEvent(uint32_t events);
void onRead(uint64_t read_buffer_size);
Expand Down
1 change: 1 addition & 0 deletions source/common/router/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ envoy_cc_library(
":metadatamatchcriteria_lib",
":retry_state_lib",
":router_ratelimit_lib",
"//include/envoy/config:typed_metadata_interface",
"//include/envoy/http:header_map_interface",
"//include/envoy/router:router_interface",
"//include/envoy/runtime:runtime_interface",
Expand Down
Loading