From 395ddcf7da05a13a286d5367852fd279909df985 Mon Sep 17 00:00:00 2001 From: Felix Hagemans Date: Thu, 12 Sep 2024 15:42:10 +0200 Subject: [PATCH] feat: allow disabling KubernetesHealthIndicator using endpoints.health.kubernetes.enabled: false (#755) --- .../client/KubernetesHealthIndicatorSpec.groovy | 14 +++++++++++++- .../health/KubernetesHealthIndicator.java | 2 ++ src/main/docs/guide/health-checks.adoc | 10 ++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/examples/example-client/src/test/groovy/micronaut/client/KubernetesHealthIndicatorSpec.groovy b/examples/example-client/src/test/groovy/micronaut/client/KubernetesHealthIndicatorSpec.groovy index c080b5269..74043a897 100644 --- a/examples/example-client/src/test/groovy/micronaut/client/KubernetesHealthIndicatorSpec.groovy +++ b/examples/example-client/src/test/groovy/micronaut/client/KubernetesHealthIndicatorSpec.groovy @@ -1,5 +1,6 @@ package micronaut.client +import io.micronaut.context.ApplicationContext import io.micronaut.context.annotation.Property import io.micronaut.context.env.Environment import io.micronaut.http.annotation.Get @@ -45,6 +46,17 @@ class KubernetesHealthIndicatorSpec extends KubernetesSpecification { details.kubernetes.details.containerStatuses.first().ready == true } + void "bean of type KubernetesHealthIndicator does not exist if you set endpoints.health.kubernetes.enabled=false"() { + when: + ApplicationContext applicationContext = ApplicationContext.run(['endpoints.health.kubernetes.enabled': 'false']) + + then: + !applicationContext.containsBean(KubernetesHealthIndicator) + + cleanup: + applicationContext.close() + } + @Client("http://localhost:9999") @MicronautRequires(property = "spec.name", value = "KubernetesHealthIndicatorSpec") static interface ServiceClient { @@ -55,4 +67,4 @@ class KubernetesHealthIndicatorSpec extends KubernetesSpecification { } -} \ No newline at end of file +} diff --git a/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/health/KubernetesHealthIndicator.java b/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/health/KubernetesHealthIndicator.java index bef989cc6..2b3a36d61 100644 --- a/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/health/KubernetesHealthIndicator.java +++ b/kubernetes-discovery-client/src/main/java/io/micronaut/kubernetes/health/KubernetesHealthIndicator.java @@ -22,6 +22,7 @@ import io.kubernetes.client.openapi.models.V1PodStatus; import io.micronaut.context.annotation.Requires; import io.micronaut.context.env.Environment; +import io.micronaut.core.util.StringUtils; import io.micronaut.health.HealthStatus; import io.micronaut.kubernetes.KubernetesConfiguration; import io.micronaut.kubernetes.client.reactor.CoreV1ApiReactorClient; @@ -50,6 +51,7 @@ @Requires(beans = HealthEndpoint.class) @Requires(env = Environment.KUBERNETES) @Requires(property = HOSTNAME_ENV_VARIABLE_IN_PROPERTY_FORMAT) +@Requires(property = HealthEndpoint.PREFIX + ".kubernetes.enabled", notEquals = StringUtils.FALSE) public class KubernetesHealthIndicator extends AbstractHealthIndicator> { public static final String NAME = "kubernetes"; diff --git a/src/main/docs/guide/health-checks.adoc b/src/main/docs/guide/health-checks.adoc index 5e9869365..871bfd328 100644 --- a/src/main/docs/guide/health-checks.adoc +++ b/src/main/docs/guide/health-checks.adoc @@ -84,3 +84,13 @@ Also note that in order to see the full details of the health checks you may nee https://docs.micronaut.io/latest/guide/index.html#healthEndpoint[the documentation of the Health Endpoint] for more information about how to configure it. ==== + +By default the KubernetesHealthIndicator is enabled. To disable it use the following configuration: + +[source,yaml] +---- +endpoints: + health: + kubernetes: + enabled: false +----