Skip to content

Commit 6272205

Browse files
committed
Activate health probes in Cloud Foundry environments
This commit ensures that Health probes are automatically enabled when the application runs on Cloud Foundry. This was already the case for Kubernetes, but now that Cloud Foundry supports this feature we should do the same. Closes gh-39804
1 parent 5da51aa commit 6272205

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/availability/AvailabilityProbesAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,6 +91,9 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
9191
if (CloudPlatform.getActive(environment) == CloudPlatform.KUBERNETES) {
9292
return ConditionOutcome.match(message.because("running on Kubernetes"));
9393
}
94+
if (CloudPlatform.getActive(environment) == CloudPlatform.CLOUD_FOUNDRY) {
95+
return ConditionOutcome.match(message.because("running on Cloud Foundry"));
96+
}
9497
return ConditionOutcome.noMatch(message.because("not running on a supported cloud platform"));
9598
}
9699

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/availability/AvailabilityProbesAutoConfigurationTests.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.autoconfigure.AutoConfigurations;
2424
import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration;
2525
import org.springframework.boot.availability.ApplicationAvailability;
26+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2627
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2728

2829
import static org.assertj.core.api.Assertions.assertThat;
@@ -40,43 +41,47 @@ class AvailabilityProbesAutoConfigurationTests {
4041

4142
@Test
4243
void probesWhenNotKubernetesAddsNoBeans() {
43-
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ApplicationAvailability.class)
44-
.doesNotHaveBean(LivenessStateHealthIndicator.class)
45-
.doesNotHaveBean(ReadinessStateHealthIndicator.class)
46-
.doesNotHaveBean(AvailabilityProbesHealthEndpointGroupsPostProcessor.class));
44+
this.contextRunner.run(this::doesNotHaveProbeBeans);
4745
}
4846

4947
@Test
5048
void probesWhenKubernetesAddsBeans() {
51-
this.contextRunner.withPropertyValues("spring.main.cloud-platform=kubernetes")
52-
.run((context) -> assertThat(context).hasSingleBean(ApplicationAvailability.class)
53-
.hasSingleBean(LivenessStateHealthIndicator.class)
54-
.hasBean("livenessStateHealthIndicator")
55-
.hasSingleBean(ReadinessStateHealthIndicator.class)
56-
.hasBean("readinessStateHealthIndicator")
57-
.hasSingleBean(AvailabilityProbesHealthEndpointGroupsPostProcessor.class));
49+
this.contextRunner.withPropertyValues("spring.main.cloud-platform=kubernetes").run(this::hasProbesBeans);
50+
}
51+
52+
@Test
53+
void probesWhenCloudFoundryAddsBeans() {
54+
this.contextRunner.withPropertyValues("spring.main.cloud-platform=cloud_foundry").run(this::hasProbesBeans);
5855
}
5956

6057
@Test
6158
void probesWhenPropertyEnabledAddsBeans() {
6259
this.contextRunner.withPropertyValues("management.endpoint.health.probes.enabled=true")
63-
.run((context) -> assertThat(context).hasSingleBean(ApplicationAvailability.class)
64-
.hasSingleBean(LivenessStateHealthIndicator.class)
65-
.hasBean("livenessStateHealthIndicator")
66-
.hasSingleBean(ReadinessStateHealthIndicator.class)
67-
.hasBean("readinessStateHealthIndicator")
68-
.hasSingleBean(AvailabilityProbesHealthEndpointGroupsPostProcessor.class));
60+
.run(this::hasProbesBeans);
6961
}
7062

7163
@Test
7264
void probesWhenKubernetesAndPropertyDisabledAddsNotBeans() {
7365
this.contextRunner
7466
.withPropertyValues("spring.main.cloud-platform=kubernetes",
7567
"management.endpoint.health.probes.enabled=false")
76-
.run((context) -> assertThat(context).hasSingleBean(ApplicationAvailability.class)
77-
.doesNotHaveBean(LivenessStateHealthIndicator.class)
78-
.doesNotHaveBean(ReadinessStateHealthIndicator.class)
79-
.doesNotHaveBean(AvailabilityProbesHealthEndpointGroupsPostProcessor.class));
68+
.run(this::doesNotHaveProbeBeans);
69+
}
70+
71+
private void hasProbesBeans(AssertableApplicationContext context) {
72+
assertThat(context).hasSingleBean(ApplicationAvailability.class)
73+
.hasSingleBean(LivenessStateHealthIndicator.class)
74+
.hasBean("livenessStateHealthIndicator")
75+
.hasSingleBean(ReadinessStateHealthIndicator.class)
76+
.hasBean("readinessStateHealthIndicator")
77+
.hasSingleBean(AvailabilityProbesHealthEndpointGroupsPostProcessor.class);
78+
}
79+
80+
private void doesNotHaveProbeBeans(AssertableApplicationContext context) {
81+
assertThat(context).hasSingleBean(ApplicationAvailability.class)
82+
.doesNotHaveBean(LivenessStateHealthIndicator.class)
83+
.doesNotHaveBean(ReadinessStateHealthIndicator.class)
84+
.doesNotHaveBean(AvailabilityProbesHealthEndpointGroupsPostProcessor.class);
8085
}
8186

8287
}

0 commit comments

Comments
 (0)