-
Notifications
You must be signed in to change notification settings - Fork 5.3k
extension: add test case #13915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
extension: add test case #13915
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
da57203
add per host cluster upstream extension in test
lambdai af7f280
Initial integration test
lambdai 1dca4f8
extract cluster and host metadata
lambdai e77b918
massage registry factory
lambdai 14e14cb
clean up
lambdai 4888b9f
address comment
lambdai c9e5869
the target in test doesn't need to be cc_extension
lambdai 74fce14
Merge remote-tracking branch 'origin/master' into smallextension
lambdai 118fe55
merge master and update api
lambdai e32f973
address test.cc
lambdai 4898a4c
remove config.cc
lambdai 06251c1
inherit HttpConnPool
lambdai 4ea3506
merge merge
lambdai dd7d5a8
inherit HttpUpstream to reduce complexity
lambdai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
test/integration/cluster_upstream_extension_integration_test.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #include "envoy/config/bootstrap/v3/bootstrap.pb.h" | ||
| #include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h" | ||
| #include "envoy/registry/registry.h" | ||
| #include "envoy/router/router.h" | ||
|
|
||
| #include "common/buffer/buffer_impl.h" | ||
|
|
||
| #include "test/integration/fake_upstream.h" | ||
| #include "test/integration/http_integration.h" | ||
| #include "test/integration/upstreams/per_host_upstream_config.h" | ||
| #include "test/test_common/registry.h" | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| namespace { | ||
| class ClusterUpstreamExtensionIntegrationTest | ||
| : public testing::TestWithParam<Network::Address::IpVersion>, | ||
| public HttpIntegrationTest { | ||
| public: | ||
| ClusterUpstreamExtensionIntegrationTest() | ||
| : HttpIntegrationTest(Http::CodecClient::Type::HTTP1, GetParam()) {} | ||
|
|
||
| void populateMetadataTestData(envoy::config::core::v3::Metadata& metadata, | ||
| const std::string& key1, const std::string& key2, | ||
| const std::string& value) { | ||
|
|
||
| ProtobufWkt::Struct struct_obj; | ||
| (*struct_obj.mutable_fields())[key2] = ValueUtil::stringValue(value); | ||
| (*metadata.mutable_filter_metadata())[key1] = struct_obj; | ||
| } | ||
|
|
||
| void initialize() override { | ||
| config_helper_.addConfigModifier([this](envoy::config::bootstrap::v3::Bootstrap& bootstrap) { | ||
| auto* cluster = bootstrap.mutable_static_resources()->mutable_clusters(0); | ||
| cluster->mutable_upstream_config()->set_name("envoy.filters.connection_pools.http.per_host"); | ||
| cluster->mutable_upstream_config()->mutable_typed_config(); | ||
| populateMetadataTestData(*cluster->mutable_metadata(), "foo", "bar", "cluster-value"); | ||
| populateMetadataTestData(*cluster->mutable_load_assignment() | ||
| ->mutable_endpoints(0) | ||
| ->mutable_lb_endpoints(0) | ||
| ->mutable_metadata(), | ||
| "foo", "bar", "host-value"); | ||
| }); | ||
| HttpIntegrationTest::initialize(); | ||
| } | ||
| PerHostGenericConnPoolFactory per_host_upstream_factory_; | ||
| }; | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(IpVersions, ClusterUpstreamExtensionIntegrationTest, | ||
| testing::ValuesIn(TestEnvironment::getIpVersionsForTest()), | ||
| TestUtility::ipTestParamsToString); | ||
|
|
||
| // This test verifies that cluster upstream extensions can fulfill the requirement that they rewrite | ||
| // http headers after cluster and host are selected. See | ||
| // https://github.com/envoyproxy/envoy/issues/12236 This test case should be rewritten once upstream | ||
| // http filters(https://github.com/envoyproxy/envoy/issues/10455) is landed. | ||
| TEST_P(ClusterUpstreamExtensionIntegrationTest, | ||
| VerifyRequestHeadersAreRewrittenByClusterAndHostMetadata) { | ||
lambdai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| initialize(); | ||
| Registry::InjectFactory<Router::GenericConnPoolFactory> registration(per_host_upstream_factory_); | ||
|
|
||
| codec_client_ = makeHttpConnection(lookupPort("http")); | ||
| auto response = sendRequestAndWaitForResponse( | ||
| default_request_headers_, 0, Http::TestResponseHeaderMapImpl{{":status", "200"}}, 0); | ||
| EXPECT_TRUE(upstream_request_->complete()); | ||
|
|
||
| { | ||
| const auto header_values = upstream_request_->headers().get(Http::LowerCaseString("X-foo")); | ||
| ASSERT_EQ(1, header_values.size()); | ||
| EXPECT_EQ("foo-common", header_values[0]->value().getStringView()); | ||
| } | ||
| { | ||
| const auto cluster_header_values = | ||
| upstream_request_->headers().get(Http::LowerCaseString("X-cluster-foo")); | ||
| ASSERT_EQ(1, cluster_header_values.size()); | ||
| EXPECT_EQ("cluster-value", cluster_header_values[0]->value().getStringView()); | ||
| } | ||
| { | ||
| const auto host_header_values = | ||
| upstream_request_->headers().get(Http::LowerCaseString("X-host-foo")); | ||
| ASSERT_EQ(1, host_header_values.size()); | ||
| EXPECT_EQ("host-value", host_header_values[0]->value().getStringView()); | ||
| } | ||
|
|
||
| response->waitForEndStream(); | ||
| ASSERT_TRUE(response->complete()); | ||
| EXPECT_EQ("200", response->headers().getStatusValue()); | ||
| } | ||
| } // namespace | ||
| } // namespace Envoy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| load( | ||
| "//bazel:envoy_build_system.bzl", | ||
| "envoy_cc_library", | ||
| "envoy_package", | ||
| ) | ||
|
|
||
| licenses(["notice"]) # Apache 2 | ||
|
|
||
| envoy_package() | ||
|
|
||
| envoy_cc_library( | ||
| name = "per_host_upstream_config", | ||
| srcs = [ | ||
| "per_host_upstream_config.h", | ||
| ], | ||
| deps = [ | ||
| "//include/envoy/http:codes_interface", | ||
| "//include/envoy/http:conn_pool_interface", | ||
| "//include/envoy/http:filter_interface", | ||
| "//include/envoy/upstream:cluster_manager_interface", | ||
| "//include/envoy/upstream:upstream_interface", | ||
| "//source/common/common:assert_lib", | ||
| "//source/common/http:header_map_lib", | ||
| "//source/common/http:headers_lib", | ||
| "//source/common/router:router_lib", | ||
| "//source/common/upstream:load_balancer_lib", | ||
| "//source/extensions/upstreams/http/http:upstream_request_lib", | ||
| "//source/extensions/upstreams/http/tcp:upstream_request_lib", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/http/conn_pool.h" | ||
| #include "envoy/http/metadata_interface.h" | ||
| #include "envoy/http/protocol.h" | ||
| #include "envoy/router/router.h" | ||
| #include "envoy/upstream/cluster_manager.h" | ||
| #include "envoy/upstream/host_description.h" | ||
|
|
||
| #include "common/http/header_map_impl.h" | ||
| #include "common/router/router.h" | ||
| #include "common/router/upstream_request.h" | ||
|
|
||
| #include "extensions/upstreams/http/http/upstream_request.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| namespace { | ||
|
|
||
| /** | ||
| * A helper to add header into header map from metadata. The added value is `metadata[key1][key2]`. | ||
| * | ||
| * @param header_map The mutable header map. | ||
| * @param header_name The target header entry in the `header_map`. | ||
| * @param metadata The source of header value. | ||
| * @param key1 The key to the value in metadata. | ||
| * @param key2 The second key to the value after `key1` is selected. | ||
| */ | ||
| void addHeader(Envoy::Http::RequestHeaderMap& header_map, absl::string_view header_name, | ||
| const envoy::config::core::v3::Metadata& metadata, absl::string_view key1, | ||
| absl::string_view key2) { | ||
| if (auto filter_metadata = metadata.filter_metadata().find(key1); | ||
| filter_metadata != metadata.filter_metadata().end()) { | ||
| const ProtobufWkt::Struct& data_struct = filter_metadata->second; | ||
| const auto& fields = data_struct.fields(); | ||
| if (auto iter = fields.find(key2); iter != fields.end()) { | ||
| if (iter->second.kind_case() == ProtobufWkt::Value::kStringValue) { | ||
| header_map.setCopy(Envoy::Http::LowerCaseString(std::string(header_name)), | ||
| iter->second.string_value()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } // namespace | ||
|
|
||
| // The http upstream to remember the host and encode the host metadata and cluster metadata into | ||
| // upstream http request. | ||
| class PerHostHttpUpstream : public Extensions::Upstreams::Http::Http::HttpUpstream { | ||
| public: | ||
| PerHostHttpUpstream(Router::UpstreamToDownstream& upstream_request, | ||
| Envoy::Http::RequestEncoder* encoder, | ||
| Upstream::HostDescriptionConstSharedPtr host) | ||
| : HttpUpstream(upstream_request, encoder), host_(host) {} | ||
|
|
||
| Http::Status encodeHeaders(const Envoy::Http::RequestHeaderMap& headers, | ||
| bool end_stream) override { | ||
| auto dup = Envoy::Http::RequestHeaderMapImpl::create(); | ||
| Envoy::Http::HeaderMapImpl::copyFrom(*dup, headers); | ||
| dup->setCopy(Envoy::Http::LowerCaseString("X-foo"), "foo-common"); | ||
| addHeader(*dup, "X-cluster-foo", host_->cluster().metadata(), "foo", "bar"); | ||
| if (host_->metadata() != nullptr) { | ||
| addHeader(*dup, "X-host-foo", *host_->metadata(), "foo", "bar"); | ||
| } | ||
| return HttpUpstream::encodeHeaders(*dup, end_stream); | ||
| } | ||
|
|
||
| private: | ||
| Upstream::HostDescriptionConstSharedPtr host_; | ||
| }; | ||
|
|
||
| class PerHostHttpConnPool : public Extensions::Upstreams::Http::Http::HttpConnPool { | ||
| public: | ||
| PerHostHttpConnPool(Upstream::ClusterManager& cm, bool is_connect, | ||
| const Router::RouteEntry& route_entry, | ||
| absl::optional<Envoy::Http::Protocol> downstream_protocol, | ||
| Upstream::LoadBalancerContext* ctx) | ||
| : HttpConnPool(cm, is_connect, route_entry, downstream_protocol, ctx) {} | ||
|
|
||
| void onPoolReady(Envoy::Http::RequestEncoder& callbacks_encoder, | ||
| Upstream::HostDescriptionConstSharedPtr host, | ||
| const StreamInfo::StreamInfo& info) override { | ||
| conn_pool_stream_handle_ = nullptr; | ||
| auto upstream = std::make_unique<PerHostHttpUpstream>(callbacks_->upstreamToDownstream(), | ||
| &callbacks_encoder, host); | ||
| callbacks_->onPoolReady(std::move(upstream), host, | ||
| callbacks_encoder.getStream().connectionLocalAddress(), info); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Config registration for the HttpConnPool. @see Router::GenericConnPoolFactory | ||
| */ | ||
| class PerHostGenericConnPoolFactory : public Router::GenericConnPoolFactory { | ||
| public: | ||
| std::string name() const override { return "envoy.filters.connection_pools.http.per_host"; } | ||
| std::string category() const override { return "envoy.upstreams"; } | ||
| Router::GenericConnPoolPtr | ||
| createGenericConnPool(Upstream::ClusterManager& cm, bool is_connect, | ||
| const Router::RouteEntry& route_entry, | ||
| absl::optional<Envoy::Http::Protocol> downstream_protocol, | ||
| Upstream::LoadBalancerContext* ctx) const override { | ||
| if (is_connect) { | ||
| // This example factory doesn't support terminating CONNECT stream. | ||
| return nullptr; | ||
| } | ||
| auto upstream_http_conn_pool = std::make_unique<PerHostHttpConnPool>( | ||
| cm, is_connect, route_entry, downstream_protocol, ctx); | ||
| return (upstream_http_conn_pool->valid() ? std::move(upstream_http_conn_pool) : nullptr); | ||
| } | ||
|
|
||
| ProtobufTypes::MessagePtr createEmptyConfigProto() override { | ||
| return std::make_unique<ProtobufWkt::Struct>(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace Envoy |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.