Skip to content

Commit ddb5cc3

Browse files
committed
Polish "Make Kubernetes detection slightly more efficient"
See gh-19002
1 parent aee22bf commit ddb5cc3

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

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

Lines changed: 13 additions & 20 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.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.cloud;
1818

19-
import java.io.File;
20-
2119
import org.springframework.core.env.ConfigurableEnvironment;
2220
import org.springframework.core.env.EnumerablePropertySource;
2321
import org.springframework.core.env.Environment;
@@ -75,22 +73,16 @@ public boolean isActive(Environment environment) {
7573
*/
7674
KUBERNETES {
7775

78-
private static final String SERVICE_HOST_SUFFIX = "_SERVICE_HOST";
79-
80-
private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT";
81-
82-
private static final String SECRET_LOCATION = "/var/run/secrets/kubernetes.io";
83-
8476
private static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST";
8577

8678
private static final String KUBERNETES_SERVICE_PORT = "KUBERNETES_SERVICE_PORT";
8779

80+
private static final String SERVICE_HOST_SUFFIX = "_SERVICE_HOST";
81+
82+
private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT";
83+
8884
@Override
8985
public boolean isActive(Environment environment) {
90-
if (environment.containsProperty(KUBERNETES_SERVICE_HOST)
91-
|| environment.containsProperty(KUBERNETES_SERVICE_PORT) || isSecretLocationExists()) {
92-
return true;
93-
}
9486
if (environment instanceof ConfigurableEnvironment) {
9587
return isActive((ConfigurableEnvironment) environment);
9688
}
@@ -100,8 +92,14 @@ public boolean isActive(Environment environment) {
10092
private boolean isActive(ConfigurableEnvironment environment) {
10193
PropertySource<?> environmentPropertySource = environment.getPropertySources()
10294
.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
103-
if (environmentPropertySource instanceof EnumerablePropertySource) {
104-
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+
}
105103
}
106104
return false;
107105
}
@@ -119,11 +117,6 @@ private boolean isActive(EnumerablePropertySource<?> environmentPropertySource)
119117
return false;
120118
}
121119

122-
private boolean isSecretLocationExists() {
123-
File file = new File(SECRET_LOCATION);
124-
return file.exists() && file.isDirectory();
125-
}
126-
127120
};
128121

129122
/**

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

Lines changed: 36 additions & 25 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.cloud;
1818

19-
import java.io.File;
2019
import java.util.Collections;
2120
import java.util.HashMap;
2221
import java.util.Map;
@@ -85,45 +84,57 @@ void getActiveWhenHasHcLandscapeShouldReturnSap() {
8584
}
8685

8786
@Test
88-
void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() {
89-
MockEnvironment environment = new MockEnvironment();
90-
Map<String, Object> source = new HashMap<>();
91-
source.put("EXAMPLE_SERVICE_HOST", "---");
92-
source.put("EXAMPLE_SERVICE_PORT", "8080");
93-
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
94-
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
95-
environment.getPropertySources().addFirst(propertySource);
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);
9692
CloudPlatform platform = CloudPlatform.getActive(environment);
9793
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
9894
assertThat(platform.isActive(environment)).isTrue();
9995
}
10096

10197
@Test
102-
void getActiveWhenHasKubernetesHostAndPortShouldReturnKubernetes() {
103-
MockEnvironment environment = new MockEnvironment();
104-
Map<String, Object> source = new HashMap<>();
105-
source.put("KUBERNETES_SERVICE_HOST", "---");
106-
source.put("KUBERNETES_SERVICE_PORT", "8080");
107-
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
108-
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source);
109-
environment.getPropertySources().addFirst(propertySource);
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+
113+
@Test
114+
void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() {
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);
110119
CloudPlatform platform = CloudPlatform.getActive(environment);
111120
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
112121
assertThat(platform.isActive(environment)).isTrue();
113122
}
114123

115124
@Test
116125
void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
126+
Environment environment = getEnvironmentWithEnvVariables(
127+
Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---"));
128+
CloudPlatform platform = CloudPlatform.getActive(environment);
129+
assertThat(platform).isNull();
130+
}
131+
132+
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
117133
MockEnvironment environment = new MockEnvironment();
118134
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
119-
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
120-
Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---"));
135+
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environmentVariables);
121136
environment.getPropertySources().addFirst(propertySource);
122-
CloudPlatform platform = CloudPlatform.getActive(environment);
123-
File path = new File("/var/run/secrets/kubernetes.io");
124-
if (!path.exists() && !path.isDirectory()) {
125-
assertThat(platform).isNull();
126-
}
137+
return environment;
127138
}
128139

129140
}

0 commit comments

Comments
 (0)