Skip to content

Commit

Permalink
golang filter: add VirtualClusterName getter (envoyproxy#28406)
Browse files Browse the repository at this point in the history
Add a new method to get matched virtual cluster name
Risk Level: Low
Testing: Integration
Docs Changes:
Release Notes:
Platform Specific Features:

Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored Jul 18, 2023
1 parent 004132a commit ccefe07
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contrib/golang/common/go/api/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ type StreamInfo interface {
UpstreamClusterName() (string, bool)
// FilterState return the filter state interface.
FilterState() FilterState
// VirtualClusterName returns the name of the virtual cluster which got matched
VirtualClusterName() (string, bool)
}

type StreamFilterCallbacks interface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (
ValueDownstreamRemoteAddress = 8
ValueUpstreamHostAddress = 9
ValueUpstreamClusterName = 10
ValueVirtualClusterName = 11
)

type httpCApiImpl struct{}
Expand Down
4 changes: 4 additions & 0 deletions contrib/golang/filters/http/source/go/pkg/http/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ func (s *streamInfo) UpstreamClusterName() (string, bool) {
return cAPI.HttpGetStringValue(unsafe.Pointer(s.request), ValueUpstreamClusterName)
}

func (s *streamInfo) VirtualClusterName() (string, bool) {
return cAPI.HttpGetStringValue(unsafe.Pointer(s.request), ValueVirtualClusterName)
}

type filterState struct {
request *httpRequest
}
Expand Down
6 changes: 6 additions & 0 deletions contrib/golang/filters/http/source/golang_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,12 @@ CAPIStatus Filter::getStringValue(int id, GoString* value_str) {
return CAPIStatus::CAPIValueNotFound;
}
break;
case EnvoyValue::VirtualClusterName:
if (!state.streamInfo().virtualClusterName().has_value()) {
return CAPIStatus::CAPIValueNotFound;
}
req_->strValue = state.streamInfo().virtualClusterName().value();
break;
default:
RELEASE_ASSERT(false, absl::StrCat("invalid string value id: ", id));
}
Expand Down
1 change: 1 addition & 0 deletions contrib/golang/filters/http/source/golang_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ enum class EnvoyValue {
DownstreamRemoteAddress,
UpstreamHostAddress,
UpstreamClusterName,
VirtualClusterName,
};

struct httpRequestInternal;
Expand Down
12 changes: 12 additions & 0 deletions contrib/golang/filters/http/test/golang_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ name: golang
hcm.mutable_route_config()->mutable_virtual_hosts(0)->mutable_routes(0)->set_name(
"test-route-name");
hcm.mutable_route_config()->mutable_virtual_hosts(0)->set_domains(0, domain);

auto* virtual_cluster =
hcm.mutable_route_config()->mutable_virtual_hosts(0)->add_virtual_clusters();
virtual_cluster->set_name("test_vcluster");
auto* headers = virtual_cluster->add_headers();
// use test_vcluster if presents
headers->set_name("existed-header");
headers->set_present_match(true);

hcm.mutable_route_config()
->mutable_virtual_hosts(0)
->mutable_routes(0)
Expand Down Expand Up @@ -372,6 +381,9 @@ name: golang
// check response attempt count in encode phase
EXPECT_EQ("1", getHeader(response->headers(), "rsp-attempt-count"));

// check virtual cluster name
EXPECT_EQ("test_vcluster", getHeader(response->headers(), "rsp-virtual-cluster-name"));

// verify response status
EXPECT_EQ("200", getHeader(response->headers(), "rsp-status"));

Expand Down
5 changes: 5 additions & 0 deletions contrib/golang/filters/http/test/test_data/basic/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ func (f *filter) encodeHeaders(header api.ResponseHeaderMap, endStream bool) api
header.Set("rsp-route-name", f.callbacks.StreamInfo().GetRouteName())
header.Set("rsp-filter-chain-name", f.callbacks.StreamInfo().FilterChainName())
header.Set("rsp-attempt-count", strconv.Itoa(int(f.callbacks.StreamInfo().AttemptCount())))
if name, ok := f.callbacks.StreamInfo().VirtualClusterName(); ok {
header.Set("rsp-virtual-cluster-name", name)
} else {
header.Set("rsp-virtual-cluster-name", "not found")
}

if f.panic == "encode-header" {
badcode()
Expand Down

0 comments on commit ccefe07

Please sign in to comment.