Skip to content

Fix NPE when apps with multiple versions exists #1598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.cloudfoundry.multiapps.controller.process.Messages;
import org.cloudfoundry.multiapps.controller.process.steps.ProcessContext;
import org.cloudfoundry.multiapps.controller.process.variables.Variables;
import org.cloudfoundry.multiapps.mta.model.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -74,14 +75,50 @@ private boolean isDeployedMtaBackupDescriptorMissing(ProcessContext context, Dep
if (deployedMta == null) {
return true;
}
String deployedMtaVersion = deployedMta.getMetadata()
.getVersion()
.toString();
Version deployedMtaVersion = getDeployedMtaVersion(deployedMta);
if (deployedMtaVersion == null) {
return true;
}
return isBackupDescriptorMissing(context, deployedMtaVersion);
}

private Version getDeployedMtaVersion(DeployedMta deployedMta) {
Version deployedMtaVersion = deployedMta.getMetadata()
.getVersion();
if (deployedMtaVersion == null) {
return findLiveProductizationStateVersion(deployedMta);
}
return deployedMtaVersion;
}

private Version findLiveProductizationStateVersion(DeployedMta deployedMta) {
for (DeployedMtaApplication deployedMtaApplication : deployedMta.getApplications()) {
if (hasMtaVersionAnnotation(deployedMtaApplication) && isLiveProductizationState(deployedMtaApplication)) {
return Version.parseVersion(deployedMtaApplication.getV3Metadata()
.getAnnotations()
.get(MtaMetadataAnnotations.MTA_VERSION));
}
}
return null;
}

private boolean hasMtaVersionAnnotation(DeployedMtaApplication deployedMtaApplication) {
return deployedMtaApplication.getV3Metadata()
.getAnnotations()
.containsKey(MtaMetadataAnnotations.MTA_VERSION);
}

private boolean isLiveProductizationState(DeployedMtaApplication deployedMtaApplication) {
return deployedMtaApplication.getProductizationState()
.equals(ProductizationState.LIVE);
}

private boolean isBackupDescriptorMissing(ProcessContext context, Version deployedMtaVersion) {
return descriptorBackupService.createQuery()
.mtaId(context.getVariable(Variables.MTA_ID))
.spaceId(context.getVariable(Variables.SPACE_GUID))
.namespace(context.getVariable(Variables.MTA_NAMESPACE))
.mtaVersion(deployedMtaVersion)
.mtaVersion(deployedMtaVersion.toString())
.list()
.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,25 @@ private static Stream<Arguments> testCalculateExistingAppsToBackup() {
List.of("app-1-live", "app-1-idle"), Collections.emptyList()),
// (7) Deployed mta does not have backup descriptor in db and won't be preserved
Arguments.of(List.of(new TestApplication("app-1", "app-1", "1"), new TestApplication("app-2", "app-2", "1")),
Collections.emptyList(), "2", false, Collections.emptyList(), Collections.emptyList()));
Collections.emptyList(), "2", false, Collections.emptyList(), Collections.emptyList()),
// (8) Deployed mta contains applications with different versions and existing backup
Arguments.of(List.of(new TestApplication("app-1", "app-1-live", "2"),
new TestApplication("app-1", "app-1-idle", "3", ProductizationState.IDLE)),
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "3", true, List.of("app-1-live"),
List.of(ImmutableCloudApplication.builder()
.name("app-1-live")
.v3Metadata(Metadata.builder()
.annotation(MtaMetadataAnnotations.MTA_VERSION,
"2")
.build())
.build())),
// (9) Missing deployed mta and existing backup
Arguments.of(Collections.emptyList(), List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "2", false,
Collections.emptyList(), Collections.emptyList()),
// (10) Deployed mta contains applications with missing versions and existing backup
Arguments.of(List.of(new TestApplication("app-1", "app-1", null)),
List.of(new TestApplication("app-1", "mta-backup-app-1", "1")), "2", false, Collections.emptyList(),
Collections.emptyList()));
}

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

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

List<CloudApplication> appsToUndeploy = getAppsToUndeploy(deployedMta.getApplications(), appNamesToUndeploy);
List<CloudApplication> appsToUndeploy = getAppsToUndeploy(deployedMta == null ? Collections.emptyList()
: deployedMta.getApplications(), appNamesToUndeploy);
List<CloudApplication> appsToBackup = calculator.calculateExistingAppsToBackup(context, appsToUndeploy,
mtaVersionOfCurrentDescriptor);

Expand Down Expand Up @@ -191,10 +210,13 @@ private DeployedMta getDeployedMta(List<TestApplication> deployedApplications) {
deployedMtaApplications.add(ImmutableDeployedMtaApplication.builder()
.moduleName(application.moduleName)
.name(application.appName)
.v3Metadata(Metadata.builder()
.annotation(MtaMetadataAnnotations.MTA_VERSION,
application.mtaVersion)
.build())
.v3Metadata(application.mtaVersion != null ? Metadata.builder()
.annotation(MtaMetadataAnnotations.MTA_VERSION,
application.mtaVersion)
.build()
: Metadata.builder()
.annotations(Collections.emptyMap())
.build())
.productizationState(application.productizationState)
.build());
}
Expand All @@ -203,7 +225,10 @@ private DeployedMta getDeployedMta(List<TestApplication> deployedApplications) {
.applications(deployedMtaApplications)
.metadata(ImmutableMtaMetadata.builder()
.id(MTA_ID)
.version(mtaVersion != null ? Version.parseVersion(mtaVersion) : null)
.version(mtaVersion != null && deployedApplications.stream()
.allMatch(deployedApplication -> mtaVersion.equals(deployedApplication.mtaVersion))
? Version.parseVersion(mtaVersion)
: null)
.build())

.build();
Expand Down