fix(lambda): preserve Kubernetes bootstrap paths (#6585)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-27 15:47:30 -04:00
1 parent 24d989cb07
commit 196ae07aa8
7 files changed
+202 -34

No files matched your search

+5 -2
View File
@@ -1319,10 +1319,13 @@ of `{}` or with an empty `Filters` array clears any existing filters.
Any runtime that has an official AWS Lambda container image works with Floci (e.g. `nodejs22.x`, `python3.13`, `java21`, `go1.x`, `provided.al2023`).
With the Docker executor, ZIP-based `provided.*` runtimes execute the package's
With the Docker and Kubernetes executors, ZIP-based `provided.*` runtimes execute the package's
`/var/task/bootstrap`, falling back to `/opt/bootstrap` only when the package has
no bootstrap. The supplied executable stays in place, so scripts that locate
companion files relative to their own path continue to work.
companion files relative to their own path continue to work. The Kubernetes init
container restores executable permission on package and layer bootstrap files
after extraction and installs the shared delegate in `/var/runtime`. Managed
runtimes keep their image-provided `/var/runtime` unchanged.
By default, Floci builds each runtime image reference from `floci.services.lambda.ecr-base-uri`
and the runtime tag. To pin one runtime to a full image reference, set
@@ -5,9 +5,9 @@ package io.github.hectorvent.floci.services.lambda.launcher;
* its package or layer directory. Moving the supplied file changes its {@code $0}
* and breaks runtimes that locate companion files relative to that path.
*/
final class ProvidedRuntimeBootstrap {
public final class ProvidedRuntimeBootstrap {
static final String SCRIPT = """
public static final String SCRIPT = """
#!/bin/sh
set -eu
if [ -e /var/task/bootstrap ]; then
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.config.ContainerCaBundle;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.lambda.launcher.ProvidedRuntimeBootstrap;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -89,7 +90,7 @@ public class LambdaPodSpecFactory {
}
// The provided.* base images exec /var/runtime/bootstrap with no /var/task
// fallback, and their /var/runtime ships empty, so masking it with an emptyDir
// that the init container copies bootstrap into is safe and required.
// that holds the bootstrap delegate is safe and required.
boolean needsRuntimeDir = providedRuntime && hasCode;
if (needsRuntimeDir) {
volumes.add(emptyDirVolume(nodes, "runtime"));
@@ -195,12 +196,16 @@ public class LambdaPodSpecFactory {
}
script.append("rm -f /tmp/code.zip /tmp/layer*.zip\n");
if (providedRuntime) {
// The provided.* entrypoint execs /var/runtime/bootstrap, mirroring the
// docker path's tar-copy into RUNTIME_DIR. busybox unzip drops unix mode
// bits, so the exec bit must be restored explicitly.
script.append("if [ -f ").append(TASK_DIR).append("/bootstrap ]; then cp ")
.append(TASK_DIR).append("/bootstrap ").append(RUNTIME_DIR)
.append("/bootstrap && chmod +x ").append(RUNTIME_DIR).append("/bootstrap; fi\n");
// busybox unzip drops Unix mode bits. Restore each supplied bootstrap
// in place so the shared delegate preserves its companion-file path.
for (String directory : List.of(TASK_DIR, OPT_DIR)) {
script.append("if [ -f ").append(directory).append("/bootstrap ]; then chmod +x ")
.append(directory).append("/bootstrap; fi\n");
}
script.append("cat > ").append(RUNTIME_DIR).append("/bootstrap <<'FLOCI_BOOTSTRAP'\n")
.append(ProvidedRuntimeBootstrap.SCRIPT)
.append("FLOCI_BOOTSTRAP\n")
.append("chmod +x ").append(RUNTIME_DIR).append("/bootstrap\n");
}
return script.toString();
}
@@ -0,0 +1,29 @@
package io.github.hectorvent.floci.services.lambda.launcher;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertTrue;
public final class BootstrapProcess {
private BootstrapProcess() {
}
public static Result run(ProcessBuilder builder) throws Exception {
Process process = builder.redirectErrorStream(true).start();
try {
assertTrue(process.waitFor(5, TimeUnit.SECONDS), "bootstrap process must terminate");
return new Result(process.exitValue(),
new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
} finally {
if (process.isAlive()) {
process.destroyForcibly();
assertTrue(process.waitFor(5, TimeUnit.SECONDS), "bootstrap process must be reaped");
}
}
}
public record Result(int exit, String output) {
}
}
@@ -1,15 +1,14 @@
package io.github.hectorvent.floci.services.lambda.launcher;
import io.github.hectorvent.floci.services.lambda.launcher.BootstrapProcess.Result;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -95,19 +94,6 @@ class ProvidedRuntimeBootstrapTest {
private Result run(String... arguments) throws Exception {
List<String> command = new ArrayList<>(List.of("/bin/sh", entrypoint.toString()));
command.addAll(List.of(arguments));
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
try {
assertTrue(process.waitFor(5, TimeUnit.SECONDS), "bootstrap must terminate");
return new Result(process.exitValue(),
new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
} finally {
if (process.isAlive()) {
process.destroyForcibly();
assertTrue(process.waitFor(5, TimeUnit.SECONDS), "test process must be reaped");
}
}
}
private record Result(int exit, String output) {
return BootstrapProcess.run(new ProcessBuilder(command));
}
}
@@ -0,0 +1,142 @@
package io.github.hectorvent.floci.services.lambda.launcher.kubernetes;
import io.github.hectorvent.floci.services.lambda.launcher.BootstrapProcess;
import io.github.hectorvent.floci.services.lambda.launcher.BootstrapProcess.Result;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class LambdaPodBootstrapTest {
@TempDir
Path root;
private Path task;
private Path layer;
private Path runtime;
private Path bin;
private final LambdaPodSpecFactory factory = new LambdaPodSpecFactory(null);
@BeforeEach
void setUp() throws Exception {
task = Files.createDirectory(root.resolve("task"));
layer = Files.createDirectory(root.resolve("opt"));
runtime = Files.createDirectory(root.resolve("runtime"));
bin = Files.createDirectory(root.resolve("bin"));
Files.createDirectory(root.resolve("tmp"));
// Replace only transport: generated download commands copy local zip fixtures.
// Real unzip, chmod, shell redirection and the installed delegate execute below.
Path wget = bin.resolve("wget");
Files.writeString(wget, "#!/bin/sh\n[ \"$1\" = -q ] && [ \"$2\" = -O ] || exit 99\nexec cp \"$4\" \"$3\"\n");
assertTrue(wget.toFile().setExecutable(true));
}
@Test
void taskBootstrapKeepsCompanionPathArgumentsAndPrecedence() throws Exception {
Path code = zip("code", Map.of("bootstrap", companionScript(), "value", "task"));
Path overlay = zip("layer", Map.of("bootstrap", "#!/bin/sh\nexit 93\n"));
assertEquals(0, init(code, List.of(overlay), true).exit());
Result result = run(List.of("/bin/sh", runtime.resolve("bootstrap").toString(), "two words", "tail"));
assertEquals(0, result.exit(), result.output());
assertEquals("task:two words:tail", result.output());
assertTrue(Files.isExecutable(task.resolve("bootstrap")));
assertTrue(Files.isExecutable(layer.resolve("bootstrap")));
assertTrue(Files.isExecutable(runtime.resolve("bootstrap")));
assertEquals(companionScript(), Files.readString(task.resolve("bootstrap")));
}
@Test
void layerOnlyBootstrapKeepsCompanionPathAndRestoresMode() throws Exception {
Path code = zip("code", Map.of("handler", "unused"));
Path overlay = zip("layer", Map.of("bootstrap", companionScript(), "value", "layer"));
assertEquals(0, init(code, List.of(overlay), true).exit());
Result result = run(List.of("/bin/sh", runtime.resolve("bootstrap").toString(), "two words", "tail"));
assertEquals(0, result.exit(), result.output());
assertEquals("layer:two words:tail", result.output());
assertTrue(Files.isExecutable(layer.resolve("bootstrap")));
}
@Test
void missingBootstrapUsesSharedErrorAndExitStatus() throws Exception {
assertEquals(0, init(zip("code", Map.of("handler", "unused")), List.of(), true).exit());
Result result = run(List.of(runtime.resolve("bootstrap").toString()));
assertEquals(127, result.exit());
assertEquals("no bootstrap found at " + task + "/bootstrap or " + layer + "/bootstrap\n", result.output());
}
@Test
void failingTaskDoesNotFallBackToLayer() throws Exception {
Path code = zip("code", Map.of("bootstrap", "#!/bin/sh\nprintf task-failed\nexit 42\n"));
Path overlay = zip("layer", Map.of("bootstrap", "#!/bin/sh\nprintf unexpected-layer\n"));
assertEquals(0, init(code, List.of(overlay), true).exit());
Result result = run(List.of(runtime.resolve("bootstrap").toString()));
assertEquals(42, result.exit());
assertEquals("task-failed", result.output());
}
@Test
void failedDownloadDoesNotInstallDelegate() throws Exception {
assertTrue(init(root.resolve("absent.zip"), List.of(), true).exit() != 0);
assertFalse(Files.exists(runtime.resolve("bootstrap")));
}
@Test
void failedExtractionDoesNotInstallDelegate() throws Exception {
Path invalid = Files.writeString(root.resolve("invalid.zip"), "not a zip");
assertTrue(init(invalid, List.of(), true).exit() != 0);
assertFalse(Files.exists(runtime.resolve("bootstrap")));
}
@Test
void managedRuntimeDoesNotInstallDelegateOrChangeBootstrapMode() throws Exception {
Path code = zip("code", Map.of("bootstrap", companionScript()));
assertEquals(0, init(code, List.of(), false).exit());
assertFalse(Files.exists(runtime.resolve("bootstrap")));
assertFalse(Files.isExecutable(task.resolve("bootstrap")));
}
private String companionScript() {
return "#!/bin/sh\ncat \"${0%/*}/value\"\nprintf ':%s:%s' \"$1\" \"$2\"\n";
}
private Path zip(String name, Map<String, String> files) throws Exception {
Path archive = root.resolve(name + ".zip");
try (ZipOutputStream output = new ZipOutputStream(Files.newOutputStream(archive))) {
for (Map.Entry<String, String> entry : files.entrySet()) {
output.putNextEntry(new ZipEntry(entry.getKey()));
output.write(entry.getValue().getBytes(StandardCharsets.UTF_8));
output.closeEntry();
}
}
return archive;
}
private Result init(Path code, List<Path> layers, boolean provided) throws Exception {
String script = factory.initScript(code.toString(), layers.stream().map(Path::toString).toList(), provided);
// Rebase container directories without requiring root, a cluster or a chroot.
script = script.replace("/var/task", task.toString()).replace("/var/runtime", runtime.toString())
.replace("/opt", layer.toString())
.replace("/tmp/code.zip", root.resolve("tmp/code.zip").toString())
.replace("/tmp/layer", root.resolve("tmp/layer").toString());
Path init = Files.writeString(root.resolve("init.sh"), script);
return run(List.of("/bin/sh", init.toString()));
}
private Result run(List<String> command) throws Exception {
ProcessBuilder builder = new ProcessBuilder(new ArrayList<>(command));
builder.environment().put("PATH", bin + ":" + builder.environment().get("PATH"));
return BootstrapProcess.run(builder);
}
}
@@ -171,26 +171,29 @@ class LambdaPodSpecFactoryTest {
}
@Test
void providedRuntimeCopiesBootstrapIntoVarRuntime() {
void providedRuntimeInstallsDelegateIntoVarRuntime() {
// The provided.* entrypoint execs /var/runtime/bootstrap with no /var/task
// fallback, so the init container must copy it there and restore the exec bit.
// fallback, so the init container installs a delegate and restores exec bits.
ObjectNode pod = factory.buildPod("floci-lambda-custom-abc12345", "custom",
"public.ecr.aws/lambda/provided:al2023",
List.of(),
"http://10.0.0.5:4566/awslambda-us-east-1-tasks/snapshots/000000000000/custom",
List.of(), true, "bootstrap", null, 128, Optional.empty());
List.of("http://10.0.0.5:4566/b/layers/1"), true, "bootstrap", null, 128, Optional.empty());
JsonNode spec = spec(pod);
assertThat(elements(spec.path("volumes"))).extracting(n -> n.path("name").asText()).contains("runtime");
assertThat(elements(elements(spec.path("containers")).getFirst().path("volumeMounts")))
.extracting(n -> n.path("name").asText(), n -> n.path("mountPath").asText())
.contains(tuple("runtime", "/var/runtime"));
.containsExactly(tuple("task", "/var/task"), tuple("opt", "/opt"), tuple("runtime", "/var/runtime"));
JsonNode init = elements(spec.path("initContainers")).getFirst();
assertThat(elements(init.path("volumeMounts")))
.extracting(n -> n.path("name").asText())
.contains("runtime");
.extracting(n -> n.path("name").asText(), n -> n.path("mountPath").asText())
.containsExactly(tuple("task", "/var/task"), tuple("opt", "/opt"), tuple("runtime", "/var/runtime"));
assertThat(texts(init.path("command")).get(2))
.contains("cp /var/task/bootstrap /var/runtime/bootstrap")
.contains("exec /var/task/bootstrap \"$@\"")
.contains("exec /opt/bootstrap \"$@\"")
.contains("chmod +x /var/task/bootstrap")
.contains("chmod +x /opt/bootstrap")
.contains("chmod +x /var/runtime/bootstrap");
}