Skip to content

Commit ffc2cff

Browse files
committed
Override CloudPlatform auto-detection with configuration property
This commit adds the new "`spring.main.cloud-platform`" configuration property. This allows applications to override the auto-detection and force a specific Cloud Platform. This is useful for testing behavior on a local machine or force the detection of a particular platform. This commit also adds a new `CloudPlatform.NONE` value that allows applications to disable the auto-detection of the Cloud Platform, thus avoiding issues with false positives. Closes gh-20553
1 parent f4c68db commit ffc2cff

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,36 @@
2323
import org.springframework.core.env.StandardEnvironment;
2424

2525
/**
26-
* Simple detection for well known cloud platforms. For more advanced cloud provider
27-
* integration consider the Spring Cloud project.
26+
* Simple detection for well known cloud platforms. Detection can be forced using the
27+
* {@code "spring.main.cloud-platform"} configuration property. For more advanced cloud
28+
* provider integration consider the Spring Cloud project.
2829
*
2930
* @author Phillip Webb
31+
* @author Brian Clozel
3032
* @since 1.3.0
31-
* @see "https://cloud.spring.io"
33+
* @see "https://spring.io/projects/spring-cloud"
3234
*/
3335
public enum CloudPlatform {
3436

37+
/**
38+
* No Cloud platform. Useful when false-positives are detected.
39+
*/
40+
NONE {
41+
42+
@Override
43+
public boolean isAutoDetected(Environment environment) {
44+
return false;
45+
}
46+
47+
},
48+
3549
/**
3650
* Cloud Foundry platform.
3751
*/
3852
CLOUD_FOUNDRY {
3953

4054
@Override
41-
public boolean isActive(Environment environment) {
55+
public boolean isAutoDetected(Environment environment) {
4256
return environment.containsProperty("VCAP_APPLICATION") || environment.containsProperty("VCAP_SERVICES");
4357
}
4458

@@ -50,7 +64,7 @@ public boolean isActive(Environment environment) {
5064
HEROKU {
5165

5266
@Override
53-
public boolean isActive(Environment environment) {
67+
public boolean isAutoDetected(Environment environment) {
5468
return environment.containsProperty("DYNO");
5569
}
5670

@@ -62,7 +76,7 @@ public boolean isActive(Environment environment) {
6276
SAP {
6377

6478
@Override
65-
public boolean isActive(Environment environment) {
79+
public boolean isAutoDetected(Environment environment) {
6680
return environment.containsProperty("HC_LANDSCAPE");
6781
}
6882

@@ -82,14 +96,14 @@ public boolean isActive(Environment environment) {
8296
private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT";
8397

8498
@Override
85-
public boolean isActive(Environment environment) {
99+
public boolean isAutoDetected(Environment environment) {
86100
if (environment instanceof ConfigurableEnvironment) {
87-
return isActive((ConfigurableEnvironment) environment);
101+
return isAutoDetected((ConfigurableEnvironment) environment);
88102
}
89103
return false;
90104
}
91105

92-
private boolean isActive(ConfigurableEnvironment environment) {
106+
private boolean isAutoDetected(ConfigurableEnvironment environment) {
93107
PropertySource<?> environmentPropertySource = environment.getPropertySources()
94108
.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
95109
if (environmentPropertySource != null) {
@@ -98,13 +112,13 @@ private boolean isActive(ConfigurableEnvironment environment) {
98112
return true;
99113
}
100114
if (environmentPropertySource instanceof EnumerablePropertySource) {
101-
return isActive((EnumerablePropertySource<?>) environmentPropertySource);
115+
return isAutoDetected((EnumerablePropertySource<?>) environmentPropertySource);
102116
}
103117
}
104118
return false;
105119
}
106120

107-
private boolean isActive(EnumerablePropertySource<?> environmentPropertySource) {
121+
private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySource) {
108122
for (String propertyName : environmentPropertySource.getPropertyNames()) {
109123
if (propertyName.endsWith(SERVICE_HOST_SUFFIX)) {
110124
String serviceName = propertyName.substring(0,
@@ -124,7 +138,31 @@ private boolean isActive(EnumerablePropertySource<?> environmentPropertySource)
124138
* @param environment the environment
125139
* @return if the platform is active.
126140
*/
127-
public abstract boolean isActive(Environment environment);
141+
public boolean isActive(Environment environment) {
142+
return isEnforced(environment) || isAutoDetected(environment);
143+
}
144+
145+
/**
146+
* Detemines if the platform is enforced by looking at the
147+
* {@code "spring.main.cloud-platform"} configuration property.
148+
* @param environment the environment
149+
* @return if the platform is enforced
150+
*/
151+
public boolean isEnforced(Environment environment) {
152+
String platform = environment.getProperty("spring.main.cloud-platform");
153+
if (platform != null) {
154+
return this.name().equalsIgnoreCase(platform);
155+
}
156+
return false;
157+
}
158+
159+
/**
160+
* Determines if the platform is auto-detected by looking for platform-specific
161+
* environment variables.
162+
* @param environment the environment
163+
* @return if the platform is auto-detected.
164+
*/
165+
public abstract boolean isAutoDetected(Environment environment);
128166

129167
/**
130168
* Returns if the platform is behind a load balancer and uses

spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,11 @@
640640
"description": "Mode used to display the banner when the application runs.",
641641
"defaultValue": "console"
642642
},
643+
{
644+
"name": "spring.main.cloud-platform",
645+
"type": "org.springframework.boot.cloud.CloudPlatform",
646+
"description": "Override the Cloud Platform auto-detection."
647+
},
643648
{
644649
"name": "spring.main.lazy-initialization",
645650
"type": "java.lang.Boolean",

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
129129
assertThat(platform).isNull();
130130
}
131131

132+
@Test
133+
void getActiveWhenHasEnforcedCloudPlatform() {
134+
Environment environment = getEnvironmentWithEnvVariables(
135+
Collections.singletonMap("spring.main.cloud-platform", "kubernetes"));
136+
CloudPlatform platform = CloudPlatform.getActive(environment);
137+
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
138+
}
139+
132140
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
133141
MockEnvironment environment = new MockEnvironment();
134142
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(

0 commit comments

Comments
 (0)