Skip to content
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
4 changes: 2 additions & 2 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def envoy_api_deps(skip_targets):
]
for t in http_filter_bind_targets:
native.bind(
name = "envoy_filter_" + t,
name = "envoy_filter_http_" + t,
actual = "@envoy_api//api/filter/http:" + t + "_cc",
)
network_filter_bind_targets = [
Expand All @@ -168,7 +168,7 @@ def envoy_api_deps(skip_targets):
]
for t in network_filter_bind_targets:
native.bind(
name = "envoy_filter_" + t,
name = "envoy_filter_network_" + t,
actual = "@envoy_api//api/filter/network:" + t + "_cc",
)
native.bind(
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/router/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ envoy_cc_library(
envoy_cc_library(
name = "route_config_provider_manager_interface",
hdrs = ["route_config_provider_manager.h"],
external_deps = ["envoy_filter_http_connection_manager"],
external_deps = ["envoy_filter_http_http_connection_manager"],
deps = [
":rds_interface",
"//include/envoy/event:dispatcher_interface",
Expand Down
7 changes: 5 additions & 2 deletions source/common/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ envoy_cc_library(
name = "filter_json_lib",
srcs = ["filter_json.cc"],
hdrs = ["filter_json.h"],
external_deps = ["envoy_filter_http_connection_manager"],
external_deps = [
"envoy_filter_http_http_connection_manager",
"envoy_filter_network_mongo_proxy",
],
deps = [
":json_utility_lib",
":protocol_json_lib",
Expand Down Expand Up @@ -274,7 +277,7 @@ envoy_cc_library(
"envoy_eds",
"envoy_lds",
"envoy_rds",
"envoy_filter_http_connection_manager",
"envoy_filter_http_http_connection_manager",
],
deps = [
":json_utility_lib",
Expand Down
16 changes: 16 additions & 0 deletions source/common/config/filter_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,21 @@ void FilterJson::translateHttpConnectionManager(
}
}

void FilterJson::translateMongoProxy(const Json::Object& json_mongo_proxy,
envoy::api::v2::filter::network::MongoProxy& mongo_proxy) {
json_mongo_proxy.validateSchema(Json::Schema::MONGO_PROXY_NETWORK_FILTER_SCHEMA);

JSON_UTIL_SET_STRING(json_mongo_proxy, mongo_proxy, stat_prefix);
JSON_UTIL_SET_STRING(json_mongo_proxy, mongo_proxy, access_log);
if (json_mongo_proxy.hasObject("fault")) {
const auto json_fault = json_mongo_proxy.getObject("fault")->getObject("fixed_delay");
auto* delay = mongo_proxy.mutable_delay();

delay->set_type(envoy::api::v2::filter::FaultDelay::FIXED);
delay->set_percent(static_cast<uint32_t>(json_fault->getInteger("percent")));
JSON_UTIL_SET_DURATION_FROM_FIELD(*json_fault, *delay, fixed_delay, duration);
}
}

} // namespace Config
} // namespace Envoy
10 changes: 10 additions & 0 deletions source/common/config/filter_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "envoy/json/json_object.h"

#include "api/filter/http/http_connection_manager.pb.h"
#include "api/filter/network/mongo_proxy.pb.h"

namespace Envoy {
namespace Config {
Expand Down Expand Up @@ -36,6 +37,15 @@ class FilterJson {
static void translateHttpConnectionManager(
const Json::Object& json_http_connection_manager,
envoy::api::v2::filter::http::HttpConnectionManager& http_connection_manager);

/**
* Translate a v1 JSON Mongo proxy object to v2 envoy::api::v2::filter::network::MongoProxy.
* @param json_mongo_proxy source v1 JSON HTTP connection manager object.
* @param mongo_proxy destination v2
* envoy::api::v2::filter::network::MongoProxy.
*/
static void translateMongoProxy(const Json::Object& json_mongo_proxy,
envoy::api::v2::filter::network::MongoProxy& mongo_proxy);
};

} // namespace Config
Expand Down
10 changes: 10 additions & 0 deletions source/common/config/json_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@
1000 * (json).getInteger(#field_name "_s"))); \
} \
} while (0)

// Set a google.protobuf.Duration field in a protobuf message with the specified field's
// milliseconds value from a JSON object if the field is set in the JSON object.
#define JSON_UTIL_SET_DURATION_FROM_FIELD(json, message, dst_field, src_field) \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: probably should define JSON_UTIL_SET_DURATION in terms of this macro now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

can I do this in next PR for redis?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes that's fine. Please do in the next PR.

do { \
if ((json).hasObject(#src_field "_ms")) { \
(message).mutable_##dst_field()->CopyFrom( \
Protobuf::util::TimeUtil::MillisecondsToDuration((json).getInteger(#src_field "_ms"))); \
} \
} while (0)
2 changes: 1 addition & 1 deletion source/common/http/access_log/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ envoy_cc_library(
name = "access_log_lib",
srcs = ["access_log_impl.cc"],
hdrs = ["access_log_impl.h"],
external_deps = ["envoy_filter_http_connection_manager"],
external_deps = ["envoy_filter_http_http_connection_manager"],
deps = [
"//include/envoy/access_log:access_log_interface",
"//include/envoy/filesystem:filesystem_interface",
Expand Down
3 changes: 3 additions & 0 deletions source/common/mongo/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ envoy_cc_library(
name = "proxy_lib",
srcs = ["proxy.cc"],
hdrs = ["proxy.h"],
external_deps = ["envoy_filter_network_mongo_proxy"],
deps = [
":codec_lib",
":utility_lib",
Expand All @@ -57,7 +58,9 @@ envoy_cc_library(
"//source/common/common:logger_lib",
"//source/common/common:singleton",
"//source/common/common:utility_lib",
"//source/common/config:filter_json_lib",
"//source/common/network:filter_lib",
"//source/common/protobuf:utility_lib",
],
)

Expand Down
13 changes: 6 additions & 7 deletions source/common/mongo/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
#include "common/buffer/buffer_impl.h"
#include "common/common/logger.h"
#include "common/common/singleton.h"
#include "common/json/json_loader.h"
#include "common/mongo/utility.h"
#include "common/network/filter_impl.h"
#include "common/protobuf/utility.h"

#include "api/filter/network/mongo_proxy.pb.h"

namespace Envoy {
namespace Mongo {
Expand Down Expand Up @@ -92,12 +94,9 @@ typedef std::shared_ptr<AccessLog> AccessLogSharedPtr;
*/
class FaultConfig {
public:
FaultConfig(const Json::Object& fault_config)
: delay_percent_(
static_cast<uint32_t>(fault_config.getObject("fixed_delay")->getInteger("percent"))),
duration_ms_(static_cast<uint64_t>(
fault_config.getObject("fixed_delay")->getInteger("duration_ms"))) {}

FaultConfig(const envoy::api::v2::filter::FaultDelay& fault_config)
: delay_percent_(fault_config.percent()),
duration_ms_(PROTOBUF_GET_MS_REQUIRED(fault_config, fixed_delay)) {}
uint32_t delayPercent() const { return delay_percent_; }
uint64_t delayDuration() const { return duration_ms_; }

Expand Down
4 changes: 2 additions & 2 deletions source/common/router/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ envoy_cc_library(
srcs = ["rds_impl.cc"],
hdrs = ["rds_impl.h"],
external_deps = [
"envoy_filter_http_connection_manager",
"envoy_filter_http_http_connection_manager",
"envoy_rds",
],
deps = [
Expand Down Expand Up @@ -85,7 +85,7 @@ envoy_cc_library(
srcs = ["rds_subscription.cc"],
hdrs = ["rds_subscription.h"],
external_deps = [
"envoy_filter_http_connection_manager",
"envoy_filter_http_http_connection_manager",
],
deps = [
"//include/envoy/config:subscription_interface",
Expand Down
2 changes: 1 addition & 1 deletion source/server/config/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ envoy_cc_library(
name = "file_access_log_lib",
srcs = ["file_access_log.cc"],
hdrs = ["file_access_log.h"],
external_deps = ["envoy_filter_http_connection_manager"],
external_deps = ["envoy_filter_http_http_connection_manager"],
deps = [
"//include/envoy/registry",
"//include/envoy/server:access_log_config_interface",
Expand Down
2 changes: 0 additions & 2 deletions source/server/config/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ envoy_cc_library(
"//include/envoy/registry",
"//include/envoy/server:filter_config_interface",
"//source/common/config:well_known_names",
"//source/common/json:config_schemas_lib",
"//source/common/json:json_loader_lib",
"//source/common/mongo:proxy_lib",
],
)
Expand Down
41 changes: 30 additions & 11 deletions source/server/config/network/mongo_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@
#include "envoy/network/connection.h"
#include "envoy/registry/registry.h"

#include "common/json/config_schemas.h"
#include "common/config/filter_json.h"
#include "common/mongo/proxy.h"

#include "api/filter/network/mongo_proxy.pb.h"

namespace Envoy {
namespace Server {
namespace Configuration {

NetworkFilterFactoryCb
MongoProxyFilterConfigFactory::createFilterFactory(const Json::Object& config,
FactoryContext& context) {
config.validateSchema(Json::Schema::MONGO_PROXY_NETWORK_FILTER_SCHEMA);
NetworkFilterFactoryCb MongoProxyFilterConfigFactory::createMongoProxyFactory(
const envoy::api::v2::filter::network::MongoProxy& mongo_proxy, FactoryContext& context) {

ASSERT(!mongo_proxy.stat_prefix().empty());
std::string stat_prefix = "mongo." + mongo_proxy.stat_prefix() + ".";

std::string stat_prefix = "mongo." + config.getString("stat_prefix") + ".";
Mongo::AccessLogSharedPtr access_log;
if (config.hasObject("access_log")) {
access_log.reset(
new Mongo::AccessLog(config.getString("access_log"), context.accessLogManager()));
if (!mongo_proxy.access_log().empty()) {
access_log.reset(new Mongo::AccessLog(mongo_proxy.access_log(), context.accessLogManager()));
}

Mongo::FaultConfigSharedPtr fault_config;
if (config.hasObject("fault")) {
fault_config = std::make_shared<Mongo::FaultConfig>(*config.getObject("fault"));
if (mongo_proxy.has_delay()) {
auto delay = mongo_proxy.delay();
ASSERT(delay.has_fixed_delay());
fault_config = std::make_shared<Mongo::FaultConfig>(mongo_proxy.delay());
}

return [stat_prefix, &context, access_log,
Expand All @@ -36,6 +39,22 @@ MongoProxyFilterConfigFactory::createFilterFactory(const Json::Object& config,
};
}

NetworkFilterFactoryCb
MongoProxyFilterConfigFactory::createFilterFactory(const Json::Object& json_mongo_proxy,
FactoryContext& context) {
envoy::api::v2::filter::network::MongoProxy mongo_proxy;
Config::FilterJson::translateMongoProxy(json_mongo_proxy, mongo_proxy);

return createMongoProxyFactory(mongo_proxy, context);
}

NetworkFilterFactoryCb
MongoProxyFilterConfigFactory::createFilterFactoryFromProto(const Protobuf::Message& config,
FactoryContext& context) {
return createMongoProxyFactory(
dynamic_cast<const envoy::api::v2::filter::network::MongoProxy&>(config), context);
}

/**
* Static registration for the mongo filter. @see RegisterFactory.
*/
Expand Down
9 changes: 9 additions & 0 deletions source/server/config/network/mongo_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "common/config/well_known_names.h"

#include "api/filter/network/mongo_proxy.pb.h"

namespace Envoy {
namespace Server {
namespace Configuration {
Expand All @@ -18,8 +20,15 @@ class MongoProxyFilterConfigFactory : public NamedNetworkFilterConfigFactory {
// NamedNetworkFilterConfigFactory
NetworkFilterFactoryCb createFilterFactory(const Json::Object& config,
FactoryContext& context) override;
NetworkFilterFactoryCb createFilterFactoryFromProto(const Protobuf::Message& config,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we make sure we have tests that cover both the legacy and factory config flows also when you fix this. Thank you!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I have not touched any existing tests other than fixing up the class names. And the existing tests use JSON config for testing. i.e. they exercise the conversion and tests pass.

Copy link
Copy Markdown
Member

@mattklein123 mattklein123 Oct 27, 2017

Choose a reason for hiding this comment

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

Right my point is can you please make sure that the native proto loading path for v2 config is covered, and not just the conversion path. You may need to add new tests. I think this is important as we add the rest of the filters.

FactoryContext& context) override;

std::string name() override { return Config::NetworkFilterNames::get().MONGO_PROXY; }

private:
NetworkFilterFactoryCb
createMongoProxyFactory(const envoy::api::v2::filter::network::MongoProxy& mongo_proxy,
FactoryContext& context);
};

} // namespace Configuration
Expand Down
2 changes: 1 addition & 1 deletion test/common/http/access_log/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ envoy_cc_test(
envoy_cc_test(
name = "access_log_impl_test",
srcs = ["access_log_impl_test.cc"],
external_deps = ["envoy_filter_http_connection_manager"],
external_deps = ["envoy_filter_http_http_connection_manager"],
deps = [
"//include/envoy/upstream:cluster_manager_interface",
"//include/envoy/upstream:upstream_interface",
Expand Down
15 changes: 5 additions & 10 deletions test/common/mongo/proxy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "test/mocks/runtime/mocks.h"
#include "test/test_common/printers.h"

#include "api/filter/fault.pb.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -76,17 +77,11 @@ class MongoProxyFilterTest : public testing::Test {
}

void setupDelayFault(bool enable_fault) {
const std::string json_config = R"EOF(
{
"fixed_delay": {
"percent": 50,
"duration_ms": 10
}
}
)EOF";
Json::ObjectSharedPtr config = Json::Factory::loadFromString(json_config);
envoy::api::v2::filter::FaultDelay fault{};
fault.set_percent(50);
fault.mutable_fixed_delay()->CopyFrom(Protobuf::util::TimeUtil::MillisecondsToDuration(10));

fault_config_.reset(new FaultConfig(*config));
fault_config_.reset(new FaultConfig(fault));

EXPECT_CALL(runtime_.snapshot_, featureEnabled(_, _)).Times(AnyNumber());
EXPECT_CALL(runtime_.snapshot_, featureEnabled("mongo.fault.fixed_delay.percent", 50))
Expand Down
29 changes: 29 additions & 0 deletions test/server/config/network/mongo_proxy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "test/mocks/server/mocks.h"
#include "test/test_common/utility.h"

#include "api/filter/network/mongo_proxy.pb.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -32,6 +33,20 @@ TEST(MongoFilterConfigTest, CorrectConfigurationNoFaults) {
cb(connection);
}

TEST(MongoFilterConfigTest, ValidProtoConfigurationNoFaults) {
envoy::api::v2::filter::network::MongoProxy config{};

config.set_access_log("path/to/access/log");
config.set_stat_prefix("my_stat_prefix");

NiceMock<MockFactoryContext> context;
MongoProxyFilterConfigFactory factory;
NetworkFilterFactoryCb cb = factory.createFilterFactoryFromProto(config, context);
Network::MockConnection connection;
EXPECT_CALL(connection, addFilter(_));
cb(connection);
}

void handleInvalidConfiguration(const std::string& json_string) {
Json::ObjectSharedPtr json_config = Json::Factory::loadFromString(json_string);
NiceMock<MockFactoryContext> context;
Expand Down Expand Up @@ -217,6 +232,20 @@ TEST(MongoFilterConfigTest, CorrectFaultConfiguration) {
cb(connection);
}

TEST(MongoFilterConfigTest, CorrectFaultConfigurationInProto) {
envoy::api::v2::filter::network::MongoProxy config{};
config.set_stat_prefix("my_stat_prefix");
config.mutable_delay()->set_percent(50);
config.mutable_delay()->mutable_fixed_delay()->set_seconds(500);

NiceMock<MockFactoryContext> context;
MongoProxyFilterConfigFactory factory;
NetworkFilterFactoryCb cb = factory.createFilterFactoryFromProto(config, context);
Network::MockConnection connection;
EXPECT_CALL(connection, addFilter(_));
cb(connection);
}

} // namespace Configuration
} // namespace Server
} // namespace Envoy