mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(lambda): Lambda layer support (PublishLayerVersion, GetLayerVersion, ListLayerVersions, DeleteLayerVersion) (#1015)
* feat: support PublishLayerVersion, GetLayerVersion, ListLayerVersions, DeleteLayerVersion and layer attachment
Implements full Lambda Layer lifecycle:
- PublishLayerVersion: POST /2018-10-31/layers/{name}/versions
- GetLayerVersion: GET /2018-10-31/layers/{name}/versions/{ver}
- ListLayerVersions: GET /2018-10-31/layers/{name}/versions
- DeleteLayerVersion: DELETE /2018-10-31/layers/{name}/versions/{ver}
- ListLayers: GET /2018-10-31/layers (now returns real data)
Layer content is extracted to disk and copied into Lambda containers
at /opt during launch, matching real AWS behavior where layers are
available at /opt/nodejs/node_modules/ etc.
CreateFunction and UpdateFunctionConfiguration already stored layer
ARNs; this commit makes the layer data actually resolvable and
mountable at runtime.
Closes #875
* feat: add Node SDK compatibility tests for Lambda Layers
Adds lambda-layers.test.ts covering:
- PublishLayerVersion (version 1 and 2)
- GetLayerVersion (success and 404)
- ListLayerVersions
- ListLayers
- DeleteLayerVersion (success, idempotent for non-existent)
- Layer attachment on CreateFunction
- Layer round-trip via GetFunctionConfiguration
- Layer update/removal via UpdateFunctionConfiguration
This commit is contained in:
1 parent
1dfa7bfcab
commit
7e43b015e6
9 files changed
+1160
-15
No files matched your search
+134
-12
@@ -1,21 +1,34 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Lambda layer endpoints — use the /2018-10-31 API version prefix.
|
||||
* Lambda layer endpoints — uses the /2018-10-31 API version prefix.
|
||||
*
|
||||
* ListLayers: GET /2018-10-31/layers
|
||||
* ListLayerVersions: GET /2018-10-31/layers/{LayerName}/versions
|
||||
* PublishLayerVersion: POST /2018-10-31/layers/{LayerName}/versions
|
||||
* ListLayerVersions: GET /2018-10-31/layers/{LayerName}/versions
|
||||
* GetLayerVersion: GET /2018-10-31/layers/{LayerName}/versions/{VersionNumber}
|
||||
* DeleteLayerVersion: DELETE /2018-10-31/layers/{LayerName}/versions/{VersionNumber}
|
||||
* ListLayers: GET /2018-10-31/layers
|
||||
*/
|
||||
@Path("/2018-10-31")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@@ -23,25 +36,134 @@ import jakarta.ws.rs.core.Response;
|
||||
public class LambdaLayerController {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final LambdaLayerService layerService;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public LambdaLayerController(ObjectMapper objectMapper) {
|
||||
public LambdaLayerController(ObjectMapper objectMapper,
|
||||
LambdaLayerService layerService,
|
||||
RegionResolver regionResolver) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.layerService = layerService;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/layers")
|
||||
public Response listLayers() {
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
root.putArray("Layers");
|
||||
return Response.ok(root).build();
|
||||
@POST
|
||||
@Path("/layers/{layerName}/versions")
|
||||
public Response publishLayerVersion(@PathParam("layerName") String layerName,
|
||||
@Context HttpHeaders headers,
|
||||
Map<String, Object> request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
LambdaLayerVersion lv = layerService.publishLayerVersion(region, layerName, request);
|
||||
return Response.status(201).entity(buildLayerVersionResponse(lv)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/layers/{layerName}/versions")
|
||||
public Response listLayerVersions(@PathParam("layerName") String layerName) {
|
||||
public Response listLayerVersions(@PathParam("layerName") String layerName,
|
||||
@Context HttpHeaders headers) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<LambdaLayerVersion> versions = layerService.listLayerVersions(region, layerName);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
root.putArray("LayerVersions");
|
||||
ArrayNode arr = root.putArray("LayerVersions");
|
||||
for (LambdaLayerVersion lv : versions) {
|
||||
arr.add(buildLayerVersionSummary(lv));
|
||||
}
|
||||
return Response.ok(root).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/layers/{layerName}/versions/{versionNumber}")
|
||||
public Response getLayerVersion(@PathParam("layerName") String layerName,
|
||||
@PathParam("versionNumber") long versionNumber,
|
||||
@Context HttpHeaders headers) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
LambdaLayerVersion lv = layerService.getLayerVersion(region, layerName, versionNumber);
|
||||
return Response.ok(buildLayerVersionResponse(lv)).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/layers/{layerName}/versions/{versionNumber}")
|
||||
public Response deleteLayerVersion(@PathParam("layerName") String layerName,
|
||||
@PathParam("versionNumber") long versionNumber,
|
||||
@Context HttpHeaders headers) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
layerService.deleteLayerVersion(region, layerName, versionNumber);
|
||||
return Response.noContent().build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/layers")
|
||||
public Response listLayers(@Context HttpHeaders headers) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<LambdaLayerVersion> layers = layerService.listLayers(region);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode arr = root.putArray("Layers");
|
||||
for (LambdaLayerVersion lv : layers) {
|
||||
ObjectNode layerNode = objectMapper.createObjectNode();
|
||||
layerNode.put("LayerName", lv.getLayerName());
|
||||
layerNode.put("LayerArn", lv.getLayerArn());
|
||||
ObjectNode latestVersion = layerNode.putObject("LatestMatchingVersion");
|
||||
latestVersion.put("LayerVersionArn", lv.getLayerVersionArn());
|
||||
latestVersion.put("Version", lv.getVersion());
|
||||
latestVersion.put("Description", lv.getDescription() != null ? lv.getDescription() : "");
|
||||
latestVersion.put("CreatedDate", lv.getCreatedDate());
|
||||
latestVersion.put("LicenseInfo", lv.getLicenseInfo() != null ? lv.getLicenseInfo() : "");
|
||||
if (lv.getCompatibleRuntimes() != null && !lv.getCompatibleRuntimes().isEmpty()) {
|
||||
ArrayNode runtimes = latestVersion.putArray("CompatibleRuntimes");
|
||||
lv.getCompatibleRuntimes().forEach(runtimes::add);
|
||||
}
|
||||
if (lv.getCompatibleArchitectures() != null && !lv.getCompatibleArchitectures().isEmpty()) {
|
||||
ArrayNode archs = latestVersion.putArray("CompatibleArchitectures");
|
||||
lv.getCompatibleArchitectures().forEach(archs::add);
|
||||
}
|
||||
arr.add(layerNode);
|
||||
}
|
||||
return Response.ok(root).build();
|
||||
}
|
||||
|
||||
private ObjectNode buildLayerVersionResponse(LambdaLayerVersion lv) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("LayerArn", lv.getLayerArn());
|
||||
node.put("LayerVersionArn", lv.getLayerVersionArn());
|
||||
node.put("Version", lv.getVersion());
|
||||
node.put("Description", lv.getDescription() != null ? lv.getDescription() : "");
|
||||
node.put("CreatedDate", lv.getCreatedDate());
|
||||
node.put("LicenseInfo", lv.getLicenseInfo() != null ? lv.getLicenseInfo() : "");
|
||||
|
||||
// Content block
|
||||
ObjectNode contentNode = node.putObject("Content");
|
||||
contentNode.put("CodeSha256", lv.getCodeSha256() != null ? lv.getCodeSha256() : "");
|
||||
contentNode.put("CodeSize", lv.getCodeSizeBytes());
|
||||
// Location is a presigned URL in real AWS; we provide a placeholder
|
||||
contentNode.put("Location", "");
|
||||
|
||||
if (lv.getCompatibleRuntimes() != null && !lv.getCompatibleRuntimes().isEmpty()) {
|
||||
ArrayNode runtimes = node.putArray("CompatibleRuntimes");
|
||||
lv.getCompatibleRuntimes().forEach(runtimes::add);
|
||||
}
|
||||
if (lv.getCompatibleArchitectures() != null && !lv.getCompatibleArchitectures().isEmpty()) {
|
||||
ArrayNode archs = node.putArray("CompatibleArchitectures");
|
||||
lv.getCompatibleArchitectures().forEach(archs::add);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private ObjectNode buildLayerVersionSummary(LambdaLayerVersion lv) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("LayerVersionArn", lv.getLayerVersionArn());
|
||||
node.put("Version", lv.getVersion());
|
||||
node.put("Description", lv.getDescription() != null ? lv.getDescription() : "");
|
||||
node.put("CreatedDate", lv.getCreatedDate());
|
||||
node.put("LicenseInfo", lv.getLicenseInfo() != null ? lv.getLicenseInfo() : "");
|
||||
if (lv.getCompatibleRuntimes() != null && !lv.getCompatibleRuntimes().isEmpty()) {
|
||||
ArrayNode runtimes = node.putArray("CompatibleRuntimes");
|
||||
lv.getCompatibleRuntimes().forEach(runtimes::add);
|
||||
}
|
||||
if (lv.getCompatibleArchitectures() != null && !lv.getCompatibleArchitectures().isEmpty()) {
|
||||
ArrayNode archs = node.putArray("CompatibleArchitectures");
|
||||
lv.getCompatibleArchitectures().forEach(archs::add);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Object;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Business logic for Lambda Layer management.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class LambdaLayerService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(LambdaLayerService.class);
|
||||
|
||||
private final LambdaLayerStore layerStore;
|
||||
private final ZipExtractor zipExtractor;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
private final S3Service s3Service;
|
||||
|
||||
@Inject
|
||||
public LambdaLayerService(LambdaLayerStore layerStore,
|
||||
ZipExtractor zipExtractor,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver,
|
||||
S3Service s3Service) {
|
||||
this.layerStore = layerStore;
|
||||
this.zipExtractor = zipExtractor;
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
this.s3Service = s3Service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes a new layer version. Each call with the same layer name creates a new version.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public LambdaLayerVersion publishLayerVersion(String region, String layerName, Map<String, Object> request) {
|
||||
if (layerName == null || layerName.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValueException", "LayerName is required", 400);
|
||||
}
|
||||
|
||||
Map<String, Object> content = (Map<String, Object>) request.get("Content");
|
||||
if (content == null) {
|
||||
throw new AwsException("InvalidParameterValueException", "Content is required", 400);
|
||||
}
|
||||
|
||||
String description = (String) request.get("Description");
|
||||
String licenseInfo = (String) request.get("LicenseInfo");
|
||||
|
||||
List<String> compatibleRuntimes = request.get("CompatibleRuntimes") instanceof List
|
||||
? (List<String>) request.get("CompatibleRuntimes") : null;
|
||||
List<String> compatibleArchitectures = request.get("CompatibleArchitectures") instanceof List
|
||||
? (List<String>) request.get("CompatibleArchitectures") : null;
|
||||
|
||||
// Resolve the zip content
|
||||
byte[] zipBytes = resolveLayerContent(content);
|
||||
|
||||
// Determine the next version number
|
||||
long nextVersion = layerStore.getLatestVersion(region, layerName) + 1;
|
||||
|
||||
// Extract the layer zip to disk
|
||||
Path layerPath = getLayerCodePath(layerName, nextVersion);
|
||||
try {
|
||||
zipExtractor.extractTo(zipBytes, layerPath);
|
||||
} catch (IOException e) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Failed to extract layer archive: " + e.getMessage(), 400);
|
||||
}
|
||||
|
||||
// Compute SHA-256
|
||||
String codeSha256 = computeSha256(zipBytes);
|
||||
|
||||
// Build the layer version
|
||||
String accountId = regionResolver.getAccountId();
|
||||
String layerArn = String.format("arn:aws:lambda:%s:%s:layer:%s", region, accountId, layerName);
|
||||
String layerVersionArn = layerArn + ":" + nextVersion;
|
||||
|
||||
LambdaLayerVersion layerVersion = new LambdaLayerVersion();
|
||||
layerVersion.setLayerName(layerName);
|
||||
layerVersion.setLayerArn(layerArn);
|
||||
layerVersion.setLayerVersionArn(layerVersionArn);
|
||||
layerVersion.setVersion(nextVersion);
|
||||
layerVersion.setDescription(description);
|
||||
layerVersion.setLicenseInfo(licenseInfo);
|
||||
layerVersion.setCompatibleRuntimes(compatibleRuntimes != null ? new ArrayList<>(compatibleRuntimes) : null);
|
||||
layerVersion.setCompatibleArchitectures(compatibleArchitectures != null ? new ArrayList<>(compatibleArchitectures) : null);
|
||||
layerVersion.setCreatedDate(DateTimeFormatter.ISO_INSTANT.format(Instant.now().atOffset(ZoneOffset.UTC)));
|
||||
layerVersion.setCodeSizeBytes(zipBytes.length);
|
||||
layerVersion.setCodeSha256(codeSha256);
|
||||
layerVersion.setCodeLocalPath(layerPath.toAbsolutePath().normalize().toString());
|
||||
|
||||
layerStore.save(region, layerVersion);
|
||||
LOG.infov("Published layer version: {0} v{1} in region {2}", layerName, nextVersion, region);
|
||||
return layerVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about a specific layer version.
|
||||
*/
|
||||
public LambdaLayerVersion getLayerVersion(String region, String layerName, long versionNumber) {
|
||||
return layerStore.get(region, layerName, versionNumber)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Layer version " + versionNumber + " for layer " + layerName + " not found.", 404));
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all versions of a layer.
|
||||
*/
|
||||
public List<LambdaLayerVersion> listLayerVersions(String region, String layerName) {
|
||||
return layerStore.listVersions(region, layerName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all layers in a region (returns the latest version of each).
|
||||
*/
|
||||
public List<LambdaLayerVersion> listLayers(String region) {
|
||||
return layerStore.listLayers(region);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a specific layer version.
|
||||
*/
|
||||
public void deleteLayerVersion(String region, String layerName, long versionNumber) {
|
||||
LambdaLayerVersion lv = layerStore.get(region, layerName, versionNumber).orElse(null);
|
||||
if (lv == null) {
|
||||
// AWS returns 204 even if the layer version doesn't exist
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete the extracted code from disk
|
||||
if (lv.getCodeLocalPath() != null) {
|
||||
Path codePath = Path.of(lv.getCodeLocalPath());
|
||||
deleteDirectory(codePath);
|
||||
}
|
||||
|
||||
layerStore.delete(region, layerName, versionNumber);
|
||||
LOG.infov("Deleted layer version: {0} v{1} in region {2}", layerName, versionNumber, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a layer version ARN to its local code path.
|
||||
* Used by the container launcher to copy layer content into /opt.
|
||||
*/
|
||||
public LambdaLayerVersion resolveLayerByArn(String layerVersionArn) {
|
||||
// ARN format: arn:aws:lambda:{region}:{account}:layer:{name}:{version}
|
||||
String[] parts = layerVersionArn.split(":");
|
||||
if (parts.length < 8) {
|
||||
return null;
|
||||
}
|
||||
String region = parts[3];
|
||||
String layerName = parts[6];
|
||||
long version;
|
||||
try {
|
||||
version = Long.parseLong(parts[7]);
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
return layerStore.get(region, layerName, version).orElse(null);
|
||||
}
|
||||
|
||||
private byte[] resolveLayerContent(Map<String, Object> content) {
|
||||
String zipFileBase64 = (String) content.get("ZipFile");
|
||||
if (zipFileBase64 != null) {
|
||||
return Base64.getDecoder().decode(zipFileBase64);
|
||||
}
|
||||
|
||||
String s3Bucket = (String) content.get("S3Bucket");
|
||||
String s3Key = (String) content.get("S3Key");
|
||||
if (s3Bucket != null && s3Key != null) {
|
||||
if (s3Service == null) {
|
||||
throw new AwsException("ServiceUnavailableException", "S3 service not available", 503);
|
||||
}
|
||||
try {
|
||||
S3Object obj = s3Service.getObject(s3Bucket, s3Key);
|
||||
return obj.getData();
|
||||
} catch (Exception e) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Unable to fetch layer content from s3://" + s3Bucket + "/" + s3Key + ": " + e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Layer content must include either ZipFile or S3Bucket/S3Key", 400);
|
||||
}
|
||||
|
||||
private Path getLayerCodePath(String layerName, long version) {
|
||||
String sanitized = layerName.replaceAll("[^a-zA-Z0-9_\\-.]", "_");
|
||||
return Path.of(config.services().lambda().codePath())
|
||||
.resolve("layers")
|
||||
.resolve(sanitized)
|
||||
.resolve(String.valueOf(version));
|
||||
}
|
||||
|
||||
private String computeSha256(byte[] data) {
|
||||
try {
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256").digest(data);
|
||||
return Base64.getEncoder().encodeToString(digest);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteDirectory(Path dir) {
|
||||
if (!Files.exists(dir)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Files.walk(dir)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.forEach(p -> {
|
||||
try {
|
||||
Files.delete(p);
|
||||
} catch (IOException e) {
|
||||
LOG.warnv("Failed to delete {0}: {1}", p, e.getMessage());
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
LOG.warnv("Failed to delete layer directory {0}: {1}", dir, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Wraps the storage backend for Lambda Layer Versions.
|
||||
* Keys follow the pattern: layer::{region}::{layerName}::{version}
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class LambdaLayerStore {
|
||||
|
||||
private final StorageBackend<String, LambdaLayerVersion> backend;
|
||||
|
||||
@Inject
|
||||
public LambdaLayerStore(StorageFactory storageFactory) {
|
||||
this.backend = storageFactory.create("lambda", "lambda-layers.json",
|
||||
new TypeReference<Map<String, LambdaLayerVersion>>() {});
|
||||
}
|
||||
|
||||
LambdaLayerStore(StorageBackend<String, LambdaLayerVersion> backend) {
|
||||
this.backend = backend;
|
||||
}
|
||||
|
||||
public void save(String region, LambdaLayerVersion layerVersion) {
|
||||
backend.put(key(region, layerVersion.getLayerName(), layerVersion.getVersion()), layerVersion);
|
||||
}
|
||||
|
||||
public Optional<LambdaLayerVersion> get(String region, String layerName, long version) {
|
||||
return backend.get(key(region, layerName, version));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all versions of a given layer in a region, sorted by version ascending.
|
||||
*/
|
||||
public List<LambdaLayerVersion> listVersions(String region, String layerName) {
|
||||
String prefix = "layer::" + region + "::" + layerName + "::";
|
||||
return backend.scan(k -> k.startsWith(prefix)).stream()
|
||||
.sorted((a, b) -> Long.compare(a.getVersion(), b.getVersion()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the latest version number for a layer, or 0 if no versions exist.
|
||||
*/
|
||||
public long getLatestVersion(String region, String layerName) {
|
||||
return listVersions(region, layerName).stream()
|
||||
.mapToLong(LambdaLayerVersion::getVersion)
|
||||
.max()
|
||||
.orElse(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all distinct layers in a region (latest version of each).
|
||||
*/
|
||||
public List<LambdaLayerVersion> listLayers(String region) {
|
||||
String prefix = "layer::" + region + "::";
|
||||
List<LambdaLayerVersion> all = backend.scan(k -> k.startsWith(prefix));
|
||||
// Group by layer name and return the latest version of each
|
||||
return all.stream()
|
||||
.collect(java.util.stream.Collectors.groupingBy(LambdaLayerVersion::getLayerName))
|
||||
.values().stream()
|
||||
.map(versions -> versions.stream()
|
||||
.max((a, b) -> Long.compare(a.getVersion(), b.getVersion()))
|
||||
.orElseThrow())
|
||||
.toList();
|
||||
}
|
||||
|
||||
public void delete(String region, String layerName, long version) {
|
||||
backend.delete(key(region, layerName, version));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all versions of a layer.
|
||||
*/
|
||||
public void deleteAll(String region, String layerName) {
|
||||
listVersions(region, layerName).forEach(lv ->
|
||||
backend.delete(key(region, lv.getLayerName(), lv.getVersion())));
|
||||
}
|
||||
|
||||
private static String key(String region, String layerName, long version) {
|
||||
return "layer::" + region + "::" + layerName + "::" + version;
|
||||
}
|
||||
}
|
||||
+24
-1
@@ -8,8 +8,10 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLayerService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.ContainerState;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer;
|
||||
import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServerFactory;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
@@ -60,6 +62,7 @@ public class ContainerLauncher {
|
||||
private final EmulatorConfig config;
|
||||
private final EcrRegistryManager ecrRegistryManager;
|
||||
private final EmbeddedDnsServer embeddedDnsServer;
|
||||
private final LambdaLayerService layerService;
|
||||
|
||||
/** Matches an AWS-shaped ECR image URI: {@code <account>.dkr.ecr.<region>.amazonaws.com/<repo>[:tag]}. */
|
||||
private static final java.util.regex.Pattern AWS_ECR_URI =
|
||||
@@ -74,7 +77,8 @@ public class ContainerLauncher {
|
||||
DockerHostResolver dockerHostResolver,
|
||||
EmulatorConfig config,
|
||||
EcrRegistryManager ecrRegistryManager,
|
||||
EmbeddedDnsServer embeddedDnsServer) {
|
||||
EmbeddedDnsServer embeddedDnsServer,
|
||||
LambdaLayerService layerService) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.logStreamer = logStreamer;
|
||||
@@ -84,6 +88,7 @@ public class ContainerLauncher {
|
||||
this.config = config;
|
||||
this.ecrRegistryManager = ecrRegistryManager;
|
||||
this.embeddedDnsServer = embeddedDnsServer;
|
||||
this.layerService = layerService;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,6 +267,24 @@ public class ContainerLauncher {
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Copy layer contents into /opt (layers are merged in order)
|
||||
if (fn.getLayers() != null && !fn.getLayers().isEmpty()) {
|
||||
for (String layerArn : fn.getLayers()) {
|
||||
LambdaLayerVersion layer = layerService.resolveLayerByArn(layerArn);
|
||||
if (layer != null && layer.getCodeLocalPath() != null) {
|
||||
Path layerPath = Path.of(layer.getCodeLocalPath());
|
||||
if (Files.exists(layerPath)) {
|
||||
copyDirToContainer(dockerClient, containerId, layerPath, "/opt", fn.getFunctionName());
|
||||
LOG.debugv("Copied layer {0} into container {1} at /opt", layerArn, containerId);
|
||||
} else {
|
||||
LOG.warnv("Layer code path not found for {0}: {1}", layerArn, layer.getCodeLocalPath());
|
||||
}
|
||||
} else {
|
||||
LOG.warnv("Could not resolve layer ARN: {0} for function {1}", layerArn, fn.getFunctionName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now start the container with code in place
|
||||
lifecycleManager.startCreated(containerId, spec);
|
||||
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package io.github.hectorvent.floci.services.lambda.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a single published version of a Lambda layer.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class LambdaLayerVersion {
|
||||
|
||||
private String layerName;
|
||||
private String layerArn;
|
||||
private String layerVersionArn;
|
||||
private long version;
|
||||
private String description;
|
||||
private String licenseInfo;
|
||||
private List<String> compatibleRuntimes;
|
||||
private List<String> compatibleArchitectures;
|
||||
private String createdDate;
|
||||
private long codeSizeBytes;
|
||||
private String codeSha256;
|
||||
private String codeLocalPath;
|
||||
|
||||
public LambdaLayerVersion() {
|
||||
}
|
||||
|
||||
public String getLayerName() { return layerName; }
|
||||
public void setLayerName(String layerName) { this.layerName = layerName; }
|
||||
|
||||
public String getLayerArn() { return layerArn; }
|
||||
public void setLayerArn(String layerArn) { this.layerArn = layerArn; }
|
||||
|
||||
public String getLayerVersionArn() { return layerVersionArn; }
|
||||
public void setLayerVersionArn(String layerVersionArn) { this.layerVersionArn = layerVersionArn; }
|
||||
|
||||
public long getVersion() { return version; }
|
||||
public void setVersion(long version) { this.version = version; }
|
||||
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
|
||||
public String getLicenseInfo() { return licenseInfo; }
|
||||
public void setLicenseInfo(String licenseInfo) { this.licenseInfo = licenseInfo; }
|
||||
|
||||
public List<String> getCompatibleRuntimes() { return compatibleRuntimes; }
|
||||
public void setCompatibleRuntimes(List<String> compatibleRuntimes) { this.compatibleRuntimes = compatibleRuntimes; }
|
||||
|
||||
public List<String> getCompatibleArchitectures() { return compatibleArchitectures; }
|
||||
public void setCompatibleArchitectures(List<String> compatibleArchitectures) { this.compatibleArchitectures = compatibleArchitectures; }
|
||||
|
||||
public String getCreatedDate() { return createdDate; }
|
||||
public void setCreatedDate(String createdDate) { this.createdDate = createdDate; }
|
||||
|
||||
public long getCodeSizeBytes() { return codeSizeBytes; }
|
||||
public void setCodeSizeBytes(long codeSizeBytes) { this.codeSizeBytes = codeSizeBytes; }
|
||||
|
||||
public String getCodeSha256() { return codeSha256; }
|
||||
public void setCodeSha256(String codeSha256) { this.codeSha256 = codeSha256; }
|
||||
|
||||
public String getCodeLocalPath() { return codeLocalPath; }
|
||||
public void setCodeLocalPath(String codeLocalPath) { this.codeLocalPath = codeLocalPath; }
|
||||
}
|
||||
Reference in new issue
Block a user