Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/extensions/load_balancing_policies/round_robin/v3/round_robin.pb.h" 4 : #include "envoy/extensions/load_balancing_policies/round_robin/v3/round_robin.pb.validate.h" 5 : #include "envoy/upstream/load_balancer.h" 6 : 7 : #include "source/common/common/logger.h" 8 : #include "source/common/upstream/load_balancer_impl.h" 9 : #include "source/extensions/load_balancing_policies/common/factory_base.h" 10 : 11 : namespace Envoy { 12 : namespace Extensions { 13 : namespace LoadBalancingPolices { 14 : namespace RoundRobin { 15 : 16 : using RoundRobinLbProto = envoy::extensions::load_balancing_policies::round_robin::v3::RoundRobin; 17 : using ClusterProto = envoy::config::cluster::v3::Cluster; 18 : using LegacyRoundRobinLbProto = ClusterProto::RoundRobinLbConfig; 19 : 20 : /** 21 : * Load balancer config that used to wrap the legacy proto config. 22 : */ 23 : class LegacyRoundRobinLbConfig : public Upstream::LoadBalancerConfig { 24 : public: 25 : LegacyRoundRobinLbConfig(const ClusterProto& cluster); 26 : 27 306 : OptRef<const LegacyRoundRobinLbProto> lbConfig() const { 28 306 : if (lb_config_.has_value()) { 29 0 : return lb_config_.value(); 30 0 : } 31 306 : return {}; 32 306 : }; 33 : 34 : private: 35 : absl::optional<LegacyRoundRobinLbProto> lb_config_; 36 : }; 37 : 38 : /** 39 : * Load balancer config that used to wrap the proto config. 40 : */ 41 : class TypedRoundRobinLbConfig : public Upstream::LoadBalancerConfig { 42 : public: 43 : TypedRoundRobinLbConfig(const RoundRobinLbProto& lb_config); 44 : 45 : const RoundRobinLbProto lb_config_; 46 : }; 47 : 48 : struct RoundRobinCreator : public Logger::Loggable<Logger::Id::upstream> { 49 : Upstream::LoadBalancerPtr operator()(Upstream::LoadBalancerParams params, 50 : OptRef<const Upstream::LoadBalancerConfig> lb_config, 51 : const Upstream::ClusterInfo& cluster_info, 52 : const Upstream::PrioritySet& priority_set, 53 : Runtime::Loader& runtime, Random::RandomGenerator& random, 54 : TimeSource& time_source); 55 : }; 56 : 57 : class Factory : public Common::FactoryBase<RoundRobinLbProto, RoundRobinCreator> { 58 : public: 59 13 : Factory() : FactoryBase("envoy.load_balancing_policies.round_robin") {} 60 : 61 : Upstream::LoadBalancerConfigPtr loadConfig(const Protobuf::Message& config, 62 159 : ProtobufMessage::ValidationVisitor&) override { 63 : 64 159 : auto active_or_legacy = Common::ActiveOrLegacy<RoundRobinLbProto, ClusterProto>::get(&config); 65 159 : ASSERT(active_or_legacy.hasLegacy() || active_or_legacy.hasActive()); 66 : 67 159 : return active_or_legacy.hasLegacy() 68 159 : ? Upstream::LoadBalancerConfigPtr{new LegacyRoundRobinLbConfig( 69 159 : *active_or_legacy.legacy())} 70 159 : : Upstream::LoadBalancerConfigPtr{ 71 0 : new TypedRoundRobinLbConfig(*active_or_legacy.active())}; 72 159 : } 73 : }; 74 : 75 : DECLARE_FACTORY(Factory); 76 : 77 : } // namespace RoundRobin 78 : } // namespace LoadBalancingPolices 79 : } // namespace Extensions 80 : } // namespace Envoy