|
| 1 | +/* |
| 2 | + * Copyright 2024 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.linecorp.armeria.xds.client.endpoint; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 21 | +import static com.linecorp.armeria.xds.client.endpoint.XdsConstants.SUBSET_LOAD_BALANCING_FILTER_NAME; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Map.Entry; |
| 26 | +import java.util.function.Predicate; |
| 27 | + |
| 28 | +import com.google.common.base.Strings; |
| 29 | +import com.google.protobuf.Struct; |
| 30 | +import com.google.protobuf.Value; |
| 31 | + |
| 32 | +import com.linecorp.armeria.client.Endpoint; |
| 33 | + |
| 34 | +import io.envoyproxy.envoy.config.core.v3.SocketAddress; |
| 35 | +import io.envoyproxy.envoy.config.endpoint.v3.ClusterLoadAssignment; |
| 36 | +import io.envoyproxy.envoy.config.endpoint.v3.LbEndpoint; |
| 37 | + |
| 38 | +final class XdsEndpointUtil { |
| 39 | + |
| 40 | + static List<Endpoint> convertEndpoints(ClusterLoadAssignment clusterLoadAssignment) { |
| 41 | + return convertEndpoints(clusterLoadAssignment, lbEndpoint -> true); |
| 42 | + } |
| 43 | + |
| 44 | + static List<Endpoint> convertEndpoints(ClusterLoadAssignment clusterLoadAssignment, Struct filterMetadata) { |
| 45 | + checkArgument(filterMetadata.getFieldsCount() > 0, |
| 46 | + "filterMetadata.getFieldsCount(): %s (expected: > 0)", filterMetadata.getFieldsCount()); |
| 47 | + final Predicate<LbEndpoint> lbEndpointPredicate = lbEndpoint -> { |
| 48 | + final Struct endpointMetadata = lbEndpoint.getMetadata().getFilterMetadataOrDefault( |
| 49 | + SUBSET_LOAD_BALANCING_FILTER_NAME, Struct.getDefaultInstance()); |
| 50 | + if (endpointMetadata.getFieldsCount() == 0) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + return containsFilterMetadata(filterMetadata, endpointMetadata); |
| 54 | + }; |
| 55 | + return convertEndpoints(clusterLoadAssignment, lbEndpointPredicate); |
| 56 | + } |
| 57 | + |
| 58 | + private static List<Endpoint> convertEndpoints(ClusterLoadAssignment clusterLoadAssignment, |
| 59 | + Predicate<LbEndpoint> lbEndpointPredicate) { |
| 60 | + return clusterLoadAssignment.getEndpointsList().stream().flatMap( |
| 61 | + localityLbEndpoints -> localityLbEndpoints |
| 62 | + .getLbEndpointsList() |
| 63 | + .stream() |
| 64 | + .filter(lbEndpointPredicate) |
| 65 | + .map(lbEndpoint -> { |
| 66 | + final SocketAddress socketAddress = |
| 67 | + lbEndpoint.getEndpoint().getAddress().getSocketAddress(); |
| 68 | + final String hostname = lbEndpoint.getEndpoint().getHostname(); |
| 69 | + if (!Strings.isNullOrEmpty(hostname)) { |
| 70 | + return Endpoint.of(hostname, socketAddress.getPortValue()) |
| 71 | + .withIpAddr(socketAddress.getAddress()); |
| 72 | + } else { |
| 73 | + return Endpoint.of(socketAddress.getAddress(), socketAddress.getPortValue()); |
| 74 | + } |
| 75 | + })).collect(toImmutableList()); |
| 76 | + } |
| 77 | + |
| 78 | + private static boolean containsFilterMetadata(Struct filterMetadata, Struct endpointMetadata) { |
| 79 | + final Map<String, Value> endpointMetadataMap = endpointMetadata.getFieldsMap(); |
| 80 | + for (Entry<String, Value> entry : filterMetadata.getFieldsMap().entrySet()) { |
| 81 | + final Value value = endpointMetadataMap.get(entry.getKey()); |
| 82 | + if (value == null || !value.equals(entry.getValue())) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + private XdsEndpointUtil() {} |
| 90 | +} |
0 commit comments