Add support to detect Kubernetes platform in CloudPlatform
Closes gh-15537
This commit is contained in:
parent
0f0adb4cd0
commit
adea7014a9
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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<String, Object> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue