Skip to content

Commit ab72237

Browse files
committed
Merge pull request #19002 from medamines1
* pr/19002: Polish "Make Kubernetes detection slightly more efficient" Make Kubernetes detection slightly more efficient Closes gh-19002
2 parents 37575d7 + ddb5cc3 commit ab72237

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,6 +73,10 @@ public boolean isActive(Environment environment) {
7373
*/
7474
KUBERNETES {
7575

76+
private static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST";
77+
78+
private static final String KUBERNETES_SERVICE_PORT = "KUBERNETES_SERVICE_PORT";
79+
7680
private static final String SERVICE_HOST_SUFFIX = "_SERVICE_HOST";
7781

7882
private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT";
@@ -88,8 +92,14 @@ public boolean isActive(Environment environment) {
8892
private boolean isActive(ConfigurableEnvironment environment) {
8993
PropertySource<?> environmentPropertySource = environment.getPropertySources()
9094
.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
91-
if (environmentPropertySource instanceof EnumerablePropertySource) {
92-
return isActive((EnumerablePropertySource<?>) environmentPropertySource);
95+
if (environmentPropertySource != null) {
96+
if (environmentPropertySource.containsProperty(KUBERNETES_SERVICE_HOST)
97+
&& environmentPropertySource.containsProperty(KUBERNETES_SERVICE_PORT)) {
98+
return true;
99+
}
100+
if (environmentPropertySource instanceof EnumerablePropertySource) {
101+
return isActive((EnumerablePropertySource<?>) environmentPropertySource);
102+
}
93103
}
94104
return false;
95105
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,29 +83,58 @@ void getActiveWhenHasHcLandscapeShouldReturnSap() {
8383
assertThat(platform.isActive(environment)).isTrue();
8484
}
8585

86+
@Test
87+
void getActiveWhenHasKubernetesServiceHostAndPortShouldReturnKubernetes() {
88+
Map<String, Object> envVars = new HashMap<>();
89+
envVars.put("KUBERNETES_SERVICE_HOST", "---");
90+
envVars.put("KUBERNETES_SERVICE_PORT", "8080");
91+
Environment environment = getEnvironmentWithEnvVariables(envVars);
92+
CloudPlatform platform = CloudPlatform.getActive(environment);
93+
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
94+
assertThat(platform.isActive(environment)).isTrue();
95+
}
96+
97+
@Test
98+
void getActiveWhenHasKubernetesServiceHostAndNoKubernetesServicePortShouldNotReturnKubernetes() {
99+
Environment environment = getEnvironmentWithEnvVariables(
100+
Collections.singletonMap("KUBERNETES_SERVICE_HOST", "---"));
101+
CloudPlatform platform = CloudPlatform.getActive(environment);
102+
assertThat(platform).isNull();
103+
}
104+
105+
@Test
106+
void getActiveWhenHasKubernetesServicePortAndNoKubernetesServiceHostShouldNotReturnKubernetes() {
107+
Environment environment = getEnvironmentWithEnvVariables(
108+
Collections.singletonMap("KUBERNETES_SERVICE_PORT", "8080"));
109+
CloudPlatform platform = CloudPlatform.getActive(environment);
110+
assertThat(platform).isNull();
111+
}
112+
86113
@Test
87114
void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() {
88-
MockEnvironment environment = new MockEnvironment();
89-
Map<String, Object> source = new HashMap<>();
90-
source.put("EXAMPLE_SERVICE_HOST", "---");
91-
source.put("EXAMPLE_SERVICE_PORT", "8080");
92-
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
93-
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
94-
environment.getPropertySources().addFirst(propertySource);
115+
Map<String, Object> envVars = new HashMap<>();
116+
envVars.put("EXAMPLE_SERVICE_HOST", "---");
117+
envVars.put("EXAMPLE_SERVICE_PORT", "8080");
118+
Environment environment = getEnvironmentWithEnvVariables(envVars);
95119
CloudPlatform platform = CloudPlatform.getActive(environment);
96120
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
97121
assertThat(platform.isActive(environment)).isTrue();
98122
}
99123

100124
@Test
101125
void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
102-
MockEnvironment environment = new MockEnvironment();
103-
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
104-
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
126+
Environment environment = getEnvironmentWithEnvVariables(
105127
Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---"));
106-
environment.getPropertySources().addFirst(propertySource);
107128
CloudPlatform platform = CloudPlatform.getActive(environment);
108129
assertThat(platform).isNull();
109130
}
110131

132+
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
133+
MockEnvironment environment = new MockEnvironment();
134+
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
135+
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environmentVariables);
136+
environment.getPropertySources().addFirst(propertySource);
137+
return environment;
138+
}
139+
111140
}

0 commit comments

Comments
 (0)