@@ -536,6 +536,17 @@ type EndpointOptions struct {
536536 // Ports is a set of ports on "localhost" where the endpoints corresponding
537537 // to this resource reside.
538538 Ports []uint32
539+
540+ // PortsInLocalities represent ports in different localities. The first
541+ // dimension represents a locality, and the second represents the ports
542+ // within that locality.
543+ PortsInLocalities [][]uint32
544+
545+ // LocalityWeights are the weights of localities specified in the first
546+ // dimension of PortsInLocalities. Must be the same length as the first
547+ // dimension of PortsInLocalities.
548+ LocalityWeights []uint32
549+
539550 // DropPercents is a map from drop category to a drop percentage. If unset,
540551 // no drops are configured.
541552 DropPercents map [string ]int
@@ -550,6 +561,62 @@ func DefaultEndpoint(clusterName string, host string, ports []uint32) *v3endpoin
550561 })
551562}
552563
564+ // EndpointResourceWithOptionsMultipleLocalities returns an xDS Endpoint
565+ // resource which specifies multiple localities, with the ports specified per
566+ // locality placed into each localities endpoints specification.
567+ func EndpointResourceWithOptionsMultipleLocalities (opts EndpointOptions ) * v3endpointpb.ClusterLoadAssignment {
568+ var endpoints []* v3endpointpb.LocalityLbEndpoints
569+ for i , portsInLocality := range opts .PortsInLocalities {
570+ var lbEndpoints []* v3endpointpb.LbEndpoint
571+ for _ , port := range portsInLocality {
572+ lbEndpoints = append (lbEndpoints , & v3endpointpb.LbEndpoint {
573+ HostIdentifier : & v3endpointpb.LbEndpoint_Endpoint {Endpoint : & v3endpointpb.Endpoint {
574+ Address : & v3corepb.Address {Address : & v3corepb.Address_SocketAddress {
575+ SocketAddress : & v3corepb.SocketAddress {
576+ Protocol : v3corepb .SocketAddress_TCP ,
577+ Address : opts .Host ,
578+ PortSpecifier : & v3corepb.SocketAddress_PortValue {PortValue : port }},
579+ }},
580+ }},
581+ LoadBalancingWeight : & wrapperspb.UInt32Value {Value : 1 },
582+ })
583+ }
584+
585+ endpoints = append (endpoints , & v3endpointpb.LocalityLbEndpoints {
586+ Locality : & v3corepb.Locality {
587+ Region : fmt .Sprintf ("region%d" , i ),
588+ Zone : fmt .Sprintf ("zone%d" , i ),
589+ SubZone : fmt .Sprintf ("subzone%d" , i ),
590+ },
591+ LbEndpoints : lbEndpoints ,
592+ LoadBalancingWeight : & wrapperspb.UInt32Value {Value : opts .LocalityWeights [i ]},
593+ Priority : 0 ,
594+ })
595+ }
596+
597+ cla := & v3endpointpb.ClusterLoadAssignment {
598+ ClusterName : opts .ClusterName ,
599+ Endpoints : endpoints ,
600+ }
601+
602+ var drops []* v3endpointpb.ClusterLoadAssignment_Policy_DropOverload
603+ for category , val := range opts .DropPercents {
604+ drops = append (drops , & v3endpointpb.ClusterLoadAssignment_Policy_DropOverload {
605+ Category : category ,
606+ DropPercentage : & v3typepb.FractionalPercent {
607+ Numerator : uint32 (val ),
608+ Denominator : v3typepb .FractionalPercent_HUNDRED ,
609+ },
610+ })
611+ }
612+ if len (drops ) != 0 {
613+ cla .Policy = & v3endpointpb.ClusterLoadAssignment_Policy {
614+ DropOverloads : drops ,
615+ }
616+ }
617+ return cla
618+ }
619+
553620// EndpointResourceWithOptions returns an xds Endpoint resource configured with
554621// the provided options.
555622func EndpointResourceWithOptions (opts EndpointOptions ) * v3endpointpb.ClusterLoadAssignment {
@@ -564,18 +631,20 @@ func EndpointResourceWithOptions(opts EndpointOptions) *v3endpointpb.ClusterLoad
564631 PortSpecifier : & v3corepb.SocketAddress_PortValue {PortValue : port }},
565632 }},
566633 }},
634+ LoadBalancingWeight : & wrapperspb.UInt32Value {Value : 1 },
567635 })
568636 }
569637 cla := & v3endpointpb.ClusterLoadAssignment {
570638 ClusterName : opts .ClusterName ,
571- Endpoints : []* v3endpointpb.LocalityLbEndpoints {{
572- Locality : & v3corepb.Locality {SubZone : "subzone" },
573- LbEndpoints : lbEndpoints ,
574- LoadBalancingWeight : & wrapperspb.UInt32Value {Value : 1 },
575- Priority : 0 ,
576- }},
639+ Endpoints : []* v3endpointpb.LocalityLbEndpoints {
640+ {
641+ Locality : & v3corepb.Locality {SubZone : "subzone" },
642+ LbEndpoints : lbEndpoints ,
643+ LoadBalancingWeight : & wrapperspb.UInt32Value {Value : 1 },
644+ Priority : 0 ,
645+ },
646+ },
577647 }
578-
579648 var drops []* v3endpointpb.ClusterLoadAssignment_Policy_DropOverload
580649 for category , val := range opts .DropPercents {
581650 drops = append (drops , & v3endpointpb.ClusterLoadAssignment_Policy_DropOverload {
0 commit comments