1
#pragma once
2

            
3
#include "envoy/config/xds_manager.h"
4

            
5
#include "source/common/common/thread.h"
6
#include "source/common/config/subscription_factory_impl.h"
7
#include "source/common/config/xds_resource.h"
8

            
9
namespace Envoy {
10
namespace Config {
11

            
12
class XdsManagerImpl : public XdsManager {
13
public:
14
  XdsManagerImpl(Event::Dispatcher& main_thread_dispatcher, Api::Api& api, Stats::Store& stats,
15
                 const LocalInfo::LocalInfo& local_info,
16
                 ProtobufMessage::ValidationContext& validation_context, Server::Instance& server)
17
10769
      : server_(server), main_thread_dispatcher_(main_thread_dispatcher), api_(api),
18
10769
        random_(api.randomGenerator()), stats_(stats), local_info_(local_info),
19
10769
        validation_context_(validation_context) {}
20

            
21
  // Config::XdsManager
22
  absl::Status initialize(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
23
                          Upstream::ClusterManager* cm) override;
24
  absl::Status
25
  initializeAdsConnections(const envoy::config::bootstrap::v3::Bootstrap& bootstrap) override;
26
  void startXdstpAdsMuxes() override;
27
  absl::StatusOr<SubscriptionPtr> subscribeToSingletonResource(
28
      absl::string_view resource_name, OptRef<const envoy::config::core::v3::ConfigSource> config,
29
      absl::string_view type_url, Stats::Scope& scope, SubscriptionCallbacks& callbacks,
30
      OpaqueResourceDecoderSharedPtr resource_decoder, const SubscriptionOptions& options) override;
31
12131
  ScopedResume pause(const std::string& type_url) override {
32
12131
    return pause(std::vector<std::string>{type_url});
33
12131
  }
34
  ScopedResume pause(const std::vector<std::string>& type_urls) override;
35
10717
  void shutdown() override { ads_mux_.reset(); }
36
  absl::Status
37
  setAdsConfigSource(const envoy::config::core::v3::ApiConfigSource& config_source) override;
38

            
39
37334
  Config::GrpcMuxSharedPtr adsMux() override { return ads_mux_; }
40
12593
  SubscriptionFactory& subscriptionFactory() override { return *subscription_factory_; }
41

            
42
private:
43
  class AuthorityData {
44
  public:
45
    AuthorityData(const envoy::config::core::v3::ConfigSource& config,
46
                  absl::flat_hash_set<std::string>&& authority_names, GrpcMuxSharedPtr&& grpc_mux)
47
137
        : config_(config), authority_names_(std::move(authority_names)),
48
137
          grpc_mux_(std::move(grpc_mux)) {}
49

            
50
    const envoy::config::core::v3::ConfigSource config_;
51
    // The set of authority names this config-source supports.
52
    // Note that only the `default_config_source` may have an empty list of authority names.
53
    absl::flat_hash_set<std::string> authority_names_;
54
    // The ADS gRPC mux to the server.
55
    Config::GrpcMuxSharedPtr grpc_mux_;
56
  };
57

            
58
  // Creates an authority based on a given config source.
59
  // Returns the new authority, or an error if one occurred.
60
  absl::StatusOr<AuthorityData>
61
  createAuthority(const envoy::config::core::v3::ConfigSource& config_source,
62
                  bool allow_no_authority_names);
63

            
64
  // Validates (syntactically) the config_source by doing the PGV validation.
65
  absl::Status validateAdsConfig(const envoy::config::core::v3::ApiConfigSource& config_source);
66

            
67
  /**
68
   * Replaces the current ADS mux with a new one based on the given config.
69
   * Assumes that the given ads_config is syntactically valid (according to the PGV constraints).
70
   * @param ads_config an ADS config source to use.
71
   * @return the status of the operation.
72
   */
73
  absl::Status replaceAdsMux(const envoy::config::core::v3::ApiConfigSource& ads_config);
74

            
75
  Server::Instance& server_;
76
  Event::Dispatcher& main_thread_dispatcher_;
77
  Api::Api& api_;
78
  Random::RandomGenerator& random_;
79
  Stats::Store& stats_;
80
  const LocalInfo::LocalInfo& local_info_;
81
  ProtobufMessage::ValidationContext& validation_context_;
82
  XdsResourcesDelegatePtr xds_resources_delegate_;
83
  XdsConfigTrackerPtr xds_config_tracker_;
84
  std::unique_ptr<SubscriptionFactoryImpl> subscription_factory_;
85
  // The cm_ will only be valid after the cluster-manager is initialized.
86
  // Note that this implies that the xDS-manager must be shut down properly
87
  // prior to the cluster-manager deletion.
88
  Upstream::ClusterManager* cm_;
89
  GrpcMuxSharedPtr ads_mux_;
90

            
91
  // Stores all authorities as configured in the bootstrap under config_sources.
92
  // It does not include the default config-source.
93
  std::vector<AuthorityData> authorities_;
94

            
95
  // The default authority that will be used for cases where the authority in a resource doesn't
96
  // exist, or doesn't match. This will only be populated if default_config_source
97
  // is defined in the bootstrap.
98
  std::unique_ptr<AuthorityData> default_authority_;
99
};
100

            
101
} // namespace Config
102
} // namespace Envoy