(lbEndpoints []*v3endpointpb.LbEndpoint, uniqueEndpointAddrs map[string]bool)
| 99 | } |
| 100 | |
| 101 | func parseEndpoints(lbEndpoints []*v3endpointpb.LbEndpoint, uniqueEndpointAddrs map[string]bool) ([]Endpoint, error) { |
| 102 | endpoints := make([]Endpoint, 0, len(lbEndpoints)) |
| 103 | var totalWeight uint64 |
| 104 | for _, lbEndpoint := range lbEndpoints { |
| 105 | // If the load_balancing_weight field is specified, it must be set to a |
| 106 | // value of at least 1. If unspecified, each host is presumed to have |
| 107 | // equal weight in a locality. |
| 108 | weight := uint32(1) |
| 109 | if w := lbEndpoint.GetLoadBalancingWeight(); w != nil { |
| 110 | if w.GetValue() == 0 { |
| 111 | return nil, fmt.Errorf("EDS response contains an endpoint with zero weight: %+v", lbEndpoint) |
| 112 | } |
| 113 | weight = w.GetValue() |
| 114 | } |
| 115 | |
| 116 | totalWeight += uint64(weight) |
| 117 | if totalWeight > math.MaxUint32 { |
| 118 | return nil, fmt.Errorf("sum of weights of endpoints in the same locality exceeds maximum value %d", uint64(math.MaxUint32)) |
| 119 | } |
| 120 | |
| 121 | addrs := []string{parseAddress(lbEndpoint.GetEndpoint().GetAddress().GetSocketAddress())} |
| 122 | if envconfig.XDSDualstackEndpointsEnabled { |
| 123 | for _, sa := range lbEndpoint.GetEndpoint().GetAdditionalAddresses() { |
| 124 | addrs = append(addrs, parseAddress(sa.GetAddress().GetSocketAddress())) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | address := []resolver.Address{} |
| 129 | for _, a := range addrs { |
| 130 | address = append(address, resolver.Address{Addr: a}) |
| 131 | if uniqueEndpointAddrs[a] { |
| 132 | return nil, fmt.Errorf("duplicate endpoint with the same address %s", a) |
| 133 | } |
| 134 | uniqueEndpointAddrs[a] = true |
| 135 | } |
| 136 | |
| 137 | var endpointMetadata map[string]any |
| 138 | var hashKey string |
| 139 | if envconfig.XDSHTTPConnectEnabled || !envconfig.XDSEndpointHashKeyBackwardCompat { |
| 140 | var err error |
| 141 | endpointMetadata, err = validateAndConstructMetadata(lbEndpoint.GetMetadata()) |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | // "The xDS resolver, described in A74, will be changed to set the hash_key |
| 147 | // endpoint attribute to the value of LbEndpoint.Metadata envoy.lb hash_key |
| 148 | // field, as described in Envoy's documentation for the ring hash load |
| 149 | // balancer." - A76 |
| 150 | if !envconfig.XDSEndpointHashKeyBackwardCompat { |
| 151 | hashKey = hashKeyFromMetadata(endpointMetadata) |
| 152 | } |
| 153 | } |
| 154 | endpoint := resolver.Endpoint{Addresses: address} |
| 155 | endpoint = hostname.Set(endpoint, lbEndpoint.GetEndpoint().GetHostname()) |
| 156 | endpoint = ringhash.SetHashKey(endpoint, hashKey) |
| 157 | endpoints = append(endpoints, Endpoint{ |
| 158 | ResolverEndpoint: endpoint, |
no test coverage detected