Set an environment variable on backing apps with the value of the service instance GUID.

This commit is contained in:
Scott Frederick
2019-01-31 17:34:28 -06:00
committed by Oliver Hughes
parent 6cd9c68058
commit 219aa16e1e
18 changed files with 124 additions and 77 deletions

View File

@@ -388,7 +388,8 @@ public class CloudFoundryAppDeployer implements AppDeployer, ResourceLoaderAware
ApplicationManifest.Builder manifest = ApplicationManifest.builder()
.name(request.getName())
.path(getApplication(appResource))
.environmentVariables(getEnvironmentVariables(deploymentProperties, request.getEnvironment()))
.environmentVariables(getEnvironmentVariables(deploymentProperties,
request.getEnvironment(), request.getServiceInstanceId()))
.services(request.getServices())
.instances(instances(deploymentProperties))
.memory(memory(deploymentProperties))
@@ -533,7 +534,8 @@ public class CloudFoundryAppDeployer implements AppDeployer, ResourceLoaderAware
}
private Map<String, Object> getEnvironmentVariables(Map<String, String> properties,
Map<String, Object> environment) {
Map<String, Object> environment,
String serviceInstanceId) {
Map<String, Object> envVariables = getApplicationEnvironment(properties, environment);
String javaOpts = javaOpts(properties);
@@ -541,15 +543,16 @@ public class CloudFoundryAppDeployer implements AppDeployer, ResourceLoaderAware
envVariables.put("JAVA_OPTS", javaOpts);
}
envVariables.put("SPRING_CLOUD_APPLICATION_GUID", "${vcap.application.name}:${vcap.application.instance_index}");
envVariables.put("SPRING_APPLICATION_INDEX", "${vcap.application.instance_index}");
if (serviceInstanceId != null) {
envVariables.put("SPRING_CLOUD_APPBROKER_SERVICE_INSTANCE_ID", serviceInstanceId);
}
return envVariables;
}
private Map<String, Object> getApplicationEnvironment(Map<String, String> properties,
Map<String, Object> environment) {
Map<String, Object> applicationEnvironment = getSanitizedApplicationEnvironment(environment);
Map<String, Object> applicationEnvironment = sanitizeApplicationEnvironment(environment);
if (!applicationEnvironment.isEmpty() && useSpringApplicationJson(properties)) {
try {
@@ -564,7 +567,7 @@ public class CloudFoundryAppDeployer implements AppDeployer, ResourceLoaderAware
return applicationEnvironment;
}
private Map<String, Object> getSanitizedApplicationEnvironment(Map<String, Object> environment) {
private Map<String, Object> sanitizeApplicationEnvironment(Map<String, Object> environment) {
Map<String, Object> applicationEnvironment = new HashMap<>(environment);
// Remove server.port as CF assigns a port for us, and we don't want to override that

View File

@@ -69,6 +69,7 @@ class CloudFoundryAppDeployerTest {
private static final String APP_NAME = "test-app";
private static final String APP_PATH = "test.jar";
private static final String SERVICE_INSTANCE_ID = "service-instance-id";
private AppDeployer appDeployer;
@@ -117,6 +118,7 @@ class CloudFoundryAppDeployerTest {
DeployApplicationRequest request = DeployApplicationRequest.builder()
.name(APP_NAME)
.path(APP_PATH)
.serviceInstanceId(SERVICE_INSTANCE_ID)
.build();
StepVerifier.create(appDeployer.deploy(request))
@@ -136,6 +138,7 @@ class CloudFoundryAppDeployerTest {
DeployApplicationRequest request = DeployApplicationRequest.builder()
.name(APP_NAME)
.path(APP_PATH)
.serviceInstanceId(SERVICE_INSTANCE_ID)
.property(DeploymentProperties.COUNT_PROPERTY_KEY, "3")
.property(DeploymentProperties.MEMORY_PROPERTY_KEY, "2G")
.property(DeploymentProperties.DISK_PROPERTY_KEY, "3G")
@@ -182,6 +185,7 @@ class CloudFoundryAppDeployerTest {
DeployApplicationRequest request = DeployApplicationRequest.builder()
.name(APP_NAME)
.path(APP_PATH)
.serviceInstanceId(SERVICE_INSTANCE_ID)
.build();
StepVerifier.create(appDeployer.deploy(request))
@@ -218,6 +222,7 @@ class CloudFoundryAppDeployerTest {
DeployApplicationRequest request = DeployApplicationRequest.builder()
.name(APP_NAME)
.path(APP_PATH)
.serviceInstanceId(SERVICE_INSTANCE_ID)
.property(DeploymentProperties.COUNT_PROPERTY_KEY, "5")
.property(DeploymentProperties.MEMORY_PROPERTY_KEY, "4G")
.property(DeploymentProperties.DISK_PROPERTY_KEY, "5G")
@@ -257,6 +262,7 @@ class CloudFoundryAppDeployerTest {
DeployApplicationRequest request = DeployApplicationRequest.builder()
.name(APP_NAME)
.path(APP_PATH)
.serviceInstanceId(SERVICE_INSTANCE_ID)
.property(CloudFoundryDeploymentProperties.JAVA_OPTS_PROPERTY_KEY, "-Xms512m -Xmx1024m")
.environment("ENV_VAR_1", "value1")
.environment("ENV_VAR_2", "value2")
@@ -283,6 +289,7 @@ class CloudFoundryAppDeployerTest {
DeployApplicationRequest request = DeployApplicationRequest.builder()
.name(APP_NAME)
.path(APP_PATH)
.serviceInstanceId(SERVICE_INSTANCE_ID)
.property(CloudFoundryDeploymentProperties.JAVA_OPTS_PROPERTY_KEY, "-Xms512m -Xmx1024m")
.environment("ENV_VAR_1", "value1")
.environment("ENV_VAR_2", "value2")
@@ -469,8 +476,7 @@ class CloudFoundryAppDeployerTest {
private ApplicationManifest.Builder baseManifest() {
return ApplicationManifest.builder()
.environmentVariable("SPRING_APPLICATION_INDEX", "${vcap.application.instance_index}")
.environmentVariable("SPRING_CLOUD_APPLICATION_GUID", "${vcap.application.name}:${vcap.application.instance_index}")
.environmentVariable("SPRING_CLOUD_APPBROKER_SERVICE_INSTANCE_ID", SERVICE_INSTANCE_ID)
.services(new ArrayList<>());
}