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 81be02b9694..c3f45d44888 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 @@ -16,7 +16,11 @@ package org.springframework.boot.cloud; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.StandardEnvironment; +import org.springframework.util.StringUtils; /** * Simple detection for well known cloud platforms. For more advanced cloud provider @@ -63,6 +67,33 @@ public enum CloudPlatform { return environment.containsProperty("HC_LANDSCAPE"); } + }, + + /** + * Kubernetes platform. + */ + KUBERNETES { + @Override + public boolean isActive(Environment environment) { + if (environment instanceof ConfigurableEnvironment) { + MapPropertySource propertySource = (MapPropertySource) ((ConfigurableEnvironment) environment) + .getPropertySources() + .get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); + if (propertySource != null) { + for (String name : propertySource.getPropertyNames()) { + if (name.endsWith("_SERVICE_HOST")) { + String serviceName = StringUtils.split(name, + "_SERVICE_HOST")[0]; + if (propertySource + .getProperty(serviceName + "_SERVICE_PORT") != null) { + return true; + } + } + } + } + } + return false; + } }; /** 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 495fa39367a..c2325ec6c60 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 @@ -16,9 +16,16 @@ package org.springframework.boot.cloud; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + import org.junit.Test; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; +import org.springframework.core.env.StandardEnvironment; +import org.springframework.core.env.SystemEnvironmentPropertySource; import org.springframework.mock.env.MockEnvironment; import static org.assertj.core.api.Assertions.assertThat; @@ -79,4 +86,29 @@ public class CloudPlatformTests { assertThat(platform.isActive(environment)).isTrue(); } + @Test + public void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() { + MockEnvironment environment = new MockEnvironment(); + Map source = new HashMap<>(); + source.put("EXAMPLE_SERVICE_HOST", "---"); + source.put("EXAMPLE_SERVICE_PORT", "8080"); + PropertySource propertySource = new SystemEnvironmentPropertySource( + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source); + environment.getPropertySources().addFirst(propertySource); + CloudPlatform platform = CloudPlatform.getActive(environment); + assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES); + assertThat(platform.isActive(environment)).isTrue(); + } + + @Test + public void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() { + MockEnvironment environment = new MockEnvironment(); + PropertySource propertySource = new SystemEnvironmentPropertySource( + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, + Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---")); + environment.getPropertySources().addFirst(propertySource); + CloudPlatform platform = CloudPlatform.getActive(environment); + assertThat(platform).isNull(); + } + }