Merge pull request #19002 from medamines1

* pr/19002:
  Polish "Make Kubernetes detection slightly more efficient"
  Make Kubernetes detection slightly more efficient

Closes gh-19002
pull/20222/head
Stephane Nicoll 5 years ago
commit ab72237b1b

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -73,6 +73,10 @@ public enum CloudPlatform {
*/ */
KUBERNETES { KUBERNETES {
private static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST";
private static final String KUBERNETES_SERVICE_PORT = "KUBERNETES_SERVICE_PORT";
private static final String SERVICE_HOST_SUFFIX = "_SERVICE_HOST"; private static final String SERVICE_HOST_SUFFIX = "_SERVICE_HOST";
private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT"; private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT";
@ -88,9 +92,15 @@ public enum CloudPlatform {
private boolean isActive(ConfigurableEnvironment environment) { private boolean isActive(ConfigurableEnvironment environment) {
PropertySource<?> environmentPropertySource = environment.getPropertySources() PropertySource<?> environmentPropertySource = environment.getPropertySources()
.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); .get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
if (environmentPropertySource != null) {
if (environmentPropertySource.containsProperty(KUBERNETES_SERVICE_HOST)
&& environmentPropertySource.containsProperty(KUBERNETES_SERVICE_PORT)) {
return true;
}
if (environmentPropertySource instanceof EnumerablePropertySource) { if (environmentPropertySource instanceof EnumerablePropertySource) {
return isActive((EnumerablePropertySource<?>) environmentPropertySource); return isActive((EnumerablePropertySource<?>) environmentPropertySource);
} }
}
return false; return false;
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -83,15 +83,39 @@ class CloudPlatformTests {
assertThat(platform.isActive(environment)).isTrue(); assertThat(platform.isActive(environment)).isTrue();
} }
@Test
void getActiveWhenHasKubernetesServiceHostAndPortShouldReturnKubernetes() {
Map<String, Object> envVars = new HashMap<>();
envVars.put("KUBERNETES_SERVICE_HOST", "---");
envVars.put("KUBERNETES_SERVICE_PORT", "8080");
Environment environment = getEnvironmentWithEnvVariables(envVars);
CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
assertThat(platform.isActive(environment)).isTrue();
}
@Test
void getActiveWhenHasKubernetesServiceHostAndNoKubernetesServicePortShouldNotReturnKubernetes() {
Environment environment = getEnvironmentWithEnvVariables(
Collections.singletonMap("KUBERNETES_SERVICE_HOST", "---"));
CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isNull();
}
@Test
void getActiveWhenHasKubernetesServicePortAndNoKubernetesServiceHostShouldNotReturnKubernetes() {
Environment environment = getEnvironmentWithEnvVariables(
Collections.singletonMap("KUBERNETES_SERVICE_PORT", "8080"));
CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isNull();
}
@Test @Test
void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() { void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() {
MockEnvironment environment = new MockEnvironment(); Map<String, Object> envVars = new HashMap<>();
Map<String, Object> source = new HashMap<>(); envVars.put("EXAMPLE_SERVICE_HOST", "---");
source.put("EXAMPLE_SERVICE_HOST", "---"); envVars.put("EXAMPLE_SERVICE_PORT", "8080");
source.put("EXAMPLE_SERVICE_PORT", "8080"); Environment environment = getEnvironmentWithEnvVariables(envVars);
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
environment.getPropertySources().addFirst(propertySource);
CloudPlatform platform = CloudPlatform.getActive(environment); CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES); assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
assertThat(platform.isActive(environment)).isTrue(); assertThat(platform.isActive(environment)).isTrue();
@ -99,13 +123,18 @@ class CloudPlatformTests {
@Test @Test
void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() { void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
MockEnvironment environment = new MockEnvironment(); Environment environment = getEnvironmentWithEnvVariables(
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---")); Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---"));
environment.getPropertySources().addFirst(propertySource);
CloudPlatform platform = CloudPlatform.getActive(environment); CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isNull(); assertThat(platform).isNull();
} }
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
MockEnvironment environment = new MockEnvironment();
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environmentVariables);
environment.getPropertySources().addFirst(propertySource);
return environment;
}
} }

Loading…
Cancel
Save