fix(ec2): skip raw block cleanup for logical root volumes (#8508)

EC2 deleted a terminated instance's root volume through the raw
block-device helper, though a root has no raw backing: it is the
container's own file system. Volume deletion now takes the same root
check that already skips the detach, so termination and reconciliation
remove a root's metadata and tags without calling the helper, while
data volumes keep their backing-storage cleanup. Failed-launch rollback
records the launch's root volume ids before terminating, so a root that
DeleteOnTermination=false kept, or that reconciliation detached first,
is deleted the same way.

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-15 18:17:50 -04:00
1 parent 0fccd475b7
commit ac6f28a73b
3 files changed
+76 -6

No files matched your search

@@ -539,6 +539,74 @@ class Ec2ServiceTest {
.collect(Collectors.toSet()));
}
@ParameterizedTest
@ValueSource(strings = {"terminate", "settle", "rollback"})
void logicalRootDeletionDoesNotRequireABlockDeviceHelper(String operation) {
Ec2VolumeBlockDeviceManager devices = mock(Ec2VolumeBlockDeviceManager.class);
when(devices.isAvailable()).thenReturn(true);
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory(),
null, null, devices);
Reservation launch = service.runInstances("us-east-1", "ami-test", "t3.micro", 1, 1,
null, List.of(), null, null, List.of(), null, null);
Instance instance = launch.getInstances().getFirst();
String rootId = instance.getRootVolumeId();
service.createTags("us-east-1", List.of(rootId), List.of(new Tag("Owner", "root")));
switch (operation) {
case "terminate" -> service.terminateInstances("us-east-1", List.of(instance.getInstanceId()));
case "rollback" -> service.rollBackLaunch("us-east-1", launch);
case "settle" -> instance.setState(InstanceState.terminated());
default -> throw new IllegalArgumentException(operation);
}
assertTrue(service.describeVolumes("us-east-1", List.of(), Map.of()).isEmpty());
assertTrue(service.describeTags("us-east-1", Map.of("resource-id", List.of(rootId))).isEmpty());
verify(devices, never()).deleteVolume(anyString());
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void rollbackDeletesRetainedLogicalRootWithoutBlockDeviceCleanup(boolean settleFirst) {
Ec2VolumeBlockDeviceManager devices = mock(Ec2VolumeBlockDeviceManager.class);
when(devices.isAvailable()).thenReturn(true);
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory(),
null, null, devices);
LaunchTemplateData.Ebs ebs = new LaunchTemplateData.Ebs();
ebs.setVolumeSize(5);
ebs.setDeleteOnTermination(false);
LaunchTemplateData.BlockDeviceMapping root = new LaunchTemplateData.BlockDeviceMapping();
root.setDeviceName("/dev/xvda");
root.setEbs(ebs);
LaunchTemplateData.BlockDeviceMapping data = new LaunchTemplateData.BlockDeviceMapping();
data.setDeviceName("/dev/sdf");
data.setEbs(ebs);
Reservation launch = service.runInstances("us-east-1", "ami-test", "t3.micro", 1, 1,
null, List.of(), null, null, List.of(), null, null,
null, null, 0, null, null, null, null, false, null, null,
List.of(root, data), false, null, null);
Instance instance = launch.getInstances().getFirst();
String rootId = instance.getRootVolumeId();
String dataId = launch.getLaunchVolumeIds().stream()
.filter(id -> !rootId.equals(id)).findFirst().orElseThrow();
service.createTags("us-east-1", List.of(rootId, dataId), List.of(new Tag("Owner", "launch")));
if (settleFirst) {
instance.setState(InstanceState.terminated());
assertTrue(service.describeVolumes("us-east-1", List.of(), Map.of()).stream()
.allMatch(volume -> volume.getAttachments().isEmpty()));
}
service.rollBackLaunch("us-east-1", launch);
assertTrue(service.describeVolumes("us-east-1", List.of(), Map.of()).isEmpty());
assertTrue(service.describeTags("us-east-1", Map.of("resource-id", List.of(rootId, dataId))).isEmpty());
verify(devices, never()).deleteVolume(rootId);
verify(devices).deleteVolume(dataId);
}
private static Reservation launchKeepingADataVolume(Ec2Service service) {
LaunchTemplateData.Ebs ebs = new LaunchTemplateData.Ebs();
ebs.setVolumeSize(5);