@Scheduled(fixedDelayString = "${spring.cloud.kubernetes.discovery.catalogServicesWatchDelay:30000}") public void catalogServicesWatch() { try { List<String> previousState = catalogEndpointsState.get(); //not all pods participate in the service discovery. only those that have endpoints. List<Endpoints> endpoints = kubernetesClient.endpoints().list().getItems(); List<String> endpointsPodNames = endpoints.stream() .map(Endpoints::getSubsets) .filter(Objects::nonNull) .flatMap(Collection::stream) .map(EndpointSubset::getAddresses) .filter(Objects::nonNull) .flatMap(Collection::stream) .map(EndpointAddress::getTargetRef) .filter(Objects::nonNull) .map(ObjectReference::getName) // pod name unique in namespace .sorted(String::compareTo).collect(Collectors.toList()); catalogEndpointsState.set(endpointsPodNames); if (!endpointsPodNames.equals(previousState)) { logger.trace("Received endpoints update from kubernetesClient: {}", endpointsPodNames); publisher.publishEvent(new HeartbeatEvent(this, endpointsPodNames)); } } catch (Exception e) { logger.error("Error watching Kubernetes Services", e); } }
"[Assertion failed] - the object argument must be null"); Endpoints endpoints = client.endpoints().withName(serviceId).get(); List<EndpointSubset> subsets = null != endpoints ? endpoints.getSubsets() : new ArrayList<>(); List<ServiceInstance> instances = new ArrayList<>();
@Override public MixedOperation<Endpoints, EndpointsList, DoneableEndpoints, Resource<Endpoints, DoneableEndpoints>> endpoints() { return delegate.endpoints(); }
@Override public List<ServiceInstance> getInstances(String serviceId) { Assert.notNull(serviceId, "[Assertion failed] - the object argument must be null"); return Optional.ofNullable(client.endpoints().withName(serviceId).get()).orElse(new Endpoints()) .getSubsets() .stream() .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(serviceId, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false))) .collect(Collectors.toList()); }
private static List<ServiceInstance> getInstances(KubernetesClient client, String name, String namespace) { Assert.notNull(name, "[Assertion failed] - the service name must not be null"); return Optional.ofNullable(client.endpoints().inNamespace(namespace).withName(name).get()) .orElse(new Endpoints()) .getSubsets() .stream() .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(name, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false))) .collect(Collectors.toList()); }
@Override public ServiceInstance getLocalServiceInstance() { String serviceName = properties.getServiceName(); String podName = System.getenv(HOSTNAME); ServiceInstance defaultInstance = new DefaultServiceInstance(serviceName, "localhost", 8080, false); Endpoints endpoints = client.endpoints().withName(serviceName).get(); if (Utils.isNullOrEmpty(podName) || endpoints == null) { return defaultInstance; } try { return endpoints.getSubsets() .stream() .filter(s -> s.getAddresses().get(0).getTargetRef().getName().equals(podName)) .map(s -> (ServiceInstance) new KubernetesServiceInstance(serviceName, s.getAddresses().stream().findFirst().orElseThrow(IllegalStateException::new), s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false)) .findFirst().orElse(defaultInstance); } catch (Throwable t) { return defaultInstance; } }
@Override protected MixedOperation<Endpoints, EndpointsList, DoneableEndpoints, Resource<Endpoints, DoneableEndpoints>> operation() { return client.endpoints(); } }
@Override public MixedOperation<Endpoints, EndpointsList, DoneableEndpoints, Resource<Endpoints, DoneableEndpoints>> endpoints() { return delegate.endpoints(); }
@Override public List<ServiceInstance> getInstances(String serviceId) { Assert.notNull(serviceId, "[Assertion failed] - the object argument must be null"); return Optional.ofNullable(client.endpoints().withName(serviceId).get()).orElse(new Endpoints()) .getSubsets() .stream() .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(serviceId, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false))) .collect(Collectors.toList()); }
public Collection<Instance> getInstanceList() throws Exception { List<Instance> result = new ArrayList<Instance>(); for (Endpoints endpoint : client.endpoints().withLabels(labels).list().getItems()) { try { result.addAll(toInstances(endpoint)); } catch (Throwable t) { LOGGER.error("Error processing endpoint", t); } } return result; }
private static List<ServiceInstance> getInstances(KubernetesClient client, String name, String namespace) { Assert.notNull(name, "[Assertion failed] - the service name must not be null"); return Optional.ofNullable(client.endpoints().inNamespace(namespace).withName(name).get()) .orElse(new Endpoints()) .getSubsets() .stream() .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(name, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false))) .collect(Collectors.toList()); }
private static List<ServiceInstance> getInstances(KubernetesClient client, String name, String namespace) { Assert.notNull(name, "[Assertion failed] - the service name must not be null"); return Optional.ofNullable(client.endpoints().inNamespace(namespace).withName(name).get()) .orElse(new Endpoints()) .getSubsets() .stream() .flatMap(s -> s.getAddresses().stream().map(a -> (ServiceInstance) new KubernetesServiceInstance(name, a ,s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new), false))) .collect(Collectors.toList()); }
@Override protected void mocker(KubernetesClient mockClient, MixedOperation op) { when(mockClient.endpoints()).thenReturn(op); }
@Override public ServiceInstance getLocalServiceInstance() { String podName = System.getenv(HOSTNAME); return client.endpoints().withName(localServiceId).get().getSubsets() .stream() .filter(s -> s.getAddresses().iterator().next().getIp().equals(podName)) .map(s -> new KubernetesServiceInstance(localServiceId, s.getPorts().iterator().next().getName(), s, false)) .findFirst().get(); }
@Override public List<ServiceInstance> getInstances(String serviceId) { return client.endpoints().withName(serviceId).get() .getSubsets() .stream().map(s -> new KubernetesServiceInstance(serviceId, s.getPorts().iterator().next().getName(), s, false)) .collect(Collectors.toList()); }
@Override public String getFailureMessage() { Endpoints endpoints = client.endpoints().inNamespace(namespace).withName(serviceName).get(); return "Service endpoints in " + namespace + " for serviceName= " + serviceName + " not ready: " + endpoints; } };
@Override public Endpoints endpoints() { logger.debug("get endpoints for service {}, namespace {}", serviceName, namespace); return client().endpoints().inNamespace(namespace).withName(serviceName).get(); }