diff --git a/include/envoy/server/BUILD b/include/envoy/server/BUILD index 42acd80d8f161..3a3179362ce49 100644 --- a/include/envoy/server/BUILD +++ b/include/envoy/server/BUILD @@ -175,9 +175,14 @@ envoy_cc_library( name = "transport_socket_config_interface", hdrs = ["transport_socket_config.h"], deps = [ + "//include/envoy/event:dispatcher_interface", + "//include/envoy/local_info:local_info_interface", "//include/envoy/network:transport_socket_interface", + "//include/envoy/runtime:runtime_interface", "//include/envoy/secret:secret_manager_interface", "//include/envoy/ssl:context_manager_interface", + "//include/envoy/stats:stats_interface", + "//include/envoy/upstream:cluster_manager_interface", "//source/common/protobuf", ], ) diff --git a/include/envoy/server/transport_socket_config.h b/include/envoy/server/transport_socket_config.h index 4e85386cb16d8..de36c3d963393 100644 --- a/include/envoy/server/transport_socket_config.h +++ b/include/envoy/server/transport_socket_config.h @@ -2,9 +2,14 @@ #include +#include "envoy/event/dispatcher.h" +#include "envoy/local_info/local_info.h" #include "envoy/network/transport_socket.h" +#include "envoy/runtime/runtime.h" #include "envoy/secret/secret_manager.h" #include "envoy/ssl/context_manager.h" +#include "envoy/stats/stats.h" +#include "envoy/upstream/cluster_manager.h" #include "common/protobuf/protobuf.h" @@ -33,6 +38,31 @@ class TransportSocketFactoryContext { * Return the instance of secret manager. */ virtual Secret::SecretManager& secretManager() PURE; + + /** + * @return the instance of ClusterManager. + */ + virtual Upstream::ClusterManager& clusterManager() PURE; + + /** + * @return information about the local environment the server is running in. + */ + virtual const LocalInfo::LocalInfo& localInfo() PURE; + + /** + * @return Event::Dispatcher& the main thread's dispatcher. + */ + virtual Event::Dispatcher& dispatcher() PURE; + + /** + * @return RandomGenerator& the random generator for the server. + */ + virtual Envoy::Runtime::RandomGenerator& random() PURE; + + /** + * @return the server-wide stats store. + */ + virtual Stats::Store& stats() PURE; }; class TransportSocketConfigFactory { diff --git a/source/common/upstream/BUILD b/source/common/upstream/BUILD index 939f0f58b220d..545a660a4368c 100644 --- a/source/common/upstream/BUILD +++ b/source/common/upstream/BUILD @@ -362,6 +362,7 @@ envoy_cc_library( "//source/common/protobuf", "//source/common/protobuf:utility_lib", "//source/extensions/transport_sockets:well_known_names", + "//source/server:transport_socket_config_lib", "@envoy_api//envoy/api/v2/core:base_cc", ], ) diff --git a/source/common/upstream/eds.cc b/source/common/upstream/eds.cc index 46435a6de4d3c..8272738fe5d4f 100644 --- a/source/common/upstream/eds.cc +++ b/source/common/upstream/eds.cc @@ -17,23 +17,25 @@ namespace Envoy { namespace Upstream { -EdsClusterImpl::EdsClusterImpl(const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, - Stats::Store& stats, Ssl::ContextManager& ssl_context_manager, - const LocalInfo::LocalInfo& local_info, ClusterManager& cm, - Event::Dispatcher& dispatcher, Runtime::RandomGenerator& random, - bool added_via_api) - : BaseDynamicClusterImpl(cluster, cm.bindConfig(), runtime, stats, ssl_context_manager, - cm.clusterManagerFactory().secretManager(), added_via_api), - cm_(cm), local_info_(local_info), +EdsClusterImpl::EdsClusterImpl( + const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api) + : BaseDynamicClusterImpl(cluster, runtime, factory_context, std::move(stats_scope), + added_via_api), + cm_(factory_context.clusterManager()), local_info_(factory_context.localInfo()), cluster_name_(cluster.eds_cluster_config().service_name().empty() ? cluster.name() : cluster.eds_cluster_config().service_name()) { - Config::Utility::checkLocalInfo("eds", local_info); + Config::Utility::checkLocalInfo("eds", local_info_); const auto& eds_config = cluster.eds_cluster_config().eds_config(); + Event::Dispatcher& dispatcher = factory_context.dispatcher(); + Runtime::RandomGenerator& random = factory_context.random(); + Upstream::ClusterManager& cm = factory_context.clusterManager(); subscription_ = Config::SubscriptionFactory::subscriptionFromConfigSource< envoy::api::v2::ClusterLoadAssignment>( - eds_config, local_info.node(), dispatcher, cm, random, info_->statsScope(), + eds_config, local_info_.node(), dispatcher, cm, random, info_->statsScope(), [this, &eds_config, &cm, &dispatcher, &random]() -> Config::Subscription* { return new SdsSubscription(info_->stats(), eds_config, cm, dispatcher, random); diff --git a/source/common/upstream/eds.h b/source/common/upstream/eds.h index d84f02091799e..b4c10794e9ccf 100644 --- a/source/common/upstream/eds.h +++ b/source/common/upstream/eds.h @@ -19,10 +19,8 @@ class EdsClusterImpl : public BaseDynamicClusterImpl, Config::SubscriptionCallbacks { public: EdsClusterImpl(const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, - Stats::Store& stats, Ssl::ContextManager& ssl_context_manager, - const LocalInfo::LocalInfo& local_info, ClusterManager& cm, - Event::Dispatcher& dispatcher, Runtime::RandomGenerator& random, - bool added_via_api); + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api); // Upstream::Cluster InitializePhase initializePhase() const override { return InitializePhase::Secondary; } diff --git a/source/common/upstream/logical_dns_cluster.cc b/source/common/upstream/logical_dns_cluster.cc index 5b3709041b021..10ffac930685c 100644 --- a/source/common/upstream/logical_dns_cluster.cc +++ b/source/common/upstream/logical_dns_cluster.cc @@ -15,21 +15,18 @@ namespace Envoy { namespace Upstream { -LogicalDnsCluster::LogicalDnsCluster(const envoy::api::v2::Cluster& cluster, - Runtime::Loader& runtime, Stats::Store& stats, - Ssl::ContextManager& ssl_context_manager, - const LocalInfo::LocalInfo& local_info, - Network::DnsResolverSharedPtr dns_resolver, - ThreadLocal::SlotAllocator& tls, ClusterManager& cm, - Event::Dispatcher& dispatcher, bool added_via_api) - : ClusterImplBase(cluster, cm.bindConfig(), runtime, stats, ssl_context_manager, - cm.clusterManagerFactory().secretManager(), added_via_api), +LogicalDnsCluster::LogicalDnsCluster( + const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, + Network::DnsResolverSharedPtr dns_resolver, ThreadLocal::SlotAllocator& tls, + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api) + : ClusterImplBase(cluster, runtime, factory_context, std::move(stats_scope), added_via_api), dns_resolver_(dns_resolver), dns_refresh_rate_ms_( std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(cluster, dns_refresh_rate, 5000))), - tls_(tls.allocateSlot()), - resolve_timer_(dispatcher.createTimer([this]() -> void { startResolve(); })), - local_info_(local_info), + tls_(tls.allocateSlot()), resolve_timer_(factory_context.dispatcher().createTimer( + [this]() -> void { startResolve(); })), + local_info_(factory_context.localInfo()), load_assignment_(cluster.has_load_assignment() ? cluster.load_assignment() : Config::Utility::translateClusterHosts(cluster.hosts())) { diff --git a/source/common/upstream/logical_dns_cluster.h b/source/common/upstream/logical_dns_cluster.h index 2feec15579b78..cf3660073762c 100644 --- a/source/common/upstream/logical_dns_cluster.h +++ b/source/common/upstream/logical_dns_cluster.h @@ -29,10 +29,9 @@ namespace Upstream { class LogicalDnsCluster : public ClusterImplBase { public: LogicalDnsCluster(const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, - Stats::Store& stats, Ssl::ContextManager& ssl_context_manager, - const LocalInfo::LocalInfo& local_info, Network::DnsResolverSharedPtr dns_resolver, ThreadLocal::SlotAllocator& tls, - ClusterManager& cm, Event::Dispatcher& dispatcher, bool added_via_api); + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api); ~LogicalDnsCluster(); diff --git a/source/common/upstream/original_dst_cluster.cc b/source/common/upstream/original_dst_cluster.cc index e00f39f9410d9..f519d4c543c41 100644 --- a/source/common/upstream/original_dst_cluster.cc +++ b/source/common/upstream/original_dst_cluster.cc @@ -122,15 +122,15 @@ OriginalDstCluster::LoadBalancer::requestOverrideHost(LoadBalancerContext* conte return request_host; } -OriginalDstCluster::OriginalDstCluster(const envoy::api::v2::Cluster& config, - Runtime::Loader& runtime, Stats::Store& stats, - Ssl::ContextManager& ssl_context_manager, ClusterManager& cm, - Event::Dispatcher& dispatcher, bool added_via_api) - : ClusterImplBase(config, cm.bindConfig(), runtime, stats, ssl_context_manager, - cm.clusterManagerFactory().secretManager(), added_via_api), - dispatcher_(dispatcher), cleanup_interval_ms_(std::chrono::milliseconds( - PROTOBUF_GET_MS_OR_DEFAULT(config, cleanup_interval, 5000))), - cleanup_timer_(dispatcher.createTimer([this]() -> void { cleanup(); })) { +OriginalDstCluster::OriginalDstCluster( + const envoy::api::v2::Cluster& config, Runtime::Loader& runtime, + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api) + : ClusterImplBase(config, runtime, factory_context, std::move(stats_scope), added_via_api), + dispatcher_(factory_context.dispatcher()), + cleanup_interval_ms_( + std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(config, cleanup_interval, 5000))), + cleanup_timer_(dispatcher_.createTimer([this]() -> void { cleanup(); })) { cleanup_timer_->enableTimer(cleanup_interval_ms_); } diff --git a/source/common/upstream/original_dst_cluster.h b/source/common/upstream/original_dst_cluster.h index 5cb5107a3a4aa..74d02a8627700 100644 --- a/source/common/upstream/original_dst_cluster.h +++ b/source/common/upstream/original_dst_cluster.h @@ -6,6 +6,7 @@ #include #include "envoy/secret/secret_manager.h" +#include "envoy/server/transport_socket_config.h" #include "envoy/thread_local/thread_local.h" #include "common/common/empty_string.h" @@ -24,8 +25,8 @@ namespace Upstream { class OriginalDstCluster : public ClusterImplBase { public: OriginalDstCluster(const envoy::api::v2::Cluster& config, Runtime::Loader& runtime, - Stats::Store& stats, Ssl::ContextManager& ssl_context_manager, - ClusterManager& cm, Event::Dispatcher& dispatcher, bool added_via_api); + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api); // Upstream::Cluster InitializePhase initializePhase() const override { return InitializePhase::Primary; } diff --git a/source/common/upstream/upstream_impl.cc b/source/common/upstream/upstream_impl.cc index 0ac8dc532041b..05d1396405b89 100644 --- a/source/common/upstream/upstream_impl.cc +++ b/source/common/upstream/upstream_impl.cc @@ -33,6 +33,8 @@ #include "common/upstream/logical_dns_cluster.h" #include "common/upstream/original_dst_cluster.h" +#include "server/transport_socket_config_impl.h" + #include "extensions/transport_sockets/well_known_names.h" namespace Envoy { @@ -265,9 +267,9 @@ ClusterLoadReportStats ClusterInfoImpl::generateLoadReportStats(Stats::Scope& sc ClusterInfoImpl::ClusterInfoImpl(const envoy::api::v2::Cluster& config, const envoy::api::v2::core::BindConfig& bind_config, - Runtime::Loader& runtime, Stats::Store& stats, - Ssl::ContextManager& ssl_context_manager, - Secret::SecretManager& secret_manager, bool added_via_api) + Runtime::Loader& runtime, + Network::TransportSocketFactoryPtr&& socket_factory, + Stats::ScopePtr&& stats_scope, bool added_via_api) : runtime_(runtime), name_(config.name()), type_(config.type()), max_requests_per_connection_( PROTOBUF_GET_WRAPPED_OR_DEFAULT(config, max_requests_per_connection, 0)), @@ -275,9 +277,7 @@ ClusterInfoImpl::ClusterInfoImpl(const envoy::api::v2::Cluster& config, std::chrono::milliseconds(PROTOBUF_GET_MS_REQUIRED(config, connect_timeout))), per_connection_buffer_limit_bytes_( PROTOBUF_GET_WRAPPED_OR_DEFAULT(config, per_connection_buffer_limit_bytes, 1024 * 1024)), - stats_scope_(stats.createScope(fmt::format( - "cluster.{}.", - config.alt_stat_name().empty() ? name_ : std::string(config.alt_stat_name())))), + transport_socket_factory_(std::move(socket_factory)), stats_scope_(std::move(stats_scope)), stats_(generateStats(*stats_scope_)), load_report_stats_(generateLoadReportStats(load_report_stats_store_)), features_(parseFeatures(config)), @@ -286,32 +286,11 @@ ClusterInfoImpl::ClusterInfoImpl(const envoy::api::v2::Cluster& config, maintenance_mode_runtime_key_(fmt::format("upstream.maintenance_mode.{}", name_)), source_address_(getSourceAddress(config, bind_config)), lb_ring_hash_config_(envoy::api::v2::Cluster::RingHashLbConfig(config.ring_hash_lb_config())), - ssl_context_manager_(ssl_context_manager), added_via_api_(added_via_api), + added_via_api_(added_via_api), lb_subset_(LoadBalancerSubsetInfoImpl(config.lb_subset_config())), metadata_(config.metadata()), common_lb_config_(config.common_lb_config()), cluster_socket_options_(parseClusterSocketOptions(config, bind_config)), - drain_connections_on_host_removal_(config.drain_connections_on_host_removal()), - secret_manager_(secret_manager) { - - // If the cluster doesn't have a transport socket configured, override with the default transport - // socket implementation based on the tls_context. We copy by value first then override if - // necessary. - auto transport_socket = config.transport_socket(); - if (!config.has_transport_socket()) { - if (config.has_tls_context()) { - transport_socket.set_name(Extensions::TransportSockets::TransportSocketNames::get().Tls); - MessageUtil::jsonConvert(config.tls_context(), *transport_socket.mutable_config()); - } else { - transport_socket.set_name( - Extensions::TransportSockets::TransportSocketNames::get().RawBuffer); - } - } - - auto& config_factory = Config::Utility::getAndCheckFactory< - Server::Configuration::UpstreamTransportSocketConfigFactory>(transport_socket.name()); - ProtobufTypes::MessagePtr message = - Config::Utility::translateToFactoryConfig(transport_socket, config_factory); - transport_socket_factory_ = config_factory.createTransportSocketFactory(*message, *this); + drain_connections_on_host_removal_(config.drain_connections_on_host_removal()) { switch (config.lb_policy()) { case envoy::api::v2::Cluster::ROUND_ROBIN: @@ -354,6 +333,40 @@ ClusterInfoImpl::ClusterInfoImpl(const envoy::api::v2::Cluster& config, } } +namespace { + +Stats::ScopePtr generateStatsScope(const envoy::api::v2::Cluster& config, Stats::Store& stats) { + return stats.createScope(fmt::format("cluster.{}.", config.alt_stat_name().empty() + ? config.name() + : std::string(config.alt_stat_name()))); +} + +Network::TransportSocketFactoryPtr createTransportSocketFactory( + const envoy::api::v2::Cluster& config, + Server::Configuration::TransportSocketFactoryContext& factory_context) { + // If the cluster config doesn't have a transport socket configured, override with the default + // transport socket implementation based on the tls_context. We copy by value first then override + // if necessary. + auto transport_socket = config.transport_socket(); + if (!config.has_transport_socket()) { + if (config.has_tls_context()) { + transport_socket.set_name(Extensions::TransportSockets::TransportSocketNames::get().Tls); + MessageUtil::jsonConvert(config.tls_context(), *transport_socket.mutable_config()); + } else { + transport_socket.set_name( + Extensions::TransportSockets::TransportSocketNames::get().RawBuffer); + } + } + + auto& config_factory = Config::Utility::getAndCheckFactory< + Server::Configuration::UpstreamTransportSocketConfigFactory>(transport_socket.name()); + ProtobufTypes::MessagePtr message = + Config::Utility::translateToFactoryConfig(transport_socket, config_factory); + return config_factory.createTransportSocketFactory(*message, factory_context); +} + +} // namespace + ClusterSharedPtr ClusterImplBase::create( const envoy::api::v2::Cluster& cluster, ClusterManager& cm, Stats::Store& stats, ThreadLocal::Instance& tls, Network::DnsResolverSharedPtr dns_resolver, @@ -380,19 +393,23 @@ ClusterSharedPtr ClusterImplBase::create( selected_dns_resolver = dispatcher.createDnsResolver(resolvers); } + auto stats_scope = generateStatsScope(cluster, stats); + Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *stats_scope, cm, local_info, dispatcher, random, stats); + switch (cluster.type()) { case envoy::api::v2::Cluster::STATIC: - new_cluster.reset(new StaticClusterImpl(cluster, runtime, stats, ssl_context_manager, - local_info, cm, added_via_api)); + new_cluster.reset(new StaticClusterImpl(cluster, runtime, factory_context, + std::move(stats_scope), added_via_api)); break; case envoy::api::v2::Cluster::STRICT_DNS: - new_cluster.reset(new StrictDnsClusterImpl(cluster, runtime, stats, ssl_context_manager, - local_info, selected_dns_resolver, cm, dispatcher, + new_cluster.reset(new StrictDnsClusterImpl(cluster, runtime, selected_dns_resolver, + factory_context, std::move(stats_scope), added_via_api)); break; case envoy::api::v2::Cluster::LOGICAL_DNS: - new_cluster.reset(new LogicalDnsCluster(cluster, runtime, stats, ssl_context_manager, - local_info, selected_dns_resolver, tls, cm, dispatcher, + new_cluster.reset(new LogicalDnsCluster(cluster, runtime, selected_dns_resolver, tls, + factory_context, std::move(stats_scope), added_via_api)); break; case envoy::api::v2::Cluster::ORIGINAL_DST: @@ -404,8 +421,8 @@ ClusterSharedPtr ClusterImplBase::create( throw EnvoyException(fmt::format( "cluster: cluster type 'original_dst' may not be used with lb_subset_config")); } - new_cluster.reset(new OriginalDstCluster(cluster, runtime, stats, ssl_context_manager, cm, - dispatcher, added_via_api)); + new_cluster.reset(new OriginalDstCluster(cluster, runtime, factory_context, + std::move(stats_scope), added_via_api)); break; case envoy::api::v2::Cluster::EDS: if (!cluster.has_eds_cluster_config()) { @@ -413,8 +430,8 @@ ClusterSharedPtr ClusterImplBase::create( } // We map SDS to EDS, since EDS provides backwards compatibility with SDS. - new_cluster.reset(new EdsClusterImpl(cluster, runtime, stats, ssl_context_manager, local_info, - cm, dispatcher, random, added_via_api)); + new_cluster.reset(new EdsClusterImpl(cluster, runtime, factory_context, std::move(stats_scope), + added_via_api)); break; default: NOT_REACHED_GCOVR_EXCL_LINE; @@ -435,14 +452,15 @@ ClusterSharedPtr ClusterImplBase::create( return std::move(new_cluster); } -ClusterImplBase::ClusterImplBase(const envoy::api::v2::Cluster& cluster, - const envoy::api::v2::core::BindConfig& bind_config, - Runtime::Loader& runtime, Stats::Store& stats, - Ssl::ContextManager& ssl_context_manager, - Secret::SecretManager& secret_manager, bool added_via_api) - : runtime_(runtime), - info_(new ClusterInfoImpl(cluster, bind_config, runtime, stats, ssl_context_manager, - secret_manager, added_via_api)) { +ClusterImplBase::ClusterImplBase( + const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api) + : runtime_(runtime) { + auto socket_factory = createTransportSocketFactory(cluster, factory_context); + info_ = std::make_unique(cluster, factory_context.clusterManager().bindConfig(), + runtime, std::move(socket_factory), + std::move(stats_scope), added_via_api); // Create the default (empty) priority set before registering callbacks to // avoid getting an update the first time it is accessed. priority_set_.getOrCreateHostSet(0); @@ -768,14 +786,12 @@ void PriorityStateManager::updateClusterPrioritySet( hosts_removed.value_or({})); } -StaticClusterImpl::StaticClusterImpl(const envoy::api::v2::Cluster& cluster, - Runtime::Loader& runtime, Stats::Store& stats, - Ssl::ContextManager& ssl_context_manager, - const LocalInfo::LocalInfo& local_info, ClusterManager& cm, - bool added_via_api) - : ClusterImplBase(cluster, cm.bindConfig(), runtime, stats, ssl_context_manager, - cm.clusterManagerFactory().secretManager(), added_via_api), - priority_state_manager_(new PriorityStateManager(*this, local_info)) { +StaticClusterImpl::StaticClusterImpl( + const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api) + : ClusterImplBase(cluster, runtime, factory_context, std::move(stats_scope), added_via_api), + priority_state_manager_(new PriorityStateManager(*this, factory_context.localInfo())) { // TODO(dio): Use by-reference when cluster.hosts() is removed. const envoy::api::v2::ClusterLoadAssignment cluster_load_assignment( cluster.has_load_assignment() ? cluster.load_assignment() @@ -952,16 +968,14 @@ bool BaseDynamicClusterImpl::updateDynamicHostList(const HostVector& new_hosts, } } -StrictDnsClusterImpl::StrictDnsClusterImpl(const envoy::api::v2::Cluster& cluster, - Runtime::Loader& runtime, Stats::Store& stats, - Ssl::ContextManager& ssl_context_manager, - const LocalInfo::LocalInfo& local_info, - Network::DnsResolverSharedPtr dns_resolver, - ClusterManager& cm, Event::Dispatcher& dispatcher, - bool added_via_api) - : BaseDynamicClusterImpl(cluster, cm.bindConfig(), runtime, stats, ssl_context_manager, - cm.clusterManagerFactory().secretManager(), added_via_api), - local_info_(local_info), dns_resolver_(dns_resolver), +StrictDnsClusterImpl::StrictDnsClusterImpl( + const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, + Network::DnsResolverSharedPtr dns_resolver, + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api) + : BaseDynamicClusterImpl(cluster, runtime, factory_context, std::move(stats_scope), + added_via_api), + local_info_(factory_context.localInfo()), dns_resolver_(dns_resolver), dns_refresh_rate_ms_( std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(cluster, dns_refresh_rate, 5000))) { switch (cluster.dns_lookup_family()) { @@ -987,8 +1001,8 @@ StrictDnsClusterImpl::StrictDnsClusterImpl(const envoy::api::v2::Cluster& cluste const auto& host = lb_endpoint.endpoint().address(); const std::string& url = fmt::format("tcp://{}:{}", host.socket_address().address(), host.socket_address().port_value()); - resolve_targets_.emplace_back( - new ResolveTarget(*this, dispatcher, url, locality_lb_endpoint, lb_endpoint)); + resolve_targets_.emplace_back(new ResolveTarget(*this, factory_context.dispatcher(), url, + locality_lb_endpoint, lb_endpoint)); } } } diff --git a/source/common/upstream/upstream_impl.h b/source/common/upstream/upstream_impl.h index f9b4c4e57cfd4..4b0989df38611 100644 --- a/source/common/upstream/upstream_impl.h +++ b/source/common/upstream/upstream_impl.h @@ -328,13 +328,12 @@ class PrioritySetImpl : public PrioritySet { /** * Implementation of ClusterInfo that reads from JSON. */ -class ClusterInfoImpl : public ClusterInfo, - public Server::Configuration::TransportSocketFactoryContext { +class ClusterInfoImpl : public ClusterInfo { public: ClusterInfoImpl(const envoy::api::v2::Cluster& config, const envoy::api::v2::core::BindConfig& bind_config, Runtime::Loader& runtime, - Stats::Store& stats, Ssl::ContextManager& ssl_context_manager, - Secret::SecretManager& secret_manager, bool added_via_api); + Network::TransportSocketFactoryPtr&& socket_factory, + Stats::ScopePtr&& stats_scope, bool added_via_api); static ClusterStats generateStats(Stats::Scope& scope); static ClusterLoadReportStats generateLoadReportStats(Stats::Scope& scope); @@ -375,17 +374,12 @@ class ClusterInfoImpl : public ClusterInfo, const LoadBalancerSubsetInfo& lbSubsetInfo() const override { return lb_subset_; } const envoy::api::v2::core::Metadata& metadata() const override { return metadata_; } - // Server::Configuration::TransportSocketFactoryContext - Ssl::ContextManager& sslContextManager() override { return ssl_context_manager_; } - const Network::ConnectionSocket::OptionsSharedPtr& clusterSocketOptions() const override { return cluster_socket_options_; }; bool drainConnectionsOnHostRemoval() const override { return drain_connections_on_host_removal_; } - Secret::SecretManager& secretManager() override { return secret_manager_; } - private: struct ResourceManagers { ResourceManagers(const envoy::api::v2::Cluster& config, Runtime::Loader& runtime, @@ -406,11 +400,11 @@ class ClusterInfoImpl : public ClusterInfo, const std::chrono::milliseconds connect_timeout_; absl::optional idle_timeout_; const uint32_t per_connection_buffer_limit_bytes_; + Network::TransportSocketFactoryPtr transport_socket_factory_; Stats::ScopePtr stats_scope_; mutable ClusterStats stats_; Stats::IsolatedStoreImpl load_report_stats_store_; mutable ClusterLoadReportStats load_report_stats_; - Network::TransportSocketFactoryPtr transport_socket_factory_; const uint64_t features_; const Http::Http2Settings http2_settings_; mutable ResourceManagers resource_managers_; @@ -418,14 +412,12 @@ class ClusterInfoImpl : public ClusterInfo, const Network::Address::InstanceConstSharedPtr source_address_; LoadBalancerType lb_type_; absl::optional lb_ring_hash_config_; - Ssl::ContextManager& ssl_context_manager_; const bool added_via_api_; LoadBalancerSubsetInfoImpl lb_subset_; const envoy::api::v2::core::Metadata metadata_; const envoy::api::v2::Cluster::CommonLbConfig common_lb_config_; const Network::ConnectionSocket::OptionsSharedPtr cluster_socket_options_; const bool drain_connections_on_host_removal_; - Secret::SecretManager& secret_manager_; }; /** @@ -478,10 +470,9 @@ class ClusterImplBase : public Cluster, protected Logger::Loggable callback) override; protected: - ClusterImplBase(const envoy::api::v2::Cluster& cluster, - const envoy::api::v2::core::BindConfig& bind_config, Runtime::Loader& runtime, - Stats::Store& stats, Ssl::ContextManager& ssl_context_manager, - Secret::SecretManager& secret_manager, bool added_via_api); + ClusterImplBase(const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api); /** * Overridden by every concrete cluster. The cluster should do whatever pre-init is needed. E.g., @@ -576,8 +567,8 @@ typedef std::unique_ptr PriorityStateManagerPtr; class StaticClusterImpl : public ClusterImplBase { public: StaticClusterImpl(const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, - Stats::Store& stats, Ssl::ContextManager& ssl_context_manager, - const LocalInfo::LocalInfo& local_info, ClusterManager& cm, bool added_via_api); + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api); // Upstream::Cluster InitializePhase initializePhase() const override { return InitializePhase::Primary; } @@ -607,10 +598,9 @@ class BaseDynamicClusterImpl : public ClusterImplBase { class StrictDnsClusterImpl : public BaseDynamicClusterImpl { public: StrictDnsClusterImpl(const envoy::api::v2::Cluster& cluster, Runtime::Loader& runtime, - Stats::Store& stats, Ssl::ContextManager& ssl_context_manager, - const LocalInfo::LocalInfo& local_info, - Network::DnsResolverSharedPtr dns_resolver, ClusterManager& cm, - Event::Dispatcher& dispatcher, bool added_via_api); + Network::DnsResolverSharedPtr dns_resolver, + Server::Configuration::TransportSocketFactoryContext& factory_context, + Stats::ScopePtr&& stats_scope, bool added_via_api); // Upstream::Cluster InitializePhase initializePhase() const override { return InitializePhase::Primary; } diff --git a/source/server/BUILD b/source/server/BUILD index 1f02f4424c4cf..130b048592a22 100644 --- a/source/server/BUILD +++ b/source/server/BUILD @@ -217,6 +217,7 @@ envoy_cc_library( ":drain_manager_lib", ":init_manager_lib", ":lds_api_lib", + ":transport_socket_config_lib", "//include/envoy/server:filter_config_interface", "//include/envoy/server:listener_manager_interface", "//include/envoy/server:transport_socket_config_interface", @@ -349,3 +350,11 @@ envoy_cc_library( "//source/common/common:thread_lib", ], ) + +envoy_cc_library( + name = "transport_socket_config_lib", + hdrs = ["transport_socket_config_impl.h"], + deps = [ + "//include/envoy/server:transport_socket_config_interface", + ], +) diff --git a/source/server/listener_manager_impl.cc b/source/server/listener_manager_impl.cc index 3e3d25bb4f185..5008eaafe462b 100644 --- a/source/server/listener_manager_impl.cc +++ b/source/server/listener_manager_impl.cc @@ -17,6 +17,7 @@ #include "server/configuration_impl.h" #include "server/drain_manager_impl.h" +#include "server/transport_socket_config_impl.h" #include "extensions/filters/listener/well_known_names.h" #include "extensions/filters/network/well_known_names.h" @@ -225,12 +226,15 @@ ListenerImpl::ListenerImpl(const envoy::api::v2::Listener& config, const std::st std::vector application_protocols( filter_chain_match.application_protocols().begin(), filter_chain_match.application_protocols().end()); - - addFilterChain(PROTOBUF_GET_WRAPPED_OR_DEFAULT(filter_chain_match, destination_port, 0), - destination_ips, server_names, filter_chain_match.transport_protocol(), - application_protocols, - config_factory.createTransportSocketFactory(*message, *this, server_names), - parent_.factory_.createNetworkFilterFactoryList(filter_chain.filters(), *this)); + Server::Configuration::TransportSocketFactoryContextImpl factory_context( + parent_.server_.sslContextManager(), *listener_scope_, parent_.server_.clusterManager(), + parent_.server_.localInfo(), parent_.server_.dispatcher(), parent_.server_.random(), + parent_.server_.stats()); + addFilterChain( + PROTOBUF_GET_WRAPPED_OR_DEFAULT(filter_chain_match, destination_port, 0), destination_ips, + server_names, filter_chain_match.transport_protocol(), application_protocols, + config_factory.createTransportSocketFactory(*message, factory_context, server_names), + parent_.factory_.createNetworkFilterFactoryList(filter_chain.filters(), *this)); need_tls_inspector |= filter_chain_match.transport_protocol() == "tls" || (filter_chain_match.transport_protocol().empty() && diff --git a/source/server/listener_manager_impl.h b/source/server/listener_manager_impl.h index 14dfadf30ab58..89193b7def039 100644 --- a/source/server/listener_manager_impl.h +++ b/source/server/listener_manager_impl.h @@ -190,7 +190,6 @@ class ListenerImpl : public Network::ListenerConfig, public Network::DrainDecision, public Network::FilterChainManager, public Network::FilterChainFactory, - public Configuration::TransportSocketFactoryContext, Logger::Loggable { public: /** @@ -301,11 +300,6 @@ class ListenerImpl : public Network::ListenerConfig, const std::vector& factories) override; bool createListenerFilterChain(Network::ListenerFilterManager& manager) override; - // Configuration::TransportSocketFactoryContext - Ssl::ContextManager& sslContextManager() override { return parent_.server_.sslContextManager(); } - Stats::Scope& statsScope() const override { return *listener_scope_; } - Secret::SecretManager& secretManager() override { return parent_.server_.secretManager(); } - SystemTime last_updated_; private: diff --git a/source/server/transport_socket_config_impl.h b/source/server/transport_socket_config_impl.h new file mode 100644 index 0000000000000..74e50d35e6b00 --- /dev/null +++ b/source/server/transport_socket_config_impl.h @@ -0,0 +1,52 @@ +#pragma once + +#include "envoy/server/transport_socket_config.h" + +namespace Envoy { +namespace Server { +namespace Configuration { + +/** + * Implementation of TransportSocketFactoryContext. + */ +class TransportSocketFactoryContextImpl : public TransportSocketFactoryContext { +public: + TransportSocketFactoryContextImpl(Ssl::ContextManager& context_manager, Stats::Scope& stats_scope, + Upstream::ClusterManager& cm, + const LocalInfo::LocalInfo& local_info, + Event::Dispatcher& dispatcher, + Envoy::Runtime::RandomGenerator& random, Stats::Store& stats) + : context_manager_(context_manager), stats_scope_(stats_scope), cluster_manager_(cm), + local_info_(local_info), dispatcher_(dispatcher), random_(random), stats_(stats) {} + + Ssl::ContextManager& sslContextManager() override { return context_manager_; } + + Stats::Scope& statsScope() const override { return stats_scope_; } + + Upstream::ClusterManager& clusterManager() override { return cluster_manager_; } + + Secret::SecretManager& secretManager() override { + return cluster_manager_.clusterManagerFactory().secretManager(); + } + + const LocalInfo::LocalInfo& localInfo() override { return local_info_; } + + Event::Dispatcher& dispatcher() override { return dispatcher_; } + + Envoy::Runtime::RandomGenerator& random() override { return random_; } + + Stats::Store& stats() override { return stats_; } + +private: + Ssl::ContextManager& context_manager_; + Stats::Scope& stats_scope_; + Upstream::ClusterManager& cluster_manager_; + const LocalInfo::LocalInfo& local_info_; + Event::Dispatcher& dispatcher_; + Envoy::Runtime::RandomGenerator& random_; + Stats::Store& stats_; +}; + +} // namespace Configuration +} // namespace Server +} // namespace Envoy diff --git a/test/common/upstream/BUILD b/test/common/upstream/BUILD index 441ce2437613f..789dd66c28a1a 100644 --- a/test/common/upstream/BUILD +++ b/test/common/upstream/BUILD @@ -71,6 +71,7 @@ envoy_cc_test( "//source/common/config:utility_lib", "//source/common/upstream:eds_lib", "//source/extensions/transport_sockets/raw_buffer:config", + "//source/server:transport_socket_config_lib", "//test/mocks/local_info:local_info_mocks", "//test/mocks/runtime:runtime_mocks", "//test/mocks/ssl:ssl_mocks", @@ -171,6 +172,7 @@ envoy_cc_test( "//source/common/upstream:logical_dns_cluster_lib", "//source/common/upstream:upstream_lib", "//source/extensions/transport_sockets/raw_buffer:config", + "//source/server:transport_socket_config_lib", "//test/mocks:common_lib", "//test/mocks/local_info:local_info_mocks", "//test/mocks/network:network_mocks", @@ -193,8 +195,10 @@ envoy_cc_test( "//source/common/upstream:upstream_lib", "//source/extensions/transport_sockets/raw_buffer:config", "//test/mocks:common_lib", + "//test/mocks/local_info:local_info_mocks", "//test/mocks/network:network_mocks", "//test/mocks/runtime:runtime_mocks", + "//test/mocks/server:server_mocks", "//test/mocks/ssl:ssl_mocks", "//test/mocks/upstream:upstream_mocks", "//test/test_common:utility_lib", @@ -282,6 +286,7 @@ envoy_cc_test( "//source/common/network:utility_lib", "//source/common/upstream:eds_lib", "//source/extensions/transport_sockets/raw_buffer:config", + "//source/server:transport_socket_config_lib", "//test/mocks/local_info:local_info_mocks", "//test/mocks/runtime:runtime_mocks", "//test/mocks/ssl:ssl_mocks", @@ -326,6 +331,7 @@ envoy_cc_test( "//source/common/upstream:upstream_includes", "//source/common/upstream:upstream_lib", "//source/extensions/transport_sockets/raw_buffer:config", + "//source/server:transport_socket_config_lib", "//test/mocks:common_lib", "//test/mocks/local_info:local_info_mocks", "//test/mocks/network:network_mocks", diff --git a/test/common/upstream/eds_test.cc b/test/common/upstream/eds_test.cc index 1d8555da76228..33dd33578df34 100644 --- a/test/common/upstream/eds_test.cc +++ b/test/common/upstream/eds_test.cc @@ -5,6 +5,8 @@ #include "common/config/utility.h" #include "common/upstream/eds.h" +#include "server/transport_socket_config_impl.h" + #include "test/common/upstream/utility.h" #include "test/mocks/local_info/mocks.h" #include "test/mocks/runtime/mocks.h" @@ -51,8 +53,14 @@ class EdsTest : public testing::Test { EXPECT_CALL(cm_, clusters()).WillOnce(Return(cluster_map)); EXPECT_CALL(cluster, info()).Times(2); EXPECT_CALL(*cluster.info_, addedViaApi()); - cluster_.reset(new EdsClusterImpl(eds_cluster_, runtime_, stats_, ssl_context_manager_, - local_info_, cm_, dispatcher_, random_, false)); + Envoy::Stats::ScopePtr scope = stats_.createScope( + fmt::format("cluster.{}.", eds_cluster_.alt_stat_name().empty() + ? eds_cluster_.name() + : std::string(eds_cluster_.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager_, *scope, cm_, local_info_, dispatcher_, random_, stats_); + cluster_.reset( + new EdsClusterImpl(eds_cluster_, runtime_, factory_context, std::move(scope), false)); EXPECT_EQ(Cluster::InitializePhase::Secondary, cluster_->initializePhase()); } diff --git a/test/common/upstream/logical_dns_cluster_test.cc b/test/common/upstream/logical_dns_cluster_test.cc index 80a5c7b705fc5..ba927f178985f 100644 --- a/test/common/upstream/logical_dns_cluster_test.cc +++ b/test/common/upstream/logical_dns_cluster_test.cc @@ -7,6 +7,8 @@ #include "common/network/utility.h" #include "common/upstream/logical_dns_cluster.h" +#include "server/transport_socket_config_impl.h" + #include "test/common/upstream/utility.h" #include "test/mocks/common.h" #include "test/mocks/local_info/mocks.h" @@ -34,9 +36,15 @@ class LogicalDnsClusterTest : public testing::Test { void setupFromV1Json(const std::string& json) { resolve_timer_ = new Event::MockTimer(&dispatcher_); NiceMock cm; - cluster_.reset(new LogicalDnsCluster(parseClusterFromJson(json), runtime_, stats_store_, - ssl_context_manager_, local_info_, dns_resolver_, tls_, cm, - dispatcher_, false)); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats_store_.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager_, *scope, cm, local_info_, dispatcher_, random_, stats_store_); + cluster_.reset(new LogicalDnsCluster(cluster_config, runtime_, dns_resolver_, tls_, + factory_context, std::move(scope), false)); cluster_->prioritySet().addMemberUpdateCb( [&](uint32_t, const HostVector&, const HostVector&) -> void { membership_updated_.ready(); @@ -47,9 +55,15 @@ class LogicalDnsClusterTest : public testing::Test { void setupFromV2Yaml(const std::string& yaml) { resolve_timer_ = new Event::MockTimer(&dispatcher_); NiceMock cm; - cluster_.reset(new LogicalDnsCluster(parseClusterFromV2Yaml(yaml), runtime_, stats_store_, - ssl_context_manager_, local_info_, dns_resolver_, tls_, cm, - dispatcher_, false)); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats_store_.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager_, *scope, cm, local_info_, dispatcher_, random_, stats_store_); + cluster_.reset(new LogicalDnsCluster(cluster_config, runtime_, dns_resolver_, tls_, + factory_context, std::move(scope), false)); cluster_->prioritySet().addMemberUpdateCb( [&](uint32_t, const HostVector&, const HostVector&) -> void { membership_updated_.ready(); @@ -168,6 +182,7 @@ class LogicalDnsClusterTest : public testing::Test { std::shared_ptr> dns_resolver_{ new NiceMock}; Network::MockActiveDnsQuery active_dns_query_; + NiceMock random_; Network::DnsResolver::ResolveCb dns_callback_; NiceMock tls_; Event::MockTimer* resolve_timer_; diff --git a/test/common/upstream/original_dst_cluster_test.cc b/test/common/upstream/original_dst_cluster_test.cc index 098723c4d0ddc..a15109db7cf50 100644 --- a/test/common/upstream/original_dst_cluster_test.cc +++ b/test/common/upstream/original_dst_cluster_test.cc @@ -9,8 +9,11 @@ #include "common/upstream/original_dst_cluster.h" #include "common/upstream/upstream_impl.h" +#include "server/transport_socket_config_impl.h" + #include "test/common/upstream/utility.h" #include "test/mocks/common.h" +#include "test/mocks/local_info/mocks.h" #include "test/mocks/network/mocks.h" #include "test/mocks/runtime/mocks.h" #include "test/mocks/ssl/mocks.h" @@ -59,8 +62,15 @@ class OriginalDstClusterTest : public testing::Test { void setup(const std::string& json) { NiceMock cm; - cluster_.reset(new OriginalDstCluster(parseClusterFromJson(json), runtime_, stats_store_, - ssl_context_manager_, cm, dispatcher_, false)); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats_store_.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager_, *scope, cm, local_info_, dispatcher_, random_, stats_store_); + cluster_.reset( + new OriginalDstCluster(cluster_config, runtime_, factory_context, std::move(scope), false)); cluster_->prioritySet().addMemberUpdateCb( [&](uint32_t, const HostVector&, const HostVector&) -> void { membership_updated_.ready(); @@ -76,6 +86,8 @@ class OriginalDstClusterTest : public testing::Test { NiceMock runtime_; NiceMock dispatcher_; Event::MockTimer* cleanup_timer_; + NiceMock random_; + NiceMock local_info_; }; TEST(OriginalDstClusterConfigTest, BadConfig) { diff --git a/test/common/upstream/sds_test.cc b/test/common/upstream/sds_test.cc index cd01ae7539ccb..85a7dfb180b54 100644 --- a/test/common/upstream/sds_test.cc +++ b/test/common/upstream/sds_test.cc @@ -13,6 +13,8 @@ #include "common/protobuf/protobuf.h" #include "common/upstream/eds.h" +#include "server/transport_socket_config_impl.h" + #include "test/common/upstream/utility.h" #include "test/mocks/local_info/mocks.h" #include "test/mocks/runtime/mocks.h" @@ -30,6 +32,7 @@ using testing::InSequence; using testing::Invoke; using testing::NiceMock; using testing::Return; +using testing::ReturnRef; using testing::SaveArg; using testing::WithArg; using testing::_; @@ -62,8 +65,14 @@ class SdsTest : public testing::Test { EXPECT_CALL(cm_, clusters()).WillOnce(Return(cluster_map)); EXPECT_CALL(cluster, info()).Times(2); EXPECT_CALL(*cluster.info_, addedViaApi()); - cluster_.reset(new EdsClusterImpl(sds_cluster_, runtime_, stats_, ssl_context_manager_, - local_info_, cm_, dispatcher_, random_, false)); + Envoy::Stats::ScopePtr scope = stats_.createScope( + fmt::format("cluster.{}.", sds_cluster_.alt_stat_name().empty() + ? sds_cluster_.name() + : std::string(sds_cluster_.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager_, *scope, cm_, local_info_, dispatcher_, random_, stats_); + cluster_.reset( + new EdsClusterImpl(sds_cluster_, runtime_, factory_context, std::move(scope), false)); EXPECT_EQ(Cluster::InitializePhase::Secondary, cluster_->initializePhase()); } diff --git a/test/common/upstream/upstream_impl_test.cc b/test/common/upstream/upstream_impl_test.cc index f4b43c393d2d6..4334b46717143 100644 --- a/test/common/upstream/upstream_impl_test.cc +++ b/test/common/upstream/upstream_impl_test.cc @@ -15,6 +15,8 @@ #include "common/network/utility.h" #include "common/upstream/upstream_impl.h" +#include "server/transport_socket_config_impl.h" + #include "test/common/upstream/utility.h" #include "test/mocks/common.h" #include "test/mocks/local_info/mocks.h" @@ -30,6 +32,7 @@ using testing::ContainerEq; using testing::Invoke; using testing::NiceMock; +using testing::ReturnRef; using testing::_; namespace Envoy { @@ -122,7 +125,7 @@ TEST_P(StrictDnsParamTest, ImmediateResolve) { NiceMock runtime; NiceMock local_info; ReadyWatcher initialized; - + NiceMock random; const std::string json = R"EOF( { "name": "name", @@ -142,8 +145,16 @@ TEST_P(StrictDnsParamTest, ImmediateResolve) { return nullptr; })); NiceMock cm; - StrictDnsClusterImpl cluster(parseClusterFromJson(json), runtime, stats, ssl_context_manager, - local_info, dns_resolver, cm, dispatcher, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + + StrictDnsClusterImpl cluster(cluster_config, runtime, dns_resolver, factory_context, + std::move(scope), false); cluster.initialize([&]() -> void { initialized.ready(); }); EXPECT_EQ(2UL, cluster.prioritySet().hostSetsPerPriority()[0]->hosts().size()); EXPECT_EQ(2UL, cluster.prioritySet().hostSetsPerPriority()[0]->healthyHosts().size()); @@ -158,6 +169,7 @@ TEST(StrictDnsClusterImplTest, ZeroHostsHealthChecker) { NiceMock runtime; NiceMock cm; NiceMock local_info; + NiceMock random; ReadyWatcher initialized; const std::string yaml = R"EOF( @@ -169,8 +181,15 @@ TEST(StrictDnsClusterImplTest, ZeroHostsHealthChecker) { )EOF"; ResolverData resolver(*dns_resolver, dispatcher); - StrictDnsClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, dns_resolver, cm, dispatcher, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StrictDnsClusterImpl cluster(cluster_config, runtime, dns_resolver, factory_context, + std::move(scope), false); std::shared_ptr health_checker(new MockHealthChecker()); EXPECT_CALL(*health_checker, start()); EXPECT_CALL(*health_checker, addHostCheckCompleteCb(_)); @@ -192,7 +211,7 @@ TEST(StrictDnsClusterImplTest, Basic) { NiceMock dispatcher; NiceMock runtime; NiceMock local_info; - + NiceMock random; // gmock matches in LIFO order which is why these are swapped. ResolverData resolver2(*dns_resolver, dispatcher); ResolverData resolver1(*dns_resolver, dispatcher); @@ -228,8 +247,15 @@ TEST(StrictDnsClusterImplTest, Basic) { )EOF"; NiceMock cm; - StrictDnsClusterImpl cluster(parseClusterFromJson(json), runtime, stats, ssl_context_manager, - local_info, dns_resolver, cm, dispatcher, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StrictDnsClusterImpl cluster(cluster_config, runtime, dns_resolver, factory_context, + std::move(scope), false); EXPECT_CALL(runtime.snapshot_, getInteger("circuit_breakers.name.default.max_connections", 43)); EXPECT_EQ(43U, cluster.info()->resourceManager(ResourcePriority::Default).connections().max()); EXPECT_CALL(runtime.snapshot_, @@ -334,6 +360,7 @@ TEST(StrictDnsClusterImplTest, HostRemovalActiveHealthSkipped) { NiceMock runtime; NiceMock cm; NiceMock local_info; + NiceMock random; const std::string yaml = R"EOF( name: name @@ -345,8 +372,15 @@ TEST(StrictDnsClusterImplTest, HostRemovalActiveHealthSkipped) { )EOF"; ResolverData resolver(*dns_resolver, dispatcher); - StrictDnsClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, dns_resolver, cm, dispatcher, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StrictDnsClusterImpl cluster(cluster_config, runtime, dns_resolver, factory_context, + std::move(scope), false); std::shared_ptr health_checker(new MockHealthChecker()); EXPECT_CALL(*health_checker, start()); EXPECT_CALL(*health_checker, addHostCheckCompleteCb(_)); @@ -384,7 +418,7 @@ TEST(StrictDnsClusterImplTest, LoadAssignmentBasic) { NiceMock dispatcher; NiceMock runtime; NiceMock local_info; - + NiceMock random; // gmock matches in LIFO order which is why these are swapped. ResolverData resolver2(*dns_resolver, dispatcher); ResolverData resolver1(*dns_resolver, dispatcher); @@ -437,8 +471,16 @@ TEST(StrictDnsClusterImplTest, LoadAssignmentBasic) { )EOF"; NiceMock cm; - StrictDnsClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, dns_resolver, cm, dispatcher, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StrictDnsClusterImpl cluster(cluster_config, runtime, dns_resolver, factory_context, + std::move(scope), false); + EXPECT_CALL(runtime.snapshot_, getInteger("circuit_breakers.name.default.max_connections", 43)); EXPECT_EQ(43U, cluster.info()->resourceManager(ResourcePriority::Default).connections().max()); EXPECT_CALL(runtime.snapshot_, @@ -540,7 +582,7 @@ TEST(StrictDnsClusterImplTest, LoadAssignmentBasicMultiplePriorities) { NiceMock dispatcher; NiceMock runtime; NiceMock local_info; - + NiceMock random; // gmock matches in LIFO order which is why these are swapped. ResolverData resolver3(*dns_resolver, dispatcher); ResolverData resolver2(*dns_resolver, dispatcher); @@ -587,9 +629,15 @@ TEST(StrictDnsClusterImplTest, LoadAssignmentBasicMultiplePriorities) { )EOF"; NiceMock cm; - StrictDnsClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, dns_resolver, cm, dispatcher, false); - + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StrictDnsClusterImpl cluster(cluster_config, runtime, dns_resolver, factory_context, + std::move(scope), false); ReadyWatcher membership_updated; cluster.prioritySet().addMemberUpdateCb( [&](uint32_t, const HostVector&, const HostVector&) -> void { membership_updated.ready(); }); @@ -719,8 +767,11 @@ TEST(HostImplTest, HostnameCanaryAndLocality) { TEST(StaticClusterImplTest, InitialHosts) { Stats::IsolatedStoreImpl stats; Ssl::MockContextManager ssl_context_manager; + NiceMock dispatcher; NiceMock runtime; NiceMock local_info; + NiceMock random; + const std::string yaml = R"EOF( name: staticcluster connect_timeout: 0.25s @@ -733,8 +784,14 @@ TEST(StaticClusterImplTest, InitialHosts) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); cluster.initialize([] {}); EXPECT_EQ(1UL, cluster.prioritySet().hostSetsPerPriority()[0]->healthyHosts().size()); @@ -747,6 +804,8 @@ TEST(StaticClusterImplTest, EmptyHostname) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; const std::string json = R"EOF( { @@ -759,8 +818,14 @@ TEST(StaticClusterImplTest, EmptyHostname) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromJson(json), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); cluster.initialize([] {}); EXPECT_EQ(1UL, cluster.prioritySet().hostSetsPerPriority()[0]->healthyHosts().size()); @@ -771,9 +836,10 @@ TEST(StaticClusterImplTest, EmptyHostname) { TEST(StaticClusterImplTest, LoadAssignmentEmptyHostname) { Stats::IsolatedStoreImpl stats; Ssl::MockContextManager ssl_context_manager; + NiceMock dispatcher; NiceMock runtime; NiceMock local_info; - + NiceMock random; const std::string yaml = R"EOF( name: staticcluster connect_timeout: 0.25s @@ -792,8 +858,14 @@ TEST(StaticClusterImplTest, LoadAssignmentEmptyHostname) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); cluster.initialize([] {}); EXPECT_EQ(1UL, cluster.prioritySet().hostSetsPerPriority()[0]->healthyHosts().size()); @@ -806,6 +878,8 @@ TEST(StaticClusterImplTest, LoadAssignmentMultiplePriorities) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; const std::string yaml = R"EOF( name: staticcluster @@ -843,8 +917,14 @@ TEST(StaticClusterImplTest, LoadAssignmentMultiplePriorities) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); cluster.initialize([] {}); EXPECT_EQ(2UL, cluster.prioritySet().hostSetsPerPriority()[0]->healthyHosts().size()); @@ -858,6 +938,8 @@ TEST(StaticClusterImplTest, LoadAssignmentLocality) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; const std::string yaml = R"EOF( name: staticcluster @@ -888,8 +970,14 @@ TEST(StaticClusterImplTest, LoadAssignmentLocality) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); cluster.initialize([] {}); auto& hosts = cluster.prioritySet().hostSetsPerPriority()[0]->hosts(); @@ -909,6 +997,8 @@ TEST(StaticClusterImplTest, AltStatName) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock random; + NiceMock dispatcher; const std::string yaml = R"EOF( name: staticcluster @@ -920,8 +1010,14 @@ TEST(StaticClusterImplTest, AltStatName) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); cluster.initialize([] {}); // Increment a stat and verify it is emitted with alt_stat_name cluster.info()->stats().upstream_rq_total_.inc(); @@ -933,6 +1029,8 @@ TEST(StaticClusterImplTest, RingHash) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; const std::string json = R"EOF( { @@ -945,8 +1043,14 @@ TEST(StaticClusterImplTest, RingHash) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromJson(json), runtime, stats, ssl_context_manager, - local_info, cm, true); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), true); cluster.initialize([] {}); EXPECT_EQ(1UL, cluster.prioritySet().hostSetsPerPriority()[0]->healthyHosts().size()); @@ -959,6 +1063,8 @@ TEST(StaticClusterImplTest, OutlierDetector) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock random; + NiceMock dispatcher; const std::string json = R"EOF( { @@ -972,8 +1078,14 @@ TEST(StaticClusterImplTest, OutlierDetector) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromJson(json), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); Outlier::MockDetector* detector = new Outlier::MockDetector(); EXPECT_CALL(*detector, addChangedStateCb(_)); @@ -1008,6 +1120,8 @@ TEST(StaticClusterImplTest, HealthyStat) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; const std::string json = R"EOF( { @@ -1021,8 +1135,14 @@ TEST(StaticClusterImplTest, HealthyStat) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromJson(json), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); Outlier::MockDetector* outlier_detector = new NiceMock(); cluster.setOutlierDetector(Outlier::DetectorSharedPtr{outlier_detector}); @@ -1092,6 +1212,8 @@ TEST(StaticClusterImplTest, UrlConfig) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; const std::string json = R"EOF( { @@ -1105,8 +1227,14 @@ TEST(StaticClusterImplTest, UrlConfig) { )EOF"; NiceMock cm; - StaticClusterImpl cluster(parseClusterFromJson(json), runtime, stats, ssl_context_manager, - local_info, cm, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(cluster_config, runtime, factory_context, std::move(scope), false); cluster.initialize([] {}); EXPECT_EQ(1024U, cluster.info()->resourceManager(ResourcePriority::Default).connections().max()); @@ -1138,6 +1266,8 @@ TEST(StaticClusterImplTest, UnsupportedLBType) { NiceMock runtime; NiceMock cm; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; const std::string json = R"EOF( { @@ -1150,9 +1280,22 @@ TEST(StaticClusterImplTest, UnsupportedLBType) { } )EOF"; - EXPECT_THROW(StaticClusterImpl(parseClusterFromJson(json), runtime, stats, ssl_context_manager, - local_info, cm, false), - EnvoyException); + EXPECT_THROW_WITH_MESSAGE( + { + envoy::api::v2::Cluster cluster_config = parseClusterFromJson(json); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl(cluster_config, runtime, factory_context, std::move(scope), false); + }, + EnvoyException, + "JSON at lines 2-9 does not conform to schema.\n" + " Invalid schema: #/properties/lb_type\n" + " Schema violation: enum\n" + " Offending document key: #/lb_type"); } TEST(StaticClusterImplTest, MalformedHostIP) { @@ -1160,6 +1303,8 @@ TEST(StaticClusterImplTest, MalformedHostIP) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; const std::string yaml = R"EOF( name: name @@ -1170,11 +1315,18 @@ TEST(StaticClusterImplTest, MalformedHostIP) { )EOF"; NiceMock cm; - EXPECT_THROW_WITH_MESSAGE(StaticClusterImpl(parseClusterFromV2Yaml(yaml), runtime, stats, - ssl_context_manager, local_info, cm, false), - EnvoyException, - "malformed IP address: foo.bar.com. Consider setting resolver_name or " - "setting cluster type to 'STRICT_DNS' or 'LOGICAL_DNS'"); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + EXPECT_THROW_WITH_MESSAGE( + StaticClusterImpl(cluster_config, runtime, factory_context, std::move(scope), false), + EnvoyException, + "malformed IP address: foo.bar.com. Consider setting resolver_name or " + "setting cluster type to 'STRICT_DNS' or 'LOGICAL_DNS'"); } TEST(ClusterDefinitionTest, BadClusterConfig) { @@ -1214,6 +1366,8 @@ TEST(StaticClusterImplTest, SourceAddressPriority) { Ssl::MockContextManager ssl_context_manager; NiceMock runtime; NiceMock local_info; + NiceMock dispatcher; + NiceMock random; envoy::api::v2::Cluster config; config.set_name("staticcluster"); @@ -1223,7 +1377,12 @@ TEST(StaticClusterImplTest, SourceAddressPriority) { // If the cluster manager gets a source address from the bootstrap proto, use it. NiceMock cm; cm.bind_config_.mutable_source_address()->set_address("1.2.3.5"); - StaticClusterImpl cluster(config, runtime, stats, ssl_context_manager, local_info, cm, false); + Envoy::Stats::ScopePtr scope = stats.createScope(fmt::format( + "cluster.{}.", + config.alt_stat_name().empty() ? config.name() : std::string(config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(config, runtime, factory_context, std::move(scope), false); EXPECT_EQ("1.2.3.5:0", cluster.info()->sourceAddress()->asString()); } @@ -1232,7 +1391,12 @@ TEST(StaticClusterImplTest, SourceAddressPriority) { { // Verify source address from cluster config is used when present. NiceMock cm; - StaticClusterImpl cluster(config, runtime, stats, ssl_context_manager, local_info, cm, false); + Envoy::Stats::ScopePtr scope = stats.createScope(fmt::format( + "cluster.{}.", + config.alt_stat_name().empty() ? config.name() : std::string(config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(config, runtime, factory_context, std::move(scope), false); EXPECT_EQ(cluster_address, cluster.info()->sourceAddress()->ip()->addressAsString()); } @@ -1240,7 +1404,12 @@ TEST(StaticClusterImplTest, SourceAddressPriority) { // The source address from cluster config takes precedence over one from the bootstrap proto. NiceMock cm; cm.bind_config_.mutable_source_address()->set_address("1.2.3.5"); - StaticClusterImpl cluster(config, runtime, stats, ssl_context_manager, local_info, cm, false); + Envoy::Stats::ScopePtr scope = stats.createScope(fmt::format( + "cluster.{}.", + config.alt_stat_name().empty() ? config.name() : std::string(config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StaticClusterImpl cluster(config, runtime, factory_context, std::move(scope), false); EXPECT_EQ(cluster_address, cluster.info()->sourceAddress()->ip()->addressAsString()); } } @@ -1255,6 +1424,7 @@ TEST(ClusterImplTest, CloseConnectionsOnHostHealthFailure) { NiceMock runtime; NiceMock cm; NiceMock local_info; + NiceMock random; ReadyWatcher initialized; const std::string yaml = R"EOF( @@ -1265,8 +1435,16 @@ TEST(ClusterImplTest, CloseConnectionsOnHostHealthFailure) { close_connections_on_host_health_failure: true hosts: [{ socket_address: { address: foo.bar.com, port_value: 443 }}] )EOF"; - StrictDnsClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, dns_resolver, cm, dispatcher, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + + StrictDnsClusterImpl cluster(cluster_config, runtime, dns_resolver, factory_context, + std::move(scope), false); EXPECT_TRUE(cluster.info()->features() & ClusterInfo::Features::CLOSE_CONNECTIONS_ON_HOST_HEALTH_FAILURE); } @@ -1329,6 +1507,7 @@ TEST(ClusterMetadataTest, Metadata) { NiceMock runtime; NiceMock cm; NiceMock local_info; + NiceMock random; ReadyWatcher initialized; const std::string yaml = R"EOF( @@ -1343,8 +1522,15 @@ TEST(ClusterMetadataTest, Metadata) { value: 0.3 )EOF"; - StrictDnsClusterImpl cluster(parseClusterFromV2Yaml(yaml), runtime, stats, ssl_context_manager, - local_info, dns_resolver, cm, dispatcher, false); + envoy::api::v2::Cluster cluster_config = parseClusterFromV2Yaml(yaml); + Envoy::Stats::ScopePtr scope = stats.createScope( + fmt::format("cluster.{}.", cluster_config.alt_stat_name().empty() + ? cluster_config.name() + : std::string(cluster_config.alt_stat_name()))); + Envoy::Server::Configuration::TransportSocketFactoryContextImpl factory_context( + ssl_context_manager, *scope, cm, local_info, dispatcher, random, stats); + StrictDnsClusterImpl cluster(cluster_config, runtime, dns_resolver, factory_context, + std::move(scope), false); EXPECT_EQ("test_value", Config::Metadata::metadataValue(cluster.info()->metadata(), "com.bar.foo", "baz") .string_value()); diff --git a/test/mocks/server/mocks.h b/test/mocks/server/mocks.h index 17029e659303f..f214f5d8efa6a 100644 --- a/test/mocks/server/mocks.h +++ b/test/mocks/server/mocks.h @@ -411,6 +411,12 @@ class MockTransportSocketFactoryContext : public TransportSocketFactoryContext { MOCK_METHOD0(sslContextManager, Ssl::ContextManager&()); MOCK_CONST_METHOD0(statsScope, Stats::Scope&()); + MOCK_METHOD0(clusterManager, Upstream::ClusterManager&()); + MOCK_METHOD0(secretManager, Secret::SecretManager&()); + MOCK_METHOD0(local_info, const LocalInfo::LocalInfo&()); + MOCK_METHOD0(dispatcher, Event::Dispatcher&()); + MOCK_METHOD0(random, Envoy::Runtime::RandomGenerator&()); + MOCK_METHOD0(stats, Stats::Store&()); }; class MockListenerFactoryContext : public virtual MockFactoryContext,