mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(lambda): add the ability to name and label lambda containers, code volumes (#2409)
This commit is contained in:
1 parent
3f12428d97
commit
f8b07a78ff
10 files changed
+321
-6
No files matched your search
@@ -1513,6 +1513,17 @@ public interface EmulatorConfig {
|
||||
/** Docker network to attach Lambda containers to. Empty = default bridge. */
|
||||
Optional<String> dockerNetwork();
|
||||
|
||||
/**
|
||||
* Base name prefix for the containers and code volumes Lambda spawns, replacing the
|
||||
* default {@code floci} (e.g. prefix {@code acme} names containers
|
||||
* {@code acme-<function>-<id>} and code volumes {@code acme-code-<function>-<hash>}).
|
||||
* Must be a valid Docker name segment ({@code [A-Za-z0-9][A-Za-z0-9_.-]*}); invalid
|
||||
* values are ignored with a warning. Unset or blank falls back to {@code floci}.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_LAMBDA_CONTAINER_NAME_PREFIX
|
||||
*/
|
||||
Optional<String> containerNamePrefix();
|
||||
|
||||
/**
|
||||
* Extra /etc/hosts entries added to every Lambda container, as "hostname:ip" pairs.
|
||||
* The ip may be the literal "host-gateway" to map to the Docker host, mirroring
|
||||
@@ -1917,5 +1928,22 @@ public interface EmulatorConfig {
|
||||
String username();
|
||||
String password();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra Docker labels applied to every container and volume Floci creates,
|
||||
* alongside the reserved {@code floci}, {@code floci_emulator} and
|
||||
* {@code floci_namespace} labels (entries using a reserved key are ignored).
|
||||
* A list of key/value entries rather than a {@code Map} so label keys with
|
||||
* characters outside SmallRye's env-var naming convention (dots, colons,
|
||||
* mixed case) survive:
|
||||
* {@code FLOCI_DOCKER_EXTRA_LABELS_0__KEY} / {@code FLOCI_DOCKER_EXTRA_LABELS_0__VALUE}.
|
||||
*/
|
||||
@WithDefault("")
|
||||
List<LabelEntry> extraLabels();
|
||||
|
||||
interface LabelEntry {
|
||||
String key();
|
||||
String value();
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -52,14 +52,50 @@ public final class ContainerStorageHelper {
|
||||
return "floci-" + namespace + "-" + baseName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link #dockerName} but with a caller-supplied base prefix in place of the default
|
||||
* {@code floci}: {@code <prefix>-<rest>}, or {@code <prefix>-<namespace>-<rest>} when a
|
||||
* resource namespace is configured.
|
||||
*/
|
||||
public static String prefixedDockerName(EmulatorConfig config, String prefix, String rest) {
|
||||
String namespace = resourceNamespace(config);
|
||||
if (namespace.isBlank()) {
|
||||
return prefix + "-" + rest;
|
||||
}
|
||||
return prefix + "-" + namespace + "-" + rest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Label keys reserved for the emulator itself. {@code floci} and {@code floci_emulator}
|
||||
* drive container/volume discovery and pruning (e.g.
|
||||
* {@code docker volume prune --filter label=floci=true}); {@code floci_namespace} scopes
|
||||
* resources when multiple Floci processes share one daemon. Extra labels using these keys
|
||||
* are ignored so user configuration can never break cleanup.
|
||||
*/
|
||||
private static final java.util.Set<String> RESERVED_LABEL_KEYS =
|
||||
java.util.Set.of("floci", "floci_emulator", "floci_namespace");
|
||||
|
||||
/**
|
||||
* Labels applied to every emulator-created container and volume:
|
||||
* {@code floci=true} (umbrella across all Floci emulators),
|
||||
* {@code floci_emulator=floci-aws} (per-emulator discriminator), and
|
||||
* {@code floci_namespace} when a resource namespace is configured.
|
||||
* User-configured {@code floci.docker.extra-labels} entries are included first;
|
||||
* reserved keys always win on conflict.
|
||||
*/
|
||||
public static Map<String, String> defaultLabels(EmulatorConfig config) {
|
||||
Map<String, String> labels = new LinkedHashMap<>();
|
||||
if (config != null && config.docker() != null && config.docker().extraLabels() != null) {
|
||||
for (EmulatorConfig.DockerConfig.LabelEntry entry : config.docker().extraLabels()) {
|
||||
String key = entry.key() == null ? "" : entry.key().trim();
|
||||
if (key.isEmpty() || RESERVED_LABEL_KEYS.contains(key)) {
|
||||
LOG.warnv("Ignoring extra Docker label with {0} key: \"{1}\"",
|
||||
key.isEmpty() ? "blank" : "reserved", key);
|
||||
continue;
|
||||
}
|
||||
labels.put(key, entry.value() == null ? "" : entry.value());
|
||||
}
|
||||
}
|
||||
labels.put("floci", "true");
|
||||
labels.put("floci_emulator", "floci-" + CLOUD);
|
||||
String namespace = resourceNamespace(config);
|
||||
|
||||
+42
-6
@@ -68,6 +68,32 @@ public class ContainerLauncher {
|
||||
private static final String TASK_DIR = "/var/task";
|
||||
private static final String RUNTIME_DIR = "/var/runtime";
|
||||
|
||||
/** Default base prefix for the containers and code volumes Lambda spawns. */
|
||||
static final String DEFAULT_NAME_PREFIX = "floci";
|
||||
/** A prefix must be a legal Docker name on its own: names must start alphanumeric. */
|
||||
private static final java.util.regex.Pattern SAFE_NAME_PREFIX =
|
||||
java.util.regex.Pattern.compile("^[A-Za-z0-9][A-Za-z0-9_.-]*$");
|
||||
|
||||
/**
|
||||
* The base name prefix for Lambda containers and code volumes:
|
||||
* {@code floci.services.lambda.container-name-prefix} when set and Docker-safe,
|
||||
* otherwise the default {@code floci}.
|
||||
*/
|
||||
static String resolveContainerNamePrefix(EmulatorConfig config) {
|
||||
String configured = config.services().lambda().containerNamePrefix()
|
||||
.map(String::trim).orElse("");
|
||||
if (configured.isEmpty()) {
|
||||
return DEFAULT_NAME_PREFIX;
|
||||
}
|
||||
if (!SAFE_NAME_PREFIX.matcher(configured).matches()) {
|
||||
LOG.warnv("Ignoring floci.services.lambda.container-name-prefix \"{0}\": not a valid "
|
||||
+ "Docker name prefix ([A-Za-z0-9][A-Za-z0-9_.-]*); using \"{1}\"",
|
||||
configured, DEFAULT_NAME_PREFIX);
|
||||
return DEFAULT_NAME_PREFIX;
|
||||
}
|
||||
return configured;
|
||||
}
|
||||
|
||||
/**
|
||||
* In-container location of Floci's CA certificate, injected when TLS is enabled so the
|
||||
* container trusts Floci's self-signed HTTPS endpoint. {@code /etc} exists in every Lambda
|
||||
@@ -214,7 +240,8 @@ public class ContainerLauncher {
|
||||
|
||||
// Give the container a human-readable name (needed for log stream name below)
|
||||
String shortId = java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 8);
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-" + fn.getFunctionName() + "-" + shortId);
|
||||
String containerName = ContainerStorageHelper.prefixedDockerName(config,
|
||||
resolveContainerNamePrefix(config), fn.getFunctionName() + "-" + shortId);
|
||||
|
||||
// CloudWatch log coordinates — computed here so they can be injected as env vars
|
||||
String cwLogGroup = "/aws/lambda/" + fn.getFunctionName();
|
||||
@@ -583,7 +610,6 @@ public class ContainerLauncher {
|
||||
// nothing under that name, silently mounting an empty /var/task into the next container.
|
||||
// ensureCodeVolume re-checks lifecycleManager.volumeExists() rather than trusting this alone.
|
||||
private static final String CODE_VOLUME_MARKER_DIR = "lambda-codevol-markers";
|
||||
private static final String CODE_VOLUME_MARKER_PREFIX = "floci-code-";
|
||||
private final java.util.Set<String> populatedCodeVolumes = java.util.concurrent.ConcurrentHashMap.newKeySet();
|
||||
// Deliberately never pruned: removing an entry while a caller elsewhere still held a reference
|
||||
// to its lock object let a third caller's computeIfAbsent create a replacement lock for the same
|
||||
@@ -652,7 +678,7 @@ public class ContainerLauncher {
|
||||
* just mounts the volume read-only, turning a ~95s per-container copy into a ~0.2s mount.
|
||||
*/
|
||||
String ensureCodeVolume(LambdaFunction fn, String image) {
|
||||
String volName = codeVolumeName(fn);
|
||||
String volName = codeVolumeName(resolveContainerNamePrefix(config), fn);
|
||||
// Held for the whole resolve-and-reconcile, not just the populate branch: this is the same
|
||||
// lock cleanupSupersededVolumes acquires before claiming a volume for deletion, so a launch
|
||||
// that resolves a volume can never race a sweep that's about to delete that exact volume out
|
||||
@@ -805,7 +831,7 @@ public class ContainerLauncher {
|
||||
// A minimal helper container (sleep) with the volume mounted read-write at /var/task; we
|
||||
// tar-copy the code into it, then discard it — the data persists in the volume.
|
||||
ContainerSpec helperSpec = containerBuilder.newContainer(image)
|
||||
.withName("floci-codevol-" + fn.getFunctionName() + "-" + shortId)
|
||||
.withName(resolveContainerNamePrefix(config) + "-codevol-" + fn.getFunctionName() + "-" + shortId)
|
||||
.withEnv(java.util.List.of())
|
||||
.withEntrypoint(java.util.List.of("sleep"))
|
||||
.withCmd(java.util.List.of("3600"))
|
||||
@@ -907,9 +933,14 @@ public class ContainerLauncher {
|
||||
if (volumeNames.isEmpty() || !Files.isDirectory(markerDir)) {
|
||||
return;
|
||||
}
|
||||
// Markers are named after their volume, so the prune filter must track the configured
|
||||
// name prefix. Markers written under a previously configured prefix are left alone —
|
||||
// an orphaned marker file is harmless, and pruning only what this configuration could
|
||||
// have written can never delete a concurrent process's live markers.
|
||||
String markerPrefix = resolveContainerNamePrefix(config) + "-code-";
|
||||
try (java.util.stream.Stream<Path> markers = Files.list(markerDir)) {
|
||||
markers.filter(path -> Files.isRegularFile(path, java.nio.file.LinkOption.NOFOLLOW_LINKS))
|
||||
.filter(path -> path.getFileName().toString().startsWith(CODE_VOLUME_MARKER_PREFIX))
|
||||
.filter(path -> path.getFileName().toString().startsWith(markerPrefix))
|
||||
.filter(path -> !volumeNames.get().contains(path.getFileName().toString()))
|
||||
.forEach(path -> {
|
||||
try {
|
||||
@@ -966,6 +997,11 @@ public class ContainerLauncher {
|
||||
* Prefers the code SHA-256; falls back to last-modified when the SHA is unavailable.
|
||||
*/
|
||||
static String codeVolumeName(LambdaFunction fn) {
|
||||
return codeVolumeName(DEFAULT_NAME_PREFIX, fn);
|
||||
}
|
||||
|
||||
/** {@link #codeVolumeName(LambdaFunction)} with a configured base prefix in place of {@code floci}. */
|
||||
static String codeVolumeName(String namePrefix, LambdaFunction fn) {
|
||||
String key = fn.getCodeSha256();
|
||||
if (key == null || key.isBlank()) {
|
||||
key = Long.toString(fn.getLastModified());
|
||||
@@ -978,7 +1014,7 @@ public class ContainerLauncher {
|
||||
h = "0";
|
||||
}
|
||||
String fname = fn.getFunctionName().replaceAll("[^a-zA-Z0-9_.-]", "-");
|
||||
return "floci-code-" + fname + "-" + h;
|
||||
return namePrefix + "-code-" + fname + "-" + h;
|
||||
}
|
||||
|
||||
static String efsVolumeName(String accessPointArn) {
|
||||
|
||||
Reference in new issue
Block a user