Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/root/version_history/current.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ New Features
* ext_authz: added :ref:`query_parameters_to_set <envoy_v3_api_field_service.auth.v3.OkHttpResponse.query_parameters_to_set>` and :ref:`query_parameters_to_remove <envoy_v3_api_field_service.auth.v3.OkHttpResponse.query_parameters_to_remove>` for adding and removing query string parameters when using a gRPC authorization server.
* http: added support for :ref:`retriable health check status codes <envoy_v3_api_field_config.core.v3.HealthCheck.HttpHealthCheck.retriable_statuses>`.
* thrift_proxy: add upstream response zone metrics in the form ``cluster.cluster_name.zone.local_zone.upstream_zone.thrift.upstream_resp_success``.
* thrift_proxy: support subset lb when using request or route metadata.
* upstream: added the ability to :ref:`configure max connection duration <envoy_v3_api_field_config.core.v3.HttpProtocolOptions.max_connection_duration>` for upstream clusters.
* vcl_socket_interface: added VCL socket interface extension for fd.io VPP integration to :ref:`contrib images <install_contrib>`. This can be enabled via :ref:`VCL <envoy_v3_api_msg_extensions.vcl.v3alpha.VclSocketInterface>` configuration.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "envoy/upstream/load_balancer.h"

#include "source/common/http/header_utility.h"
#include "source/common/router/metadatamatchcriteria_impl.h"
#include "source/common/upstream/load_balancer_impl.h"
#include "source/extensions/filters/network/thrift_proxy/conn_manager.h"
#include "source/extensions/filters/network/thrift_proxy/filters/filter.h"
Expand Down Expand Up @@ -266,7 +267,25 @@ class Router : public Tcp::ConnectionPool::UpstreamCallbacks,
// Upstream::LoadBalancerContext
const Network::Connection* downstreamConnection() const override;
const Envoy::Router::MetadataMatchCriteria* metadataMatchCriteria() override {
return route_entry_ ? route_entry_->metadataMatchCriteria() : nullptr;
const Envoy::Router::MetadataMatchCriteria* route_criteria =
(route_entry_ != nullptr) ? route_entry_->metadataMatchCriteria() : nullptr;

// Support getting metadata match criteria from thrift request.
const auto& request_metadata = callbacks_->streamInfo().dynamicMetadata().filter_metadata();
const auto filter_it = request_metadata.find(Envoy::Config::MetadataFilters::get().ENVOY_LB);

if (filter_it == request_metadata.end()) {
return route_criteria;
}

if (route_criteria != nullptr) {
metadata_match_criteria_ = route_criteria->mergeMatchCriteria(filter_it->second);
} else {
metadata_match_criteria_ =
std::make_unique<Envoy::Router::MetadataMatchCriteriaImpl>(filter_it->second);
}

return metadata_match_criteria_.get();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can simplify this a bit:

    if (filter_it == request_metadata.end()) {
      return route_criteria;
    }

    metadata_match_criteria_ = route_criteria != nullptr ? route_criteria->mergeMatchCriteria(filter_it->second) : std::make_unique<Envoy::Router::MetadataMatchCriteriaImpl>(filter_it->second);
    return metadata_match_criteria_.get();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using ifelse is better?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx for review
using ifelse is better?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks great -- I am just a fan of the ternary operator 😁.

}

// Tcp::ConnectionPool::UpstreamCallbacks
Expand All @@ -282,6 +301,7 @@ class Router : public Tcp::ConnectionPool::UpstreamCallbacks,
std::unique_ptr<UpstreamResponseCallbacksImpl> upstream_response_callbacks_{};
RouteConstSharedPtr route_{};
const RouteEntry* route_entry_{};
Envoy::Router::MetadataMatchCriteriaConstPtr metadata_match_criteria_;

std::unique_ptr<UpstreamRequest> upstream_request_;
Buffer::OwnedImpl upstream_request_buffer_;
Expand Down
126 changes: 126 additions & 0 deletions test/extensions/filters/network/thrift_proxy/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,96 @@ class ThriftRouterTestBase {
metadata_->setSequenceId(sequence_id);
}

