Skip to content

Commit 9dd304e

Browse files
committed
simplify CloudFOundryPostProcessor
1 parent a67bf5a commit 9dd304e

File tree

3 files changed

+91
-31
lines changed

3 files changed

+91
-31
lines changed

src/autoscaler/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/CloudFoundryConfigurationProcessor.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ public void postProcessEnvironment(
5151
"Found scheduler-config service in VCAP_SERVICES, applying configuration overrides");
5252
allConfigs.putAll(schedulerConfig);
5353
}
54-
// Process VCAP_APPLICATION for org GUID (this should override scheduler-config)
55-
Map<String, Object> vcapAppConfig = extractVcapApplicationConfig(environment, allConfigs);
56-
if (vcapAppConfig != null && !vcapAppConfig.isEmpty()) {
57-
logger.info("Found VCAP_APPLICATION, applying cfserver configuration overrides");
58-
allConfigs.putAll(vcapAppConfig);
54+
55+
Map<String, Object> vcapOrgAndGuid = getOrgAndSpaceGuidFromVcap(environment);
56+
Map<String, Object> cfServerConfig = (Map<String, Object>) allConfigs.get("cfserver");
57+
if (cfServerConfig != null && !cfServerConfig.isEmpty()) {
58+
logger.info(
59+
"Found org and space guid service in VCAP_APPLICATIONS, applying configuration"
60+
+ " overrides");
61+
cfServerConfig.putAll(vcapOrgAndGuid);
5962
}
6063

6164
// Process database services
@@ -89,7 +92,8 @@ public void postProcessEnvironment(
8992
}
9093
}
9194

92-
private Map<String, Object> extractVcapApplicationConfig(ConfigurableEnvironment environment, Map<String, Object> allConfigs) {
95+
private Map<String, Object> getOrgAndSpaceGuidFromVcap(ConfigurableEnvironment environment) {
96+
Map<String, Object> orgSpaceMap;
9397
try {
9498
String vcapApplication = environment.getProperty(VCAP_APPLICATION);
9599

@@ -100,23 +104,16 @@ private Map<String, Object> extractVcapApplicationConfig(ConfigurableEnvironment
100104
TypeReference<Map<String, Object>> typeRef = new TypeReference<>() {};
101105
Map<String, Object> vcapApp = objectMapper.readValue(vcapApplication, typeRef);
102106

103-
Map<String, Object> config = new java.util.HashMap<>();
104-
boolean foundConfig = false;
105-
106-
Map<String, Object> cfserverConfig = (Map<String, Object>) allConfigs.get("cfserver");
107-
if (cfserverConfig == null) {
108-
cfserverConfig = new java.util.HashMap<>();
109-
config.put("cfserver", cfserverConfig);
110-
}
107+
orgSpaceMap = new java.util.HashMap<>();
111108

112109
// Extract organization_id
113110
Object organizationId = vcapApp.get("organization_id");
114111
if (organizationId instanceof String && !((String) organizationId).trim().isEmpty()) {
115112
String orgGuid = (String) organizationId;
116113
logger.info(
117114
"Setting cfserver.validOrgGuid from VCAP_APPLICATION organization_id: {}", orgGuid);
118-
cfserverConfig.put("validOrgGuid", orgGuid);
119-
foundConfig = true;
115+
orgSpaceMap.put("validOrgGuid", orgGuid);
116+
120117
} else {
121118
logger.warn("organization_id not found or empty in VCAP_APPLICATION");
122119
}
@@ -127,17 +124,15 @@ private Map<String, Object> extractVcapApplicationConfig(ConfigurableEnvironment
127124
String spaceGuid = (String) spaceId;
128125
logger.info(
129126
"Setting cfserver.validSpaceGuid from VCAP_APPLICATION space_id: {}", spaceGuid);
130-
cfserverConfig.put("validSpaceGuid", spaceGuid);
131-
foundConfig = true;
127+
orgSpaceMap.put("validSpaceGuid", spaceGuid);
132128
} else {
133129
logger.warn("space_id not found or empty in VCAP_APPLICATION");
134130
}
135-
136-
return foundConfig ? config : null;
137131
} catch (Exception e) {
138132
logger.error("Failed to parse VCAP_APPLICATION JSON", e);
139133
return null;
140134
}
135+
return orgSpaceMap;
141136
}
142137

143138
private Map<String, Object> extractSchedulerConfig(String vcapServices) {

src/autoscaler/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/filter/HttpAuthFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ protected void doFilterInternal(
4444
request.getMethod(),
4545
request.getScheme(),
4646
forwardedProto,
47-
isHealthEndpoint, cfServerConfiguration.getHealthserver().getUsername());
47+
isHealthEndpoint,
48+
cfServerConfiguration.getHealthserver().getUsername());
4849

4950
if (isHealthEndpoint) {
5051
handleHealthEndpoint(request, response);

src/autoscaler/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/conf/CloudFoundryConfigurationProcessorTest.java

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void testVcapServicesWithDatabaseService() {
161161
}
162162
]
163163
}
164-
""";
164+
""";
165165

166166
environment
167167
.getPropertySources()
@@ -255,7 +255,7 @@ public void testVcapServicesWithClientCertCredentialMapping() {
255255
}
256256
]
257257
}
258-
""";
258+
""";
259259

