1
#pragma once
2

            
3
#include "envoy/api/api.h"
4
#include "envoy/common/backoff_strategy.h"
5
#include "envoy/config/core/v3/config_source.pb.h"
6
#include "envoy/config/custom_config_validators.h"
7
#include "envoy/config/grpc_mux.h"
8
#include "envoy/config/subscription.h"
9
#include "envoy/config/typed_config.h"
10
#include "envoy/local_info/local_info.h"
11
#include "envoy/protobuf/message_validator.h"
12
#include "envoy/stats/scope.h"
13
#include "envoy/upstream/load_stats_reporter.h"
14

            
15
#include "xds/core/v3/resource_locator.pb.h"
16

            
17
namespace Envoy {
18

            
19
namespace Server {
20
class Instance;
21
} // namespace Server
22
namespace Grpc {
23
class RawAsyncClient;
24
} // namespace Grpc
25

            
26
namespace Config {
27
class XdsResourcesDelegate;
28
class XdsConfigTracker;
29

            
30
class SubscriptionFactory {
31
public:
32
93535
  virtual ~SubscriptionFactory() = default;
33

            
34
  static constexpr uint32_t RetryInitialDelayMs = 500;
35
  static constexpr uint32_t RetryMaxDelayMs = 30000; // Do not cross more than 30s
36

            
37
  /**
38
   * @return true if a config source comes from the local filesystem.
39
   */
40
  static bool
41
914
  isPathBasedConfigSource(envoy::config::core::v3::ConfigSource::ConfigSourceSpecifierCase type) {
42
914
    return type == envoy::config::core::v3::ConfigSource::ConfigSourceSpecifierCase::kPath ||
43
914
           type ==
44
913
               envoy::config::core::v3::ConfigSource::ConfigSourceSpecifierCase::kPathConfigSource;
45
914
  }
46

            
47
  /**
48
   * Subscription factory interface.
49
   *
50
   * @param config envoy::config::core::v3::ConfigSource to construct from.
51
   * @param type_url type URL for the resource being subscribed to.
52
   * @param scope stats scope for any stats tracked by the subscription.
53
   * @param callbacks the callbacks needed by all Subscription objects, to deliver config updates.
54
   *                  The callbacks must not result in the deletion of the Subscription object.
55
   * @param resource_decoder how incoming opaque resource objects are to be decoded.
56
   * @param options subscription options.
57
   *
58
   * @return SubscriptionPtr subscription object corresponding for config and type_url or error
59
   * status.
60
   */
61
  virtual absl::StatusOr<SubscriptionPtr> subscriptionFromConfigSource(
62
      const envoy::config::core::v3::ConfigSource& config, absl::string_view type_url,
63
      Stats::Scope& scope, SubscriptionCallbacks& callbacks,
64
      OpaqueResourceDecoderSharedPtr resource_decoder, const SubscriptionOptions& options) PURE;
65

            
66
  /**
67
   * Subscription factory for a given ADS grpc-mux.
68
   *
69
   * @param ads_grpc_mux the ADS GrpcMux this subscription should use.
70
   * @param config envoy::config::core::v3::ConfigSource to construct from.
71
   * @param type_url type URL for the resource being subscribed to.
72
   * @param scope stats scope for any stats tracked by the subscription.
73
   * @param callbacks the callbacks needed by all Subscription objects, to deliver config updates.
74
   *                  The callbacks must not result in the deletion of the Subscription object.
75
   * @param resource_decoder how incoming opaque resource objects are to be decoded.
76
   * @param options subscription options.
77
   *
78
   * @return SubscriptionPtr subscription object corresponding for config and type_url or error
79
   * status.
80
   */
81
  virtual absl::StatusOr<SubscriptionPtr> subscriptionOverAdsGrpcMux(
82
      GrpcMuxSharedPtr& ads_grpc_mux, const envoy::config::core::v3::ConfigSource& config,
83
      absl::string_view type_url, Stats::Scope& scope, SubscriptionCallbacks& callbacks,
84
      OpaqueResourceDecoderSharedPtr resource_decoder, const SubscriptionOptions& options) PURE;
85

            
86
  /**
87
   * Collection subscription factory interface for xDS-TP URLs.
88
   *
89
   * @param collection_locator collection resource locator.
90
   * @param config envoy::config::core::v3::ConfigSource for authority resolution.
91
   * @param type_url type URL for the resources inside the collection.
92
   * @param scope stats scope for any stats tracked by the subscription.
93
   * @param callbacks the callbacks needed by all [Collection]Subscription objects, to deliver
94
   *                  config updates. The callbacks must not result in the deletion of the
95
   *                  CollectionSubscription object.
96
   * @param resource_decoder how incoming opaque resource objects are to be decoded.
97
   *
98
   * @return SubscriptionPtr subscription object corresponding for collection_locator or error
99
   * status.
100
   */
101
  virtual absl::StatusOr<SubscriptionPtr>
102
  collectionSubscriptionFromUrl(const xds::core::v3::ResourceLocator& collection_locator,
103
                                const envoy::config::core::v3::ConfigSource& config,
104
                                absl::string_view type_url, Stats::Scope& scope,
105
                                SubscriptionCallbacks& callbacks,
106
                                OpaqueResourceDecoderSharedPtr resource_decoder) PURE;
107
};
108

            
109
// A factory class that individual config subscriptions can subclass to be factory-created.
110
class ConfigSubscriptionFactory : public Config::UntypedFactory {
111
public:
112
  struct SubscriptionData {
113
    const LocalInfo::LocalInfo& local_info_;
114
    Event::Dispatcher& dispatcher_;
115
    Upstream::ClusterManager& cm_;
116
    ProtobufMessage::ValidationVisitor& validation_visitor_;
117
    Api::Api& api_;
118
    const Server::Instance& server_;
119
    OptRef<XdsResourcesDelegate> xds_resources_delegate_;
120
    OptRef<XdsConfigTracker> xds_config_tracker_;
121

            
122
    const envoy::config::core::v3::ConfigSource& config_;
123
    absl::string_view type_url_;
124
    Stats::Scope& scope_;
125
    SubscriptionCallbacks& callbacks_;
126
    OpaqueResourceDecoderSharedPtr resource_decoder_;
127
    const SubscriptionOptions& options_;
128
    OptRef<const xds::core::v3::ResourceLocator> collection_locator_;
129
    SubscriptionStats stats_;
130
    // An optional ADS gRPC mux to be used. Must be provided if ADS
131
    // is used.
132
    GrpcMuxSharedPtr ads_grpc_mux_;
133
  };
134

            
135
4397
  std::string category() const override { return "envoy.config_subscription"; }
136
  virtual SubscriptionPtr create(SubscriptionData& data) PURE;
137
};
138

            
139
class MuxFactory : public Config::UntypedFactory {
140
public:
141
2509
  std::string category() const override { return "envoy.config_mux"; }
142
  virtual void shutdownAll() PURE;
143
  virtual std::shared_ptr<GrpcMux>
144
  create(std::shared_ptr<Grpc::RawAsyncClient>&& async_client,
145
         std::shared_ptr<Grpc::RawAsyncClient>&& async_failover_client,
146
         Event::Dispatcher& dispatcher, Random::RandomGenerator& random, Stats::Scope& scope,
147
         const envoy::config::core::v3::ApiConfigSource& ads_config,
148
         const LocalInfo::LocalInfo& local_info,
149
         std::unique_ptr<CustomConfigValidators>&& config_validators,
150
         BackOffStrategyPtr&& backoff_strategy, OptRef<XdsConfigTracker> xds_config_tracker,
151
         OptRef<XdsResourcesDelegate> xds_resources_delegate,
152
         std::function<std::unique_ptr<Upstream::LoadStatsReporter>()> load_stats_reporter_factory)
153
      PURE;
154
};
155

            
156
} // namespace Config
157
} // namespace Envoy