From aee22bfb5120249640c6a5eadc3cc8687c567d01 Mon Sep 17 00:00:00 2001 From: "med-amine.dahmen" Date: Wed, 11 Dec 2019 15:38:43 +0100 Subject: [PATCH] Make Kubernetes detection slightly more efficient See gh-19002 --- .../boot/cloud/CloudPlatform.java | 17 ++++++++++++++++ .../boot/cloud/CloudPlatformTests.java | 20 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java index 2db1b48e56..9f1c19c856 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java @@ -16,6 +16,8 @@ package org.springframework.boot.cloud; +import java.io.File; + import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.Environment; @@ -77,8 +79,18 @@ public enum CloudPlatform { private static final String SERVICE_PORT_SUFFIX = "_SERVICE_PORT"; + private static final String SECRET_LOCATION = "/var/run/secrets/kubernetes.io"; + + private static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST"; + + private static final String KUBERNETES_SERVICE_PORT = "KUBERNETES_SERVICE_PORT"; + @Override public boolean isActive(Environment environment) { + if (environment.containsProperty(KUBERNETES_SERVICE_HOST) + || environment.containsProperty(KUBERNETES_SERVICE_PORT) || isSecretLocationExists()) { + return true; + } if (environment instanceof ConfigurableEnvironment) { return isActive((ConfigurableEnvironment) environment); } @@ -107,6 +119,11 @@ public enum CloudPlatform { return false; } + private boolean isSecretLocationExists() { + File file = new File(SECRET_LOCATION); + return file.exists() && file.isDirectory(); + } + }; /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java index 1eb711b981..7589a998e3 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.cloud; +import java.io.File; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -97,6 +98,20 @@ class CloudPlatformTests { assertThat(platform.isActive(environment)).isTrue(); } + @Test + void getActiveWhenHasKubernetesHostAndPortShouldReturnKubernetes() { + MockEnvironment environment = new MockEnvironment(); + Map source = new HashMap<>(); + source.put("KUBERNETES_SERVICE_HOST", "---"); + source.put("KUBERNETES_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 void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() { MockEnvironment environment = new MockEnvironment(); @@ -105,7 +120,10 @@ class CloudPlatformTests { Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---")); environment.getPropertySources().addFirst(propertySource); CloudPlatform platform = CloudPlatform.getActive(environment); - assertThat(platform).isNull(); + File path = new File("/var/run/secrets/kubernetes.io"); + if (!path.exists() && !path.isDirectory()) { + assertThat(platform).isNull(); + } } }