Skip to content

Commit e92c5e5

Browse files
committed
review: implement Cluster.endpoints as LocalityLbEndpoints
Signed-off-by: Dhi Aurrahman <[email protected]>
1 parent 8b2ee7f commit e92c5e5

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed

source/common/upstream/logical_dns_cluster.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ LogicalDnsCluster::LogicalDnsCluster(const envoy::api::v2::Cluster& cluster,
4545
NOT_REACHED;
4646
}
4747

48-
const auto& socket_address = endpoints_[0].address().socket_address();
48+
const envoy::api::v2::endpoint::Endpoint& endpoint = endpoints_[0].endpoint();
49+
const envoy::api::v2::core::SocketAddress& socket_address = endpoint.address().socket_address();
4950
dns_url_ = fmt::format("tcp://{}:{}", socket_address.address(), socket_address.port_value());
5051
hostname_ = Network::Utility::hostFromTcpUrl(dns_url_);
5152
Network::Utility::portFromTcpUrl(dns_url_);
5253

53-
health_check_config_ = endpoints_[0].health_check_config();
54+
health_check_config_ = endpoint.health_check_config();
5455

5556
tls_->set([](Event::Dispatcher&) -> ThreadLocal::ThreadLocalObjectSharedPtr {
5657
return std::make_shared<PerThreadCurrentHostData>();

source/common/upstream/upstream_impl.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ ClusterImplBase::ClusterImplBase(const envoy::api::v2::Cluster& cluster,
415415
Ssl::ContextManager& ssl_context_manager, bool added_via_api)
416416
: runtime_(runtime), info_(new ClusterInfoImpl(cluster, bind_config, runtime, stats,
417417
ssl_context_manager, added_via_api)),
418-
endpoints_(cluster.endpoints()) {
418+
endpoints_(cluster.endpoints().lb_endpoints()) {
419419
// Create the default (empty) priority set before registering callbacks to
420420
// avoid getting an update the first time it is accessed.
421421
priority_set_.getOrCreateHostSet(0);
@@ -447,8 +447,8 @@ void ClusterImplBase::translateClusterHosts(
447447

448448
endpoints_.Reserve(hosts.size());
449449
for (const envoy::api::v2::core::Address& host : hosts) {
450-
envoy::api::v2::endpoint::Endpoint* endpoint = endpoints_.Add();
451-
endpoint->mutable_address()->CopyFrom(host);
450+
envoy::api::v2::endpoint::LbEndpoint* lb_endpoint = endpoints_.Add();
451+
lb_endpoint->mutable_endpoint()->mutable_address()->CopyFrom(host);
452452
}
453453
}
454454

@@ -641,10 +641,11 @@ StaticClusterImpl::StaticClusterImpl(const envoy::api::v2::Cluster& cluster,
641641
bool added_via_api)
642642
: ClusterImplBase(cluster, cm.bindConfig(), runtime, stats, ssl_context_manager, added_via_api),
643643
initial_hosts_(new HostVector()) {
644-
for (const auto& endpoint : endpoints_) {
645-
const auto& host = endpoint.address();
644+
for (const envoy::api::v2::endpoint::LbEndpoint& lb_endpoint : endpoints_) {
645+
const envoy::api::v2::endpoint::Endpoint& endpoint = lb_endpoint.endpoint();
646646
initial_hosts_->emplace_back(HostSharedPtr{new HostImpl(
647-
info_, "", resolveProtoAddress(host), envoy::api::v2::core::Metadata::default_instance(), 1,
647+
info_, EMPTY_STRING, resolveProtoAddress(endpoint.address()),
648+
envoy::api::v2::core::Metadata::default_instance(), 1,
648649
envoy::api::v2::core::Locality().default_instance(), endpoint.health_check_config())});
649650
}
650651
}
@@ -807,13 +808,13 @@ StrictDnsClusterImpl::StrictDnsClusterImpl(const envoy::api::v2::Cluster& cluste
807808
NOT_REACHED;
808809
}
809810

810-
for (const auto& endpoint : endpoints_) {
811-
const auto& host = endpoint.address();
812-
resolve_targets_.emplace_back(
813-
new ResolveTarget(*this, dispatcher,
814-
fmt::format("tcp://{}:{}", host.socket_address().address(),
815-
host.socket_address().port_value()),
816-
endpoint.health_check_config()));
811+
for (const envoy::api::v2::endpoint::LbEndpoint& lb_endpoint : endpoints_) {
812+
const envoy::api::v2::core::SocketAddress& socket_address =
813+
lb_endpoint.endpoint().address().socket_address();
814+
resolve_targets_.emplace_back(new ResolveTarget(
815+
*this, dispatcher,
816+
fmt::format("tcp://{}:{}", socket_address.address(), socket_address.port_value()),
817+
lb_endpoint.endpoint().health_check_config()));
817818
}
818819
}
819820

source/common/upstream/upstream_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class ClusterImplBase : public Cluster, protected Logger::Loggable<Logger::Id::u
477477

478478
protected:
479479
PrioritySetImpl priority_set_;
480-
Protobuf::RepeatedPtrField<envoy::api::v2::endpoint::Endpoint> endpoints_;
480+
Protobuf::RepeatedPtrField<envoy::api::v2::endpoint::LbEndpoint> endpoints_;
481481

482482
private:
483483
void finishInitialization();

test/common/upstream/upstream_impl_test.cc

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,22 @@ TEST(StrictDnsClusterImplTest, BasicEndpoints) {
325325
connect_timeout: 0.25s
326326
lb_policy: ROUND_ROBIN
327327
endpoints:
328-
- address:
329-
socket_address:
330-
address: localhost1
331-
port_value: 11001
332-
health_check_config:
333-
port_value: 8000
334-
- address:
335-
socket_address:
336-
address: localhost2
337-
port_value: 11002
338-
health_check_config:
339-
port_value: 8000
328+
lb_endpoints:
329+
- endpoint:
330+
address:
331+
socket_address:
332+
address: localhost1
333+
port_value: 11001
334+
health_check_config:
335+
port_value: 8000
336+
337+
- endpoint:
338+
address:
339+
socket_address:
340+
address: localhost2
341+
port_value: 11002
342+
health_check_config:
343+
port_value: 8000
340344
)EOF";
341345

342346
NiceMock<MockClusterManager> cm;
@@ -492,12 +496,14 @@ TEST(StaticClusterImplTest, StaticEndpoints) {
492496
type: STATIC
493497
lb_policy: ROUND_ROBIN
494498
endpoints:
495-
- address:
496-
socket_address:
497-
address: 10.0.0.1
498-
port_value: 443
499-
health_check_config:
500-
port_value: 8000
499+
lb_endpoints:
500+
- endpoint:
501+
address:
502+
socket_address:
503+
address: 10.0.0.1
504+
port_value: 443
505+
health_check_config:
506+
port_value: 8000
501507
)EOF";
502508

503509
NiceMock<MockClusterManager> cm;

0 commit comments

Comments
 (0)