-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Changes from all commits
accef78
e135bed
740bde3
8f424e7
46bb1ce
6007101
4a11c77
f2c3e36
b1214dd
504076d
a443d5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 {}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we please call this HttpTyped... / HttpRouteTyped... ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And we have all sorts of "Instance" classes. :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on |
||
/** | ||
* An individual resolved route entry. | ||
*/ | ||
|
@@ -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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: unnecessary diff..
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.