Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/config/cluster/v3/cluster.pb.h" 4 : #include "envoy/config/endpoint/v3/endpoint_components.pb.h" 5 : 6 : #include "source/common/upstream/cluster_factory_impl.h" 7 : #include "source/common/upstream/upstream_impl.h" 8 : 9 : namespace Envoy { 10 : namespace Upstream { 11 : 12 : /** 13 : * Implementation of Upstream::Cluster that does periodic DNS resolution and updates the host 14 : * member set if the DNS members change. 15 : */ 16 : class StrictDnsClusterImpl : public BaseDynamicClusterImpl { 17 : public: 18 : StrictDnsClusterImpl(const envoy::config::cluster::v3::Cluster& cluster, 19 : ClusterFactoryContext& context, Network::DnsResolverSharedPtr dns_resolver); 20 : 21 : // Upstream::Cluster 22 0 : InitializePhase initializePhase() const override { return InitializePhase::Primary; } 23 : 24 : private: 25 : struct ResolveTarget { 26 : ResolveTarget(StrictDnsClusterImpl& parent, Event::Dispatcher& dispatcher, 27 : const std::string& dns_address, const uint32_t dns_port, 28 : const envoy::config::endpoint::v3::LocalityLbEndpoints& locality_lb_endpoint, 29 : const envoy::config::endpoint::v3::LbEndpoint& lb_endpoint); 30 : ~ResolveTarget(); 31 : void startResolve(); 32 : 33 : StrictDnsClusterImpl& parent_; 34 : Network::ActiveDnsQuery* active_query_{}; 35 : const envoy::config::endpoint::v3::LocalityLbEndpoints& locality_lb_endpoints_; 36 : const envoy::config::endpoint::v3::LbEndpoint& lb_endpoint_; 37 : const std::string dns_address_; 38 : const std::string hostname_; 39 : const uint32_t port_; 40 : const Event::TimerPtr resolve_timer_; 41 : HostVector hosts_; 42 : 43 : // Host map for current resolve target. When we have multiple resolve targets, multiple targets 44 : // may contain two different hosts with the same address. This has two effects: 45 : // 1) This host map cannot be replaced by the cross-priority global host map in the priority 46 : // set. 47 : // 2) Cross-priority global host map may not be able to search for the expected host based on 48 : // the address. 49 : HostMap all_hosts_; 50 : }; 51 : 52 : using ResolveTargetPtr = std::unique_ptr<ResolveTarget>; 53 : 54 : void updateAllHosts(const HostVector& hosts_added, const HostVector& hosts_removed, 55 : uint32_t priority); 56 : 57 : // ClusterImplBase 58 : void startPreInit() override; 59 : 60 : // Keep load assignment as a member to make sure its data referenced in 61 : // resolve_targets_ outlives them. 62 : const envoy::config::endpoint::v3::ClusterLoadAssignment load_assignment_; 63 : const LocalInfo::LocalInfo& local_info_; 64 : Network::DnsResolverSharedPtr dns_resolver_; 65 : std::list<ResolveTargetPtr> resolve_targets_; 66 : const std::chrono::milliseconds dns_refresh_rate_ms_; 67 : BackOffStrategyPtr failure_backoff_strategy_; 68 : const bool respect_dns_ttl_; 69 : Network::DnsLookupFamily dns_lookup_family_; 70 : uint32_t overprovisioning_factor_; 71 : bool weighted_priority_health_; 72 : }; 73 : 74 : /** 75 : * Factory for StrictDnsClusterImpl 76 : */ 77 : class StrictDnsClusterFactory : public ClusterFactoryImplBase { 78 : public: 79 2 : StrictDnsClusterFactory() : ClusterFactoryImplBase("envoy.cluster.strict_dns") {} 80 : 81 : private: 82 : absl::StatusOr<std::pair<ClusterImplBaseSharedPtr, ThreadAwareLoadBalancerPtr>> 83 : createClusterImpl(const envoy::config::cluster::v3::Cluster& cluster, 84 : ClusterFactoryContext& context) override; 85 : }; 86 : 87 : DECLARE_FACTORY(StrictDnsClusterFactory); 88 : 89 : } // namespace Upstream 90 : } // namespace Envoy