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

xDS: type url aggregation #9649

Merged
merged 15 commits into from
Mar 18, 2020
9 changes: 9 additions & 0 deletions include/envoy/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ envoy_cc_library(
"//source/common/protobuf",
],
)

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

#include "envoy/config/subscription.h"

namespace Envoy {
namespace Config {

class RdsRouteConfigSubscriptionBase : public Config::SubscriptionCallbacks {};
Shikugawa marked this conversation as resolved.
Show resolved Hide resolved
class ScopedRdsConfigSubscriptionBase : public Config::SubscriptionCallbacks {};
class VhdsSubscriptionBase : public Config::SubscriptionCallbacks {};
class RtdsSubscriptionBase : public Config::SubscriptionCallbacks {};
class SdsApiBase : public Config::SubscriptionCallbacks {};
class CdsApiBase : public Config::SubscriptionCallbacks {};
class EdsClusterBase : public Config::SubscriptionCallbacks {};
class LdsApiBase : public Config::SubscriptionCallbacks {};

} // namespace Config
} // namespace Envoy
1 change: 1 addition & 0 deletions include/envoy/upstream/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ envoy_cc_library(
":locality_lib",
":resource_manager_interface",
"//include/envoy/common:callback",
"//include/envoy/config:subscription_interface",
"//include/envoy/config:typed_metadata_interface",
"//include/envoy/http:codec_interface",
"//include/envoy/network:connection_interface",
Expand Down
18 changes: 18 additions & 0 deletions source/common/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,21 @@ envoy_cc_library(
"//source/common/singleton:const_singleton",
],
)

envoy_cc_library(
name = "type_url_loader_lib",
hdrs = ["type_url_loader.h"],
deps = [
"//include/envoy/config:discovery_service_base_interface",
"@envoy_api//envoy/api/v2:pkg_cc_proto",
"@envoy_api//envoy/api/v2/auth:pkg_cc_proto",
"@envoy_api//envoy/api/v2/route:pkg_cc_proto",
"@envoy_api//envoy/config/cluster/v3alpha:pkg_cc_proto",
"@envoy_api//envoy/config/endpoint/v3alpha:pkg_cc_proto",
"@envoy_api//envoy/config/listener/v3alpha:pkg_cc_proto",
"@envoy_api//envoy/config/route/v3alpha:pkg_cc_proto",
"@envoy_api//envoy/extensions/transport_sockets/tls/v3alpha:pkg_cc_proto",
"@envoy_api//envoy/service/discovery/v2:pkg_cc_proto",
"@envoy_api//envoy/service/runtime/v3alpha:pkg_cc_proto",
],
)
88 changes: 88 additions & 0 deletions source/common/config/type_url_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#pragma once

#include <string>
#include <type_traits>

#include "envoy/api/v2/auth/cert.pb.h"
#include "envoy/api/v2/cluster.pb.h"
#include "envoy/api/v2/endpoint.pb.h"
#include "envoy/api/v2/listener.pb.h"
#include "envoy/api/v2/rds.pb.h"
#include "envoy/api/v2/route/route_components.pb.h"
#include "envoy/api/v2/scoped_route.pb.h"
#include "envoy/config/cluster/v3alpha/cluster.pb.h"
#include "envoy/config/discovery_service_base.h"
#include "envoy/config/endpoint/v3alpha/endpoint.pb.h"
#include "envoy/config/listener/v3alpha/listener.pb.h"
#include "envoy/config/route/v3alpha/route.pb.h"
#include "envoy/config/route/v3alpha/route_components.pb.h"
#include "envoy/config/route/v3alpha/scoped_route.pb.h"
#include "envoy/extensions/transport_sockets/tls/v3alpha/cert.pb.h"
#include "envoy/service/discovery/v2/rtds.pb.h"
#include "envoy/service/runtime/v3alpha/rtds.pb.h"

