fix(lambda): keep MicroVM version update timestamps independent (#9021)

Each MicroVM image version now keeps its own updatedAt instead of
reporting the parent image's. Building another version leaves older
versions' timestamps alone, and a status update advances only the
version it changes. A stored version without the field reports its own
createdAt, including after a restart, and reading it does not rewrite
storage.

Closes #8616
This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-11-19 18:32:56 -04:00
1 parent a283c7a49b
commit c1e7b240ea
5 files changed
+147 -1

No files matched your search

+1
View File
@@ -60,6 +60,7 @@ inputs cannot be reconstructed.
- **ARN or bare identifier:** images, MicroVMs and connectors resolve by either in URI paths.
- **Passing a connector:** when IAM enforcement is on, `CreateMicrovmImage`, `UpdateMicrovmImage` and `RunMicrovm` need `lambda:PassNetworkConnector` on each connector the request names, and on the Lambda-managed `INTERNET_EGRESS` connector, or `HTTP_INGRESS` for ingress, when it names none, as on AWS (see [IAM](iam.md)).
- **Image configuration:** Create and Update retain the requested base image version, egress connectors, logging, environment variables, OS capabilities and CPU architectures. Create retains its tags. A full-replace Update resets omitted configuration to defaults, including base image version `1.0`. Version responses retain the runtime configuration from their build; omitting egress connectors or supplying an empty list uses the managed `INTERNET_EGRESS` connector.
- **Version timestamps:** Each image version keeps its own `updatedAt`. A later image rebuild does not change older versions, while a version status update advances that version's timestamp. Stored versions without an update time report their creation time, including after a parent update or restart. Reading them does not rewrite storage.
- **Run configuration:** RunMicrovm retains the requested image version, execution role, ingress and egress connectors, maximum duration and idle policy in both its response and GetMicrovm. An omitted image version uses the latest active version; a requested version must exist. Empty or omitted connector lists keep the managed defaults. The maximum duration defaults to 28,800 seconds and must be between 1 and 28,800; idle policies require all three fields, a maximum idle duration of at least 60 seconds and a nonnegative suspended duration. These fields describe the emulated run; they do not start an execution environment or schedule idle timers.
- **Connector versions:** a connector starts at version 1. As on AWS, an update that changes the subnets, the security groups, the network protocol or the operator role makes the next version, and one that only lists the subnets in another order changes nothing. A connector also resolves by its name, and `:<version>` after an id, a name or an ARN names one version. The latest version cannot be deleted on its own, and `AssociatedComputeResourceTypes` cannot change. Errors use the AWS wording.
- **Builds per generation:** each version is built once per Graviton generation the service targets, so a version has more than one build.
@@ -408,7 +408,8 @@ public class LambdaMicrovmsController {
node.put("state", version.state);
node.put("status", version.status);
node.put("createdAt", EpochSeconds.fromInstant(version.createdAt));
node.put("updatedAt", EpochSeconds.fromInstant(image.updatedAt));
node.put("updatedAt", EpochSeconds.fromInstant(
version.updatedAt != null ? version.updatedAt : version.createdAt));
node.put("baseImageArn", buildSpec.baseImageArn());
node.put("baseImageVersion", buildSpec.baseImageVersion());
node.put("buildRoleArn", buildSpec.buildRoleArn());
@@ -161,6 +161,7 @@ public class LambdaMicrovmsService implements ResourceProvider {
public String state;
public String status;
public Instant createdAt;
public Instant updatedAt;
public final List<MicrovmBuild> builds = new ArrayList<>();
public BuildSpec buildSpec;
public RuntimeSettings runtimeSettings;
@@ -231,6 +232,7 @@ public class LambdaMicrovmsService implements ResourceProvider {
version.state = "SUCCESSFUL";
version.status = "ACTIVE";
version.createdAt = Instant.now();
version.updatedAt = version.createdAt;
version.buildSpec = BuildSpec.from(image);
version.runtimeSettings = image.runtimeSettings;
// A version is built once per Graviton generation the service targets,
@@ -342,6 +344,7 @@ public class LambdaMicrovmsService implements ResourceProvider {
MicrovmImage image = getImage(region, name);
MicrovmImageVersion version = getVersion(region, name, imageVersion);
version.status = status;
version.updatedAt = Instant.now();
persist(images, region, image.name, image);
return version;
}
@@ -0,0 +1,90 @@
package io.floci.aws.services.lambdamicrovms;
import io.floci.aws.testing.PartitionCleanup;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.config.JsonPathConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.math.BigDecimal;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class LambdaMicrovmImageVersionTimestampIntegrationTest {
private static final String IMAGES = "/2025-09-09/microvm-images";
private static final JsonPathConfig EXACT_NUMBERS =
new JsonPathConfig(JsonPathConfig.NumberReturnType.BIG_DECIMAL);
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Test
void getPreservesVersionTimestampWhenParentBuildsAnotherVersion() {
String image = createImage();
BigDecimal original = versionTimestamp(image, "1.0");
updateImage(image);
assertEquals(original, versionTimestamp(image, "1.0"));
}
@Test
void listPreservesVersionTimestampWhenParentBuildsAnotherVersion() {
String image = createImage();
BigDecimal original = versionTimestamp(image, "1.0");
updateImage(image);
BigDecimal listed = given().when().get(IMAGES + "/" + image + "/versions")
.then().statusCode(200).extract().jsonPath(EXACT_NUMBERS).getObject("items[0].updatedAt", BigDecimal.class);
assertEquals(original, listed);
}
@Test
void statusChangeUpdatesOnlyTheTargetVersionTimestamp() {
String image = createImage();
updateImage(image);
BigDecimal original = versionTimestamp(image, "1.0");
BigDecimal other = versionTimestamp(image, "2.0");
BigDecimal parent = given().when().get(IMAGES + "/" + image).then().statusCode(200)
.extract().jsonPath(EXACT_NUMBERS).getObject("updatedAt", BigDecimal.class);
given().contentType("application/json").body("{\"status\":\"INACTIVE\"}")
.when().patch(IMAGES + "/" + image + "/versions/1.0").then().statusCode(200);
assertTrue(versionTimestamp(image, "1.0").compareTo(original) > 0,
"Changing a version's status must advance its own updatedAt");
assertEquals(other, versionTimestamp(image, "2.0"));
assertEquals(parent, given().when().get(IMAGES + "/" + image).then().statusCode(200)
.extract().jsonPath(EXACT_NUMBERS).getObject("updatedAt", BigDecimal.class));
}
private String createImage() {
String image = "version-time-" + UUID.randomUUID();
given().contentType("application/json")
.body("{\"name\":\"" + image + "\"," + fields("original") + "}")
.when().post(IMAGES).then().statusCode(201);
cleanup.register(() -> given().when().delete(IMAGES + "/" + image).then().statusCode(200));
return image;
}
private void updateImage(String image) {
given().contentType("application/json").body("{" + fields("replacement") + "}")
.when().put(IMAGES + "/" + image).then().statusCode(200);
}
private BigDecimal versionTimestamp(String image, String version) {
return given().when().get(IMAGES + "/" + image + "/versions/" + version)
.then().statusCode(200).extract().jsonPath(EXACT_NUMBERS).getObject("updatedAt", BigDecimal.class);
}
private static String fields(String label) {
return "\"baseImageArn\":\"arn:aws:lambda:us-east-1:aws:microvm-image:" + label + "\","
+ "\"buildRoleArn\":\"arn:aws:iam::000000000000:role/build\","
+ "\"codeArtifact\":{\"uri\":\"s3://artifacts/" + label + ".zip\"}";
}
}
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.PersistentStorage;
@@ -15,6 +16,7 @@ import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -22,6 +24,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
@@ -127,6 +130,54 @@ class LambdaMicrovmsServicePersistenceTest {
assertEquals(beforeRead, Files.readString(file));
}
@Test
void versionUpdateTimeSurvivesReloadWithoutChangingOtherVersionsOrTheParent() {
createAndUpdateVersionedImage();
LambdaMicrovmsService first = process();
Instant otherVersionTime = first.getVersion(REGION, "versioned-image", "2.0").updatedAt;
Instant parentTime = first.getImage(REGION, "versioned-image").updatedAt;
Instant changed = first.updateVersionStatus(REGION, "versioned-image", "1.0", "INACTIVE").updatedAt;
LambdaMicrovmsService reloaded = process();
assertNotNull(changed);
assertEquals(changed, reloaded.getVersion(REGION, "versioned-image", "1.0").updatedAt);
assertEquals(otherVersionTime, reloaded.getVersion(REGION, "versioned-image", "2.0").updatedAt);
assertEquals(parentTime, reloaded.getImage(REGION, "versioned-image").updatedAt);
assertThrows(AwsException.class,
() -> reloaded.updateVersionStatus(REGION, "versioned-image", "1.0", "INVALID"));
assertEquals(changed, process().getVersion(REGION, "versioned-image", "1.0").updatedAt);
}
@Test
void legacyVersionTimeStaysAtCreationAfterParentUpdateAndReload() throws IOException {
createAndUpdateVersionedImage();
Path file = storageDir.resolve("lambda-microvm-images.json");
ObjectMapper mapper = new ObjectMapper();
JsonNode stored = mapper.readTree(file.toFile());
for (JsonNode versions : stored.findValues("versions")) {
for (JsonNode version : versions) {
((ObjectNode) version).remove("updatedAt");
}
}
mapper.writeValue(file.toFile(), stored);
String beforeRead = Files.readString(file);
JsonNode initial = versionResponse(process(), "1.0");
assertEquals(initial.path("createdAt"), initial.path("updatedAt"));
assertEquals(beforeRead, Files.readString(file));
LambdaMicrovmsService reloaded = process();
reloaded.updateImage(REGION, "versioned-image",
"arn:aws:lambda:us-east-1:aws:microvm-image:replacement",
"arn:aws:iam::000000000000:role/replacement", "s3://artifacts/newest.zip", null,
"3.0", LambdaMicrovmsService.RuntimeSettings.defaults());
String afterUpdate = Files.readString(file);
LambdaMicrovmsService restarted = process();
assertNull(restarted.getVersion(REGION, "versioned-image", "1.0").updatedAt);
assertEquals(initial.path("createdAt"), versionResponse(restarted, "1.0").path("updatedAt"));
assertEquals(initial.path("createdAt"), versionResponse(process(), "1.0").path("updatedAt"));
assertEquals(afterUpdate, Files.readString(file));
}
private void createAndUpdateVersionedImage() {
LambdaMicrovmsService service = process();
service.createImage(REGION, ACCOUNT_ID, "versioned-image",