public IstioServiceEntryRegistry()

in oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/envoy/als/istio/IstioServiceEntryRegistry.java [56:130]


    public IstioServiceEntryRegistry(final EnvoyMetricReceiverConfig config) {
        this.config = config;

        serviceNameFormatter = new ServiceNameFormatter(config.getIstioServiceNameRule());

        final var ignoredNamespaces = config.getIstioServiceEntryIgnoredNamespaces();

        final var cacheBuilder = CacheBuilder.newBuilder().expireAfterWrite(Duration.ofMinutes(3));

        ipServiceMetaInfoMap = cacheBuilder.build(new CacheLoader<>() {
            @Override
            public ServiceMetaInfo load(String ip) {
                final var serviceEntry = IstioServiceEntries
                    .INSTANCE
                    .list()
                    .parallelStream()
                    .filter(se -> se.getMetadata() != null)
                    .filter(se -> se.getSpec() != null)
                    .filter(se -> !ignoredNamespaces.contains(se.getMetadata().getNamespace()))
                    .filter(se -> {
                        final var spec = se.getSpec();
                        if (spec.getResolution() == null) {
                            log.debug("Unsupported service entry resolution: {}", spec.getResolution());
                            return false;
                        }
                        switch (spec.getResolution()) {
                            case STATIC:
                                return spec
                                    .getAddresses()
                                    .parallelStream()
                                    .anyMatch(address -> {
                                        if (address.contains("/")) { // CIDR
                                            final var subnet = new SubnetUtils(address);
                                            return subnet.getInfo().isInRange(ip);
                                        }
                                        return Objects.equals(ip, address);
                                    }) ||
                                    spec
                                        .getEndpoints()
                                        .parallelStream()
                                        .map(WorkloadEntrySpec::getAddress)
                                        .anyMatch(address -> Objects.equals(address, ip));
                            case DNS:
                            case DNS_ROUND_ROBIN:
                                return spec
                                    .getHosts()
                                    .parallelStream()
                                    .map(host -> hostnameResolvers.computeIfAbsent(host, it -> {
                                        final var endpointGroup = DnsAddressEndpointGroup.of(it);
                                        endpointGroup.whenReady().join(); // Wait for the first resolution
                                        return endpointGroup;
                                    }))
                                    .anyMatch(dnsAddressEndpointGroup ->
                                        dnsAddressEndpointGroup
                                            .endpoints()
                                            .parallelStream()
                                            .anyMatch(endpoint -> Objects.equals(endpoint.ipAddr(), ip)));
                            default:
                                log.debug("Unsupported service entry resolution: {}", spec.getResolution());
                                return false;
                        }
                    })
                    .findFirst();

                if (serviceEntry.isEmpty()) {
                    log.debug("No corresponding service entry for IP: {}", ip);
                    return config.serviceMetaInfoFactory().unknown();
                }

                log.debug(
                    "Composing service meta info from service entry for IP: {}", ip);
                return composeServiceMetaInfo(serviceEntry.get(), ip);
            }
        });
    }