fix(lambda): include Lambda MicroVM resources (#7352)

This commit is contained in:
KrishmalX authored and GitHub committed 2026-11-05 04:57:31 -04:00
1 parent a15dfb621e
commit 50aef92124
8 files changed
+221 -45

No files matched your search

@@ -2,9 +2,8 @@ package io.github.hectorvent.floci.services.lambda;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
import io.github.hectorvent.floci.core.common.TaggingApiOnly;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
@@ -26,47 +25,30 @@ import java.util.Map;
* TagResource: POST /2017-03-31/tags/{ARN}
* ListTags: GET /2017-03-31/tags/{ARN}
* UntagResource: DELETE /2017-03-31/tags/{ARN}?tagKeys=...
*
* MicroVM-family ARNs share this route with classic Lambda resources; {@link LambdaTagHandler}
* dispatches between them.
*/
@Path("/2017-03-31")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class LambdaTagController {
private final LambdaService lambdaService;
private final LambdaMicrovmsService microvmsService;
private final LambdaTagHandler tagHandler;
private final ObjectMapper objectMapper;
@Inject
public LambdaTagController(LambdaService lambdaService,
LambdaMicrovmsService microvmsService,
ObjectMapper objectMapper) {
this.lambdaService = lambdaService;
this.microvmsService = microvmsService;
public LambdaTagController(@TaggingApiOnly LambdaTagHandler tagHandler, ObjectMapper objectMapper) {
this.tagHandler = tagHandler;
this.objectMapper = objectMapper;
}
/**
* MicroVM-family ARNs (microvm-image, microvm, network-connector) share
* the /2017-03-31/tags route with classic Lambda resources; dispatch on
* the ARN's resource type. The region comes from the ARN itself.
*/
private static String arnRegion(String arn) {
return AwsArnUtils.parse(arn).region();
}
@GET
@Path("/tags/{arn}")
public Response listTags(@PathParam("arn") String arn) {
if (LambdaMicrovmsService.ownsArn(arn)) {
ObjectNode mvRoot = objectMapper.createObjectNode();
ObjectNode mvTags = mvRoot.putObject("Tags");
microvmsService.listTags(arnRegion(arn), arn).forEach(mvTags::put);
return Response.ok(mvRoot).build();
}
Map<String, String> tags = lambdaService.listTags(arn);
ObjectNode root = objectMapper.createObjectNode();
ObjectNode tagsNode = root.putObject("Tags");
tags.forEach(tagsNode::put);
tagHandler.listTags(null, arn).forEach(tagsNode::put);
return Response.ok(root).build();
}
@@ -80,11 +62,7 @@ public class LambdaTagController {
if (tags == null) {
throw new AwsException("InvalidParameterValueException", "Tags is required", 400);
}
if (LambdaMicrovmsService.ownsArn(arn)) {
microvmsService.tagResource(arnRegion(arn), arn, tags);
} else {
lambdaService.tagResource(arn, tags);
}
tagHandler.tagResource(null, arn, tags);
return Response.noContent().build();
} catch (AwsException e) {
throw e;
@@ -97,11 +75,7 @@ public class LambdaTagController {
@Path("/tags/{arn}")
public Response untagResource(@PathParam("arn") String arn,
@QueryParam("tagKeys") List<String> tagKeys) {
if (LambdaMicrovmsService.ownsArn(arn)) {
microvmsService.untagResource(arnRegion(arn), arn, tagKeys);
} else {
lambdaService.untagResource(arn, tagKeys);
}
tagHandler.untagResource(null, arn, tagKeys);
return Response.noContent().build();
}
}
@@ -1,7 +1,9 @@
package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.TagHandler;
import io.github.hectorvent.floci.core.common.TaggingApiOnly;
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -9,7 +11,12 @@ import java.util.List;
import java.util.Map;
/**
* Lets Resource Groups Tagging API writes reach a function's own tags.
* Lets Resource Groups Tagging API writes reach a function's own tags, and those of a MicroVM
* image or network connector.
*
* <p>Also the one place that routes MicroVM-family ARNs apart from classic Lambda resources:
* {@link LambdaTagController} calls it too. The {@code region} argument is unused, because a
* MicroVM ARN carries its own.
*
* <p>A separate {@link TaggingApiOnly} bean because Lambda tags on its own
* {@code /2017-03-31/tags/{arn}} path: a default-qualified handler would also serve the
@@ -20,10 +27,12 @@ import java.util.Map;
public class LambdaTagHandler implements TagHandler {
private final LambdaService lambdaService;
private final LambdaMicrovmsService microvmsService;
@Inject
public LambdaTagHandler(LambdaService lambdaService) {
public LambdaTagHandler(LambdaService lambdaService, LambdaMicrovmsService microvmsService) {
this.lambdaService = lambdaService;
this.microvmsService = microvmsService;
}
@Override
@@ -33,16 +42,31 @@ public class LambdaTagHandler implements TagHandler {
@Override
public Map<String, String> listTags(String region, String arn) {
if (LambdaMicrovmsService.ownsArn(arn)) {
return microvmsService.listTags(arnRegion(arn), arn);
}
return lambdaService.listTags(arn);
}
@Override
public void tagResource(String region, String arn, Map<String, String> tags) {
if (LambdaMicrovmsService.ownsArn(arn)) {
microvmsService.tagResource(arnRegion(arn), arn, tags);
return;
}
lambdaService.tagResource(arn, tags);
}
@Override
public void untagResource(String region, String arn, List<String> tagKeys) {
if (LambdaMicrovmsService.ownsArn(arn)) {
microvmsService.untagResource(arnRegion(arn), arn, tagKeys);
return;
}
lambdaService.untagResource(arn, tagKeys);
}
private static String arnRegion(String arn) {
return AwsArnUtils.parse(arn).region();
}
}
@@ -394,7 +394,7 @@ public class LambdaMicrovmsController {
node.putNull("tags");
} else if (shape == ImageShape.GET) {
ObjectNode tags = node.putObject("tags");
image.tags.forEach(tags::put);
service.snapshotTags(image.tags).forEach(tags::put);
}
return node;
}
@@ -4,6 +4,9 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.resource.ExplorerResource;
import io.github.hectorvent.floci.core.resource.ResourceProvider;
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
@@ -46,8 +49,10 @@ import java.util.stream.Stream;
* observes the resting state.</p>
*/
@ApplicationScoped
public class LambdaMicrovmsService {
public class LambdaMicrovmsService implements ResourceProvider {
private static final String IMAGE_TYPE = "lambda:microvm-image";
private static final String CONNECTOR_TYPE = "lambda:network-connector";
private static final Pattern NAME_PATTERN = Pattern.compile("[a-zA-Z0-9-_]{1,64}");
private static final int MAX_CONNECTOR_SUBNETS = 16;
private static final Pattern CONNECTOR_VERSION = Pattern.compile("[1-9][0-9]{0,17}");
@@ -60,6 +65,8 @@ public class LambdaMicrovmsService {
private final RegionResolver regionResolver;
private final StorageFactory storageFactory;
/** Guards every read and write of an image's or connector's tag map. */
private final Object tagLock = new Object();
/**
* Keyed {@code region/id}. The account is not part of the key here: the backend prefixes every
@@ -710,6 +717,44 @@ public class LambdaMicrovmsService {
// ------------------------------------------------------------------ tags
@Override
public List<ExplorerResource> getResources() {
List<ExplorerResource> resources = new ArrayList<>();
for (MicrovmImage image : images.values()) {
addResource(resources, image.imageArn, IMAGE_TYPE, image.updatedAt, image.tags);
}
for (NetworkConnector connector : connectors.values()) {
addResource(resources, connector.arn, CONNECTOR_TYPE, connector.lastModified, connector.tags);
}
return resources;
}
private void addResource(List<ExplorerResource> resources, String arn, String type,
Instant lastModified, Map<String, String> tags) {
if (arn == null) {
return;
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
resources.add(new ExplorerResource(arn, type, "lambda", parsed.region(), parsed.accountId(),
lastModified != null ? lastModified : Instant.now(), snapshotTags(tags)));
}
/** A copy taken under the tag lock, so a concurrent tag change cannot break the iteration. */
Map<String, String> snapshotTags(Map<String, String> tags) {
synchronized (tagLock) {
return new LinkedHashMap<>(tags);
}
}
/**
* Empty on purpose: AWS ListSupportedResourceTypes advertises neither type, so the images and
* connectors stay discoverable through {@link #getResources()} without widening that list.
*/
@Override
public Set<SupportedResourceType> getSupportedResourceTypes() {
return Set.of();
}
/** True when the ARN names a MicroVM-family resource this service owns. */
public static boolean ownsArn(String arn) {
String resource = AwsArnUtils.resourceIfArnFor(arn, "lambda").orElse("");
@@ -745,7 +790,7 @@ public class LambdaMicrovmsService {
}
public Map<String, String> listTags(String region, String arn) {
return taggable(region, arn);
return snapshotTags(taggable(region, arn));
}
public void tagResource(String region, String arn, Map<String, String> tags) {
@@ -768,13 +813,17 @@ public class LambdaMicrovmsService {
String resource = tagTargetResource(arn);
if (resource.startsWith(IMAGE_RESOURCE)) {
MicrovmImage image = getImage(region, resource.substring(IMAGE_RESOURCE.length()));
mutation.accept(image.tags);
synchronized (tagLock) {
mutation.accept(image.tags);
}
persist(images, region, image.name, image);
return;
}
if (resource.startsWith(CONNECTOR_RESOURCE)) {
NetworkConnector connector = getConnector(region, resource.substring(CONNECTOR_RESOURCE.length()));
mutation.accept(connector.tags);
synchronized (tagLock) {
mutation.accept(connector.tags);
}
persist(connectors, region, connector.id, connector);
return;
}