namespace Envoy {
namespace Config {

template <typename DiscoveryServiceSubscription> std::string v2ResourceNameEmitter() {
if (std::is_base_of<RdsRouteConfigSubscriptionBase, DiscoveryServiceSubscription>::value) {
return envoy::api::v2::RouteConfiguration().GetDescriptor()->full_name();
} else if (std::is_base_of<ScopedRdsConfigSubscriptionBase,
Shikugawa marked this conversation as resolved.
Show resolved Hide resolved
DiscoveryServiceSubscription>::value) {
return envoy::api::v2::ScopedRouteConfiguration().GetDescriptor()->full_name();
} else if (std::is_base_of<VhdsSubscriptionBase, DiscoveryServiceSubscription>::value) {
return envoy::api::v2::route::VirtualHost().GetDescriptor()->full_name();
} else if (std::is_base_of<RtdsSubscriptionBase, DiscoveryServiceSubscription>::value) {
return envoy::service::discovery::v2::Runtime().GetDescriptor()->full_name();
} else if (std::is_base_of<SdsApiBase, DiscoveryServiceSubscription>::value) {
return envoy::api::v2::auth::Secret().GetDescriptor()->full_name();
} else if (std::is_base_of<CdsApiBase, DiscoveryServiceSubscription>::value) {
return envoy::api::v2::Cluster().GetDescriptor()->full_name();
} else if (std::is_base_of<EdsClusterBase, DiscoveryServiceSubscription>::value) {
return envoy::api::v2::ClusterLoadAssignment().GetDescriptor()->full_name();
} else if (std::is_base_of<LdsApiBase, DiscoveryServiceSubscription>::value) {
return envoy::api::v2::Listener().GetDescriptor()->full_name();
}
NOT_REACHED_GCOVR_EXCL_LINE;
}

template <typename DiscoveryServiceSubscription> std::string v3alphaResourceNameEmitter() {
if (std::is_base_of<RdsRouteConfigSubscriptionBase, DiscoveryServiceSubscription>::value) {
return envoy::config::route::v3alpha::RouteConfiguration().GetDescriptor()->full_name();
} else if (std::is_base_of<ScopedRdsConfigSubscriptionBase,
DiscoveryServiceSubscription>::value) {
return envoy::config::route::v3alpha::ScopedRouteConfiguration().GetDescriptor()->full_name();
} else if (std::is_base_of<VhdsSubscriptionBase, DiscoveryServiceSubscription>::value) {
return envoy::config::route::v3alpha::VirtualHost().GetDescriptor()->full_name();
} else if (std::is_base_of<RtdsSubscriptionBase, DiscoveryServiceSubscription>::value) {
return envoy::service::runtime::v3alpha::Runtime().GetDescriptor()->full_name();
} else if (std::is_base_of<SdsApiBase, DiscoveryServiceSubscription>::value) {
return envoy::extensions::transport_sockets::tls::v3alpha::Secret()
.GetDescriptor()
->full_name();
} else if (std::is_base_of<CdsApiBase, DiscoveryServiceSubscription>::value) {
return envoy::config::cluster::v3alpha::Cluster().GetDescriptor()->full_name();
} else if (std::is_base_of<EdsClusterBase, DiscoveryServiceSubscription>::value) {
return envoy::config::endpoint::v3alpha::ClusterLoadAssignment().GetDescriptor()->full_name();
} else if (std::is_base_of<LdsApiBase, DiscoveryServiceSubscription>::value) {
return envoy::config::listener::v3alpha::Listener().GetDescriptor()->full_name();
}
NOT_REACHED_GCOVR_EXCL_LINE;
}

template <typename DiscoveryServiceSubscription>
std::string loadTypeUrl(envoy::config::core::v3alpha::ApiVersion resource_api_version) {
switch (resource_api_version) {
// automatically set api version as V2
case envoy::config::core::v3alpha::ApiVersion::AUTO:
case envoy::config::core::v3alpha::ApiVersion::V2:
return v2ResourceNameEmitter<DiscoveryServiceSubscription>();
case envoy::config::core::v3alpha::ApiVersion::V3ALPHA:
return v3alphaResourceNameEmitter<DiscoveryServiceSubscription>();
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
}

} // namespace Config
} // namespace Envoy
6 changes: 6 additions & 0 deletions source/common/router/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ envoy_cc_library(
hdrs = ["vhds.h"],
deps = [
":config_lib",
"//include/envoy/config:discovery_service_base_interface",
"//include/envoy/config:subscription_interface",
"//include/envoy/http:codes_interface",
"//include/envoy/local_info:local_info_interface",
Expand All @@ -131,6 +132,7 @@ envoy_cc_library(
"//source/common/common:assert_lib",
"//source/common/common:minimal_logger_lib",
"//source/common/config:api_version_lib",
"//source/common/config:type_url_loader_lib",
"//source/common/config:utility_lib",
"//source/common/init:target_lib",
"//source/common/protobuf:utility_lib",
Expand All @@ -148,6 +150,7 @@ envoy_cc_library(
hdrs = ["rds_impl.h"],
deps = [
":config_lib",
"//include/envoy/config:discovery_service_base_interface",
"//include/envoy/config:subscription_interface",
"//include/envoy/http:codes_interface",
"//include/envoy/local_info:local_info_interface",
Expand All @@ -163,6 +166,7 @@ envoy_cc_library(
"//source/common/common:minimal_logger_lib",
"//source/common/config:api_version_lib",
"//source/common/config:subscription_factory_lib",
"//source/common/config:type_url_loader_lib",
"//source/common/config:utility_lib",
"//source/common/init:manager_lib",
"//source/common/init:target_lib",
Expand Down Expand Up @@ -204,6 +208,7 @@ envoy_cc_library(
":rds_lib",
":scoped_config_lib",
"//include/envoy/config:config_provider_interface",
"//include/envoy/config:discovery_service_base_interface",
"//include/envoy/config:subscription_interface",
"//include/envoy/router:route_config_provider_manager_interface",
"//include/envoy/stats:stats_interface",
Expand All @@ -212,6 +217,7 @@ envoy_cc_library(
"//source/common/common:minimal_logger_lib",
"//source/common/config:api_version_lib",
"//source/common/config:config_provider_lib",
"//source/common/config:type_url_loader_lib",
"//source/common/init:manager_lib",
"//source/common/init:watcher_lib",
"@envoy_api//envoy/admin/v3alpha:pkg_cc_proto",
Expand Down
23 changes: 4 additions & 19 deletions source/common/router/rds_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "common/common/assert.h"
#include "common/common/fmt.h"
#include "common/config/api_version.h"
#include "common/config/type_url_loader.h"
#include "common/config/utility.h"
#include "common/protobuf/utility.h"
#include "common/router/config_impl.h"
Expand Down Expand Up @@ -74,11 +75,11 @@ RdsRouteConfigSubscription::RdsRouteConfigSubscription(
stat_prefix_(stat_prefix), stats_({ALL_RDS_STATS(POOL_COUNTER(*scope_))}),
route_config_provider_manager_(route_config_provider_manager),
manager_identifier_(manager_identifier) {

const auto type_url = Envoy::Config::loadTypeUrl<RdsRouteConfigSubscription>(
rds.config_source().resource_api_version());
subscription_ =
factory_context.clusterManager().subscriptionFactory().subscriptionFromConfigSource(
rds.config_source(), loadTypeUrl(rds.config_source().resource_api_version()), *scope_,
*this);
rds.config_source(), Grpc::Common::typeUrl(API_NO_BOOST(type_url)), *scope_, *this);
config_update_info_ =
std::make_unique<RouteConfigUpdateReceiverImpl>(factory_context.timeSource(), validator);
}
Expand Down Expand Up @@ -205,22 +206,6 @@ bool RdsRouteConfigSubscription::validateUpdateSize(int num_resources) {
return true;
}

std::string RdsRouteConfigSubscription::loadTypeUrl(
envoy::config::core::v3alpha::ApiVersion resource_api_version) {
switch (resource_api_version) {
// automatically set api version as V2
case envoy::config::core::v3alpha::ApiVersion::AUTO:
case envoy::config::core::v3alpha::ApiVersion::V2:
return Grpc::Common::typeUrl(
API_NO_BOOST(envoy::api::v2::RouteConfiguration().GetDescriptor()->full_name()));
case envoy::config::core::v3alpha::ApiVersion::V3ALPHA:
return Grpc::Common::typeUrl(API_NO_BOOST(
envoy::config::route::v3alpha::RouteConfiguration().GetDescriptor()->full_name()));
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
}

RdsRouteConfigProviderImpl::RdsRouteConfigProviderImpl(
RdsRouteConfigSubscriptionSharedPtr&& subscription,
Server::Configuration::FactoryContext& factory_context)
Expand Down
4 changes: 2 additions & 2 deletions source/common/router/rds_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "envoy/admin/v3alpha/config_dump.pb.h"
#include "envoy/config/core/v3alpha/config_source.pb.h"
#include "envoy/config/discovery_service_base.h"
#include "envoy/config/route/v3alpha/route.pb.h"
#include "envoy/config/subscription.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3alpha/http_connection_manager.pb.h"
Expand Down Expand Up @@ -104,7 +105,7 @@ class RdsRouteConfigProviderImpl;
* A class that fetches the route configuration dynamically using the RDS API and updates them to
* RDS config providers.
*/
class RdsRouteConfigSubscription : Envoy::Config::SubscriptionCallbacks,
class RdsRouteConfigSubscription : Envoy::Config::RdsRouteConfigSubscriptionBase,
Logger::Loggable<Logger::Id::router> {
public:
~RdsRouteConfigSubscription() override;
Expand Down Expand Up @@ -147,7 +148,6 @@ class RdsRouteConfigSubscription : Envoy::Config::SubscriptionCallbacks,
RouteConfigProviderManagerImpl& route_config_provider_manager);

bool validateUpdateSize(int num_resources);
static std::string loadTypeUrl(envoy::config::core::v3alpha::ApiVersion resource_api_version);

Init::Manager& getRdsConfigInitManager() { return init_manager_; }

Expand Down
24 changes: 6 additions & 18 deletions source/common/router/scoped_rds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include "common/common/utility.h"
#include "common/config/api_version.h"
#include "common/config/resources.h"
#include "common/config/type_url_loader.h"
#include "common/init/manager_impl.h"
#include "common/init/watcher_impl.h"
#include "common/router/rds_impl.h"

#include "absl/strings/str_join.h"

Expand Down Expand Up @@ -104,10 +106,12 @@ ScopedRdsConfigSubscription::ScopedRdsConfigSubscription(
rds_config_source_(std::move(rds_config_source)),
validation_visitor_(factory_context.messageValidationVisitor()), stat_prefix_(stat_prefix),
route_config_provider_manager_(route_config_provider_manager) {
const auto type_url = Envoy::Config::loadTypeUrl<ScopedRdsConfigSubscription>(
rds_config_source_.resource_api_version());
subscription_ =
factory_context.clusterManager().subscriptionFactory().subscriptionFromConfigSource(
scoped_rds.scoped_rds_config_source(),
loadTypeUrl(rds_config_source_.resource_api_version()), *scope_, *this);
scoped_rds.scoped_rds_config_source(), Grpc::Common::typeUrl(API_NO_BOOST(type_url)),
*scope_, *this);

initialize([scope_key_builder]() -> Envoy::Config::ConfigProvider::ConfigConstSharedPtr {
return std::make_shared<ScopedConfigImpl>(
Expand Down Expand Up @@ -349,22 +353,6 @@ void ScopedRdsConfigSubscription::onConfigUpdate(
onConfigUpdate(to_add_repeated, to_remove_repeated, version_info);
}

std::string ScopedRdsConfigSubscription::loadTypeUrl(
envoy::config::core::v3alpha::ApiVersion resource_api_version) {
switch (resource_api_version) {
// automatically set api version as V2
case envoy::config::core::v3alpha::ApiVersion::AUTO:
case envoy::config::core::v3alpha::ApiVersion::V2:
return Grpc::Common::typeUrl(
API_NO_BOOST(envoy::api::v2::ScopedRouteConfiguration().GetDescriptor()->full_name()));
case envoy::config::core::v3alpha::ApiVersion::V3ALPHA:
return Grpc::Common::typeUrl(API_NO_BOOST(
envoy::config::route::v3alpha::ScopedRouteConfiguration().GetDescriptor()->full_name()));
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
}

ScopedRdsConfigProvider::ScopedRdsConfigProvider(
ScopedRdsConfigSubscriptionSharedPtr&& subscription)
: MutableConfigProviderCommonBase(std::move(subscription), ConfigProvider::ApiType::Delta) {}
Expand Down
4 changes: 2 additions & 2 deletions source/common/router/scoped_rds.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "envoy/common/callback.h"
#include "envoy/config/core/v3alpha/config_source.pb.h"
#include "envoy/config/discovery_service_base.h"
#include "envoy/config/route/v3alpha/scoped_route.pb.h"
#include "envoy/config/subscription.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3alpha/http_connection_manager.pb.h"
Expand Down Expand Up @@ -85,7 +86,7 @@ struct ScopedRdsStats {

// A scoped RDS subscription to be used with the dynamic scoped RDS ConfigProvider.
class ScopedRdsConfigSubscription : public Envoy::Config::DeltaConfigSubscriptionInstance,
Envoy::Config::SubscriptionCallbacks {
Envoy::Config::ScopedRdsConfigSubscriptionBase {
public:
using ScopedRouteConfigurationMap =
std::map<std::string, envoy::config::route::v3alpha::ScopedRouteConfiguration>;
Expand Down Expand Up @@ -166,7 +167,6 @@ class ScopedRdsConfigSubscription : public Envoy::Config::DeltaConfigSubscriptio
resource)
.name();
}
static std::string loadTypeUrl(envoy::config::core::v3alpha::ApiVersion resource_api_version);
// Propagate RDS updates to ScopeConfigImpl in workers.
void onRdsConfigUpdate(const std::string& scope_name,
RdsRouteConfigSubscription& rds_subscription);
Expand Down
20 changes: 3 additions & 17 deletions source/common/router/vhds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "common/common/assert.h"
#include "common/common/fmt.h"
#include "common/config/api_version.h"
#include "common/config/type_url_loader.h"
#include "common/config/utility.h"
#include "common/protobuf/utility.h"
#include "common/router/config_impl.h"
Expand Down Expand Up @@ -40,11 +41,11 @@ VhdsSubscription::VhdsSubscription(RouteConfigUpdatePtr& config_update_info,
if (config_source != envoy::config::core::v3alpha::ApiConfigSource::DELTA_GRPC) {
throw EnvoyException("vhds: only 'DELTA_GRPC' is supported as an api_type.");
}

const auto type_url = Envoy::Config::loadTypeUrl<VhdsSubscription>(resource_api_version);
subscription_ =
factory_context.clusterManager().subscriptionFactory().subscriptionFromConfigSource(
config_update_info_->routeConfiguration().vhds().config_source(),
loadTypeUrl(resource_api_version), *scope_, *this);
Grpc::Common::typeUrl(API_NO_BOOST(type_url)), *scope_, *this);
}

void VhdsSubscription::onConfigUpdateFailed(Envoy::Config::ConfigUpdateFailureReason reason,
Expand All @@ -71,20 +72,5 @@ void VhdsSubscription::onConfigUpdate(
init_target_.ready();
}

std::string
VhdsSubscription::loadTypeUrl(envoy::config::core::v3alpha::ApiVersion resource_api_version) {
switch (resource_api_version) {
// automatically set api version as V2
case envoy::config::core::v3alpha::ApiVersion::AUTO:
case envoy::config::core::v3alpha::ApiVersion::V2:
return Grpc::Common::typeUrl(
API_NO_BOOST(envoy::api::v2::route::VirtualHost().GetDescriptor()->full_name()));
case envoy::config::core::v3alpha::ApiVersion::V3ALPHA:
return Grpc::Common::typeUrl(
API_NO_BOOST(envoy::api::v2::route::VirtualHost().GetDescriptor()->full_name()));
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
}
} // namespace Router
} // namespace Envoy
Loading