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
1 change: 1 addition & 0 deletions source/common/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ envoy_cc_library(
"//include/envoy/upstream:cluster_manager_interface",
"//source/common/common:assert_lib",
"//source/common/common:hex_lib",
"//source/common/common:singleton",
"//source/common/json:config_schemas_lib",
],
)
25 changes: 18 additions & 7 deletions source/common/config/utility.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "common/config/utility.h"

#include "common/common/assert.h"
#include "common/common/hex.h"
#include "common/common/utility.h"
#include "common/config/json_utility.h"
Expand All @@ -11,18 +12,24 @@
namespace Envoy {
namespace Config {

namespace {

void translateApiConfigSource(const std::string& cluster, uint32_t refresh_delay_ms,
envoy::api::v2::ApiConfigSource& api_config_source) {
api_config_source.set_api_type(envoy::api::v2::ApiConfigSource::REST_LEGACY);
void Utility::translateApiConfigSource(const std::string& cluster, uint32_t refresh_delay_ms,
const std::string& api_type,
envoy::api::v2::ApiConfigSource& api_config_source) {
// TODO(junr03): document the option to chose an api type once we have created
// stronger constraints around v2.
if (api_type == ApiType::get().RestLegacy) {
api_config_source.set_api_type(envoy::api::v2::ApiConfigSource::REST_LEGACY);
} else if (api_type == ApiType::get().Rest) {
api_config_source.set_api_type(envoy::api::v2::ApiConfigSource::REST);
} else {
ASSERT(api_type == ApiType::get().Grpc);
api_config_source.set_api_type(envoy::api::v2::ApiConfigSource::GRPC);
}
api_config_source.add_cluster_name(cluster);
api_config_source.mutable_refresh_delay()->CopyFrom(
Protobuf::util::TimeUtil::MillisecondsToDuration(refresh_delay_ms));
}

} // namespace

void Utility::checkCluster(const std::string& error_prefix, const std::string& cluster_name,
Upstream::ClusterManager& cm) {
Upstream::ThreadLocalCluster* cluster = cm.get(cluster_name);
Expand Down Expand Up @@ -63,20 +70,23 @@ void Utility::translateEdsConfig(const Json::Object& json_config,
envoy::api::v2::ConfigSource& eds_config) {
translateApiConfigSource(json_config.getObject("cluster")->getString("name"),
json_config.getInteger("refresh_delay_ms", 30000),
json_config.getString("api_type", ApiType::get().RestLegacy),
*eds_config.mutable_api_config_source());
}

void Utility::translateCdsConfig(const Json::Object& json_config,
envoy::api::v2::ConfigSource& cds_config) {
translateApiConfigSource(json_config.getObject("cluster")->getString("name"),
json_config.getInteger("refresh_delay_ms", 30000),
json_config.getString("api_type", ApiType::get().RestLegacy),
*cds_config.mutable_api_config_source());
}

void Utility::translateRdsConfig(const Json::Object& json_rds, envoy::api::v2::filter::Rds& rds) {
json_rds.validateSchema(Json::Schema::RDS_CONFIGURATION_SCHEMA);
translateApiConfigSource(json_rds.getString("cluster"),
json_rds.getInteger("refresh_delay_ms", 30000),
json_rds.getString("api_type", ApiType::get().RestLegacy),
*rds.mutable_config_source()->mutable_api_config_source());
JSON_UTIL_SET_STRING(json_rds, rds, route_config_name);
}
Expand All @@ -86,6 +96,7 @@ void Utility::translateLdsConfig(const Json::Object& json_lds,
json_lds.validateSchema(Json::Schema::LDS_CONFIG_SCHEMA);
translateApiConfigSource(json_lds.getString("cluster"),
json_lds.getInteger("refresh_delay_ms", 30000),
json_lds.getString("api_type", ApiType::get().RestLegacy),
*lds_config.mutable_api_config_source());
}

Expand Down
24 changes: 24 additions & 0 deletions source/common/config/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "envoy/local_info/local_info.h"
#include "envoy/upstream/cluster_manager.h"

#include "common/common/singleton.h"
#include "common/protobuf/protobuf.h"

#include "api/base.pb.h"
Expand All @@ -13,6 +14,18 @@
namespace Envoy {
namespace Config {

/**
* Constant Api Type Values, used by envoy::api::v2::ApiConfigSource.
*/
class ApiTypeValues {
public:
const std::string RestLegacy{"REST_LEGACY"};
const std::string Rest{"REST"};
const std::string Grpc{"GRPC"};
};

typedef ConstSingleton<ApiTypeValues> ApiType;

/**
* General config API utilities.
*/
Expand Down Expand Up @@ -40,6 +53,17 @@ class Utility {
static std::chrono::milliseconds
apiConfigSourceRefreshDelay(const envoy::api::v2::ApiConfigSource& api_config_source);

/**
* Populate an envoy::api::v2::ApiConfigSource.
* @param cluster supplies the cluster name for the ApiConfigSource.
* @param refresh_delay_ms supplies the refresh delay for the ApiConfigSource in ms.
* @param api_type supplies the type of subscription to use for the ApiConfigSource.
* @param api_config_source a reference to the envoy::api::v2::ApiConfigSource object to populate.
*/
static void translateApiConfigSource(const std::string& cluster, uint32_t refresh_delay_ms,
const std::string& api_type,
envoy::api::v2::ApiConfigSource& api_config_source);

/**
* Check cluster info for API config sanity. Throws on error.
* @param error_prefix supplies the prefix to use in error messages.
Expand Down
4 changes: 4 additions & 0 deletions source/common/json/config_schemas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ const std::string Json::Schema::RDS_CONFIGURATION_SCHEMA(R"EOF(
"type" : "integer",
"minimum" : 0,
"exclusiveMinimum" : true
},
"api_type" : {
"type" : "string",
"enum" : ["REST_LEGACY", "REST", "GRPC"]
}
},
"required" : ["cluster", "route_config_name"],
Expand Down
26 changes: 26 additions & 0 deletions test/common/config/utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,31 @@ TEST(UtilityTest, ApiConfigSourceRefreshDelay) {
EXPECT_EQ(1234, Utility::apiConfigSourceRefreshDelay(api_config_source).count());
}

TEST(UtilityTest, TranslateApiConfigSource) {
envoy::api::v2::ApiConfigSource api_config_source_rest_legacy;
Utility::translateApiConfigSource("test_rest_legacy_cluster", 10000, ApiType::get().RestLegacy,
api_config_source_rest_legacy);
EXPECT_EQ(envoy::api::v2::ApiConfigSource::REST_LEGACY, api_config_source_rest_legacy.api_type());
EXPECT_EQ(10000, Protobuf::util::TimeUtil::DurationToMilliseconds(
api_config_source_rest_legacy.refresh_delay()));
EXPECT_EQ("test_rest_legacy_cluster", api_config_source_rest_legacy.cluster_name(0));

envoy::api::v2::ApiConfigSource api_config_source_rest;
Utility::translateApiConfigSource("test_rest_cluster", 20000, ApiType::get().Rest,
api_config_source_rest);
EXPECT_EQ(envoy::api::v2::ApiConfigSource::REST, api_config_source_rest.api_type());
EXPECT_EQ(20000, Protobuf::util::TimeUtil::DurationToMilliseconds(
api_config_source_rest.refresh_delay()));
EXPECT_EQ("test_rest_cluster", api_config_source_rest.cluster_name(0));

envoy::api::v2::ApiConfigSource api_config_source_grpc;
Utility::translateApiConfigSource("test_grpc_cluster", 30000, ApiType::get().Grpc,
api_config_source_grpc);
EXPECT_EQ(envoy::api::v2::ApiConfigSource::GRPC, api_config_source_grpc.api_type());
EXPECT_EQ(30000, Protobuf::util::TimeUtil::DurationToMilliseconds(
api_config_source_grpc.refresh_delay()));
EXPECT_EQ("test_grpc_cluster", api_config_source_grpc.cluster_name(0));
}

} // namespace Config
} // namespace Envoy