260260
environment
261261
.getPropertySources()
@@ -297,7 +297,7 @@ public void testVcapServicesWithClientCertOnlyCredentialMapping() {
297297
}
298298
]
299299
}
300-
""";
300+
""";
301301

302302
environment
303303
.getPropertySources()
@@ -337,7 +337,7 @@ public void testVcapServicesPrefersSslcertOverClientCert() {
337337
}
338338
]
339339
}
340-
""";
340+
""";
341341

342342
environment
343343
.getPropertySources()
@@ -358,7 +358,20 @@ public void testVcapApplicationWithOrgGuid() {
358358
String vcapServices =
359359
"""
360360
{
361-
"user-provided": []
361+
"user-provided": [
362+
{
363+
"name": "scheduler-config-service",
364+
"tags": ["scheduler-config"],
365+
"credentials": {
366+
"cfserver": {
367+
"healthserver": {
368+
"username": "health-user",
369+
"password": "health-password"
370+
}
371+
}
372+
}
373+
}
374+
]
362375
}
363376
""";
364377

@@ -392,7 +405,20 @@ public void testVcapApplicationWithoutOrgGuid() {
392405
String vcapServices =
393406
"""
394407
{
395-
"user-provided": []
408+
"user-provided": [
409+
{
410+
"name": "scheduler-config-service",
411+
"tags": ["scheduler-config"],
412+
"credentials": {
413+
"cfserver": {
414+
"healthserver": {
415+
"username": "health-user",
416+
"password": "health-password"
417+
}
418+
}
419+
}
420+
}
421+
]
396422
}
397423
""";
398424

@@ -546,7 +572,20 @@ public void testVcapApplicationWithOnlySpaceGuid() {
546572
String vcapServices =
547573
"""
548574
{
549-
"user-provided": []
575+
"user-provided": [
576+
{
577+
"name": "scheduler-config-service",
578+
"tags": ["scheduler-config"],
579+
"credentials": {
580+
"cfserver": {
581+
"healthserver": {
582+
"username": "health-user",
583+
"password": "health-password"
584+
}
585+
}
586+
}
587+
}
588+
]
550589
}
551590
""";
552591

@@ -579,7 +618,20 @@ public void testVcapApplicationWithEmptySpaceGuid() {
579618
String vcapServices =
580619
"""
581620
{
582-
"user-provided": []
621+
"user-provided": [
622+
{
623+
"name": "scheduler-config-service",
624+
"tags": ["scheduler-config"],
625+
"credentials": {
626+
"cfserver": {
627+
"healthserver": {
628+
"username": "health-user",
629+
"password": "health-password"
630+
}
631+
}
632+
}
633+
}
634+
]
583635
}
584636
""";
585637

@@ -613,10 +665,22 @@ public void testVcapApplicationWithoutSpaceGuid() {
613665
String vcapServices =
614666
"""
615667
{
616-
"user-provided": []
668+
"user-provided": [
669+
{
670+
"name": "scheduler-config-service",
671+
"tags": ["scheduler-config"],
672+
"credentials": {
673+
"cfserver": {
674+
"healthserver": {
675+
"username": "health-user",
676+
"password": "health-password"
677+
}
678+
}
679+
}
680+
}
681+
]
617682
}
618683
""";
619-
620684
String vcapApplication =
621685
"""
622686
{

0 commit comments

Comments
 (0)