Skip to content

Commit aee22bf

Browse files
medamines1snicoll
authored andcommitted
Make Kubernetes detection slightly more efficient
See gh-19002
1 parent 37575d7 commit aee22bf

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.cloud;
1818

19+
import java.io.File;
20+
1921
import org.springframework.core.env.ConfigurableEnvironment;
2022
import org.springframework.core.env.EnumerablePropertySource;
2123
import org.springframework.core.env.Environment;
@@ -77,8 +79,18 @@ public boolean isActive(Environment environment) {
7779

7880
private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT";
7981

82+
private static final String SECRET_LOCATION = "/var/run/secrets/kubernetes.io";
83+
84+
private static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST";
85+
86+
private static final String KUBERNETES_SERVICE_PORT = "KUBERNETES_SERVICE_PORT";
87+
8088
@Override
8189
public boolean isActive(Environment environment) {
90+
if (environment.containsProperty(KUBERNETES_SERVICE_HOST)
91+
|| environment.containsProperty(KUBERNETES_SERVICE_PORT) || isSecretLocationExists()) {
92+
return true;
93+
}
8294
if (environment instanceof ConfigurableEnvironment) {
8395
return isActive((ConfigurableEnvironment) environment);
8496
}
@@ -107,6 +119,11 @@ private boolean isActive(EnumerablePropertySource<?> environmentPropertySource)
107119
return false;
108120
}
109121

122+
private boolean isSecretLocationExists() {
123+
File file = new File(SECRET_LOCATION);
124+
return file.exists() && file.isDirectory();
125+
}
126+
110127
};
111128

112129
/**

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.cloud;
1818

19+
import java.io.File;
1920
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.Map;
@@ -97,6 +98,20 @@ void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() {
9798
assertThat(platform.isActive(environment)).isTrue();
9899
}
99100

101+
@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);
110+
CloudPlatform platform = CloudPlatform.getActive(environment);
111+
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
112+
assertThat(platform.isActive(environment)).isTrue();
113+
}
114+
100115
@Test
101116
void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
102117
MockEnvironment environment = new MockEnvironment();
@@ -105,7 +120,10 @@ void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
105120
Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---"));
106121
environment.getPropertySources().addFirst(propertySource);
107122
CloudPlatform platform = CloudPlatform.getActive(environment);
108-
assertThat(platform).isNull();
123+
File path = new File("/var/run/secrets/kubernetes.io");
124+
if (!path.exists() && !path.isDirectory()) {
125+
assertThat(platform).isNull();
126+
}
109127
}
110128

111129
}

0 commit comments

Comments
 (0)