@@ -524,6 +524,14 @@ func ClusterResourceWithOptions(opts ClusterOptions) *v3clusterpb.Cluster {
524524	return  cluster 
525525}
526526
527+ // LocalityOptions contains options to configure a Locality. 
528+ type  LocalityOptions  struct  {
529+ 	// Ports is a set of ports on "localhost" belonging to this locality. 
530+ 	Ports  []uint32 
531+ 	// Weight is the weight of the locality, used for load balancing. 
532+ 	Weight  uint32 
533+ }
534+ 
527535// EndpointOptions contains options to configure an Endpoint (or 
528536// ClusterLoadAssignment) resource. 
529537type  EndpointOptions  struct  {
@@ -533,9 +541,8 @@ type EndpointOptions struct {
533541	// Host is the hostname of the endpoints. In our e2e tests, hostname must 
534542	// always be "localhost". 
535543	Host  string 
536- 	// Ports is a set of ports on "localhost" where the endpoints corresponding 
537- 	// to this resource reside. 
538- 	Ports  []uint32 
544+ 	// Localities is a set of localities belonging to this resource. 
545+ 	Localities  []LocalityOptions 
539546	// DropPercents is a map from drop category to a drop percentage. If unset, 
540547	// no drops are configured. 
541548	DropPercents  map [string ]int 
@@ -546,34 +553,50 @@ func DefaultEndpoint(clusterName string, host string, ports []uint32) *v3endpoin
546553	return  EndpointResourceWithOptions (EndpointOptions {
547554		ClusterName : clusterName ,
548555		Host :        host ,
549- 		Ports :       ports ,
556+ 		Localities : []LocalityOptions {
557+ 			{
558+ 				Ports :  ports ,
559+ 				Weight : 1 ,
560+ 			},
561+ 		},
550562	})
551563}
552564
553565// EndpointResourceWithOptions returns an xds Endpoint resource configured with 
554566// the provided options. 
555567func  EndpointResourceWithOptions (opts  EndpointOptions ) * v3endpointpb.ClusterLoadAssignment  {
556- 	var  lbEndpoints  []* v3endpointpb.LbEndpoint 
557- 	for  _ , port  :=  range  opts .Ports  {
558- 		lbEndpoints  =  append (lbEndpoints , & v3endpointpb.LbEndpoint {
559- 			HostIdentifier : & v3endpointpb.LbEndpoint_Endpoint {Endpoint : & v3endpointpb.Endpoint {
560- 				Address : & v3corepb.Address {Address : & v3corepb.Address_SocketAddress {
561- 					SocketAddress : & v3corepb.SocketAddress {
562- 						Protocol :      v3corepb .SocketAddress_TCP ,
563- 						Address :       opts .Host ,
564- 						PortSpecifier : & v3corepb.SocketAddress_PortValue {PortValue : port }},
568+ 	var  endpoints  []* v3endpointpb.LocalityLbEndpoints 
569+ 	for  i , locality  :=  range  opts .Localities  {
570+ 		var  lbEndpoints  []* v3endpointpb.LbEndpoint 
571+ 		for  _ , port  :=  range  locality .Ports  {
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+ 					}},
565580				}},
566- 			}},
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 + 1 ),
588+ 				Zone :    fmt .Sprintf ("zone-%d" , i + 1 ),
589+ 				SubZone : fmt .Sprintf ("subzone-%d" , i + 1 ),
590+ 			},
591+ 			LbEndpoints :         lbEndpoints ,
592+ 			LoadBalancingWeight : & wrapperspb.UInt32Value {Value : locality .Weight },
593+ 			Priority :            0 ,
567594		})
568595	}
596+ 
569597	cla  :=  & v3endpointpb.ClusterLoadAssignment {
570598		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- 		}},
599+ 		Endpoints :   endpoints ,
577600	}
578601
579602	var  drops  []* v3endpointpb.ClusterLoadAssignment_Policy_DropOverload 
0 commit comments