From 70f44d3a566a6af4497b261ba98014c9ab3a87b0 Mon Sep 17 00:00:00 2001 From: nguyensach Date: Sat, 27 Feb 2021 12:16:19 +0900 Subject: [PATCH 1/2] Don't detect CloudPlatform when property is set Update `CloudPlatform.isActive` to back-off from detection when any `spring.main.cloud-platform` property is set. See gh-25455 --- .../boot/cloud/CloudPlatform.java | 19 ++++++++++++++++--- .../boot/cloud/CloudPlatformTests.java | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) 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 f74b4135f0..50e894feb6 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.springframework.core.env.StandardEnvironment; * * @author Phillip Webb * @author Brian Clozel + * @author Nguyen Sach * @since 1.3.0 */ public enum CloudPlatform { @@ -131,13 +132,15 @@ public enum CloudPlatform { }; + private static final String PROPERTY_NAME = "spring.main.cloud-platform"; + /** * Determines if the platform is active (i.e. the application is running in it). * @param environment the environment * @return if the platform is active. */ public boolean isActive(Environment environment) { - return isEnforced(environment) || isDetected(environment); + return isEnforced(environment) || (isAutoDetectionEnabled(environment) && isDetected(environment)); } /** @@ -148,7 +151,7 @@ public enum CloudPlatform { * @since 2.3.0 */ public boolean isEnforced(Environment environment) { - String platform = environment.getProperty("spring.main.cloud-platform"); + String platform = environment.getProperty(PROPERTY_NAME); return name().equalsIgnoreCase(platform); } @@ -161,6 +164,16 @@ public enum CloudPlatform { */ public abstract boolean isDetected(Environment environment); + /** + * Determines if it is enabled that the platform is detected by looking for + * platform-specific environment variables. + * @param environment the environment + * @return if the platform auto-detection is enabled. + */ + private boolean isAutoDetectionEnabled(Environment environment) { + return environment.getProperty(PROPERTY_NAME) == null; + } + /** * Returns if the platform is behind a load balancer and uses * {@literal X-Forwarded-For} headers. 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 c308c057d2..f85a218d3b 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,10 @@ package org.springframework.boot.cloud; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -34,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link CloudPlatform}. * * @author Phillip Webb + * @author Nguyen Sach */ class CloudPlatformTests { @@ -137,6 +141,18 @@ class CloudPlatformTests { assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES); } + @Test + void isActiveWhenNoCloudPlatformIsEnforcedAndHasKubernetesServiceHostAndKubernetesServicePort() { + Map envVars = new HashMap<>(); + envVars.put("EXAMPLE_SERVICE_HOST", "---"); + envVars.put("EXAMPLE_SERVICE_PORT", "8080"); + Environment environment = getEnvironmentWithEnvVariables(envVars); + ((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none"); + List activeCloudPlatforms = Stream.of(CloudPlatform.values()) + .filter((cloudPlatform) -> cloudPlatform.isActive(environment)).collect(Collectors.toList()); + assertThat(activeCloudPlatforms).containsExactly(CloudPlatform.NONE); + } + private Environment getEnvironmentWithEnvVariables(Map environmentVariables) { MockEnvironment environment = new MockEnvironment(); PropertySource propertySource = new SystemEnvironmentPropertySource( From 61ff3c98bfa99b8b9b667463eb91a8f64e030da7 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 14 Apr 2021 17:22:33 -0700 Subject: [PATCH 2/2] Polish 'Don't detect CloudPlatform when property is set' See gh-25455 --- .../boot/cloud/CloudPlatform.java | 18 ++++++------------ .../boot/cloud/CloudPlatformTests.java | 7 ++----- 2 files changed, 8 insertions(+), 17 deletions(-) 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 50e894feb6..547cb53230 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 @@ -140,7 +140,8 @@ public enum CloudPlatform { * @return if the platform is active. */ public boolean isActive(Environment environment) { - return isEnforced(environment) || (isAutoDetectionEnabled(environment) && isDetected(environment)); + String platformProperty = environment.getProperty(PROPERTY_NAME); + return isEnforced(platformProperty) || (platformProperty == null && isDetected(environment)); } /** @@ -151,7 +152,10 @@ public enum CloudPlatform { * @since 2.3.0 */ public boolean isEnforced(Environment environment) { - String platform = environment.getProperty(PROPERTY_NAME); + return isEnforced(environment.getProperty(PROPERTY_NAME)); + } + + private boolean isEnforced(String platform) { return name().equalsIgnoreCase(platform); } @@ -164,16 +168,6 @@ public enum CloudPlatform { */ public abstract boolean isDetected(Environment environment); - /** - * Determines if it is enabled that the platform is detected by looking for - * platform-specific environment variables. - * @param environment the environment - * @return if the platform auto-detection is enabled. - */ - private boolean isAutoDetectionEnabled(Environment environment) { - return environment.getProperty(PROPERTY_NAME) == null; - } - /** * Returns if the platform is behind a load balancer and uses * {@literal X-Forwarded-For} headers. 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 f85a218d3b..c92bb4e334 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 @@ -18,9 +18,7 @@ package org.springframework.boot.cloud; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -148,9 +146,8 @@ class CloudPlatformTests { envVars.put("EXAMPLE_SERVICE_PORT", "8080"); Environment environment = getEnvironmentWithEnvVariables(envVars); ((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none"); - List activeCloudPlatforms = Stream.of(CloudPlatform.values()) - .filter((cloudPlatform) -> cloudPlatform.isActive(environment)).collect(Collectors.toList()); - assertThat(activeCloudPlatforms).containsExactly(CloudPlatform.NONE); + assertThat(Stream.of(CloudPlatform.values()).filter((platform) -> platform.isActive(environment))) + .containsExactly(CloudPlatform.NONE); } private Environment getEnvironmentWithEnvVariables(Map environmentVariables) {