Detect runtime platform using CloudPlatform
Signed-off-by: kvmw <mshamsi@broadcom.com>
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.bindings.boot;
|
||||
|
||||
import org.springframework.boot.cloud.CloudPlatform;
|
||||
import org.springframework.cloud.bindings.Binding;
|
||||
import org.springframework.cloud.bindings.Bindings;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -59,15 +60,17 @@ final class EurekaBindingsPropertiesProcessor implements BindingsPropertiesProce
|
||||
properties.put("spring.cloud.loadbalancer.configurations", "zone-preference");
|
||||
|
||||
|
||||
String caCert = secret.get("ca.crt");
|
||||
if (caCert != null && !caCert.isEmpty()) {
|
||||
// generally apps using TLS bindings will be running in k8s where the host name is not meaningful,
|
||||
if (isKubernetesPlatform(environment)) {
|
||||
// generally for apps running in k8s hostname is not meaningful,
|
||||
// but we don't want to override the endpoint behavior the app has already set, in case they want to
|
||||
// explicitly set eureka.instance.hostname to route traffic through normal ingress.
|
||||
if (!environment.containsProperty("eureka.instance.preferIpAddress")) {
|
||||
properties.put("eureka.instance.preferIpAddress", true);
|
||||
}
|
||||
}
|
||||
|
||||
String caCert = secret.get("ca.crt");
|
||||
if (caCert != null && !caCert.isEmpty()) {
|
||||
String generatedPassword = PemSslStoreHelper.generatePassword();
|
||||
|
||||
// Create a trust store from the CA cert
|
||||
@@ -98,6 +101,10 @@ final class EurekaBindingsPropertiesProcessor implements BindingsPropertiesProce
|
||||
});
|
||||
}
|
||||
|
||||
private boolean isKubernetesPlatform(Environment environment) {
|
||||
return CloudPlatform.KUBERNETES == CloudPlatform.getActive(environment);
|
||||
}
|
||||
|
||||
private String hostnameFromUri(String uri) {
|
||||
if (!StringUtils.hasText(uri)) {
|
||||
return "";
|
||||
|
||||
@@ -62,21 +62,11 @@ final class EurekaBindingsPropertiesProcessorTest {
|
||||
new EurekaBindingsPropertiesProcessor().process(environment, bindings, properties);
|
||||
|
||||
assertThat(properties)
|
||||
.containsEntry("eureka.client.region", "default")
|
||||
.containsEntry("eureka.client.serviceUrl.defaultZone", "https://test-uri/eureka/")
|
||||
.containsEntry("spring.cloud.loadbalancer.configurations", "zone-preference")
|
||||
.containsEntry("eureka.instance.metadata-map.zone", "test-uri")
|
||||
.doesNotContainKey("eureka.client.oauth2.client-id")
|
||||
.doesNotContainKey("eureka.client.oauth2.access-token-uri")
|
||||
.doesNotContainKey("eureka.client.tls.trust-store")
|
||||
.doesNotContainKey("eureka.client.tls.trust-store-type")
|
||||
.doesNotContainKey("eureka.client.tls.trust-store-password")
|
||||
.doesNotContainKey("eureka.client.tls.key-alias")
|
||||
.doesNotContainKey("eureka.client.tls.key-store")
|
||||
.doesNotContainKey("eureka.client.tls.key-store-type")
|
||||
.doesNotContainKey("eureka.client.tls.key-store-password")
|
||||
.doesNotContainKey("eureka.client.tls.key-password")
|
||||
.doesNotContainKey("eureka.instance.preferIpAddress");
|
||||
.containsExactlyInAnyOrderEntriesOf(new FluentMap()
|
||||
.withEntry("eureka.client.region", "default")
|
||||
.withEntry("eureka.client.serviceUrl.defaultZone", "https://test-uri/eureka/")
|
||||
.withEntry("spring.cloud.loadbalancer.configurations", "zone-preference")
|
||||
.withEntry("eureka.instance.metadata-map.zone", "test-uri"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,8 +110,7 @@ final class EurekaBindingsPropertiesProcessorTest {
|
||||
.containsEntry("eureka.client.region", "default")
|
||||
.containsKey("eureka.client.tls.trust-store")
|
||||
.containsEntry("eureka.client.tls.trust-store-type", "PKCS12")
|
||||
.containsKey("eureka.client.tls.trust-store-password")
|
||||
.containsEntry("eureka.instance.preferIpAddress", true);
|
||||
.containsKey("eureka.client.tls.trust-store-password");
|
||||
assertDoesNotThrow(() -> {
|
||||
String path = properties.get("eureka.client.tls.trust-store").toString().substring(5);
|
||||
File f = new File(path);
|
||||
@@ -307,6 +296,27 @@ final class EurekaBindingsPropertiesProcessorTest {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("prefers ip address in kubernetes")
|
||||
void preferIpAddressInKubernetes() {
|
||||
environment.setProperty("spring.main.cloud-platform", "kubernetes");
|
||||
|
||||
new EurekaBindingsPropertiesProcessor().process(environment, bindings, properties);
|
||||
|
||||
assertThat(properties).containsEntry("eureka.instance.preferIpAddress", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("prefers ip address in kubernetes")
|
||||
void doesNotOverridePreferIpAddressInKubernetes() {
|
||||
environment.setProperty("eureka.instance.preferIpAddress", "false");
|
||||
environment.setProperty("spring.main.cloud-platform", "kubernetes");
|
||||
|
||||
new EurekaBindingsPropertiesProcessor().process(environment, bindings, properties);
|
||||
|
||||
assertThat(properties).doesNotContainKey("eureka.instance.preferIpAddress");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("can be disabled")
|
||||
void disabled() {
|
||||
|
||||
Reference in New Issue
Block a user