|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-2023 Alibaba Group Holding Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.alibaba.higress.sdk.service; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.Comparator; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.function.Function; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import org.apache.commons.collections4.CollectionUtils; |
| 24 | +import org.apache.commons.lang3.StringUtils; |
| 25 | + |
| 26 | +import com.alibaba.higress.sdk.constant.Separators; |
| 27 | +import com.alibaba.higress.sdk.model.CommonPageQuery; |
| 28 | +import com.alibaba.higress.sdk.model.PaginatedResult; |
| 29 | +import com.alibaba.higress.sdk.model.Service; |
| 30 | +import com.alibaba.higress.sdk.service.kubernetes.KubernetesClientService; |
| 31 | +import com.alibaba.higress.sdk.service.kubernetes.KubernetesModelConverter; |
| 32 | +import com.alibaba.higress.sdk.service.kubernetes.crd.mcp.V1McpBridge; |
| 33 | +import com.alibaba.higress.sdk.service.kubernetes.crd.mcp.V1RegistryConfig; |
| 34 | +import com.google.common.collect.Lists; |
| 35 | +import com.google.common.collect.Maps; |
| 36 | + |
| 37 | +import io.kubernetes.client.openapi.models.V1EndpointAddress; |
| 38 | +import io.kubernetes.client.openapi.models.V1Endpoints; |
| 39 | +import io.kubernetes.client.openapi.models.V1ObjectMeta; |
| 40 | +import io.kubernetes.client.openapi.models.V1Service; |
| 41 | +import lombok.SneakyThrows; |
| 42 | +import lombok.extern.slf4j.Slf4j; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author lvshui |
| 46 | + */ |
| 47 | +@Slf4j |
| 48 | +public class ServiceServiceByApiServerImpl implements ServiceService { |
| 49 | + public static final String MCP_SIMULATE_NS = "mcp"; |
| 50 | + private final KubernetesClientService kubernetesClientService; |
| 51 | + private final KubernetesModelConverter kubernetesModelConverter; |
| 52 | + |
| 53 | + public ServiceServiceByApiServerImpl(KubernetesClientService kubernetesClientService, |
| 54 | + KubernetesModelConverter kubernetesModelConverter) { |
| 55 | + this.kubernetesClientService = kubernetesClientService; |
| 56 | + this.kubernetesModelConverter = kubernetesModelConverter; |
| 57 | + } |
| 58 | + |
| 59 | + @SneakyThrows |
| 60 | + @Override |
| 61 | + public PaginatedResult<Service> list(CommonPageQuery query) { |
| 62 | + List<Service> resultList = Lists.newArrayList(); |
| 63 | + |
| 64 | + List<V1Service> v1Services = kubernetesClientService.listAllServiceList(); |
| 65 | + List<V1Endpoints> v1Endpoints = kubernetesClientService.listAllEndPointsList(); |
| 66 | + final Map<String, V1Endpoints> v1EndpointsMap = Maps.newHashMap(); |
| 67 | + if (CollectionUtils.isNotEmpty(v1Endpoints)) { |
| 68 | + Map<String, V1Endpoints> v1EndpointsMap1 = v1Endpoints.stream().collect( |
| 69 | + Collectors.toMap(i -> buildMetaUniqueKey(i.getMetadata()), Function.identity(), (k1, k2) -> k1)); |
| 70 | + v1EndpointsMap.putAll(v1EndpointsMap1); |
| 71 | + } |
| 72 | + |
| 73 | + if (CollectionUtils.isNotEmpty(v1Services)) { |
| 74 | + for (V1Service v1Service : v1Services) { |
| 75 | + try { |
| 76 | + Service service = kubernetesModelConverter.v1Service2Service(v1Service); |
| 77 | + String metaUniqueKey = buildMetaUniqueKey(v1Service.getMetadata()); |
| 78 | + V1Endpoints v1Endpoints1 = null; |
| 79 | + if (v1EndpointsMap.containsKey(metaUniqueKey)) { |
| 80 | + v1Endpoints1 = v1EndpointsMap.get(metaUniqueKey); |
| 81 | + } |
| 82 | + if (Objects.nonNull(v1Endpoints1) && CollectionUtils.isNotEmpty(v1Endpoints1.getSubsets())) { |
| 83 | + try { |
| 84 | + List<String> ipList = |
| 85 | + v1Endpoints1.getSubsets().stream().filter(Objects::nonNull).map(v1Subset -> { |
| 86 | + if (v1Subset.getAddresses() != null) { |
| 87 | + return v1Subset.getAddresses().stream().map(V1EndpointAddress::getIp) |
| 88 | + .collect(Collectors.toList()); |
| 89 | + } |
| 90 | + return null; |
| 91 | + }).filter(CollectionUtils::isNotEmpty).flatMap(List::stream) |
| 92 | + .collect(Collectors.toList()); |
| 93 | + service.setEndpoints(ipList); |
| 94 | + } catch (Exception e) { |
| 95 | + log.error("deal service endpoints appear error. ", e); |
| 96 | + } |
| 97 | + } |
| 98 | + resultList.add(service); |
| 99 | + } catch (Exception e) { |
| 100 | + log.error("deal service appear error. ", e); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + List<V1McpBridge> v1McpBridges = kubernetesClientService.listMcpBridge(); |
| 106 | + if (CollectionUtils.isNotEmpty(v1McpBridges)) { |
| 107 | + List<Service> externalServiceList = v1McpBridges.stream().map(i -> { |
| 108 | + List<V1RegistryConfig> registries = i.getSpec().getRegistries(); |
| 109 | + if (CollectionUtils.isEmpty(registries)) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + return registries.stream().map(r -> { |
| 113 | + Service service = new Service(); |
| 114 | + service.setNamespace(MCP_SIMULATE_NS); |
| 115 | + service.setName(StringUtils.join(r.getName(), Separators.DOT, r.getType())); |
| 116 | + service.setProtocol(r.getProtocol()); |
| 117 | + String[] endPoints = new String[] {}; |
| 118 | + if (StringUtils.isNotBlank(r.getDomain())) { |
| 119 | + endPoints = StringUtils.split(r.getDomain(), V1McpBridge.REGISTRY_TYPE_STATIC_DNS_SEPARATOR); |
| 120 | + } |
| 121 | + service.setEndpoints(Arrays.asList(endPoints)); |
| 122 | + switch (r.getType()) { |
| 123 | + case V1McpBridge.REGISTRY_TYPE_DNS: |
| 124 | + service.setPort(r.getPort()); |
| 125 | + return service; |
| 126 | + case V1McpBridge.REGISTRY_TYPE_STATIC: |
| 127 | + service.setPort(V1McpBridge.STATIC_PORT); |
| 128 | + return service; |
| 129 | + default: |
| 130 | + return null; |
| 131 | + } |
| 132 | + }).filter(Objects::nonNull).collect(Collectors.toList()); |
| 133 | + }).filter(CollectionUtils::isNotEmpty).flatMap(List::stream).collect(Collectors.toList()); |
| 134 | + |
| 135 | + if (CollectionUtils.isNotEmpty(externalServiceList)) { |
| 136 | + resultList.addAll(externalServiceList); |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + resultList.sort(Comparator.comparing(Service::getNamespace).thenComparing(Service::getName) |
| 141 | + .thenComparing(Service::getPort)); |
| 142 | + |
| 143 | + return PaginatedResult.createFromFullList(resultList, query); |
| 144 | + } |
| 145 | + |
| 146 | + private String buildMetaUniqueKey(V1ObjectMeta metadata) { |
| 147 | + return StringUtils.join(metadata.getNamespace(), ".", metadata.getName()); |
| 148 | + } |
| 149 | +} |
0 commit comments