forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscoped_rds.h
293 lines (254 loc) · 12.3 KB
/
scoped_rds.h
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/core/v3/config_source.pb.h"
#include "envoy/config/core/v3/grpc_service.pb.h"
#include "envoy/config/route/v3/route.pb.h"
#include "envoy/config/route/v3/scoped_route.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "envoy/service/discovery/v3/discovery.pb.h"
#include "source/common/config/api_version.h"
#include "test/common/grpc/grpc_client_integration.h"
#include "test/config/v2_link_hacks.h"
#include "test/integration/http_integration.h"
#include "test/test_common/printers.h"
#include "test/test_common/resources.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace Envoy {
class ScopedRdsIntegrationTest : public HttpIntegrationTest,
public Grpc::DeltaSotwIntegrationParamTest {
protected:
struct FakeUpstreamInfo {
FakeHttpConnectionPtr connection_;
FakeUpstream* upstream_{};
absl::flat_hash_map<std::string, FakeStreamPtr> stream_by_resource_name_;
};
ScopedRdsIntegrationTest() : HttpIntegrationTest(Http::CodecType::HTTP1, ipVersion()) {
// TODO(ggreenway): add tag extraction rules.
// Missing stat tag-extraction rule for stat
// 'http.scoped_rds.foo-scoped-routes.grpc.srds_cluster.streams_closed_16' and stat_prefix
// 'srds_cluster'.
skip_tag_extraction_rule_check_ = true;
config_helper_.addRuntimeOverride("envoy.reloadable_features.unified_mux",
(sotwOrDelta() == Grpc::SotwOrDelta::UnifiedSotw ||
sotwOrDelta() == Grpc::SotwOrDelta::UnifiedDelta)
? "true"
: "false");
}
~ScopedRdsIntegrationTest() override { resetConnections(); }
void setupModifications() {
if (modifications_set_up_) {
return;
}
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
// Add another static cluster for sending HTTP requests.
auto* cluster_1 = bootstrap.mutable_static_resources()->add_clusters();
cluster_1->MergeFrom(bootstrap.static_resources().clusters()[0]);
cluster_1->set_name("cluster_1");
// Add the static cluster to serve SRDS.
auto* scoped_rds_cluster = bootstrap.mutable_static_resources()->add_clusters();
scoped_rds_cluster->MergeFrom(bootstrap.static_resources().clusters()[0]);
scoped_rds_cluster->set_name("srds_cluster");
ConfigHelper::setHttp2(*scoped_rds_cluster);
// Add the static cluster to serve RDS.
auto* rds_cluster = bootstrap.mutable_static_resources()->add_clusters();
rds_cluster->MergeFrom(bootstrap.static_resources().clusters()[0]);
rds_cluster->set_name("rds_cluster");
ConfigHelper::setHttp2(*rds_cluster);
});
config_helper_.addConfigModifier(
[this](
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
http_connection_manager) {
const std::string& scope_key_builder_config_yaml = R"EOF(
fragments:
- header_value_extractor:
name: Addr
element_separator: ;
element:
key: x-foo-key
separator: =
)EOF";
envoy::extensions::filters::network::http_connection_manager::v3::ScopedRoutes::
ScopeKeyBuilder scope_key_builder;
TestUtility::loadFromYaml(scope_key_builder_config_yaml, scope_key_builder);
auto* scoped_routes = http_connection_manager.mutable_scoped_routes();
scoped_routes->set_name(srds_config_name_);
*scoped_routes->mutable_scope_key_builder() = scope_key_builder;
// Set resource api version for rds.
envoy::config::core::v3::ConfigSource* rds_config_source =
scoped_routes->mutable_rds_config_source();
rds_config_source->set_resource_api_version(envoy::config::core::v3::ApiVersion::V3);
// Set transport api version for rds.
envoy::config::core::v3::ApiConfigSource* rds_api_config_source =
rds_config_source->mutable_api_config_source();
rds_api_config_source->set_transport_api_version(envoy::config::core::v3::ApiVersion::V3);
// Add grpc service for rds.
rds_api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
envoy::config::core::v3::GrpcService* grpc_service =
rds_api_config_source->add_grpc_services();
setGrpcService(*grpc_service, "rds_cluster", getRdsFakeUpstream().localAddress());
// Set resource api version for scoped rds.
envoy::config::core::v3::ConfigSource* srds_config_source =
scoped_routes->mutable_scoped_rds()->mutable_scoped_rds_config_source();
srds_config_source->set_resource_api_version(envoy::config::core::v3::ApiVersion::V3);
if (!srds_resources_locator_.empty()) {
scoped_routes->mutable_scoped_rds()->set_srds_resources_locator(
srds_resources_locator_);
}
// Set Transport api version for scoped_rds.
envoy::config::core::v3::ApiConfigSource* srds_api_config_source =
srds_config_source->mutable_api_config_source();
srds_api_config_source->set_transport_api_version(
envoy::config::core::v3::ApiVersion::V3);
// Add grpc service for scoped rds.
if (isDelta()) {
srds_api_config_source->set_api_type(
envoy::config::core::v3::ApiConfigSource::DELTA_GRPC);
} else {
srds_api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
}
srds_api_config_source->set_transport_api_version(
envoy::config::core::v3::ApiVersion::V3);
grpc_service = srds_api_config_source->add_grpc_services();
setGrpcService(*grpc_service, "srds_cluster", getScopedRdsFakeUpstream().localAddress());
});
modifications_set_up_ = true;
}
void initialize() override {
// Setup two upstream hosts, one for each cluster.
setUpstreamCount(2);
setupModifications();
HttpIntegrationTest::initialize();
}
void createUpstreams() override {
HttpIntegrationTest::createUpstreams();
// Create the SRDS upstream.
srds_upstream_idx_ = fake_upstreams_.size();
addFakeUpstream(Http::CodecType::HTTP2);
// Create the RDS upstream.
rds_upstream_idx_ = fake_upstreams_.size();
addFakeUpstream(Http::CodecType::HTTP2);
}
void resetFakeUpstreamInfo(FakeUpstreamInfo* upstream_info) {
if (upstream_info->upstream_ == nullptr) {
return;
}
AssertionResult result = upstream_info->connection_->close();
RELEASE_ASSERT(result, result.message());
result = upstream_info->connection_->waitForDisconnect();
RELEASE_ASSERT(result, result.message());
upstream_info->connection_.reset();
}
void resetConnections() {
if (rds_upstream_info_.upstream_ != nullptr) {
resetFakeUpstreamInfo(&rds_upstream_info_);
}
resetFakeUpstreamInfo(&scoped_rds_upstream_info_);
}
FakeUpstream& getRdsFakeUpstream() const {
ASSERT(rds_upstream_idx_ < fake_upstreams_.size());
return *fake_upstreams_[rds_upstream_idx_];
}
FakeUpstream& getScopedRdsFakeUpstream() const {
ASSERT(srds_upstream_idx_ < fake_upstreams_.size());
return *fake_upstreams_[srds_upstream_idx_];
}
void createStream(FakeUpstreamInfo* upstream_info, FakeUpstream& upstream,
const std::string& resource_name) {
if (upstream_info->upstream_ == nullptr) {
// bind upstream if not yet.
upstream_info->upstream_ = &upstream;
AssertionResult result =
upstream_info->upstream_->waitForHttpConnection(*dispatcher_, upstream_info->connection_);
RELEASE_ASSERT(result, result.message());
}
if (!upstream_info->stream_by_resource_name_.try_emplace(resource_name, nullptr).second) {
RELEASE_ASSERT(false,
fmt::format("stream with resource name '{}' already exists!", resource_name));
}
auto result = upstream_info->connection_->waitForNewStream(
*dispatcher_, upstream_info->stream_by_resource_name_[resource_name]);
RELEASE_ASSERT(result, result.message());
upstream_info->stream_by_resource_name_[resource_name]->startGrpcStream();
}
void createRdsStream(const std::string& resource_name) {
createStream(&rds_upstream_info_, getRdsFakeUpstream(), resource_name);
}
void createScopedRdsStream() {
createStream(&scoped_rds_upstream_info_, getScopedRdsFakeUpstream(), srds_config_name_);
}
void sendRdsResponse(const std::string& route_config, const std::string& version) {
envoy::service::discovery::v3::DiscoveryResponse response;
std::string route_conguration_type_url =
"type.googleapis.com/envoy.config.route.v3.RouteConfiguration";
response.set_version_info(version);
response.set_type_url(route_conguration_type_url);
auto route_configuration =
TestUtility::parseYaml<envoy::config::route::v3::RouteConfiguration>(route_config);
response.add_resources()->PackFrom(route_configuration);
ASSERT(rds_upstream_info_.stream_by_resource_name_[route_configuration.name()] != nullptr);
rds_upstream_info_.stream_by_resource_name_[route_configuration.name()]->sendGrpcMessage(
response);
}
void sendSrdsResponse(const std::vector<std::string>& sotw_list,
const std::vector<std::string>& to_add_list,
const std::vector<std::string>& to_delete_list,
const std::string& version) {
if (isDelta()) {
sendDeltaScopedRdsResponse(to_add_list, to_delete_list, version);
} else {
sendSotwScopedRdsResponse(sotw_list, version);
}
}
void sendDeltaScopedRdsResponse(const std::vector<std::string>& to_add_list,
const std::vector<std::string>& to_delete_list,
const std::string& version) {
ASSERT(scoped_rds_upstream_info_.stream_by_resource_name_[srds_config_name_] != nullptr);
std::string scoped_route_configuration_type_url =
"type.googleapis.com/envoy.config.route.v3.ScopedRouteConfiguration";
envoy::service::discovery::v3::DeltaDiscoveryResponse response;
response.set_system_version_info(version);
response.set_type_url(scoped_route_configuration_type_url);
for (const auto& scope_name : to_delete_list) {
*response.add_removed_resources() = scope_name;
}
for (const auto& resource_proto : to_add_list) {
envoy::config::route::v3::ScopedRouteConfiguration scoped_route_proto;
TestUtility::loadFromYaml(resource_proto, scoped_route_proto);
auto resource = response.add_resources();
resource->set_name(scoped_route_proto.name());
resource->set_version(version);
resource->mutable_resource()->PackFrom(scoped_route_proto);
}
scoped_rds_upstream_info_.stream_by_resource_name_[srds_config_name_]->sendGrpcMessage(
response);
}
void sendSotwScopedRdsResponse(const std::vector<std::string>& resource_protos,
const std::string& version) {
ASSERT(scoped_rds_upstream_info_.stream_by_resource_name_[srds_config_name_] != nullptr);
std::string scoped_route_configuration_type_url =
"type.googleapis.com/envoy.config.route.v3.ScopedRouteConfiguration";
envoy::service::discovery::v3::DiscoveryResponse response;
response.set_version_info(version);
response.set_type_url(scoped_route_configuration_type_url);
for (const auto& resource_proto : resource_protos) {
envoy::config::route::v3::ScopedRouteConfiguration scoped_route_proto;
TestUtility::loadFromYaml(resource_proto, scoped_route_proto);
response.add_resources()->PackFrom(scoped_route_proto);
}
scoped_rds_upstream_info_.stream_by_resource_name_[srds_config_name_]->sendGrpcMessage(
response);
}
bool isDelta() {
return sotwOrDelta() == Grpc::SotwOrDelta::Delta ||
sotwOrDelta() == Grpc::SotwOrDelta::UnifiedDelta;
}
const std::string srds_config_name_{"foo-scoped-routes"};
std::size_t rds_upstream_idx_ = -1;
std::size_t srds_upstream_idx_ = -1;
FakeUpstreamInfo scoped_rds_upstream_info_;
FakeUpstreamInfo rds_upstream_info_;
std::string srds_resources_locator_;
bool modifications_set_up_ = false;
};
} // namespace Envoy