Skip to content

Commit 966f23a

Browse files
committed
Fix NPE when apps with multiple versions exists
1 parent f81be7e commit 966f23a

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

multiapps-controller-process/src/main/java/org/cloudfoundry/multiapps/controller/process/util/ExistingAppsToBackupCalculator.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.cloudfoundry.multiapps.controller.process.Messages;
1919
import org.cloudfoundry.multiapps.controller.process.steps.ProcessContext;
2020
import org.cloudfoundry.multiapps.controller.process.variables.Variables;
21+
import org.cloudfoundry.multiapps.mta.model.Version;
2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
2324

@@ -74,14 +75,50 @@ private boolean isDeployedMtaBackupDescriptorMissing(ProcessContext context, Dep
7475
if (deployedMta == null) {
7576
return true;
7677
}
77-
String deployedMtaVersion = deployedMta.getMetadata()
78-
.getVersion()
79-
.toString();
78+
Version deployedMtaVersion = getDeployedMtaVersion(deployedMta);
79+
if (deployedMtaVersion == null) {
80+
return true;
81+
}
82+
return isBackupDescriptorMissing(context, deployedMtaVersion);
83+
}
84+
85+
private Version getDeployedMtaVersion(DeployedMta deployedMta) {
86+
Version deployedMtaVersion = deployedMta.getMetadata()
87+
.getVersion();
88+
if (deployedMtaVersion == null) {
89+
return findLiveProductizationStateVersion(deployedMta);
90+
}
91+
return deployedMtaVersion;
92+
}
93+
94+
private Version findLiveProductizationStateVersion(DeployedMta deployedMta) {
95+
for (DeployedMtaApplication deployedMtaApplication : deployedMta.getApplications()) {
96+
if (hasMtaVersionAnnotation(deployedMtaApplication) && isLiveProductizationState(deployedMtaApplication)) {
97+
return Version.parseVersion(deployedMtaApplication.getV3Metadata()
98+
.getAnnotations()
99+
.get(MtaMetadataAnnotations.MTA_VERSION));
100+
}
101+
}
102+
return null;
103+
}
104+
105+
private boolean hasMtaVersionAnnotation(DeployedMtaApplication deployedMtaApplication) {
106+
return deployedMtaApplication.getV3Metadata()
107+
.getAnnotations()
108+
.containsKey(MtaMetadataAnnotations.MTA_VERSION);
109+
}
110+
111+
private boolean isLiveProductizationState(DeployedMtaApplication deployedMtaApplication) {
112+
return deployedMtaApplication.getProductizationState()
113+
.equals(ProductizationState.LIVE);
114+
}
115+
116+
private boolean isBackupDescriptorMissing(ProcessContext context, Version deployedMtaVersion) {
80117
return descriptorBackupService.createQuery()
81118
.mtaId(context.getVariable(Variables.MTA_ID))
82119
.spaceId(context.getVariable(Variables.SPACE_GUID))
83120
.namespace(context.getVariable(Variables.MTA_NAMESPACE))
84-
.mtaVersion(deployedMtaVersion)
121+
.mtaVersion(deployedMtaVersion.toString())
85122
.list()
86123
.isEmpty();
87124
}

multiapps-controller-process/src/test/java/org/cloudfoundry/multiapps/controller/process/util/ExistingAppsToBackupCalculatorTest.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,25 @@ private static Stream<Arguments> testCalculateExistingAppsToBackup() {
9898
List.of("app-1-live", "app-1-idle"), Collections.emptyList()),
9999
// (7) Deployed mta does not have backup descriptor in db and won't be preserved
100100
Arguments.of(List.of(new TestApplication("app-1", "app-1", "1"), new TestApplication("app-2", "app-2", "1")),
101-
Collections.emptyList(), "2", false, Collections.emptyList(), Collections.emptyList()));
101+
Collections.emptyList(), "2", false, Collections.emptyList(), Collections.emptyList()),
102+
// (8) Deployed mta contains applications with different versions and existing backup
103+
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "2"),
104+
new TestApplication("app-1", "app-1-idle", "3", ProductizationState.IDLE)),
105+
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "3", true, List.of("app-1-live"),
106+
List.of(ImmutableCloudApplication.builder()
107+
.name("app-1-live")
108+
.v3Metadata(Metadata.builder()
109+
.annotation(MtaMetadataAnnotations.MTA_VERSION,
110+
"2")
111+
.build())
112+
.build())),
113+
// (9) Missing deployed mta and existing backup
114+
Arguments.of(Collections.emptyList(), List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "2", false,
115+
Collections.emptyList(), Collections.emptyList()),
116+
// (10) Deployed mta contains applications with missing versions and existing backup
117+
Arguments.of(List.of(new TestApplication("app-1", "app-1", null)),
118+
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "2", false, Collections.emptyList(),
119+
Collections.emptyList()));
102120
}
103121

104122
@ParameterizedTest
@@ -113,7 +131,8 @@ void testCalculateExistingAppsToBackup(List<TestApplication> deployedApplication
113131

114132
ExistingAppsToBackupCalculator calculator = new ExistingAppsToBackupCalculator(deployedMta, backupMta, descriptorBackupService);
115133

116-
List<CloudApplication> appsToUndeploy = getAppsToUndeploy(deployedMta.getApplications(), appNamesToUndeploy);
134+
List<CloudApplication> appsToUndeploy = getAppsToUndeploy(deployedMta == null ? Collections.emptyList()
135+
: deployedMta.getApplications(), appNamesToUndeploy);
117136
List<CloudApplication> appsToBackup = calculator.calculateExistingAppsToBackup(context, appsToUndeploy,
118137
mtaVersionOfCurrentDescriptor);
119138

@@ -191,10 +210,13 @@ private DeployedMta getDeployedMta(List<TestApplication> deployedApplications) {
191210
deployedMtaApplications.add(ImmutableDeployedMtaApplication.builder()
192211
.moduleName(application.moduleName)
193212
.name(application.appName)
194-
.v3Metadata(Metadata.builder()
195-
.annotation(MtaMetadataAnnotations.MTA_VERSION,
196-
application.mtaVersion)
197-
.build())
213+
.v3Metadata(application.mtaVersion != null ? Metadata.builder()
214+
.annotation(MtaMetadataAnnotations.MTA_VERSION,
215+
application.mtaVersion)
216+
.build()
217+
: Metadata.builder()
218+
.annotations(Collections.emptyMap())
219+
.build())
198220
.productizationState(application.productizationState)
199221
.build());
200222
}
@@ -203,7 +225,10 @@ private DeployedMta getDeployedMta(List<TestApplication> deployedApplications) {
203225
.applications(deployedMtaApplications)
204226
.metadata(ImmutableMtaMetadata.builder()
205227
.id(MTA_ID)
206-
.version(mtaVersion != null ? Version.parseVersion(mtaVersion) : null)
228+
.version(mtaVersion != null && deployedApplications.stream()
229+
.allMatch(deployedApplication -> mtaVersion.equals(deployedApplication.mtaVersion))
230+
? Version.parseVersion(mtaVersion)
231+
: null)
207232
.build())
208233

209234
.build();

0 commit comments

Comments
 (0)