diff --git a/envoy/grpc/google_grpc_creds.h b/envoy/grpc/google_grpc_creds.h index 32dd71efdd75..1cc5188fb09f 100644 --- a/envoy/grpc/google_grpc_creds.h +++ b/envoy/grpc/google_grpc_creds.h @@ -2,10 +2,10 @@ #include -#include "envoy/api/api.h" #include "envoy/common/pure.h" #include "envoy/config/core/v3/grpc_service.pb.h" #include "envoy/config/typed_config.h" +#include "envoy/server/factory_context.h" #include "grpcpp/grpcpp.h" @@ -33,7 +33,7 @@ class GoogleGrpcCredentialsFactory : public Config::UntypedFactory { */ virtual std::shared_ptr getChannelCredentials(const envoy::config::core::v3::GrpcService& grpc_service_config, - Api::Api& api) PURE; + Server::Configuration::CommonFactoryContext& context) PURE; std::string category() const override { return "envoy.grpc_credentials"; } }; diff --git a/source/common/grpc/async_client_manager_impl.cc b/source/common/grpc/async_client_manager_impl.cc index f2875e30466e..9d3bf65d132c 100644 --- a/source/common/grpc/async_client_manager_impl.cc +++ b/source/common/grpc/async_client_manager_impl.cc @@ -56,11 +56,11 @@ AsyncClientFactoryImpl::AsyncClientFactoryImpl(Upstream::ClusterManager& cm, } AsyncClientManagerImpl::AsyncClientManagerImpl( - Upstream::ClusterManager& cm, ThreadLocal::Instance& tls, TimeSource& time_source, - Api::Api& api, const StatNames& stat_names, + Upstream::ClusterManager& cm, ThreadLocal::Instance& tls, + Server::Configuration::CommonFactoryContext& context, const StatNames& stat_names, const envoy::config::bootstrap::v3::Bootstrap::GrpcAsyncClientManagerConfig& config) - : cm_(cm), tls_(tls), time_source_(time_source), api_(api), stat_names_(stat_names), - raw_async_client_cache_(tls_) { + : tls_(tls), cm_(cm), context_(context), stat_names_(stat_names), + raw_async_client_cache_(context.threadLocal()) { const auto max_cached_entry_idle_duration = std::chrono::milliseconds( PROTOBUF_GET_MS_OR_DEFAULT(config, max_cached_entry_idle_duration, DefaultEntryIdleDuration)); @@ -69,11 +69,10 @@ AsyncClientManagerImpl::AsyncClientManagerImpl( return std::make_shared(dispatcher, max_cached_entry_idle_duration); }); #ifdef ENVOY_GOOGLE_GRPC - google_tls_slot_ = tls.allocateSlot(); + google_tls_slot_ = context_.threadLocal().allocateSlot(); + Api::Api& api = context_.api(); google_tls_slot_->set( [&api](Event::Dispatcher&) { return std::make_shared(api); }); -#else - UNREFERENCED_PARAMETER(api_); #endif } @@ -83,17 +82,18 @@ RawAsyncClientPtr AsyncClientFactoryImpl::createUncachedRawAsyncClient() { GoogleAsyncClientFactoryImpl::GoogleAsyncClientFactoryImpl( ThreadLocal::Instance& tls, ThreadLocal::Slot* google_tls_slot, Stats::Scope& scope, - const envoy::config::core::v3::GrpcService& config, Api::Api& api, const StatNames& stat_names, + const envoy::config::core::v3::GrpcService& config, + Server::Configuration::CommonFactoryContext& context, const StatNames& stat_names, absl::Status& creation_status) : tls_(tls), google_tls_slot_(google_tls_slot), scope_(scope.createScope(fmt::format("grpc.{}.", config.google_grpc().stat_prefix()))), - config_(config), api_(api), stat_names_(stat_names) { + config_(config), factory_context_(context), stat_names_(stat_names) { #ifndef ENVOY_GOOGLE_GRPC UNREFERENCED_PARAMETER(tls_); UNREFERENCED_PARAMETER(google_tls_slot_); UNREFERENCED_PARAMETER(scope_); UNREFERENCED_PARAMETER(config_); - UNREFERENCED_PARAMETER(api_); + UNREFERENCED_PARAMETER(factory_context_); UNREFERENCED_PARAMETER(stat_names_); creation_status = absl::InvalidArgumentError("Google C++ gRPC client is not linked"); return; @@ -128,7 +128,7 @@ RawAsyncClientPtr GoogleAsyncClientFactoryImpl::createUncachedRawAsyncClient() { GoogleGenericStubFactory stub_factory; return std::make_unique( tls_.dispatcher(), google_tls_slot_->getTyped(), stub_factory, - scope_, config_, api_, stat_names_); + scope_, config_, factory_context_, stat_names_); #else return nullptr; #endif @@ -142,11 +142,11 @@ AsyncClientManagerImpl::factoryForGrpcService(const envoy::config::core::v3::Grp switch (config.target_specifier_case()) { case envoy::config::core::v3::GrpcService::TargetSpecifierCase::kEnvoyGrpc: factory = std::make_unique(cm_, config, skip_cluster_check, - time_source_, creation_status); + context_.timeSource(), creation_status); break; case envoy::config::core::v3::GrpcService::TargetSpecifierCase::kGoogleGrpc: factory = std::make_unique( - tls_, google_tls_slot_.get(), scope, config, api_, stat_names_, creation_status); + tls_, google_tls_slot_.get(), scope, config, context_, stat_names_, creation_status); break; case envoy::config::core::v3::GrpcService::TargetSpecifierCase::TARGET_SPECIFIER_NOT_SET: PANIC_DUE_TO_PROTO_UNSET; diff --git a/source/common/grpc/async_client_manager_impl.h b/source/common/grpc/async_client_manager_impl.h index 54c3cf6b2b00..c1591dd19395 100644 --- a/source/common/grpc/async_client_manager_impl.h +++ b/source/common/grpc/async_client_manager_impl.h @@ -33,7 +33,8 @@ class GoogleAsyncClientFactoryImpl : public AsyncClientFactory { public: GoogleAsyncClientFactoryImpl(ThreadLocal::Instance& tls, ThreadLocal::Slot* google_tls_slot, Stats::Scope& scope, - const envoy::config::core::v3::GrpcService& config, Api::Api& api, + const envoy::config::core::v3::GrpcService& config, + Server::Configuration::CommonFactoryContext& context, const StatNames& stat_names, absl::Status& creation_status); RawAsyncClientPtr createUncachedRawAsyncClient() override; @@ -42,15 +43,15 @@ class GoogleAsyncClientFactoryImpl : public AsyncClientFactory { ThreadLocal::Slot* google_tls_slot_; Stats::ScopeSharedPtr scope_; const envoy::config::core::v3::GrpcService config_; - Api::Api& api_; + Server::Configuration::CommonFactoryContext& factory_context_; const StatNames& stat_names_; }; class AsyncClientManagerImpl : public AsyncClientManager { public: AsyncClientManagerImpl( - Upstream::ClusterManager& cm, ThreadLocal::Instance& tls, TimeSource& time_source, - Api::Api& api, const StatNames& stat_names, + Upstream::ClusterManager& cm, ThreadLocal::Instance& tls, + Server::Configuration::CommonFactoryContext& context, const StatNames& stat_names, const envoy::config::bootstrap::v3::Bootstrap::GrpcAsyncClientManagerConfig& config); absl::StatusOr getOrCreateRawAsyncClient(const envoy::config::core::v3::GrpcService& config, Stats::Scope& scope, @@ -92,11 +93,10 @@ class AsyncClientManagerImpl : public AsyncClientManager { }; private: - Upstream::ClusterManager& cm_; ThreadLocal::Instance& tls_; + Upstream::ClusterManager& cm_; // Need to track outside of `context_` due to startup ordering. + Server::Configuration::CommonFactoryContext& context_; ThreadLocal::SlotPtr google_tls_slot_; - TimeSource& time_source_; - Api::Api& api_; const StatNames& stat_names_; ThreadLocal::TypedSlot raw_async_client_cache_; }; diff --git a/source/common/grpc/google_async_client_impl.cc b/source/common/grpc/google_async_client_impl.cc index eea658811b45..8c0e9a6d7e36 100644 --- a/source/common/grpc/google_async_client_impl.cc +++ b/source/common/grpc/google_async_client_impl.cc @@ -82,7 +82,8 @@ GoogleAsyncClientImpl::GoogleAsyncClientImpl(Event::Dispatcher& dispatcher, GoogleStubFactory& stub_factory, Stats::ScopeSharedPtr scope, const envoy::config::core::v3::GrpcService& config, - Api::Api& api, const StatNames& stat_names) + Server::Configuration::CommonFactoryContext& context, + const StatNames& stat_names) : dispatcher_(dispatcher), tls_(tls), stat_prefix_(config.google_grpc().stat_prefix()), target_uri_(config.google_grpc().target_uri()), scope_(scope), per_stream_buffer_limit_bytes_(PROTOBUF_GET_WRAPPED_OR_DEFAULT( @@ -94,7 +95,7 @@ GoogleAsyncClientImpl::GoogleAsyncClientImpl(Event::Dispatcher& dispatcher, // smart enough to do connection pooling and reuse with identical channel args, so this should // have comparable overhead to what we are doing in Grpc::AsyncClientImpl, i.e. no expensive // new connection implied. - std::shared_ptr channel = GoogleGrpcUtils::createChannel(config, api); + std::shared_ptr channel = GoogleGrpcUtils::createChannel(config, context); // Get state with try_to_connect = true to try connection at channel creation. // This is for initializing gRPC channel at channel creation. This GetState(true) is used to poke // the gRPC lb at channel creation, it doesn't have any effect no matter it succeeds or fails. But diff --git a/source/common/grpc/google_async_client_impl.h b/source/common/grpc/google_async_client_impl.h index 9fb4ad022fd8..45eceae18434 100644 --- a/source/common/grpc/google_async_client_impl.h +++ b/source/common/grpc/google_async_client_impl.h @@ -175,7 +175,8 @@ class GoogleAsyncClientImpl final : public RawAsyncClient, Logger::Loggable getChannelCredentials(const envoy::config::core::v3::GrpcService& grpc_service_config, - Api::Api& api) override { - return CredsUtility::defaultChannelCredentials(grpc_service_config, api); + Server::Configuration::CommonFactoryContext& context) override { + return CredsUtility::defaultChannelCredentials(grpc_service_config, context.api()); } std::string name() const override { return "envoy.grpc_credentials.default"; } diff --git a/source/common/grpc/google_grpc_utils.cc b/source/common/grpc/google_grpc_utils.cc index fbe891306692..9ad8a0f5999c 100644 --- a/source/common/grpc/google_grpc_utils.cc +++ b/source/common/grpc/google_grpc_utils.cc @@ -26,7 +26,7 @@ namespace { std::shared_ptr getGoogleGrpcChannelCredentials(const envoy::config::core::v3::GrpcService& grpc_service, - Api::Api& api) { + Server::Configuration::CommonFactoryContext& context) { GoogleGrpcCredentialsFactory* credentials_factory = nullptr; const std::string& google_grpc_credentials_factory_name = grpc_service.google_grpc().credentials_factory_name(); @@ -41,7 +41,7 @@ getGoogleGrpcChannelCredentials(const envoy::config::core::v3::GrpcService& grpc throw EnvoyException(absl::StrCat("Unknown google grpc credentials factory: ", google_grpc_credentials_factory_name)); } - return credentials_factory->getChannelCredentials(grpc_service, api); + return credentials_factory->getChannelCredentials(grpc_service, context); } } // namespace @@ -133,8 +133,10 @@ GoogleGrpcUtils::channelArgsFromConfig(const envoy::config::core::v3::GrpcServic } std::shared_ptr -GoogleGrpcUtils::createChannel(const envoy::config::core::v3::GrpcService& config, Api::Api& api) { - std::shared_ptr creds = getGoogleGrpcChannelCredentials(config, api); +GoogleGrpcUtils::createChannel(const envoy::config::core::v3::GrpcService& config, + Server::Configuration::CommonFactoryContext& context) { + std::shared_ptr creds = + getGoogleGrpcChannelCredentials(config, context); const grpc::ChannelArguments args = channelArgsFromConfig(config); return CreateCustomChannel(config.google_grpc().target_uri(), creds, args); } diff --git a/source/common/grpc/google_grpc_utils.h b/source/common/grpc/google_grpc_utils.h index 859a61ccfff9..102bc0c3f25d 100644 --- a/source/common/grpc/google_grpc_utils.h +++ b/source/common/grpc/google_grpc_utils.h @@ -3,10 +3,10 @@ #include #include -#include "envoy/api/api.h" #include "envoy/buffer/buffer.h" #include "envoy/common/platform.h" #include "envoy/config/core/v3/grpc_service.pb.h" +#include "envoy/server/factory_context.h" #include "grpcpp/grpcpp.h" @@ -46,7 +46,8 @@ class GoogleGrpcUtils { * @return static std::shared_ptr a gRPC channel. */ static std::shared_ptr - createChannel(const envoy::config::core::v3::GrpcService& config, Api::Api& api); + createChannel(const envoy::config::core::v3::GrpcService& config, + Server::Configuration::CommonFactoryContext& context); }; } // namespace Grpc diff --git a/source/common/upstream/cluster_manager_impl.cc b/source/common/upstream/cluster_manager_impl.cc index 4a2a4f076afa..41e490ae0271 100644 --- a/source/common/upstream/cluster_manager_impl.cc +++ b/source/common/upstream/cluster_manager_impl.cc @@ -295,12 +295,12 @@ void ClusterManagerInitHelper::setPrimaryClustersInitializedCb( ClusterManagerImpl::ClusterManagerImpl( const envoy::config::bootstrap::v3::Bootstrap& bootstrap, ClusterManagerFactory& factory, - Stats::Store& stats, ThreadLocal::Instance& tls, Runtime::Loader& runtime, - const LocalInfo::LocalInfo& local_info, AccessLog::AccessLogManager& log_manager, - Event::Dispatcher& main_thread_dispatcher, OptRef admin, - ProtobufMessage::ValidationContext& validation_context, Api::Api& api, - Http::Context& http_context, Grpc::Context& grpc_context, Router::Context& router_context, - const Server::Instance& server) + Server::Configuration::CommonFactoryContext& context, Stats::Store& stats, + ThreadLocal::Instance& tls, Runtime::Loader& runtime, const LocalInfo::LocalInfo& local_info, + AccessLog::AccessLogManager& log_manager, Event::Dispatcher& main_thread_dispatcher, + OptRef admin, ProtobufMessage::ValidationContext& validation_context, + Api::Api& api, Http::Context& http_context, Grpc::Context& grpc_context, + Router::Context& router_context, const Server::Instance& server) : server_(server), factory_(factory), runtime_(runtime), stats_(stats), tls_(tls), random_(api.randomGenerator()), deferred_cluster_creation_(bootstrap.cluster_manager().enable_deferred_cluster_creation()), @@ -331,8 +331,7 @@ ClusterManagerImpl::ClusterManagerImpl( }); } async_client_manager_ = std::make_unique( - *this, tls, time_source_, api, grpc_context.statNames(), - bootstrap.grpc_async_client_manager_config()); + *this, tls, context, grpc_context.statNames(), bootstrap.grpc_async_client_manager_config()); const auto& cm_config = bootstrap.cluster_manager(); if (cm_config.has_outlier_detection()) { const std::string event_log_file_path = cm_config.outlier_detection().event_log_path(); @@ -2222,7 +2221,7 @@ void ClusterManagerImpl::ThreadLocalClusterManagerImpl::tcpConnPoolIsIdle( ClusterManagerPtr ProdClusterManagerFactory::clusterManagerFromProto( const envoy::config::bootstrap::v3::Bootstrap& bootstrap) { auto cluster_manager_impl = std::unique_ptr{new ClusterManagerImpl( - bootstrap, *this, stats_, tls_, context_.runtime(), context_.localInfo(), + bootstrap, *this, context_, stats_, tls_, context_.runtime(), context_.localInfo(), context_.accessLogManager(), context_.mainThreadDispatcher(), context_.admin(), context_.messageValidationContext(), context_.api(), http_context_, context_.grpcContext(), context_.routerContext(), server_)}; diff --git a/source/common/upstream/cluster_manager_impl.h b/source/common/upstream/cluster_manager_impl.h index 0c0a0c857c87..9a093d7561dc 100644 --- a/source/common/upstream/cluster_manager_impl.h +++ b/source/common/upstream/cluster_manager_impl.h @@ -386,7 +386,8 @@ class ClusterManagerImpl : public ClusterManager, // ClusterManagerImpl's constructor should not be invoked directly; create instances from the // clusterManagerFromProto() static method. The init() method must be called after construction. ClusterManagerImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap, - ClusterManagerFactory& factory, Stats::Store& stats, + ClusterManagerFactory& factory, + Server::Configuration::CommonFactoryContext& context, Stats::Store& stats, ThreadLocal::Instance& tls, Runtime::Loader& runtime, const LocalInfo::LocalInfo& local_info, AccessLog::AccessLogManager& log_manager, diff --git a/source/extensions/common/aws/signer_base_impl.h b/source/extensions/common/aws/signer_base_impl.h index 7dff4a6aed2b..a8bb754facf2 100644 --- a/source/extensions/common/aws/signer_base_impl.h +++ b/source/extensions/common/aws/signer_base_impl.h @@ -63,18 +63,22 @@ using AwsSigningHeaderExclusionVector = std::vector { public: SignerBaseImpl(absl::string_view service_name, absl::string_view region, - const CredentialsProviderSharedPtr& credentials_provider, TimeSource& time_source, + const CredentialsProviderSharedPtr& credentials_provider, + Server::Configuration::CommonFactoryContext& context, const AwsSigningHeaderExclusionVector& matcher_config, const bool query_string = false, const uint16_t expiration_time = SignatureQueryParameterValues::DefaultExpiration) - : service_name_(service_name), region_(region), credentials_provider_(credentials_provider), - query_string_(query_string), expiration_time_(expiration_time), time_source_(time_source), + : service_name_(service_name), region_(region), + excluded_header_matchers_(defaultMatchers(context)), + credentials_provider_(credentials_provider), query_string_(query_string), + expiration_time_(expiration_time), time_source_(context.timeSource()), long_date_formatter_(std::string(SignatureConstants::LongDateFormat)), short_date_formatter_(std::string(SignatureConstants::ShortDateFormat)) { for (const auto& matcher : matcher_config) { excluded_header_matchers_.emplace_back( - std::make_unique>( - matcher)); + std::make_unique< + Matchers::StringMatcherImplWithContext>( + matcher, context)); } } @@ -128,14 +132,16 @@ class SignerBaseImpl : public Signer, public Logger::Loggable { const std::map& signed_headers, const uint16_t expiration_time) const; - std::vector defaultMatchers() const { + std::vector + defaultMatchers(Server::Configuration::CommonFactoryContext& context) const { std::vector matcher_ptrs{}; for (const auto& header : default_excluded_headers_) { envoy::type::matcher::v3::StringMatcher m; m.set_exact(header); matcher_ptrs.emplace_back( - std::make_unique>( - m)); + std::make_unique< + Matchers::StringMatcherImplWithContext>( + m, context)); } return matcher_ptrs; } @@ -145,7 +151,7 @@ class SignerBaseImpl : public Signer, public Logger::Loggable { const std::vector default_excluded_headers_ = { Http::Headers::get().ForwardedFor.get(), Http::Headers::get().ForwardedProto.get(), "x-amzn-trace-id"}; - std::vector excluded_header_matchers_ = defaultMatchers(); + std::vector excluded_header_matchers_; CredentialsProviderSharedPtr credentials_provider_; const bool query_string_; const uint16_t expiration_time_; diff --git a/source/extensions/common/aws/sigv4_signer_impl.h b/source/extensions/common/aws/sigv4_signer_impl.h index f9e047ec7754..68b78dd8037f 100644 --- a/source/extensions/common/aws/sigv4_signer_impl.h +++ b/source/extensions/common/aws/sigv4_signer_impl.h @@ -41,11 +41,12 @@ using AwsSigningHeaderExclusionVector = std::vector AwsLambdaFilterFactory::createFilterFactor Extensions::Common::Aws::Utility::fetchMetadata); auto signer = std::make_shared( - service_name, region, std::move(credentials_provider), - server_context.mainThreadDispatcher().timeSource(), + service_name, region, std::move(credentials_provider), server_context, // TODO: extend API to allow specifying header exclusion. ref: // https://github.com/envoyproxy/envoy/pull/18998 Extensions::Common::Aws::AwsSigningHeaderExclusionVector{}); diff --git a/source/extensions/filters/http/aws_request_signing/config.cc b/source/extensions/filters/http/aws_request_signing/config.cc index 4727c0133f10..c0de8b785e58 100644 --- a/source/extensions/filters/http/aws_request_signing/config.cc +++ b/source/extensions/filters/http/aws_request_signing/config.cc @@ -71,9 +71,8 @@ AwsRequestSigningFilterFactory::createFilterFactoryFromProtoTyped( if (config.signing_algorithm() == AwsRequestSigning_SigningAlgorithm_AWS_SIGV4A) { signer = std::make_unique( - config.service_name(), region, credentials_provider, - server_context.mainThreadDispatcher().timeSource(), matcher_config, query_string, - expiration_time); + config.service_name(), region, credentials_provider, server_context, matcher_config, + query_string, expiration_time); } else { // Verify that we have not specified a region set when using sigv4 algorithm if (isARegionSet(region)) { @@ -81,9 +80,8 @@ AwsRequestSigningFilterFactory::createFilterFactoryFromProtoTyped( "can be specified when using signing_algorithm: AWS_SIGV4A."); } signer = std::make_unique( - config.service_name(), region, credentials_provider, - server_context.mainThreadDispatcher().timeSource(), matcher_config, query_string, - expiration_time); + config.service_name(), region, credentials_provider, server_context, matcher_config, + query_string, expiration_time); } auto filter_config = @@ -137,7 +135,7 @@ AwsRequestSigningFilterFactory::createRouteSpecificFilterConfigTyped( AwsRequestSigning_SigningAlgorithm_AWS_SIGV4A) { signer = std::make_unique( per_route_config.aws_request_signing().service_name(), region, credentials_provider, - context.mainThreadDispatcher().timeSource(), matcher_config, query_string, expiration_time); + context, matcher_config, query_string, expiration_time); } else { // Verify that we have not specified a region set when using sigv4 algorithm if (isARegionSet(region)) { @@ -146,7 +144,7 @@ AwsRequestSigningFilterFactory::createRouteSpecificFilterConfigTyped( } signer = std::make_unique( per_route_config.aws_request_signing().service_name(), region, credentials_provider, - context.mainThreadDispatcher().timeSource(), matcher_config, query_string, expiration_time); + context, matcher_config, query_string, expiration_time); } return std::make_shared( diff --git a/source/extensions/grpc_credentials/aws_iam/config.cc b/source/extensions/grpc_credentials/aws_iam/config.cc index 4e8127a19749..fc9c11a68dfb 100644 --- a/source/extensions/grpc_credentials/aws_iam/config.cc +++ b/source/extensions/grpc_credentials/aws_iam/config.cc @@ -22,11 +22,12 @@ namespace GrpcCredentials { namespace AwsIam { std::shared_ptr AwsIamGrpcCredentialsFactory::getChannelCredentials( - const envoy::config::core::v3::GrpcService& grpc_service_config, Api::Api& api) { + const envoy::config::core::v3::GrpcService& grpc_service_config, + Server::Configuration::CommonFactoryContext& context) { const auto& google_grpc = grpc_service_config.google_grpc(); std::shared_ptr creds = - Grpc::CredsUtility::defaultSslChannelCredentials(grpc_service_config, api); + Grpc::CredsUtility::defaultSslChannelCredentials(grpc_service_config, context.api()); std::shared_ptr call_creds; for (const auto& credential : google_grpc.call_credentials()) { @@ -66,10 +67,10 @@ std::shared_ptr AwsIamGrpcCredentialsFactory::getChann // usage of AWS credentials common utils. Until then we are setting nullopt for server // factory context. auto credentials_provider = std::make_shared( - api, absl::nullopt /*Empty factory context*/, region, + context.api(), absl::nullopt /*Empty factory context*/, region, Common::Aws::Utility::fetchMetadata); auto signer = std::make_unique( - config.service_name(), region, credentials_provider, api.timeSource(), + config.service_name(), region, credentials_provider, context, // TODO: extend API to allow specifying header exclusion. ref: // https://github.com/envoyproxy/envoy/pull/18998 Common::Aws::AwsSigningHeaderExclusionVector{}); diff --git a/source/extensions/grpc_credentials/aws_iam/config.h b/source/extensions/grpc_credentials/aws_iam/config.h index 5997f61903de..16efbcead7c6 100644 --- a/source/extensions/grpc_credentials/aws_iam/config.h +++ b/source/extensions/grpc_credentials/aws_iam/config.h @@ -20,7 +20,7 @@ class AwsIamGrpcCredentialsFactory : public Grpc::GoogleGrpcCredentialsFactory { public: std::shared_ptr getChannelCredentials(const envoy::config::core::v3::GrpcService& grpc_service_config, - Api::Api& api) override; + Server::Configuration::CommonFactoryContext& context) override; Envoy::ProtobufTypes::MessagePtr createEmptyConfigProto() { return std::make_unique(); diff --git a/source/extensions/grpc_credentials/example/config.cc b/source/extensions/grpc_credentials/example/config.cc index 2645fb07deea..18cebbe34ef0 100644 --- a/source/extensions/grpc_credentials/example/config.cc +++ b/source/extensions/grpc_credentials/example/config.cc @@ -13,10 +13,11 @@ namespace Example { std::shared_ptr AccessTokenExampleGrpcCredentialsFactory::getChannelCredentials( - const envoy::config::core::v3::GrpcService& grpc_service_config, Api::Api& api) { + const envoy::config::core::v3::GrpcService& grpc_service_config, + Server::Configuration::CommonFactoryContext& context) { const auto& google_grpc = grpc_service_config.google_grpc(); std::shared_ptr creds = - Grpc::CredsUtility::defaultSslChannelCredentials(grpc_service_config, api); + Grpc::CredsUtility::defaultSslChannelCredentials(grpc_service_config, context.api()); std::shared_ptr call_creds = nullptr; for (const auto& credential : google_grpc.call_credentials()) { switch (credential.credential_specifier_case()) { diff --git a/source/extensions/grpc_credentials/example/config.h b/source/extensions/grpc_credentials/example/config.h index 33cd85e6f425..28c860adcfaf 100644 --- a/source/extensions/grpc_credentials/example/config.h +++ b/source/extensions/grpc_credentials/example/config.h @@ -28,7 +28,7 @@ class AccessTokenExampleGrpcCredentialsFactory : public Grpc::GoogleGrpcCredenti public: std::shared_ptr getChannelCredentials(const envoy::config::core::v3::GrpcService& grpc_service_config, - Api::Api& api) override; + Server::Configuration::CommonFactoryContext& context) override; std::string name() const override { return "envoy.grpc_credentials.access_token_example"; } }; diff --git a/source/extensions/grpc_credentials/file_based_metadata/config.cc b/source/extensions/grpc_credentials/file_based_metadata/config.cc index 266adbf96443..ba02ec399fd1 100644 --- a/source/extensions/grpc_credentials/file_based_metadata/config.cc +++ b/source/extensions/grpc_credentials/file_based_metadata/config.cc @@ -19,10 +19,11 @@ namespace FileBasedMetadata { std::shared_ptr FileBasedMetadataGrpcCredentialsFactory::getChannelCredentials( - const envoy::config::core::v3::GrpcService& grpc_service_config, Api::Api& api) { + const envoy::config::core::v3::GrpcService& grpc_service_config, + Server::Configuration::CommonFactoryContext& context) { const auto& google_grpc = grpc_service_config.google_grpc(); std::shared_ptr creds = - Grpc::CredsUtility::defaultSslChannelCredentials(grpc_service_config, api); + Grpc::CredsUtility::defaultSslChannelCredentials(grpc_service_config, context.api()); std::shared_ptr call_creds = nullptr; for (const auto& credential : google_grpc.call_credentials()) { switch (credential.credential_specifier_case()) { @@ -39,8 +40,9 @@ FileBasedMetadataGrpcCredentialsFactory::getChannelCredentials( const auto& file_based_metadata_config = Envoy::MessageUtil::downcastAndValidate< const envoy::config::grpc_credential::v3::FileBasedMetadataConfig&>( *file_based_metadata_config_message, ProtobufMessage::getNullValidationVisitor()); - std::shared_ptr new_call_creds = grpc::MetadataCredentialsFromPlugin( - std::make_unique(file_based_metadata_config, api)); + std::shared_ptr new_call_creds = + grpc::MetadataCredentialsFromPlugin(std::make_unique( + file_based_metadata_config, context.api())); if (call_creds == nullptr) { call_creds = new_call_creds; } else { diff --git a/source/extensions/grpc_credentials/file_based_metadata/config.h b/source/extensions/grpc_credentials/file_based_metadata/config.h index 2ff1c54a161f..778ad34db9a1 100644 --- a/source/extensions/grpc_credentials/file_based_metadata/config.h +++ b/source/extensions/grpc_credentials/file_based_metadata/config.h @@ -24,7 +24,7 @@ class FileBasedMetadataGrpcCredentialsFactory : public Grpc::GoogleGrpcCredentia public: std::shared_ptr getChannelCredentials(const envoy::config::core::v3::GrpcService& grpc_service_config, - Api::Api& api) override; + Server::Configuration::CommonFactoryContext& context) override; Envoy::ProtobufTypes::MessagePtr createEmptyConfigProto() { return std::make_unique(); diff --git a/source/extensions/tracers/opencensus/config.cc b/source/extensions/tracers/opencensus/config.cc index bf33e76de248..d8f5283068a2 100644 --- a/source/extensions/tracers/opencensus/config.cc +++ b/source/extensions/tracers/opencensus/config.cc @@ -26,8 +26,7 @@ Tracing::DriverSharedPtr OpenCensusTracerFactory::createTracerDriverTyped( } } - driver_ = std::make_shared(proto_config, context.serverFactoryContext().localInfo(), - context.serverFactoryContext().api()); + driver_ = std::make_shared(proto_config, context.serverFactoryContext()); config_ = proto_config; return driver_; } diff --git a/source/extensions/tracers/opencensus/opencensus_tracer_impl.cc b/source/extensions/tracers/opencensus/opencensus_tracer_impl.cc index 5c4ea0b4dfba..8bde7883a704 100644 --- a/source/extensions/tracers/opencensus/opencensus_tracer_impl.cc +++ b/source/extensions/tracers/opencensus/opencensus_tracer_impl.cc @@ -253,8 +253,8 @@ void Span::setSampled(bool sampled) { span_.AddAnnotation("setSampled", {{"sampl } // namespace Driver::Driver(const envoy::config::trace::v3::OpenCensusConfig& oc_config, - const LocalInfo::LocalInfo& localinfo, Api::Api& api) - : oc_config_(oc_config), local_info_(localinfo) { + Server::Configuration::CommonFactoryContext& context) + : oc_config_(oc_config), local_info_(context.localInfo()) { // To give user a chance to correct initially invalid configuration and try to apply it once again // without a need to restart Envoy, validation checks must be done prior to any side effects. if (oc_config.stackdriver_exporter_enabled() && oc_config.has_stackdriver_grpc_service() && @@ -289,7 +289,7 @@ Driver::Driver(const envoy::config::trace::v3::OpenCensusConfig& oc_config, // address will be used. stackdriver_service.mutable_google_grpc()->set_target_uri(GoogleStackdriverTraceAddress); } - auto channel = Envoy::Grpc::GoogleGrpcUtils::createChannel(stackdriver_service, api); + auto channel = Envoy::Grpc::GoogleGrpcUtils::createChannel(stackdriver_service, context); // TODO(bianpengyuan): add tests for trace_service_stub and initial_metadata options with mock // stubs. opts.trace_service_stub = ::google::devtools::cloudtrace::v2::TraceService::NewStub(channel); @@ -322,7 +322,7 @@ Driver::Driver(const envoy::config::trace::v3::OpenCensusConfig& oc_config, #ifdef ENVOY_GOOGLE_GRPC const envoy::config::core::v3::GrpcService& ocagent_service = oc_config.ocagent_grpc_service(); - auto channel = Envoy::Grpc::GoogleGrpcUtils::createChannel(ocagent_service, api); + auto channel = Envoy::Grpc::GoogleGrpcUtils::createChannel(ocagent_service, context); opts.trace_service_stub = ::opencensus::proto::agent::trace::v1::TraceService::NewStub(channel); #else diff --git a/source/extensions/tracers/opencensus/opencensus_tracer_impl.h b/source/extensions/tracers/opencensus/opencensus_tracer_impl.h index db3da7856a86..3ca29cb71d30 100644 --- a/source/extensions/tracers/opencensus/opencensus_tracer_impl.h +++ b/source/extensions/tracers/opencensus/opencensus_tracer_impl.h @@ -1,8 +1,7 @@ #pragma once -#include "envoy/api/api.h" #include "envoy/config/trace/v3/opencensus.pb.h" -#include "envoy/local_info/local_info.h" +#include "envoy/server/factory_context.h" #include "envoy/tracing/trace_driver.h" #include "source/common/common/logger.h" @@ -18,7 +17,7 @@ namespace OpenCensus { class Driver : public Tracing::Driver, Logger::Loggable { public: Driver(const envoy::config::trace::v3::OpenCensusConfig& oc_config, - const LocalInfo::LocalInfo& localinfo, Api::Api& api); + Server::Configuration::CommonFactoryContext& context); // Tracing::Driver Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context, diff --git a/source/server/config_validation/cluster_manager.cc b/source/server/config_validation/cluster_manager.cc index 719919f25874..cc1834be90a9 100644 --- a/source/server/config_validation/cluster_manager.cc +++ b/source/server/config_validation/cluster_manager.cc @@ -11,7 +11,7 @@ namespace Upstream { ClusterManagerPtr ValidationClusterManagerFactory::clusterManagerFromProto( const envoy::config::bootstrap::v3::Bootstrap& bootstrap) { auto cluster_manager = std::unique_ptr{new ValidationClusterManager( - bootstrap, *this, stats_, tls_, context_.runtime(), context_.localInfo(), + bootstrap, *this, context_, stats_, tls_, context_.runtime(), context_.localInfo(), context_.accessLogManager(), context_.mainThreadDispatcher(), context_.admin(), context_.messageValidationContext(), context_.api(), http_context_, context_.grpcContext(), context_.routerContext(), server_)}; diff --git a/source/server/server.cc b/source/server/server.cc index a570ccf16cce..fc3a50beeed8 100644 --- a/source/server/server.cc +++ b/source/server/server.cc @@ -826,7 +826,7 @@ void InstanceBase::onRuntimeReady() { if (bootstrap_.has_hds_config()) { const auto& hds_config = bootstrap_.hds_config(); async_client_manager_ = std::make_unique( - *config_.clusterManager(), thread_local_, time_source_, *api_, grpc_context_.statNames(), + *config_.clusterManager(), thread_local_, server_contexts_, grpc_context_.statNames(), bootstrap_.grpc_async_client_manager_config()); TRY_ASSERT_MAIN_THREAD { THROW_IF_NOT_OK(Config::Utility::checkTransportVersion(hds_config)); diff --git a/test/common/grpc/BUILD b/test/common/grpc/BUILD index cbf20b2ab3c4..ec28b9cea693 100644 --- a/test/common/grpc/BUILD +++ b/test/common/grpc/BUILD @@ -34,6 +34,7 @@ envoy_cc_test( deps = [ "//source/common/api:api_lib", "//source/common/grpc:async_client_manager_lib", + "//test/mocks/server:server_factory_context_mocks", "//test/mocks/stats:stats_mocks", "//test/mocks/thread_local:thread_local_mocks", "//test/mocks/upstream:cluster_manager_mocks", @@ -113,6 +114,7 @@ envoy_cc_test( "//source/common/stats:stats_lib", "//source/common/tracing:http_tracer_lib", "//test/mocks/grpc:grpc_mocks", + "//test/mocks/server:server_factory_context_mocks", "//test/mocks/tracing:tracing_mocks", "//test/proto:helloworld_proto_cc_proto", "//test/test_common:test_time_lib", @@ -227,6 +229,7 @@ envoy_cc_benchmark_binary( deps = [ "//source/common/api:api_lib", "//source/common/grpc:async_client_manager_lib", + "//test/mocks/server:server_factory_context_mocks", "//test/mocks/stats:stats_mocks", "//test/mocks/thread_local:thread_local_mocks", "//test/mocks/upstream:cluster_manager_mocks", diff --git a/test/common/grpc/async_client_manager_benchmark.cc b/test/common/grpc/async_client_manager_benchmark.cc index 5b62f6c270b5..05c2b74b829a 100644 --- a/test/common/grpc/async_client_manager_benchmark.cc +++ b/test/common/grpc/async_client_manager_benchmark.cc @@ -9,6 +9,7 @@ #include "source/common/grpc/async_client_manager_impl.h" #include "test/benchmark/main.h" +#include "test/mocks/server/server_factory_context.h" #include "test/mocks/stats/mocks.h" #include "test/mocks/thread_local/mocks.h" #include "test/mocks/upstream/cluster_manager.h" @@ -27,20 +28,20 @@ namespace { class AsyncClientManagerImplTest { public: - AsyncClientManagerImplTest() - : api_(Api::createApiForTest()), stat_names_(scope_.symbolTable()), - async_client_manager_( - cm_, tls_, test_time_.timeSystem(), *api_, stat_names_, - envoy::config::bootstrap::v3::Bootstrap::GrpcAsyncClientManagerConfig()) {} + AsyncClientManagerImplTest() : api_(Api::createApiForTest()), stat_names_(scope_.symbolTable()) { + ON_CALL(context_, api()).WillByDefault(testing::ReturnRef(*api_)); + async_client_manager_ = std::make_unique( + cm_, context_.threadLocal(), context_, stat_names_, + envoy::config::bootstrap::v3::Bootstrap::GrpcAsyncClientManagerConfig()); + } Upstream::MockClusterManager cm_; - NiceMock tls_; + NiceMock context_; Stats::MockStore store_; Stats::MockScope& scope_{store_.mockScope()}; - DangerousDeprecatedTestTime test_time_; Api::ApiPtr api_; StatNames stat_names_; - AsyncClientManagerImpl async_client_manager_; + std::unique_ptr async_client_manager_; }; void testGetOrCreateAsyncClientWithConfig(::benchmark::State& state) { @@ -54,7 +55,7 @@ void testGetOrCreateAsyncClientWithConfig(::benchmark::State& state) { for (int i = 0; i < 1000; i++) { RawAsyncClientSharedPtr foo_client0 = async_client_man_test.async_client_manager_ - .getOrCreateRawAsyncClient(grpc_service, async_client_man_test.scope_, true) + ->getOrCreateRawAsyncClient(grpc_service, async_client_man_test.scope_, true) .value(); } } @@ -72,8 +73,8 @@ void testGetOrCreateAsyncClientWithHashConfig(::benchmark::State& state) { for (int i = 0; i < 1000; i++) { RawAsyncClientSharedPtr foo_client0 = async_client_man_test.async_client_manager_ - .getOrCreateRawAsyncClientWithHashKey(config_with_hash_key_a, - async_client_man_test.scope_, true) + ->getOrCreateRawAsyncClientWithHashKey(config_with_hash_key_a, + async_client_man_test.scope_, true) .value(); } } diff --git a/test/common/grpc/async_client_manager_impl_test.cc b/test/common/grpc/async_client_manager_impl_test.cc index 8cbcd82c5c5d..13581c6571f5 100644 --- a/test/common/grpc/async_client_manager_impl_test.cc +++ b/test/common/grpc/async_client_manager_impl_test.cc @@ -9,6 +9,7 @@ #include "source/common/event/dispatcher_impl.h" #include "source/common/grpc/async_client_manager_impl.h" +#include "test/mocks/server/server_factory_context.h" #include "test/mocks/stats/mocks.h" #include "test/mocks/thread_local/mocks.h" #include "test/mocks/upstream/cluster_manager.h" @@ -202,21 +203,26 @@ class AsyncClientManagerImplTest : public testing::Test { : api_(Api::createApiForTest(time_system_)), dispatcher_(api_->allocateDispatcher("test_grpc_manager")), stat_names_(scope_.symbolTable()) { - tls_.setDispatcher(dispatcher_.get()); + context_.thread_local_.setDispatcher(dispatcher_.get()); } void initialize(absl::optional config = absl::nullopt) { + ON_CALL(context_, clusterManager()).WillByDefault(testing::ReturnRef(cm_)); + ON_CALL(context_, mainThreadDispatcher()).WillByDefault(testing::ReturnRef(*dispatcher_)); + ON_CALL(context_, timeSource()).WillByDefault(testing::ReturnRef(time_system_)); + ON_CALL(context_, api()).WillByDefault(testing::ReturnRef(*api_)); if (config.has_value()) { async_client_manager_ = std::make_unique( - cm_, tls_, time_system_, *api_, stat_names_, config.value()); + cm_, context_.threadLocal(), context_, stat_names_, config.value()); } else { async_client_manager_ = std::make_unique( - cm_, tls_, time_system_, *api_, stat_names_, Bootstrap::GrpcAsyncClientManagerConfig()); + cm_, context_.threadLocal(), context_, stat_names_, + Bootstrap::GrpcAsyncClientManagerConfig()); } } + NiceMock context_; Upstream::MockClusterManager cm_; - NiceMock tls_; Stats::MockStore store_; Stats::MockScope& scope_{store_.mockScope()}; Event::SimulatedTimeSystem time_system_; diff --git a/test/common/grpc/google_async_client_impl_test.cc b/test/common/grpc/google_async_client_impl_test.cc index d0d3f7d5e67b..7e8f48655393 100644 --- a/test/common/grpc/google_async_client_impl_test.cc +++ b/test/common/grpc/google_async_client_impl_test.cc @@ -9,6 +9,7 @@ #include "source/common/stream_info/stream_info_impl.h" #include "test/mocks/grpc/mocks.h" +#include "test/mocks/server/server_factory_context.h" #include "test/mocks/tracing/mocks.h" #include "test/proto/helloworld.pb.h" #include "test/test_common/test_time.h" @@ -57,6 +58,7 @@ class EnvoyGoogleAsyncClientImplTest : public testing::Test { method_descriptor_(helloworld::Greeter::descriptor()->FindMethodByName("SayHello")), stat_names_(scope_->symbolTable()) { + ON_CALL(context_, api()).WillByDefault(testing::ReturnRef(*api_)); auto* google_grpc = config_.mutable_google_grpc(); google_grpc->set_target_uri("fake_address"); google_grpc->set_stat_prefix("test_cluster"); @@ -70,7 +72,7 @@ class EnvoyGoogleAsyncClientImplTest : public testing::Test { virtual void initialize() { grpc_client_ = std::make_unique(*dispatcher_, *tls_, stub_factory_, - scope_, config_, *api_, stat_names_); + scope_, config_, context_, stat_names_); } envoy::config::core::v3::GrpcService config_; @@ -78,6 +80,7 @@ class EnvoyGoogleAsyncClientImplTest : public testing::Test { Stats::IsolatedStoreImpl stats_store_; Api::ApiPtr api_; Event::DispatcherPtr dispatcher_; + NiceMock context_; Stats::ScopeSharedPtr scope_; GoogleAsyncClientThreadLocalPtr tls_; MockStubFactory stub_factory_; @@ -180,7 +183,7 @@ class EnvoyGoogleLessMockedAsyncClientImplTest : public EnvoyGoogleAsyncClientIm public: void initialize() override { grpc_client_ = std::make_unique(*dispatcher_, *tls_, real_stub_factory_, - scope_, config_, *api_, stat_names_); + scope_, config_, context_, stat_names_); } GoogleGenericStubFactory real_stub_factory_; diff --git a/test/common/grpc/grpc_client_integration_test_harness.h b/test/common/grpc/grpc_client_integration_test_harness.h index 0b52d9c55e82..f6a3363eb24b 100644 --- a/test/common/grpc/grpc_client_integration_test_harness.h +++ b/test/common/grpc/grpc_client_integration_test_harness.h @@ -368,9 +368,9 @@ class GrpcClientIntegrationTest : public GrpcClientIntegrationParamTest { #ifdef ENVOY_GOOGLE_GRPC google_tls_ = std::make_unique(*api_); GoogleGenericStubFactory stub_factory; - return std::make_unique(*dispatcher_, *google_tls_, stub_factory, - stats_scope_, createGoogleGrpcConfig(), *api_, - google_grpc_stat_names_); + return std::make_unique( + *dispatcher_, *google_tls_, stub_factory, stats_scope_, createGoogleGrpcConfig(), + server_factory_context_, google_grpc_stat_names_); #else PANIC("reached unexpected code"); #endif @@ -533,6 +533,7 @@ class GrpcSslClientIntegrationTest : public GrpcClientIntegrationTest { public: GrpcSslClientIntegrationTest() { ON_CALL(factory_context_.server_context_, api()).WillByDefault(ReturnRef(*api_)); + ON_CALL(server_factory_context_, api()).WillByDefault(ReturnRef(*api_)); } void TearDown() override { // Reset some state in the superclass before we destruct context_manager_ in our destructor, it diff --git a/test/common/upstream/cluster_manager_impl_test.cc b/test/common/upstream/cluster_manager_impl_test.cc index 04b65bd5c1db..17644f883a1c 100644 --- a/test/common/upstream/cluster_manager_impl_test.cc +++ b/test/common/upstream/cluster_manager_impl_test.cc @@ -105,20 +105,18 @@ class MockedUpdatedClusterManagerImpl : public TestClusterManagerImpl { public: using TestClusterManagerImpl::TestClusterManagerImpl; - MockedUpdatedClusterManagerImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap, - ClusterManagerFactory& factory, Stats::Store& stats, - ThreadLocal::Instance& tls, Runtime::Loader& runtime, - const LocalInfo::LocalInfo& local_info, - AccessLog::AccessLogManager& log_manager, - Event::Dispatcher& main_thread_dispatcher, Server::Admin& admin, - ProtobufMessage::ValidationContext& validation_context, - Api::Api& api, MockLocalClusterUpdate& local_cluster_update, - MockLocalHostsRemoved& local_hosts_removed, - Http::Context& http_context, Grpc::Context& grpc_context, - Router::Context& router_context, Server::Instance& server) - : TestClusterManagerImpl(bootstrap, factory, stats, tls, runtime, local_info, log_manager, - main_thread_dispatcher, admin, validation_context, api, http_context, - grpc_context, router_context, server), + MockedUpdatedClusterManagerImpl( + const envoy::config::bootstrap::v3::Bootstrap& bootstrap, ClusterManagerFactory& factory, + Server::Configuration::CommonFactoryContext& factory_context, Stats::Store& stats, + ThreadLocal::Instance& tls, Runtime::Loader& runtime, const LocalInfo::LocalInfo& local_info, + AccessLog::AccessLogManager& log_manager, Event::Dispatcher& main_thread_dispatcher, + Server::Admin& admin, ProtobufMessage::ValidationContext& validation_context, Api::Api& api, + MockLocalClusterUpdate& local_cluster_update, MockLocalHostsRemoved& local_hosts_removed, + Http::Context& http_context, Grpc::Context& grpc_context, Router::Context& router_context, + Server::Instance& server) + : TestClusterManagerImpl(bootstrap, factory, factory_context, stats, tls, runtime, local_info, + log_manager, main_thread_dispatcher, admin, validation_context, api, + http_context, grpc_context, router_context, server), local_cluster_update_(local_cluster_update), local_hosts_removed_(local_hosts_removed) {} protected: @@ -161,6 +159,7 @@ class MockGrpcMuxFactory : public Config::MuxFactory { class UpdateOverrideClusterManagerImpl : public TestClusterManagerImpl { public: UpdateOverrideClusterManagerImpl(const Bootstrap& bootstrap, ClusterManagerFactory& factory, + Server::Configuration::CommonFactoryContext& factory_context, Stats::Store& stats, ThreadLocal::Instance& tls, Runtime::Loader& runtime, const LocalInfo::LocalInfo& local_info, AccessLog::AccessLogManager& log_manager, @@ -169,9 +168,9 @@ class UpdateOverrideClusterManagerImpl : public TestClusterManagerImpl { Api::Api& api, Http::Context& http_context, Grpc::Context& grpc_context, Router::Context& router_context, Server::Instance& server) - : TestClusterManagerImpl(bootstrap, factory, stats, tls, runtime, local_info, log_manager, - main_thread_dispatcher, admin, validation_context, api, http_context, - grpc_context, router_context, server) {} + : TestClusterManagerImpl(bootstrap, factory, factory_context, stats, tls, runtime, local_info, + log_manager, main_thread_dispatcher, admin, validation_context, api, + http_context, grpc_context, router_context, server) {} protected: void postThreadLocalClusterUpdate(ClusterManagerCluster& cluster, @@ -208,9 +207,10 @@ class ClusterManagerImplTest : public testing::Test { virtual void create(const Bootstrap& bootstrap) { cluster_manager_ = TestClusterManagerImpl::createAndInit( - bootstrap, factory_, factory_.stats_, factory_.tls_, factory_.runtime_, - factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, validation_context_, - *factory_.api_, http_context_, grpc_context_, router_context_, server_); + bootstrap, factory_, factory_.server_context_, factory_.stats_, factory_.tls_, + factory_.runtime_, factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, + validation_context_, *factory_.api_, http_context_, grpc_context_, router_context_, + server_); cluster_manager_->setPrimaryClustersInitializedCb([this, bootstrap]() { THROW_IF_NOT_OK(cluster_manager_->initializeSecondaryClusters(bootstrap)); }); @@ -275,18 +275,19 @@ class ClusterManagerImplTest : public testing::Test { const auto& bootstrap = parseBootstrapFromV3Yaml(yaml); cluster_manager_ = std::make_unique( - bootstrap, factory_, factory_.stats_, factory_.tls_, factory_.runtime_, - factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, validation_context_, - *factory_.api_, local_cluster_update_, local_hosts_removed_, http_context_, grpc_context_, - router_context_, server_); + bootstrap, factory_, factory_.server_context_, factory_.stats_, factory_.tls_, + factory_.runtime_, factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, + validation_context_, *factory_.api_, local_cluster_update_, local_hosts_removed_, + http_context_, grpc_context_, router_context_, server_); THROW_IF_NOT_OK(cluster_manager_->init(bootstrap)); } void createWithUpdateOverrideClusterManager(const Bootstrap& bootstrap) { cluster_manager_ = std::make_unique( - bootstrap, factory_, factory_.stats_, factory_.tls_, factory_.runtime_, - factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, validation_context_, - *factory_.api_, http_context_, grpc_context_, router_context_, server_); + bootstrap, factory_, factory_.server_context_, factory_.stats_, factory_.tls_, + factory_.runtime_, factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, + validation_context_, *factory_.api_, http_context_, grpc_context_, router_context_, + server_); THROW_IF_NOT_OK(cluster_manager_->init(bootstrap)); } diff --git a/test/common/upstream/deferred_cluster_initialization_test.cc b/test/common/upstream/deferred_cluster_initialization_test.cc index a9701fc77c25..12fb3373e7cc 100644 --- a/test/common/upstream/deferred_cluster_initialization_test.cc +++ b/test/common/upstream/deferred_cluster_initialization_test.cc @@ -63,9 +63,10 @@ class DeferredClusterInitializationTest : public testing::TestWithParam { void create(const envoy::config::bootstrap::v3::Bootstrap& bootstrap) { cluster_manager_ = TestClusterManagerImpl::createAndInit( - bootstrap, factory_, factory_.stats_, factory_.tls_, factory_.runtime_, - factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, validation_context_, - *factory_.api_, http_context_, grpc_context_, router_context_, server_); + bootstrap, factory_, factory_.server_context_, factory_.stats_, factory_.tls_, + factory_.runtime_, factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, + validation_context_, *factory_.api_, http_context_, grpc_context_, router_context_, + server_); cluster_manager_->setPrimaryClustersInitializedCb([this, bootstrap]() { THROW_IF_NOT_OK(cluster_manager_->initializeSecondaryClusters(bootstrap)); }); diff --git a/test/common/upstream/test_cluster_manager.h b/test/common/upstream/test_cluster_manager.h index a8e232eacb9e..d05bfc493376 100644 --- a/test/common/upstream/test_cluster_manager.h +++ b/test/common/upstream/test_cluster_manager.h @@ -162,17 +162,18 @@ class TestClusterManagerFactory : public ClusterManagerFactory { // clusters, which is necessary in order to call updateHosts on the priority set. class TestClusterManagerImpl : public ClusterManagerImpl { public: - static std::unique_ptr - createAndInit(const envoy::config::bootstrap::v3::Bootstrap& bootstrap, - ClusterManagerFactory& factory, Stats::Store& stats, ThreadLocal::Instance& tls, - Runtime::Loader& runtime, const LocalInfo::LocalInfo& local_info, - AccessLog::AccessLogManager& log_manager, Event::Dispatcher& main_thread_dispatcher, - Server::Admin& admin, ProtobufMessage::ValidationContext& validation_context, - Api::Api& api, Http::Context& http_context, Grpc::Context& grpc_context, - Router::Context& router_context, Server::Instance& server) { - auto cluster_manager = std::unique_ptr{new TestClusterManagerImpl( - bootstrap, factory, stats, tls, runtime, local_info, log_manager, main_thread_dispatcher, - admin, validation_context, api, http_context, grpc_context, router_context, server)}; + static std::unique_ptr createAndInit( + const envoy::config::bootstrap::v3::Bootstrap& bootstrap, ClusterManagerFactory& factory, + Server::Configuration::CommonFactoryContext& context, Stats::Store& stats, + ThreadLocal::Instance& tls, Runtime::Loader& runtime, const LocalInfo::LocalInfo& local_info, + AccessLog::AccessLogManager& log_manager, Event::Dispatcher& main_thread_dispatcher, + Server::Admin& admin, ProtobufMessage::ValidationContext& validation_context, Api::Api& api, + Http::Context& http_context, Grpc::Context& grpc_context, Router::Context& router_context, + Server::Instance& server) { + auto cluster_manager = std::unique_ptr{ + new TestClusterManagerImpl(bootstrap, factory, context, stats, tls, runtime, local_info, + log_manager, main_thread_dispatcher, admin, validation_context, + api, http_context, grpc_context, router_context, server)}; THROW_IF_NOT_OK(cluster_manager->init(bootstrap)); return cluster_manager; } @@ -205,7 +206,8 @@ class TestClusterManagerImpl : public ClusterManagerImpl { using ClusterManagerImpl::ClusterManagerImpl; TestClusterManagerImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap, - ClusterManagerFactory& factory, Stats::Store& stats, + ClusterManagerFactory& factory, + Server::Configuration::CommonFactoryContext& context, Stats::Store& stats, ThreadLocal::Instance& tls, Runtime::Loader& runtime, const LocalInfo::LocalInfo& local_info, AccessLog::AccessLogManager& log_manager, @@ -213,9 +215,9 @@ class TestClusterManagerImpl : public ClusterManagerImpl { ProtobufMessage::ValidationContext& validation_context, Api::Api& api, Http::Context& http_context, Grpc::Context& grpc_context, Router::Context& router_context, Server::Instance& server) - : ClusterManagerImpl(bootstrap, factory, stats, tls, runtime, local_info, log_manager, - main_thread_dispatcher, admin, validation_context, api, http_context, - grpc_context, router_context, server) {} + : ClusterManagerImpl(bootstrap, factory, context, stats, tls, runtime, local_info, + log_manager, main_thread_dispatcher, admin, validation_context, api, + http_context, grpc_context, router_context, server) {} }; } // namespace Upstream diff --git a/test/extensions/clusters/aggregate/cluster_update_test.cc b/test/extensions/clusters/aggregate/cluster_update_test.cc index 281cd49b87e5..a432877c001b 100644 --- a/test/extensions/clusters/aggregate/cluster_update_test.cc +++ b/test/extensions/clusters/aggregate/cluster_update_test.cc @@ -43,9 +43,10 @@ class AggregateClusterUpdateTest : public Event::TestUsingSimulatedTime, const bool use_deferred_cluster = GetParam(); bootstrap.mutable_cluster_manager()->set_enable_deferred_cluster_creation(use_deferred_cluster); cluster_manager_ = Upstream::TestClusterManagerImpl::createAndInit( - bootstrap, factory_, factory_.stats_, factory_.tls_, factory_.runtime_, - factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, validation_context_, - *factory_.api_, http_context_, grpc_context_, router_context_, server_); + bootstrap, factory_, factory_.server_context_, factory_.stats_, factory_.tls_, + factory_.runtime_, factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, + validation_context_, *factory_.api_, http_context_, grpc_context_, router_context_, + server_); ASSERT_TRUE(cluster_manager_->initializeSecondaryClusters(bootstrap).ok()); EXPECT_EQ(cluster_manager_->activeClusters().size(), 1); cluster_ = cluster_manager_->getThreadLocalCluster("aggregate_cluster"); @@ -278,9 +279,9 @@ TEST_P(AggregateClusterUpdateTest, InitializeAggregateClusterAfterOtherClusters) auto bootstrap = parseBootstrapFromV2Yaml(config); cluster_manager_ = Upstream::TestClusterManagerImpl::createAndInit( - bootstrap, factory_, factory_.stats_, factory_.tls_, factory_.runtime_, factory_.local_info_, - log_manager_, factory_.dispatcher_, admin_, validation_context_, *factory_.api_, - http_context_, grpc_context_, router_context_, server_); + bootstrap, factory_, factory_.server_context_, factory_.stats_, factory_.tls_, + factory_.runtime_, factory_.local_info_, log_manager_, factory_.dispatcher_, admin_, + validation_context_, *factory_.api_, http_context_, grpc_context_, router_context_, server_); ASSERT_TRUE(cluster_manager_->initializeSecondaryClusters(bootstrap).ok()); EXPECT_EQ(cluster_manager_->activeClusters().size(), 2); cluster_ = cluster_manager_->getThreadLocalCluster("aggregate_cluster"); diff --git a/test/extensions/common/aws/BUILD b/test/extensions/common/aws/BUILD index 69b7469b8ae1..0923c214b656 100644 --- a/test/extensions/common/aws/BUILD +++ b/test/extensions/common/aws/BUILD @@ -31,6 +31,7 @@ envoy_cc_test( "//source/common/http:message_lib", "//source/extensions/common/aws:sigv4_signer_impl_lib", "//test/extensions/common/aws:aws_mocks", + "//test/mocks/server:server_factory_context_mocks", "//test/test_common:simulated_time_system_lib", "//test/test_common:utility_lib", ], @@ -45,6 +46,7 @@ envoy_cc_test( "//source/extensions/common/aws:sigv4a_key_derivation_lib", "//source/extensions/common/aws:sigv4a_signer_impl_lib", "//test/extensions/common/aws:aws_mocks", + "//test/mocks/server:server_factory_context_mocks", "//test/test_common:simulated_time_system_lib", "//test/test_common:utility_lib", ], diff --git a/test/extensions/common/aws/sigv4_signer_impl_test.cc b/test/extensions/common/aws/sigv4_signer_impl_test.cc index cab15cb91833..eb555a153ef3 100644 --- a/test/extensions/common/aws/sigv4_signer_impl_test.cc +++ b/test/extensions/common/aws/sigv4_signer_impl_test.cc @@ -4,11 +4,13 @@ #include "source/extensions/common/aws/utility.h" #include "test/extensions/common/aws/mocks.h" +#include "test/mocks/server/server_factory_context.h" #include "test/test_common/simulated_time_system.h" #include "test/test_common/utility.h" using testing::NiceMock; using testing::Return; +using testing::ReturnRef; namespace Envoy { namespace Extensions { @@ -21,11 +23,12 @@ class SigV4SignerImplTest : public testing::Test { SigV4SignerImplTest() : credentials_provider_(new NiceMock()), message_(new Http::RequestMessageImpl()), - signer_("service", "region", CredentialsProviderSharedPtr{credentials_provider_}, - time_system_, Extensions::Common::Aws::AwsSigningHeaderExclusionVector{}), + signer_("service", "region", CredentialsProviderSharedPtr{credentials_provider_}, context_, + Extensions::Common::Aws::AwsSigningHeaderExclusionVector{}), credentials_("akid", "secret"), token_credentials_("akid", "secret", "token") { // 20180102T030405Z time_system_.setSystemTime(std::chrono::milliseconds(1514862245000)); + ON_CALL(context_, timeSystem()).WillByDefault(ReturnRef(time_system_)); } void addMethod(const std::string& method) { message_->headers().setMethod(method); } @@ -49,7 +52,7 @@ class SigV4SignerImplTest : public testing::Test { headers.addCopy(Http::LowerCaseString("host"), "www.example.com"); SigV4SignerImpl signer(service_name, "region", - CredentialsProviderSharedPtr{credentials_provider}, time_system_, + CredentialsProviderSharedPtr{credentials_provider}, context_, Extensions::Common::Aws::AwsSigningHeaderExclusionVector{}, false, 5); if (use_unsigned_payload) { signer.signUnsignedPayload(headers, override_region); @@ -79,7 +82,7 @@ class SigV4SignerImplTest : public testing::Test { } SigV4SignerImpl signer(service_name, "region", - CredentialsProviderSharedPtr{credentials_provider}, time_system_, + CredentialsProviderSharedPtr{credentials_provider}, context_, Extensions::Common::Aws::AwsSigningHeaderExclusionVector{}, true, 5); signer.signUnsignedPayload(extra_headers, override_region); @@ -90,6 +93,7 @@ class SigV4SignerImplTest : public testing::Test { NiceMock* credentials_provider_; Event::SimulatedTimeSystem time_system_; + NiceMock context_; Http::RequestMessagePtr message_; SigV4SignerImpl signer_; Credentials credentials_; @@ -268,7 +272,7 @@ TEST_F(SigV4SignerImplTest, QueryStringDefault5s) { headers.addCopy(Http::LowerCaseString("host"), "example.service.zz"); headers.addCopy("testheader", "value1"); SigV4SignerImpl querysigner("service", "region", - CredentialsProviderSharedPtr{credentials_provider}, time_system_, + CredentialsProviderSharedPtr{credentials_provider}, context_, Extensions::Common::Aws::AwsSigningHeaderExclusionVector{}, true); querysigner.signUnsignedPayload(headers); diff --git a/test/extensions/common/aws/sigv4a_signer_impl_test.cc b/test/extensions/common/aws/sigv4a_signer_impl_test.cc index 5f92d3e969f8..b6e9e2f2984c 100644 --- a/test/extensions/common/aws/sigv4a_signer_impl_test.cc +++ b/test/extensions/common/aws/sigv4a_signer_impl_test.cc @@ -8,11 +8,13 @@ #include "source/extensions/common/aws/utility.h" #include "test/extensions/common/aws/mocks.h" +#include "test/mocks/server/server_factory_context.h" #include "test/test_common/simulated_time_system.h" #include "test/test_common/utility.h" using testing::NiceMock; using testing::Return; +using testing::ReturnRef; namespace Envoy { namespace Extensions { @@ -28,6 +30,7 @@ class SigV4ASignerImplTest : public testing::Test { token_credentials_("akid", "secret", "token") { // 20180102T030405Z time_system_.setSystemTime(std::chrono::milliseconds(1514862245000)); + ON_CALL(context_, timeSystem()).WillByDefault(ReturnRef(time_system_)); } void addMethod(const std::string& method) { message_->headers().setMethod(method); } @@ -54,7 +57,7 @@ class SigV4ASignerImplTest : public testing::Test { return SigV4ASignerImpl{"service", "region", getTestCredentialsProvider(), - time_system_, + context_, Extensions::Common::Aws::AwsSigningHeaderExclusionVector{}, query_string, expiration_time}; @@ -133,6 +136,7 @@ class SigV4ASignerImplTest : public testing::Test { } NiceMock* credentials_provider_; Event::SimulatedTimeSystem time_system_; + NiceMock context_; Http::RequestMessagePtr message_; Credentials credentials_; Credentials token_credentials_; @@ -478,7 +482,7 @@ TEST_F(SigV4ASignerImplTest, QueryStringDefault5s) { headers.setPath("/example/path"); headers.addCopy(Http::LowerCaseString("host"), "example.service.zz"); headers.addCopy("testheader", "value1"); - SigV4ASignerImpl querysigner("service", "region", getTestCredentialsProvider(), time_system_, + SigV4ASignerImpl querysigner("service", "region", getTestCredentialsProvider(), context_, Extensions::Common::Aws::AwsSigningHeaderExclusionVector{}, true); querysigner.signUnsignedPayload(headers); diff --git a/test/extensions/filters/http/ext_authz/config_test.cc b/test/extensions/filters/http/ext_authz/config_test.cc index 62909863a5ec..dcdfc7f81ad5 100644 --- a/test/extensions/filters/http/ext_authz/config_test.cc +++ b/test/extensions/filters/http/ext_authz/config_test.cc @@ -31,10 +31,10 @@ namespace ExtAuthz { class TestAsyncClientManagerImpl : public Grpc::AsyncClientManagerImpl { public: TestAsyncClientManagerImpl(Upstream::ClusterManager& cm, ThreadLocal::Instance& tls, - TimeSource& time_source, Api::Api& api, + Server::Configuration::CommonFactoryContext& context, const Grpc::StatNames& stat_names, const Bootstrap::GrpcAsyncClientManagerConfig& config) - : Grpc::AsyncClientManagerImpl(cm, tls, time_source, api, stat_names, config) {} + : Grpc::AsyncClientManagerImpl(cm, tls, context, stat_names, config) {} absl::StatusOr factoryForGrpcService(const envoy::config::core::v3::GrpcService&, Stats::Scope&, bool) override { return std::make_unique>(); @@ -46,10 +46,13 @@ class ExtAuthzFilterTest : public Event::TestUsingSimulatedTime, public testing::Test { public: ExtAuthzFilterTest() : RealThreadsTestHelper(5), stat_names_(symbol_table_) { + ON_CALL(context_.server_factory_context_, threadLocal()) + .WillByDefault(testing::ReturnRef(tls())); + ON_CALL(context_.server_factory_context_, api()).WillByDefault(testing::ReturnRef(api())); runOnMainBlocking([&]() { async_client_manager_ = std::make_unique( - context_.server_factory_context_.cluster_manager_, tls(), api().timeSource(), api(), - stat_names_, Bootstrap::GrpcAsyncClientManagerConfig()); + context_.server_factory_context_.cluster_manager_, tls(), + context_.server_factory_context_, stat_names_, Bootstrap::GrpcAsyncClientManagerConfig()); }); } diff --git a/test/extensions/tracers/opencensus/BUILD b/test/extensions/tracers/opencensus/BUILD index 64e7311cec58..53cf89ab6677 100644 --- a/test/extensions/tracers/opencensus/BUILD +++ b/test/extensions/tracers/opencensus/BUILD @@ -20,7 +20,7 @@ envoy_extension_cc_test( deps = [ "//source/extensions/tracers/opencensus:opencensus_tracer_impl", "//test/mocks/http:http_mocks", - "//test/mocks/local_info:local_info_mocks", + "//test/mocks/server:server_factory_context_mocks", "//test/mocks/stream_info:stream_info_mocks", "//test/mocks/tracing:tracing_mocks", "@envoy_api//envoy/config/trace/v3:pkg_cc_proto", diff --git a/test/extensions/tracers/opencensus/tracer_test.cc b/test/extensions/tracers/opencensus/tracer_test.cc index 0548b99bc569..367420d5bf73 100644 --- a/test/extensions/tracers/opencensus/tracer_test.cc +++ b/test/extensions/tracers/opencensus/tracer_test.cc @@ -11,7 +11,7 @@ #include "source/extensions/tracers/opencensus/opencensus_tracer_impl.h" #include "test/mocks/http/mocks.h" -#include "test/mocks/local_info/mocks.h" +#include "test/mocks/server/server_factory_context.h" #include "test/mocks/stream_info/mocks.h" #include "test/mocks/tracing/mocks.h" @@ -102,9 +102,8 @@ void registerSpanCatcher() { TEST(OpenCensusTracerTest, Span) { registerSpanCatcher(); OpenCensusConfig oc_config; - NiceMock local_info; - std::unique_ptr driver( - new OpenCensus::Driver(oc_config, local_info, *Api::createApiForTest())); + NiceMock context; + std::unique_ptr driver(new OpenCensus::Driver(oc_config, context)); NiceMock config; Tracing::TestTraceContextImpl request_headers{ @@ -189,7 +188,6 @@ void testIncomingHeaders( const std::initializer_list>& headers) { registerSpanCatcher(); OpenCensusConfig oc_config; - NiceMock local_info; oc_config.add_incoming_trace_context(OpenCensusConfig::NONE); oc_config.add_incoming_trace_context(OpenCensusConfig::B3); oc_config.add_incoming_trace_context(OpenCensusConfig::TRACE_CONTEXT); @@ -200,8 +198,8 @@ void testIncomingHeaders( oc_config.add_outgoing_trace_context(OpenCensusConfig::TRACE_CONTEXT); oc_config.add_outgoing_trace_context(OpenCensusConfig::GRPC_TRACE_BIN); oc_config.add_outgoing_trace_context(OpenCensusConfig::CLOUD_TRACE_CONTEXT); - std::unique_ptr driver( - new OpenCensus::Driver(oc_config, local_info, *Api::createApiForTest())); + NiceMock context; + std::unique_ptr driver(new OpenCensus::Driver(oc_config, context)); NiceMock config; Tracing::TestTraceContextImpl request_headers{ {":path", "/"}, @@ -291,9 +289,8 @@ namespace { // the exporter (either zero or one). int samplerTestHelper(const OpenCensusConfig& oc_config) { registerSpanCatcher(); - NiceMock local_info; - std::unique_ptr driver( - new OpenCensus::Driver(oc_config, local_info, *Api::createApiForTest())); + NiceMock context; + std::unique_ptr driver(new OpenCensus::Driver(oc_config, context)); auto span = ::opencensus::trace::Span::StartSpan("test_span"); span.End(); // Retrieve SpanData from the OpenCensus trace exporter. diff --git a/test/mocks/server/server_factory_context.cc b/test/mocks/server/server_factory_context.cc index 6de698d5c0a2..fb12fbe4f8b8 100644 --- a/test/mocks/server/server_factory_context.cc +++ b/test/mocks/server/server_factory_context.cc @@ -23,6 +23,7 @@ MockServerFactoryContext::MockServerFactoryContext() ON_CALL(*this, admin()).WillByDefault(Return(OptRef{admin_})); ON_CALL(*this, api()).WillByDefault(ReturnRef(api_)); ON_CALL(*this, timeSource()).WillByDefault(ReturnRef(time_system_)); + ON_CALL(*this, timeSystem()).WillByDefault(ReturnRef(time_system_)); ON_CALL(*this, messageValidationContext()).WillByDefault(ReturnRef(validation_context_)); ON_CALL(*this, messageValidationVisitor()) .WillByDefault(ReturnRef(ProtobufMessage::getStrictValidationVisitor())); diff --git a/test/mocks/server/server_factory_context.h b/test/mocks/server/server_factory_context.h index 128afe221c26..d9670dead628 100644 --- a/test/mocks/server/server_factory_context.h +++ b/test/mocks/server/server_factory_context.h @@ -67,7 +67,7 @@ class MockServerFactoryContext : public virtual ServerFactoryContext { MOCK_METHOD(ThreadLocal::Instance&, threadLocal, ()); MOCK_METHOD(OptRef, admin, ()); MOCK_METHOD(TimeSource&, timeSource, ()); - Event::TestTimeSystem& timeSystem() { return time_system_; } + MOCK_METHOD(Event::TestTimeSystem&, timeSystem, ()); MOCK_METHOD(ProtobufMessage::ValidationContext&, messageValidationContext, ()); MOCK_METHOD(ProtobufMessage::ValidationVisitor&, messageValidationVisitor, ()); MOCK_METHOD(Api::Api&, api, ()); diff --git a/test/per_file_coverage.sh b/test/per_file_coverage.sh index 5ee457fe70b3..7f9b5e00b4a6 100755 --- a/test/per_file_coverage.sh +++ b/test/per_file_coverage.sh @@ -48,7 +48,7 @@ declare -a KNOWN_LOW_COVERAGE=( "source/extensions/tracers:96.4" "source/extensions/tracers/common:74.8" "source/extensions/tracers/common/ot:72.9" -"source/extensions/tracers/opencensus:94.0" +"source/extensions/tracers/opencensus:93.9" "source/extensions/tracers/zipkin:95.8" "source/extensions/transport_sockets:97.4" "source/common/tls:94.9"