Skip to content

Commit 70f44d3

Browse files
nguyensachphilwebb
authored andcommitted
Don't detect CloudPlatform when property is set
Update `CloudPlatform.isActive` to back-off from detection when any `spring.main.cloud-platform` property is set. See gh-25455
1 parent fcb2210 commit 70f44d3

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -28,6 +28,7 @@
2828
*
2929
* @author Phillip Webb
3030
* @author Brian Clozel
31+
* @author Nguyen Sach
3132
* @since 1.3.0
3233
*/
3334
public enum CloudPlatform {
@@ -131,13 +132,15 @@ private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySo
131132

132133
};
133134

135+
private static final String PROPERTY_NAME = "spring.main.cloud-platform";
136+
134137
/**
135138
* Determines if the platform is active (i.e. the application is running in it).
136139
* @param environment the environment
137140
* @return if the platform is active.
138141
*/
139142
public boolean isActive(Environment environment) {
140-
return isEnforced(environment) || isDetected(environment);
143+
return isEnforced(environment) || (isAutoDetectionEnabled(environment) && isDetected(environment));
141144
}
142145

143146
/**
@@ -148,7 +151,7 @@ public boolean isActive(Environment environment) {
148151
* @since 2.3.0
149152
*/
150153
public boolean isEnforced(Environment environment) {
151-
String platform = environment.getProperty("spring.main.cloud-platform");
154+
String platform = environment.getProperty(PROPERTY_NAME);
152155
return name().equalsIgnoreCase(platform);
153156
}
154157

@@ -161,6 +164,16 @@ public boolean isEnforced(Environment environment) {
161164
*/
162165
public abstract boolean isDetected(Environment environment);
163166

167+
/**
168+
* Determines if it is enabled that the platform is detected by looking for
169+
* platform-specific environment variables.
170+
* @param environment the environment
171+
* @return if the platform auto-detection is enabled.
172+
*/
173+
private boolean isAutoDetectionEnabled(Environment environment) {
174+
return environment.getProperty(PROPERTY_NAME) == null;
175+
}
176+
164177
/**
165178
* Returns if the platform is behind a load balancer and uses
166179
* {@literal X-Forwarded-For} headers.

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -18,7 +18,10 @@
1818

1919
import java.util.Collections;
2020
import java.util.HashMap;
21+
import java.util.List;
2122
import java.util.Map;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
2225

2326
import org.junit.jupiter.api.Test;
2427

@@ -34,6 +37,7 @@
3437
* Tests for {@link CloudPlatform}.
3538
*
3639
* @author Phillip Webb
40+
* @author Nguyen Sach
3741
*/
3842
class CloudPlatformTests {
3943

@@ -137,6 +141,18 @@ void getActiveWhenHasEnforcedCloudPlatform() {
137141
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
138142
}
139143

144+
@Test
145+
void isActiveWhenNoCloudPlatformIsEnforcedAndHasKubernetesServiceHostAndKubernetesServicePort() {
146+
Map<String, Object> envVars = new HashMap<>();
147+
envVars.put("EXAMPLE_SERVICE_HOST", "---");
148+
envVars.put("EXAMPLE_SERVICE_PORT", "8080");
149+
Environment environment = getEnvironmentWithEnvVariables(envVars);
150+
((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none");
151+
List<CloudPlatform> activeCloudPlatforms = Stream.of(CloudPlatform.values())
152+
.filter((cloudPlatform) -> cloudPlatform.isActive(environment)).collect(Collectors.toList());
153+
assertThat(activeCloudPlatforms).containsExactly(CloudPlatform.NONE);
154+
}
155+
140156
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
141157
MockEnvironment environment = new MockEnvironment();
142158
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(

0 commit comments

Comments
 (0)