void verifyMetadataMatchCriteriaFromRequest(bool route_entry_has_match) {
ProtobufWkt::Struct request_struct;
ProtobufWkt::Value val;

// Populate metadata like StreamInfo.setDynamicMetadata() would.
auto& fields_map = *request_struct.mutable_fields();
val.set_string_value("v3.1");
fields_map["version"] = val;
val.set_string_value("devel");
fields_map["stage"] = val;
val.set_string_value("1");
fields_map["xkey_in_request"] = val;
(*callbacks_.stream_info_.metadata_
.mutable_filter_metadata())[Envoy::Config::MetadataFilters::get().ENVOY_LB] =
request_struct;

// Populate route entry's metadata which will be overridden.
val.set_string_value("v3.0");
fields_map = *request_struct.mutable_fields();
fields_map["version"] = val;
fields_map.erase("xkey_in_request");
Envoy::Router::MetadataMatchCriteriaImpl route_entry_matches(request_struct);

if (route_entry_has_match) {
ON_CALL(route_entry_, metadataMatchCriteria()).WillByDefault(Return(&route_entry_matches));
} else {
ON_CALL(route_entry_, metadataMatchCriteria()).WillByDefault(Return(nullptr));
}

auto match = router_->metadataMatchCriteria()->metadataMatchCriteria();
EXPECT_EQ(match.size(), 3);
auto it = match.begin();

// Note: metadataMatchCriteria() keeps its entries sorted, so the order for checks
// below matters.

// `stage` was only set by the request, not by the route entry.
EXPECT_EQ((*it)->name(), "stage");
EXPECT_EQ((*it)->value().value().string_value(), "devel");
it++;

// `version` should be what came from the request and override the route entry's.
EXPECT_EQ((*it)->name(), "version");
EXPECT_EQ((*it)->value().value().string_value(), "v3.1");
it++;

// `xkey_in_request` was only set by the request
EXPECT_EQ((*it)->name(), "xkey_in_request");
EXPECT_EQ((*it)->value().value().string_value(), "1");
}

void verifyMetadataMatchCriteriaFromRoute(bool route_entry_has_match) {
ProtobufWkt::Struct route_struct;
ProtobufWkt::Value val;

auto& fields_map = *route_struct.mutable_fields();
val.set_string_value("v3.1");
fields_map["version"] = val;
val.set_string_value("devel");
fields_map["stage"] = val;

Envoy::Router::MetadataMatchCriteriaImpl route_entry_matches(route_struct);

if (route_entry_has_match) {
ON_CALL(route_entry_, metadataMatchCriteria()).WillByDefault(Return(&route_entry_matches));

EXPECT_NE(nullptr, router_->metadataMatchCriteria());
auto match = router_->metadataMatchCriteria()->metadataMatchCriteria();
EXPECT_EQ(match.size(), 2);
auto it = match.begin();

// Note: metadataMatchCriteria() keeps its entries sorted, so the order for checks
// below matters.

// `stage` was set by the route entry.
EXPECT_EQ((*it)->name(), "stage");
EXPECT_EQ((*it)->value().value().string_value(), "devel");
it++;

// `version` was set by the route entry.
EXPECT_EQ((*it)->name(), "version");
EXPECT_EQ((*it)->value().value().string_value(), "v3.1");
} else {
ON_CALL(callbacks_.route_->route_entry_, metadataMatchCriteria())
.WillByDefault(Return(nullptr));

EXPECT_EQ(nullptr, router_->metadataMatchCriteria());
}
}

void initializeUpstreamZone() {
upstream_locality_.set_zone("other_zone_name");
ON_CALL(*context_.cluster_manager_.thread_local_cluster_.tcp_conn_pool_.host_, locality())
Expand Down Expand Up @@ -706,6 +796,42 @@ TEST_F(ThriftRouterTest, NoCluster) {
EXPECT_EQ(1U, context_.scope().counterFromString("test.unknown_cluster").value());
}

// Test the case where both dynamic metadata match criteria
// and route metadata match criteria is not empty.
TEST_F(ThriftRouterTest, MetadataMatchCriteriaFromRequest) {
initializeRouter();
initializeMetadata(MessageType::Call);

verifyMetadataMatchCriteriaFromRequest(true);
}

// Test the case where route metadata match criteria is empty
// but with non-empty dynamic metadata match criteria.
TEST_F(ThriftRouterTest, MetadataMatchCriteriaFromRequestNoRouteEntryMatch) {
initializeRouter();
initializeMetadata(MessageType::Call);

verifyMetadataMatchCriteriaFromRequest(false);
}

// Test the case where dynamic metadata match criteria is empty
// but with non-empty route metadata match criteria.
TEST_F(ThriftRouterTest, MetadataMatchCriteriaFromRoute) {
initializeRouter();
startRequest(MessageType::Call);

verifyMetadataMatchCriteriaFromRoute(true);
}

// Test the case where both dynamic metadata match criteria
// and route metadata match criteria is empty.
TEST_F(ThriftRouterTest, MetadataMatchCriteriaFromRouteNoRouteEntryMatch) {
initializeRouter();
startRequest(MessageType::Call);

verifyMetadataMatchCriteriaFromRoute(false);
}

TEST_F(ThriftRouterTest, ClusterMaintenanceMode) {
initializeRouter();
initializeMetadata(MessageType::Call);
Expand Down