diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java index 4f5fcff119..f440219081 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java @@ -29,6 +29,7 @@ import org.springframework.core.env.StandardEnvironment; * * @author Phillip Webb * @author Brian Clozel + * @author Nguyen Sach * @since 1.3.0 */ public enum CloudPlatform { @@ -157,7 +158,8 @@ public enum CloudPlatform { * @return if the platform is active. */ public boolean isActive(Environment environment) { - return isEnforced(environment) || isDetected(environment); + String platformProperty = environment.getProperty(PROPERTY_NAME); + return isEnforced(platformProperty) || (platformProperty == null && isDetected(environment)); } /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java index 9cbb7c1b77..afd4a26f5e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java @@ -19,6 +19,7 @@ package org.springframework.boot.cloud; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -36,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link CloudPlatform}. * * @author Phillip Webb + * @author Nguyen Sach */ class CloudPlatformTests { @@ -202,6 +204,16 @@ class CloudPlatformTests { assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isFalse(); } + void isActiveWhenNoCloudPlatformIsEnforcedAndHasKubernetesServiceHostAndKubernetesServicePort() { + Map envVars = new HashMap<>(); + envVars.put("EXAMPLE_SERVICE_HOST", "---"); + envVars.put("EXAMPLE_SERVICE_PORT", "8080"); + Environment environment = getEnvironmentWithEnvVariables(envVars); + ((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none"); + assertThat(Stream.of(CloudPlatform.values()).filter((platform) -> platform.isActive(environment))) + .containsExactly(CloudPlatform.NONE); + } + private Environment getEnvironmentWithEnvVariables(Map environmentVariables) { MockEnvironment environment = new MockEnvironment(); PropertySource propertySource = new SystemEnvironmentPropertySource(