From 4be64e8f9c219f6ce723d99d4afe6e2ef7b8369d Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 23 Mar 2020 21:17:23 +0100 Subject: [PATCH] Add "management.health.probes.enabled" config property Prior to this commit, we were relying on the `"spring.main.cloud-platform"` property for overriding cloud platform detection and enabling liveness and readiness probes. Changes made in gh-20553 have now been reverted. This commit adds the `"management.health.probes.enabled"` configuration property. The auto-configuration now enables the HTTP Probes and `HealthIndicator` if this property is enabled, or if the Kubernetes cloud platform is detected. This property is `false` by default for now, since enabling this for all Spring Boot applications would be a breaking change. In this case, the global `"/actuator/health"` endpoint could report `OUT_OF_SERVICE` during startup time because the application now reports the readiness as well. See gh-19593 --- ...besHealthContributorAutoConfiguration.java | 24 ++++++++++++++++++- ...itional-spring-configuration-metadata.json | 6 +++++ ...althContributorAutoConfigurationTests.java | 4 ++-- .../src/docs/asciidoc/deployment.adoc | 2 +- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/kubernetes/ProbesHealthContributorAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/kubernetes/ProbesHealthContributorAutoConfiguration.java index 7259724781..bd01f0592e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/kubernetes/ProbesHealthContributorAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/kubernetes/ProbesHealthContributorAutoConfiguration.java @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.autoconfigure.kubernetes; import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator; +import org.springframework.boot.actuate.autoconfigure.kubernetes.ProbesHealthContributorAutoConfiguration.KubernetesOrPropertyCondition; import org.springframework.boot.actuate.availability.LivenessProbeHealthIndicator; import org.springframework.boot.actuate.availability.ReadinessProbeHealthIndicator; import org.springframework.boot.actuate.health.HealthEndpointGroupsRegistryCustomizer; @@ -24,11 +25,14 @@ import org.springframework.boot.actuate.kubernetes.ProbesHealthEndpointGroupsReg import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.availability.ApplicationAvailabilityProvider; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; /** @@ -39,7 +43,7 @@ import org.springframework.context.annotation.Configuration; * @since 2.3.0 */ @Configuration(proxyBeanMethods = false) -@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) +@Conditional(KubernetesOrPropertyCondition.class) @AutoConfigureAfter(ApplicationAvailabilityAutoConfiguration.class) public class ProbesHealthContributorAutoConfiguration { @@ -64,4 +68,22 @@ public class ProbesHealthContributorAutoConfiguration { return new ProbesHealthEndpointGroupsRegistrar(); } + static class KubernetesOrPropertyCondition extends AnyNestedCondition { + + KubernetesOrPropertyCondition() { + super(ConfigurationPhase.PARSE_CONFIGURATION); + } + + @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) + static class Kubernetes { + + } + + @ConditionalOnProperty(prefix = "management.health.probes", name = "enabled") + static class ProbesIndicatorsEnabled { + + } + + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 1be8a99d86..4ba0d95641 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -146,6 +146,12 @@ "description": "Whether to enable ping health check.", "defaultValue": true }, + { + "name": "management.health.probes.enabled", + "type": "java.lang.Boolean", + "description": "Whether to enable liveness and readiness probes.", + "defaultValue": false + }, { "name": "management.health.rabbit.enabled", "type": "java.lang.Boolean", diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/kubernetes/ProbesHealthContributorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/kubernetes/ProbesHealthContributorAutoConfigurationTests.java index 57e0c937d5..fa919c537e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/kubernetes/ProbesHealthContributorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/kubernetes/ProbesHealthContributorAutoConfigurationTests.java @@ -47,8 +47,8 @@ class ProbesHealthContributorAutoConfigurationTests { } @Test - void probesConfiguredIfKubernetes() { - this.contextRunner.withPropertyValues("spring.main.cloud-platform=kubernetes") + void probesConfiguredIfProperty() { + this.contextRunner.withPropertyValues("management.health.probes.enabled=true") .run((context) -> assertThat(context).hasSingleBean(ApplicationAvailabilityProvider.class) .hasSingleBean(LivenessProbeHealthIndicator.class) .hasSingleBean(ReadinessProbeHealthIndicator.class) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment.adoc index 328cde83c7..5aaf91e152 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment.adoc @@ -184,7 +184,7 @@ TIP: The https://github.com/pivotal-cf/java-cfenv/[Java CFEnv] project is a bett [[cloud-deployment-kubernetes]] === Kubernetes Spring Boot auto-detects Kubernetes deployment environments by checking the environment for `"*_SERVICE_HOST"` and `"*_SERVICE_PORT"` variables. -You can override this detection with the configprop:spring.main.cloud-platform[] configuration property. +You can override this detection with the configprop:management.health.probes.enabled[] configuration property. Spring Boot helps you to <> and export it with <>.