From 1b248bfe5bb6606e9795885145ba543ecbc2520a Mon Sep 17 00:00:00 2001 From: srgibbs99 Date: Sat, 14 Nov 2020 21:35:38 +0000 Subject: [PATCH 1/3] Allow opt-in for discovering "not-ready" service endpoints --- docs/src/main/asciidoc/discovery-client.adoc | 10 ++++++++++ .../discovery/KubernetesDiscoveryClient.java | 10 ++++++++++ .../discovery/KubernetesDiscoveryProperties.java | 11 +++++++++++ 3 files changed, 31 insertions(+) diff --git a/docs/src/main/asciidoc/discovery-client.adoc b/docs/src/main/asciidoc/discovery-client.adoc index c830197b..1f6e8590 100644 --- a/docs/src/main/asciidoc/discovery-client.adoc +++ b/docs/src/main/asciidoc/discovery-client.adoc @@ -52,6 +52,16 @@ spring.cloud.kubernetes.discovery.all-namespaces=true ---- ==== +To discover service endpoint addresses that are not marked as "ready" by the kubernetes api server, you can set the following property in `application.properties` (default: false): + +==== +[source] +---- +spring.cloud.kubernetes.discovery.include-not-ready-addresses=true +---- +NOTE: This might be useful when discovering services for monitoring purposes, using a tool like https://codecentric.github.io/spring-boot-admin/current/[spring-boot-admin]. +==== + If, for any reason, you need to disable the `DiscoveryClient`, you can set the following property in `application.properties`: ==== diff --git a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java index 70db8717..421de78f 100644 --- a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java +++ b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java @@ -38,6 +38,7 @@ import org.springframework.expression.Expression; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.SimpleEvaluationContext; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import static java.util.stream.Collectors.toMap; @@ -150,6 +151,15 @@ public class KubernetesDiscoveryClient implements DiscoveryClient { } List addresses = s.getAddresses(); + + if (this.properties.isIncludeNotReadyAddresses() + && !CollectionUtils.isEmpty(s.getNotReadyAddresses())) { + if (addresses == null) { + addresses = new ArrayList(); + } + addresses.addAll(s.getNotReadyAddresses()); + } + for (EndpointAddress endpointAddress : addresses) { String instanceId = null; if (endpointAddress.getTargetRef() != null) { diff --git a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryProperties.java b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryProperties.java index c3d2bf4a..4cad1b05 100644 --- a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryProperties.java +++ b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryProperties.java @@ -44,6 +44,9 @@ public class KubernetesDiscoveryProperties { /** If discovering all namespaces. */ private boolean allNamespaces = false; + /** If endpoint addresses not marked 'ready' by the k8s api server should be discovered */ + private boolean includeNotReadyAddresses = false; + /** * SpEL expression to filter services AFTER they have been retrieved from the * Kubernetes API server. @@ -138,6 +141,14 @@ public class KubernetesDiscoveryProperties { this.allNamespaces = allNamespaces; } + public boolean isIncludeNotReadyAddresses() { + return includeNotReadyAddresses; + } + + public void setIncludeNotReadyAddresses(boolean includeNotReadyAddresses) { + this.includeNotReadyAddresses = includeNotReadyAddresses; + } + public int getOrder() { return this.order; } From 6a49ef01b0d5254ce20ed7947697adb66eb7fc26 Mon Sep 17 00:00:00 2001 From: srgibbs99 Date: Sat, 14 Nov 2020 22:07:17 +0000 Subject: [PATCH 2/3] Missing period in javadoc --- .../kubernetes/discovery/KubernetesDiscoveryProperties.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryProperties.java b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryProperties.java index 4cad1b05..a1f8648d 100644 --- a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryProperties.java +++ b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryProperties.java @@ -44,7 +44,10 @@ public class KubernetesDiscoveryProperties { /** If discovering all namespaces. */ private boolean allNamespaces = false; - /** If endpoint addresses not marked 'ready' by the k8s api server should be discovered */ + /** + * If endpoint addresses not marked 'ready' by the k8s api server should be + * discovered. + */ private boolean includeNotReadyAddresses = false; /** From 859a4902d9e75b659d1608b0a5482db77bb9228f Mon Sep 17 00:00:00 2001 From: srgibbs99 Date: Sat, 14 Nov 2020 22:55:34 +0000 Subject: [PATCH 3/3] Remove reference to 3rd party tool --- docs/src/main/asciidoc/discovery-client.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/main/asciidoc/discovery-client.adoc b/docs/src/main/asciidoc/discovery-client.adoc index 1f6e8590..8c4b7782 100644 --- a/docs/src/main/asciidoc/discovery-client.adoc +++ b/docs/src/main/asciidoc/discovery-client.adoc @@ -59,7 +59,7 @@ To discover service endpoint addresses that are not marked as "ready" by the kub ---- spring.cloud.kubernetes.discovery.include-not-ready-addresses=true ---- -NOTE: This might be useful when discovering services for monitoring purposes, using a tool like https://codecentric.github.io/spring-boot-admin/current/[spring-boot-admin]. +NOTE: This might be useful when discovering services for monitoring purposes, and would enable inspecting the `/health` endpoint of not-ready service instances. ==== If, for any reason, you need to disable the `DiscoveryClient`, you can set the following property in `application.properties`: