Skip to content

Fix NPE on external/unmanaged instance import using custom offerings#12884

Draft
winterhazel wants to merge 1 commit intoapache:4.20from
winterhazel:fix-regression-vm-import
Draft

Fix NPE on external/unmanaged instance import using custom offerings#12884
winterhazel wants to merge 1 commit intoapache:4.20from
winterhazel:fix-regression-vm-import

Conversation

@winterhazel
Copy link
Member

@winterhazel winterhazel commented Mar 25, 2026

Description

This PR addresses a regression in the import of unmanaged/external VMs using a custom compute offering (reported in https://lists.apache.org/thread/1bvxjc197zhj61mtjxpm3tz1o27znjmv).

As both serviceOffering.getCpu() and serviceOffering.getRamSize() return null when the offering is custom constrained/unconstrained, we need to check the amount of CPUs and memory returned by the hypervisor in case an unmanaged instance is being imported, or the cpuNumber and memory details in case the instance belongs to a remote host/is being imported from its disk.

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • build/CI
  • test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

Tests are still pending. The following operations need to be validated with both fixed and custom offerings:

  • Unmanaged KVM/VMware VM import.
  • KVM VM import from a remote host.
  • KVM VM import from an existing disk.
  • VMware to KVM instance conversion.

@winterhazel winterhazel added this to the 4.20.3 milestone Mar 25, 2026
@winterhazel winterhazel requested a review from abh1sar March 25, 2026 01:42
@codecov
Copy link

codecov bot commented Mar 25, 2026

Codecov Report

❌ Patch coverage is 66.15385% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 16.25%. Comparing base (c19630f) to head (9da01e3).

Files with missing lines Patch % Lines
.../apache/cloudstack/vm/UnmanagedVMsManagerImpl.java 66.15% 17 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##               4.20   #12884   +/-   ##
=========================================
  Coverage     16.25%   16.25%           
- Complexity    13415    13422    +7     
=========================================
  Files          5664     5664           
  Lines        500465   500507   +42     
  Branches      60780    60785    +5     
=========================================
+ Hits          81338    81372   +34     
- Misses       410031   410041   +10     
+ Partials       9096     9094    -2     
Flag Coverage Δ
uitests 4.15% <ø> (ø)
unittests 17.11% <66.15%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

sourceVMwareInstance = sourceInstanceDetails.first();
isClonedInstance = sourceInstanceDetails.second();

// Ensure that the configured resource limits will not be exceeded before beggining the conversion process
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Ensure that the configured resource limits will not be exceeded before beggining the conversion process
// Ensure that the configured resource limits will not be exceeded before beginning the conversion process

}

private void checkVmResourceLimitsForUnmanagedInstanceImport(Account owner, UnmanagedInstanceTO unmanagedInstance, ServiceOfferingVO serviceOffering, VMTemplateVO template, List<Reserver> reservations) throws ResourceAllocationException {
// When importing a unmanaged instance, the amount of CPUs and memory is obtained from the hypervisor unless powered off
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// When importing a unmanaged instance, the amount of CPUs and memory is obtained from the hypervisor unless powered off
// When importing an unmanaged instance, the amount of CPUs and memory is obtained from the hypervisor unless powered off

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression where importing unmanaged/external instances with custom (dynamic) compute offerings can hit NPEs when CPU/RAM are not populated on the offering, by deriving CPU/RAM from the hypervisor (unmanaged) or from provided details (external/disk-based imports) and moving resource-limit reservations closer to the specific import paths.

Changes:

  • Move VM (cpu/memory/vm count) resource-limit reservations out of top-level import methods and into specific import flows.
  • Add pre-checks to determine CPU/RAM for unmanaged-instance imports (hypervisor vs offering) and for external KVM imports (offering vs details).
  • Ensure reservations are closed via ReservationHelper.closeAll(...) around import/conversion flows.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

sourceVMwareInstance = sourceInstanceDetails.first();
isClonedInstance = sourceInstanceDetails.second();

// Ensure that the configured resource limits will not be exceeded before beggining the conversion process
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: “beggining” → “beginning”.

Suggested change
// Ensure that the configured resource limits will not be exceeded before beggining the conversion process
// Ensure that the configured resource limits will not be exceeded before beginning the conversion process

Copilot uses AI. Check for mistakes.
Comment on lines +2769 to +2790
private void checkVmResourceLimitsForExternalKvmVmImport(Account owner, ServiceOfferingVO serviceOffering, VMTemplateVO template, Map<String, String> details, List<Reserver> reservations) throws ResourceAllocationException {
// When importing an external VM, the amount of CPUs and memory is always obtained from the compute offering,
// unlike the unmanaged instance import that obtains it from the hypervisor unless the VM is powered off and the offering is fixed
Integer cpu = serviceOffering.getCpu();
Integer memory = serviceOffering.getRamSize();

if (serviceOffering.isDynamic()) {
cpu = getDetailAsInteger(VmDetailConstants.CPU_NUMBER, details);
memory = getDetailAsInteger(VmDetailConstants.MEMORY, details);
}

List<String> resourceLimitHostTags = resourceLimitService.getResourceLimitHostTags(serviceOffering, template);

CheckedReservation vmReservation = new CheckedReservation(owner, Resource.ResourceType.user_vm, resourceLimitHostTags, 1L, reservationDao, resourceLimitService);
reservations.add(vmReservation);

CheckedReservation cpuReservation = new CheckedReservation(owner, Resource.ResourceType.cpu, resourceLimitHostTags, cpu.longValue(), reservationDao, resourceLimitService);
reservations.add(cpuReservation);

CheckedReservation memReservation = new CheckedReservation(owner, Resource.ResourceType.memory, resourceLimitHostTags, memory.longValue(), reservationDao, resourceLimitService);
reservations.add(memReservation);
}
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New VM resource-limit selection logic (dynamic vs fixed offering, powered-off vs running unmanaged VM, and parsing CPU/memory from details) is not covered by unit tests in UnmanagedVMsManagerImplTest. Add focused tests for: (1) unmanaged import with null/unknown powerState, (2) external import with dynamic offering reading cpuNumber/memory, and (3) validation failures for missing/invalid values, to prevent regressions back to NPEs.

Copilot uses AI. Check for mistakes.
Integer cpu = serviceOffering.getCpu();
Integer memory = serviceOffering.getRamSize();

if (serviceOffering.isDynamic() || !unmanagedInstance.getPowerState().equals(UnmanagedInstanceTO.PowerState.PowerOff)) {
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unmanagedInstance.getPowerState() can be null (see UnmanagedInstanceTO), so calling .equals(PowerOff) here can throw an NPE during import. Make this null-safe (e.g., compare with PowerOff.equals(...) or treat null/unknown as not-PowerOff) so the resource-limit check can’t crash the import path.

Suggested change
if (serviceOffering.isDynamic() || !unmanagedInstance.getPowerState().equals(UnmanagedInstanceTO.PowerState.PowerOff)) {
if (serviceOffering.isDynamic() || !UnmanagedInstanceTO.PowerState.PowerOff.equals(unmanagedInstance.getPowerState())) {

Copilot uses AI. Check for mistakes.
Comment on lines +2772 to +2789
Integer cpu = serviceOffering.getCpu();
Integer memory = serviceOffering.getRamSize();

if (serviceOffering.isDynamic()) {
cpu = getDetailAsInteger(VmDetailConstants.CPU_NUMBER, details);
memory = getDetailAsInteger(VmDetailConstants.MEMORY, details);
}

List<String> resourceLimitHostTags = resourceLimitService.getResourceLimitHostTags(serviceOffering, template);

CheckedReservation vmReservation = new CheckedReservation(owner, Resource.ResourceType.user_vm, resourceLimitHostTags, 1L, reservationDao, resourceLimitService);
reservations.add(vmReservation);

CheckedReservation cpuReservation = new CheckedReservation(owner, Resource.ResourceType.cpu, resourceLimitHostTags, cpu.longValue(), reservationDao, resourceLimitService);
reservations.add(cpuReservation);

CheckedReservation memReservation = new CheckedReservation(owner, Resource.ResourceType.memory, resourceLimitHostTags, memory.longValue(), reservationDao, resourceLimitService);
reservations.add(memReservation);
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkVmResourceLimitsForExternalKvmVmImport can still throw an NPE: if serviceOffering.getCpu() / getRamSize() are null (e.g., custom offering metadata) and the offering is not treated as dynamic here, cpu.longValue() / memory.longValue() will crash. Add explicit validation (null/<=0) for cpu and memory (both for dynamic and non-dynamic) and throw a parameter/internal error before constructing reservations.

Copilot uses AI. Check for mistakes.
@abh1sar
Copy link
Contributor

abh1sar commented Mar 25, 2026

@blueorangutan package

@blueorangutan
Copy link

@abh1sar a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.

@blueorangutan
Copy link

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 17237

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants