forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration_impl.cc
236 lines (199 loc) · 9.76 KB
/
configuration_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "server/configuration_impl.h"
#include <chrono>
#include <list>
#include <memory>
#include <string>
#include <vector>
#include "envoy/common/exception.h"
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/metrics/v3/stats.pb.h"
#include "envoy/config/trace/v3/http_tracer.pb.h"
#include "envoy/network/connection.h"
#include "envoy/runtime/runtime.h"
#include "envoy/server/instance.h"
#include "envoy/server/tracer_config.h"
#include "envoy/ssl/context_manager.h"
#include "common/common/assert.h"
#include "common/common/utility.h"
#include "common/config/runtime_utility.h"
#include "common/config/utility.h"
#include "common/network/socket_option_factory.h"
#include "common/protobuf/utility.h"
namespace Envoy {
namespace Server {
namespace Configuration {
bool FilterChainUtility::buildFilterChain(Network::FilterManager& filter_manager,
const std::vector<Network::FilterFactoryCb>& factories) {
for (const Network::FilterFactoryCb& factory : factories) {
factory(filter_manager);
}
return filter_manager.initializeReadFilters();
}
bool FilterChainUtility::buildFilterChain(
Network::ListenerFilterManager& filter_manager,
const std::vector<Network::ListenerFilterFactoryCb>& factories) {
for (const Network::ListenerFilterFactoryCb& factory : factories) {
factory(filter_manager);
}
return true;
}
void FilterChainUtility::buildUdpFilterChain(
Network::UdpListenerFilterManager& filter_manager, Network::UdpReadFilterCallbacks& callbacks,
const std::vector<Network::UdpListenerFilterFactoryCb>& factories) {
for (const Network::UdpListenerFilterFactoryCb& factory : factories) {
factory(filter_manager, callbacks);
}
}
StatsConfigImpl::StatsConfigImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
if (bootstrap.has_stats_flush_interval() &&
bootstrap.stats_flush_case() !=
envoy::config::bootstrap::v3::Bootstrap::STATS_FLUSH_NOT_SET) {
throw EnvoyException("Only one of stats_flush_interval or stats_flush_on_admin should be set!");
}
flush_interval_ =
std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(bootstrap, stats_flush_interval, 5000));
if (bootstrap.stats_flush_case() == envoy::config::bootstrap::v3::Bootstrap::kStatsFlushOnAdmin) {
flush_on_admin_ = bootstrap.stats_flush_on_admin();
}
}
void MainImpl::initialize(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
Instance& server,
Upstream::ClusterManagerFactory& cluster_manager_factory) {
// In order to support dynamic configuration of tracing providers,
// a former server-wide HttpTracer singleton has been replaced by
// an HttpTracer instance per "envoy.filters.network.http_connection_manager" filter.
// Tracing configuration as part of bootstrap config is still supported,
// however, it's become mandatory to process it prior to static Listeners.
// Otherwise, static Listeners will be configured in assumption that
// tracing configuration is missing from the bootstrap config.
initializeTracers(bootstrap.tracing(), server);
const auto& secrets = bootstrap.static_resources().secrets();
ENVOY_LOG(info, "loading {} static secret(s)", secrets.size());
for (ssize_t i = 0; i < secrets.size(); i++) {
ENVOY_LOG(debug, "static secret #{}: {}", i, secrets[i].name());
server.secretManager().addStaticSecret(secrets[i]);
}
ENVOY_LOG(info, "loading {} cluster(s)", bootstrap.static_resources().clusters().size());
cluster_manager_ = cluster_manager_factory.clusterManagerFromProto(bootstrap);
const auto& listeners = bootstrap.static_resources().listeners();
ENVOY_LOG(info, "loading {} listener(s)", listeners.size());
for (ssize_t i = 0; i < listeners.size(); i++) {
ENVOY_LOG(debug, "listener #{}:", i);
server.listenerManager().addOrUpdateListener(listeners[i], "", false);
}
initializeWatchdogs(bootstrap, server);
initializeStatsConfig(bootstrap, server);
}
void MainImpl::initializeStatsConfig(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
Instance& server) {
ENVOY_LOG(info, "loading stats configuration");
// stats_config_ should be set before populating the sinks so that it is available
// from the ServerFactoryContext when creating the stats sinks.
stats_config_ = std::make_unique<StatsConfigImpl>(bootstrap);
for (const envoy::config::metrics::v3::StatsSink& sink_object : bootstrap.stats_sinks()) {
// Generate factory and translate stats sink custom config.
auto& factory = Config::Utility::getAndCheckFactory<StatsSinkFactory>(sink_object);
ProtobufTypes::MessagePtr message = Config::Utility::translateToFactoryConfig(
sink_object, server.messageValidationContext().staticValidationVisitor(), factory);
stats_config_->addSink(factory.createStatsSink(*message, server.serverFactoryContext()));
}
}
void MainImpl::initializeTracers(const envoy::config::trace::v3::Tracing& configuration,
Instance& server) {
ENVOY_LOG(info, "loading tracing configuration");
// Default tracing configuration must be set prior to processing of static Listeners begins.
server.setDefaultTracingConfig(configuration);
if (!configuration.has_http()) {
return;
}
// Validating tracing configuration (minimally).
ENVOY_LOG(info, " validating default server-wide tracing driver: {}",
configuration.http().name());
// Now see if there is a factory that will accept the config.
auto& factory = Config::Utility::getAndCheckFactory<TracerFactory>(configuration.http());
ProtobufTypes::MessagePtr message = Config::Utility::translateToFactoryConfig(
configuration.http(), server.messageValidationContext().staticValidationVisitor(), factory);
// Notice that the actual HttpTracer instance will be created on demand
// in the context of "envoy.filters.network.http_connection_manager" filter.
// The side effect of this is that provider-specific configuration
// is no longer validated in this step.
}
void MainImpl::initializeWatchdogs(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
Instance& server) {
if (bootstrap.has_watchdog() && bootstrap.has_watchdogs()) {
throw EnvoyException("Only one of watchdog or watchdogs should be set!");
}
if (bootstrap.has_watchdog()) {
main_thread_watchdog_ = std::make_unique<WatchdogImpl>(bootstrap.watchdog(), server);
worker_watchdog_ = std::make_unique<WatchdogImpl>(bootstrap.watchdog(), server);
} else {
main_thread_watchdog_ =
std::make_unique<WatchdogImpl>(bootstrap.watchdogs().main_thread_watchdog(), server);
worker_watchdog_ =
std::make_unique<WatchdogImpl>(bootstrap.watchdogs().worker_watchdog(), server);
}
}
WatchdogImpl::WatchdogImpl(const envoy::config::bootstrap::v3::Watchdog& watchdog,
Instance& server) {
miss_timeout_ =
std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(watchdog, miss_timeout, 200));
megamiss_timeout_ =
std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(watchdog, megamiss_timeout, 1000));
uint64_t kill_timeout = PROTOBUF_GET_MS_OR_DEFAULT(watchdog, kill_timeout, 0);
const uint64_t max_kill_timeout_jitter =
PROTOBUF_GET_MS_OR_DEFAULT(watchdog, max_kill_timeout_jitter, 0);
// Adjust kill timeout if we have skew enabled.
if (kill_timeout > 0 && max_kill_timeout_jitter > 0) {
// Increments the kill timeout with a random value in (0, max_skew].
// We shouldn't have overflow issues due to the range of Duration.
// This won't be entirely uniform, depending on how large max_skew
// is relation to uint64.
kill_timeout += (server.api().randomGenerator().random() % max_kill_timeout_jitter) + 1;
}
kill_timeout_ = std::chrono::milliseconds(kill_timeout);
multikill_timeout_ =
std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(watchdog, multikill_timeout, 0));
multikill_threshold_ = PROTOBUF_PERCENT_TO_DOUBLE_OR_DEFAULT(watchdog, multikill_threshold, 0.0);
actions_ = watchdog.actions();
}
InitialImpl::InitialImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
const Options& options)
: enable_deprecated_v2_api_(options.bootstrapVersion() == 2u) {
const auto& admin = bootstrap.admin();
admin_.access_log_path_ = admin.access_log_path();
admin_.profile_path_ =
admin.profile_path().empty() ? "/var/log/envoy/envoy.prof" : admin.profile_path();
if (admin.has_address()) {
admin_.address_ = Network::Address::resolveProtoAddress(admin.address());
}
admin_.socket_options_ = std::make_shared<std::vector<Network::Socket::OptionConstSharedPtr>>();
if (!admin.socket_options().empty()) {
Network::Socket::appendOptions(
admin_.socket_options_,
Network::SocketOptionFactory::buildLiteralOptions(admin.socket_options()));
}
if (!bootstrap.flags_path().empty()) {
flags_path_ = bootstrap.flags_path();
}
if (bootstrap.has_layered_runtime()) {
layered_runtime_.MergeFrom(bootstrap.layered_runtime());
if (layered_runtime_.layers().empty()) {
layered_runtime_.add_layers()->mutable_admin_layer();
}
} else {
Config::translateRuntime(bootstrap.hidden_envoy_deprecated_runtime(), layered_runtime_);
}
if (enable_deprecated_v2_api_) {
auto* enabled_deprecated_v2_api_layer = layered_runtime_.add_layers();
enabled_deprecated_v2_api_layer->set_name("enabled_deprecated_v2_api (auto-injected)");
auto* static_layer = enabled_deprecated_v2_api_layer->mutable_static_layer();
ProtobufWkt::Value val;
val.set_bool_value(true);
(*static_layer
->mutable_fields())["envoy.test_only.broken_in_production.enable_deprecated_v2_api"] =
val;
}
}
} // namespace Configuration
} // namespace Server
} // namespace Envoy