Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/common/config/datasource.h
Line
Count
Source
1
#pragma once
2
3
#include "envoy/api/api.h"
4
#include "envoy/common/random_generator.h"
5
#include "envoy/config/core/v3/base.pb.h"
6
#include "envoy/event/deferred_deletable.h"
7
#include "envoy/init/manager.h"
8
#include "envoy/thread_local/thread_local.h"
9
#include "envoy/upstream/cluster_manager.h"
10
11
#include "source/common/common/backoff_strategy.h"
12
#include "source/common/common/empty_string.h"
13
#include "source/common/common/enum_to_int.h"
14
#include "source/common/config/remote_data_fetcher.h"
15
#include "source/common/init/target_impl.h"
16
17
#include "absl/types/optional.h"
18
19
namespace Envoy {
20
namespace Config {
21
namespace DataSource {
22
23
class DataSourceProvider;
24
25
using ProtoDataSource = envoy::config::core::v3::DataSource;
26
using ProtoWatchedDirectory = envoy::config::core::v3::WatchedDirectory;
27
using DataSourceProviderPtr = std::unique_ptr<DataSourceProvider>;
28
29
/**
30
 * Read contents of the DataSource.
31
 * @param source data source.
32
 * @param allow_empty return an empty string if no DataSource case is specified.
33
 * @param api reference to the Api.
34
 * @param max_size max size limit of file to read, default 0 means no limit, and if the file data
35
 * would exceed the limit, it will return an error status.
36
 * @return std::string with DataSource contents. or an error status if no DataSource case is
37
 * specified and !allow_empty.
38
 */
39
absl::StatusOr<std::string> read(const envoy::config::core::v3::DataSource& source,
40
                                 bool allow_empty, Api::Api& api, uint64_t max_size = 0);
41
42
/**
43
 * @param source data source.
44
 * @return absl::optional<std::string> path to DataSource if a filename, otherwise absl::nullopt.
45
 */
46
absl::optional<std::string> getPath(const envoy::config::core::v3::DataSource& source);
47
48
class DynamicData {
49
public:
50
  struct ThreadLocalData : public ThreadLocal::ThreadLocalObject {
51
1
    ThreadLocalData(std::shared_ptr<std::string> data) : data_(std::move(data)) {}
52
    std::shared_ptr<std::string> data_;
53
  };
54
55
1
  DynamicData(DynamicData&&) = default;
56
  DynamicData(Event::Dispatcher& main_dispatcher, ThreadLocal::TypedSlotPtr<ThreadLocalData> slot,
57
              Filesystem::WatcherPtr watcher);
58
  ~DynamicData();
59
60
  const std::string& data() const;
61
62
private:
63
  Event::Dispatcher& dispatcher_;
64
  ThreadLocal::TypedSlotPtr<ThreadLocalData> slot_;
65
  Filesystem::WatcherPtr watcher_;
66
};
67
68
/**
69
 * DataSourceProvider provides a way to get the DataSource contents and watch the possible
70
 * content changes. The watch only works for filename-based DataSource and watched directory
71
 * is provided explicitly.
72
 *
73
 * NOTE: This should only be used when the envoy.config.core.v3.DataSource is necessary and
74
 * file watch is required.
75
 */
76
class DataSourceProvider {
77
public:
78
  /**
79
   * Create a DataSourceProvider from a DataSource.
80
   * @param source data source.
81
   * @param main_dispatcher reference to the main dispatcher.
82
   * @param tls reference to the thread local slot allocator.
83
   * @param api reference to the Api.
84
   * @param allow_empty return an empty string if no DataSource case is specified.
85
   * @param max_size max size limit of file to read, default 0 means no limit.
86
   * @return absl::StatusOr<DataSourceProvider> with DataSource contents. or an error
87
   * status if any error occurs.
88
   * NOTE: If file watch is enabled and the new file content does not meet the
89
   * requirements (allow_empty, max_size), the provider will keep the old content.
90
   */
91
  static absl::StatusOr<DataSourceProviderPtr>
92
  create(const ProtoDataSource& source, Event::Dispatcher& main_dispatcher,
93
         ThreadLocal::SlotAllocator& tls, Api::Api& api, bool allow_empty, uint64_t max_size = 0);
94
95
  const std::string& data() const;
96
97
private:
98
2.89k
  DataSourceProvider(std::string&& data) : data_(std::move(data)) {}
99
1
  DataSourceProvider(DynamicData&& data) : data_(std::move(data)) {}
100
101
  absl::variant<std::string, DynamicData> data_;
102
};
103
104
} // namespace DataSource
105
} // namespace Config
106
} // namespace Envoy