diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 613ef99f6f..9572f85d29 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -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 diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrap.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrap.java index dfa562d681..d9d42967a7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrap.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrap.java @@ -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 diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodSpecFactory.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodSpecFactory.java index a447ba8fd8..9346f39fd9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodSpecFactory.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodSpecFactory.java @@ -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(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/BootstrapProcess.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/BootstrapProcess.java new file mode 100644 index 0000000000..49dc321e10 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/BootstrapProcess.java @@ -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) { + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrapTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrapTest.java index 48577a0091..066566a193 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrapTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ProvidedRuntimeBootstrapTest.java @@ -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 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)); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodBootstrapTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodBootstrapTest.java new file mode 100644 index 0000000000..8d3b4daf5f --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodBootstrapTest.java @@ -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 files) throws Exception { + Path archive = root.resolve(name + ".zip"); + try (ZipOutputStream output = new ZipOutputStream(Files.newOutputStream(archive))) { + for (Map.Entry 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 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 command) throws Exception { + ProcessBuilder builder = new ProcessBuilder(new ArrayList<>(command)); + builder.environment().put("PATH", bin + ":" + builder.environment().get("PATH")); + return BootstrapProcess.run(builder); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodSpecFactoryTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodSpecFactoryTest.java index cb7a3719ae..19bf3167c9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodSpecFactoryTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/kubernetes/LambdaPodSpecFactoryTest.java @@ -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"); }