Skip to content

Commit dc90b92

Browse files
committed
Fix CloudPlatform#isActive
Fixes #25433
1 parent c35a4cc commit dc90b92

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 13 additions & 2 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.
@@ -29,6 +29,7 @@
2929
*
3030
* @author Phillip Webb
3131
* @author Brian Clozel
32+
* @author Nguyen Sach
3233
* @since 1.3.0
3334
*/
3435
public enum CloudPlatform {
@@ -140,7 +141,7 @@ private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySo
140141
* @return if the platform is active.
141142
*/
142143
public boolean isActive(Environment environment) {
143-
return isEnforced(environment) || isDetected(environment);
144+
return isEnforced(environment) || (isAutoDetectionEnabled(environment) && isDetected(environment));
144145
}
145146

146147
/**
@@ -178,6 +179,16 @@ private boolean isEnforced(String platform) {
178179
*/
179180
public abstract boolean isDetected(Environment environment);
180181

182+
/**
183+
* Determines if it is enabled that the platform is detected by
184+
* looking for platform-specific environment variables.
185+
* @param environment the environment
186+
* @return if the platform auto-detection is enabled.
187+
*/
188+
private boolean isAutoDetectionEnabled(Environment environment) {
189+
return environment.getProperty(PROPERTY_NAME) == null;
190+
}
191+
181192
/**
182193
* Returns if the platform is behind a load balancer and uses
183194
* {@literal X-Forwarded-For} headers.

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

Lines changed: 18 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

@@ -36,6 +39,7 @@
3639
* Tests for {@link CloudPlatform}.
3740
*
3841
* @author Phillip Webb
42+
* @author Nguyen Sach
3943
*/
4044
class CloudPlatformTests {
4145

@@ -177,6 +181,19 @@ void isEnforcedWhenBinderPropertyIsMissingReturnsFalse() {
177181
assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isFalse();
178182
}
179183

184+
@Test
185+
void isActiveWhenNoCloudPlatformIsEnforcedAndHasKubernetesServiceHostAndKubernetesServicePort() {
186+
Map<String, Object> envVars = new HashMap<>();
187+
envVars.put("EXAMPLE_SERVICE_HOST", "---");
188+
envVars.put("EXAMPLE_SERVICE_PORT", "8080");
189+
Environment environment = getEnvironmentWithEnvVariables(envVars);
190+
((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none");
191+
List<CloudPlatform> activeCloudPlatforms = Stream.of(CloudPlatform.values())
192+
.filter((cloudPlatform) -> cloudPlatform.isActive(environment))
193+
.collect(Collectors.toList());
194+
assertThat(activeCloudPlatforms).containsExactly(CloudPlatform.NONE);
195+
}
196+
180197
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
181198
MockEnvironment environment = new MockEnvironment();
182199
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(

0 commit comments

Comments
 (0)