Skip to content

Commit

Permalink
Add support to detect Kubernetes platform in CloudPlatform
Browse files Browse the repository at this point in the history
Closes gh-15537
  • Loading branch information
mbhave committed Feb 21, 2019
1 parent 0f0adb4 commit adea701
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,6 +67,33 @@ public boolean isActive(Environment environment) {
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;
}
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,4 +86,29 @@ public void getActiveWhenHasHcLandscapeShouldReturnSap() {
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();
}

}

0 comments on commit adea701

Please sign in to comment.