feat(lambda): support kubernetes as the lambda runner (#1941)

* feat(lambda): support kubernetes as the lambda runner

* fix(lambda): keep the port reserved when a failed launch cannot confirm pod deletion

* fix(lambda): carry the owning account in the layer Content.Location URL

The URL is fetched unsigned. Without an X-Amz-Credential query the
account filter falls back to the default account, so a layer published
under another account resolves the wrong bucket namespace and 404s.
Mirror the credential query the kubernetes executor already appends to
its download URLs.

* chore(build): raise the surefire heap to 6g

The suite outgrew the 4g default heap of 16g CI runners. At OOM a full
GC reclaims almost nothing, so the live set is real. The heap dump
shows 192 surviving QuarkusClassLoader instances, each pinned test app
keeping its own copy of the Jandex index. This PR widens the classpath
with the kubernetes client, which made each pinned copy heavier and
tipped CI over the edge. Verified locally that the full suite passes
with 6g and fails with 4g. Unpinning the leaked classloaders remains
follow-up work on main.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-08-20 12:52:32 -04:00
1 parent a732e26bc5
commit bc13801121
25 files changed
+2557 -37

No files matched your search

@@ -1577,6 +1577,55 @@ public interface EmulatorConfig {
*/
Optional<String> awsConfigPath();
/**
* Execution backend for Lambda environments: {@code docker} (default) runs each
* environment as a Docker container, {@code kubernetes} runs it as a pod in the
* cluster Floci is configured against (in-cluster config or local kubeconfig).
*
* Env var: FLOCI_SERVICES_LAMBDA_EXECUTOR
*/
@WithDefault("docker")
String executor();
KubernetesExecutor kubernetes();
interface KubernetesExecutor {
/**
* Namespace Lambda pods are created in. Multiple Floci instances must use
* separate namespaces — orphaned pods are swept by label on startup.
*
* Env var: FLOCI_SERVICES_LAMBDA_KUBERNETES_NAMESPACE
*/
@WithDefault("default")
String namespace();
/**
* Extra labels applied to Lambda pods, as {@code key=value} entries.
*
* Env var: FLOCI_SERVICES_LAMBDA_KUBERNETES_LABELS (comma-separated)
*/
Optional<List<String>> labels();
/**
* Host or IP that Lambda pods use to reach Floci (the Runtime API port range
* and the main port). When unset, Floci auto-detects its own pod address if
* running in-cluster; when running outside the cluster this must be set to an
* address the cluster's pods can reach.
*
* Env var: FLOCI_SERVICES_LAMBDA_KUBERNETES_FLOCI_ADDRESS
*/
Optional<String> flociAddress();
/**
* Image for the init container that downloads and unpacks function code into
* the pod. Must provide sh, wget and unzip.
*
* Env var: FLOCI_SERVICES_LAMBDA_KUBERNETES_INIT_IMAGE
*/
@WithDefault("busybox:1.36")
String initImage();
}
HotReload hotReload();
interface HotReload {
@@ -47,7 +47,15 @@ public class LaunchedContainerAwsEnv {
* credentials from there. Empty = inject host or placeholder credentials.
*/
public List<String> sdkBaselineEnv(String region, Optional<String> awsConfigMountDir) {
return sdkBaselineEnv(region, awsConfigMountDir, Optional.empty());
return sdkBaselineEnv(region, awsConfigMountDir, reachableEndpoint.baseUrl(), Optional.empty());
}
/**
* Variant for launchers whose workloads reach Floci at an address other than the
* Docker-reachable one (e.g. Kubernetes pods reaching Floci's pod IP).
*/
public List<String> sdkBaselineEnv(String region, Optional<String> awsConfigMountDir, String flociEndpoint) {
return sdkBaselineEnv(region, awsConfigMountDir, flociEndpoint, Optional.empty());
}
/**
@@ -56,25 +64,34 @@ public class LaunchedContainerAwsEnv {
*/
public List<String> sdkBaselineEnv(String region, Optional<String> awsConfigMountDir,
Optional<SessionCreds> injectedCredentials) {
List<String> env = new ArrayList<>();
return sdkBaselineEnv(region, awsConfigMountDir, reachableEndpoint.baseUrl(), injectedCredentials);
}
/**
* Full variant taking both the Floci endpoint the workload should target and optional
* workload-specific session credentials.
*/
public List<String> sdkBaselineEnv(String region, Optional<String> awsConfigMountDir,
String flociEndpoint, Optional<SessionCreds> injectedCredentials) {
var env = new ArrayList<String>();
env.add("AWS_DEFAULT_REGION=" + region);
env.add("AWS_REGION=" + region);
if (awsConfigMountDir.isPresent() && !awsConfigMountDir.get().isBlank()) {
// ~/.aws is mounted, so don't inject credentials. Let the SDK discover them.
// Set explicit file paths so discovery works regardless of container HOME.
String dir = awsConfigMountDir.get();
var dir = awsConfigMountDir.get();
env.add("AWS_SHARED_CREDENTIALS_FILE=" + dir + "/credentials");
env.add("AWS_CONFIG_FILE=" + dir + "/config");
} else if (injectedCredentials.isPresent()) {
SessionCreds credentials = injectedCredentials.get();
var credentials = injectedCredentials.get();
env.add("AWS_ACCESS_KEY_ID=" + credentials.accessKeyId());
env.add("AWS_SECRET_ACCESS_KEY=" + credentials.secretAccessKey());
env.add("AWS_SESSION_TOKEN=" + credentials.sessionToken());
} else {
// Use Floci's own env vars, falling back to placeholder credentials.
String ak = System.getenv("AWS_ACCESS_KEY_ID");
String sk = System.getenv("AWS_SECRET_ACCESS_KEY");
String st = System.getenv("AWS_SESSION_TOKEN");
var ak = System.getenv("AWS_ACCESS_KEY_ID");
var sk = System.getenv("AWS_SECRET_ACCESS_KEY");
var st = System.getenv("AWS_SESSION_TOKEN");
if ((ak != null || sk != null || st != null) && hostCredentialsWarned.compareAndSet(false, true)) {
// The container runs workload code, so surface that it is being handed the
// credentials Floci itself was started with (an exported key pair, aws-vault, a
@@ -89,7 +106,6 @@ public class LaunchedContainerAwsEnv {
env.add("AWS_SECRET_ACCESS_KEY=" + (sk != null ? sk : "test"));
env.add("AWS_SESSION_TOKEN=" + (st != null ? st : "test"));
}
String flociEndpoint = reachableEndpoint.baseUrl();
env.add("FLOCI_HOSTNAME=" + URI.create(flociEndpoint).getHost());
env.add("FLOCI_ENDPOINT=" + flociEndpoint);
env.add("AWS_ENDPOINT_URL=" + flociEndpoint);
@@ -3,6 +3,7 @@ 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.AwsArnUtils;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
import jakarta.inject.Inject;
@@ -17,6 +18,7 @@ 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 jakarta.ws.rs.core.UriInfo;
import java.util.List;
import java.util.Map;
@@ -52,10 +54,11 @@ public class LambdaLayerController {
@Path("/layers/{layerName}/versions")
public Response publishLayerVersion(@PathParam("layerName") String layerName,
@Context HttpHeaders headers,
@Context UriInfo uriInfo,
Map<String, Object> request) {
String region = regionResolver.resolveRegion(headers);
LambdaLayerVersion lv = layerService.publishLayerVersion(region, layerName, request);
return Response.status(201).entity(buildLayerVersionResponse(lv)).build();
return Response.status(201).entity(buildLayerVersionResponse(lv, region, uriInfo)).build();
}
@GET
@@ -76,10 +79,11 @@ public class LambdaLayerController {
@Path("/layers/{layerName}/versions/{versionNumber}")
public Response getLayerVersion(@PathParam("layerName") String layerName,
@PathParam("versionNumber") long versionNumber,
@Context HttpHeaders headers) {
@Context HttpHeaders headers,
@Context UriInfo uriInfo) {
String region = regionResolver.resolveRegion(headers);
LambdaLayerVersion lv = layerService.getLayerVersion(region, layerName, versionNumber);
return Response.ok(buildLayerVersionResponse(lv)).build();
return Response.ok(buildLayerVersionResponse(lv, region, uriInfo)).build();
}
@DELETE
@@ -122,7 +126,7 @@ public class LambdaLayerController {
return Response.ok(root).build();
}
private ObjectNode buildLayerVersionResponse(LambdaLayerVersion lv) {
private ObjectNode buildLayerVersionResponse(LambdaLayerVersion lv, String region, UriInfo uriInfo) {
ObjectNode node = objectMapper.createObjectNode();
node.put("LayerArn", lv.getLayerArn());
node.put("LayerVersionArn", lv.getLayerVersionArn());
@@ -135,8 +139,10 @@ public class LambdaLayerController {
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", "");
// Only advertise a fetchable Location when the archive was actually stored; AWS
// clients treat Content.Location as a downloadable URL, so a placeholder empty
// string is correct for pre-upgrade or failed-store layers.
contentNode.put("Location", lv.isArchiveStored() ? tasksLocation(lv, region, uriInfo) : "");
if (lv.getCompatibleRuntimes() != null && !lv.getCompatibleRuntimes().isEmpty()) {
ArrayNode runtimes = node.putArray("CompatibleRuntimes");
@@ -149,6 +155,28 @@ public class LambdaLayerController {
return node;
}
/**
* Path-style URL to the archive in Floci's own S3 (a presigned URL in real AWS),
* built from the request so it targets the same endpoint the client is talking to.
* Segments are percent-encoded directly rather than through {@code UriBuilder.path},
* whose template syntax would throw on a layer name containing braces.
* Clients fetch this URL unsigned, so an {@code X-Amz-Credential} query steers
* Floci's account filter to the layer's owning account; without it a layer owned
* by a non-default account resolves the default-account bucket and 404s.
*/
private String tasksLocation(LambdaLayerVersion lv, String region, UriInfo uriInfo) {
var base = uriInfo.getBaseUri().toString();
if (base.endsWith("/")) {
base = base.substring(0, base.length() - 1);
}
var account = AwsArnUtils.accountOrDefault(lv.getLayerVersionArn(), "000000000000");
var bucket = LambdaService.tasksBucketName(region);
var key = LambdaService.layerObjectKey(account, lv.getLayerName(), lv.getVersion());
return base + "/" + LambdaService.encodeObjectPath(bucket)
+ "/" + LambdaService.encodeObjectPath(key)
+ "?X-Amz-Credential=" + account + "%2F00010101%2F" + region + "%2Fs3%2Faws4_request";
}
private ObjectNode buildLayerVersionSummary(LambdaLayerVersion lv) {
ObjectNode node = objectMapper.createObjectNode();
node.put("LayerVersionArn", lv.getLayerVersionArn());
@@ -112,11 +112,33 @@ public class LambdaLayerService {
layerVersion.setCodeSha256(codeSha256);
layerVersion.setCodeLocalPath(layerPath.toAbsolutePath().normalize().toString());
// Store the archive before persisting the version so a load-bearing store failure
// (kubernetes executor) fails the publish instead of leaving a version pods cannot use.
boolean stored = storeLayerArchive(region, accountId, layerName, nextVersion, zipBytes);
if (!stored && LambdaService.requiresStoredTasksObject(config)) {
throw new AwsException("ServiceException",
"Could not store the layer archive for '" + layerName + "' v" + nextVersion
+ " in Floci's S3, which the kubernetes Lambda executor needs. The "
+ "layer version was not published.", 500);
}
layerVersion.setArchiveStored(stored);
layerStore.save(region, layerVersion);
LOG.infov("Published layer version: {0} v{1} in region {2}", layerName, nextVersion, region);
return layerVersion;
}
/**
* Keeps the exact layer archive in Floci's S3 so GetLayerVersion can serve a real
* Content.Location and the kubernetes executor's init container can download it.
* Best-effort unless the active Lambda executor requires stored tasks-bucket objects.
*/
private boolean storeLayerArchive(String region, String accountId, String layerName,
long version, byte[] zipBytes) {
return LambdaService.putTasksObjectQuietly(s3Service, region,
LambdaService.layerObjectKey(accountId, layerName, version), zipBytes,
"layer archive for " + layerName + " v" + version);
}
/**
* Returns information about a specific layer version.
*/
@@ -157,9 +179,24 @@ public class LambdaLayerService {
}
layerStore.delete(region, layerName, versionNumber);
deleteLayerArchive(region, lv);
LOG.infov("Deleted layer version: {0} v{1} in region {2}", layerName, versionNumber, region);
}
private void deleteLayerArchive(String region, LambdaLayerVersion lv) {
if (s3Service == null) {
return;
}
try {
var account = AwsArnUtils.accountOrDefault(lv.getLayerVersionArn(), "000000000000");
s3Service.deleteObject(LambdaService.tasksBucketName(region),
LambdaService.layerObjectKey(account, lv.getLayerName(), lv.getVersion()));
} catch (Exception e) {
LOG.debugv("Could not delete stored layer archive for {0} v{1}: {2}",
lv.getLayerName(), lv.getVersion(), e.getMessage());
}
}
/**
* Resolves a layer version ARN to its local code path.
* Used by the container launcher to copy layer content into /opt.
@@ -1524,6 +1524,25 @@ public class LambdaService {
return "snapshots/" + account + "/" + fn.getFunctionName();
}
/** Stable, account-scoped S3 key for a published layer version's archive. */
public static String layerObjectKey(String accountId, String layerName, long version) {
var account = accountId != null ? accountId : "000000000000";
return "layers/" + account + "/" + layerName + "/" + version;
}
/** Percent-encodes each path segment of a bucket path or object key for use in a URL. */
public static String encodeObjectPath(String path) {
var encoded = new StringBuilder();
for (var segment : path.split("/", -1)) {
if (!encoded.isEmpty()) {
encoded.append('/');
}
encoded.append(java.net.URLEncoder.encode(segment, java.nio.charset.StandardCharsets.UTF_8)
.replace("+", "%20"));
}
return encoded.toString();
}
private void extractZipCode(LambdaFunction fn, String zipFileBase64, String region) {
extractZipCodeBytes(fn, Base64.getDecoder().decode(zipFileBase64), region);
}
@@ -1575,8 +1594,25 @@ public class LambdaService {
}
private void storeDeploymentPackage(LambdaFunction fn, byte[] zipBytes, String region) {
boolean stored = putTasksObjectQuietly(s3Service, region, codeObjectKey(fn), zipBytes,
"deployment package for " + fn.getFunctionName());
if (!stored && requiresStoredTasksObject(config)) {
throw new AwsException("ServiceException",
"Could not store the deployment package for '" + fn.getFunctionName()
+ "' in Floci's S3, which the kubernetes Lambda executor needs to "
+ "launch pods. The function was not deployed.", 500);
}
}
/**
* Best-effort put into the per-region tasks bucket, shared by function-code and
* layer-archive storage. Returns whether the object was stored so a caller whose
* executor depends on it can fail loudly instead of silently succeeding.
*/
static boolean putTasksObjectQuietly(S3Service s3Service, String region, String key,
byte[] zipBytes, String what) {
if (s3Service == null) {
return;
return false;
}
String bucket = tasksBucketName(region);
try {
@@ -1588,15 +1624,24 @@ public class LambdaService {
throw e;
}
}
s3Service.putObject(bucket, codeObjectKey(fn), zipBytes,
"application/zip", java.util.Map.of());
s3Service.putObject(bucket, key, zipBytes, "application/zip", java.util.Map.of());
return true;
} catch (Exception e) {
// Never fail a deploy because the convenience copy failed.
LOG.warnv("Could not store deployment package for {0}: {1}",
fn.getFunctionName(), e.getMessage());
LOG.warnv("Could not store {0}: {1}", what, e.getMessage());
return false;
}
}
/**
* Whether the tasks-bucket copy is load-bearing for the active executor. The
* kubernetes executor's pods download code and layers from it, so a store failure
* must fail the deploy rather than surface later as broken cold starts.
*/
static boolean requiresStoredTasksObject(EmulatorConfig config) {
return config != null
&& "kubernetes".equalsIgnoreCase(config.services().lambda().executor().trim());
}
private void extractZipCodeFromS3(LambdaFunction fn, String s3Bucket, String s3Key, String region) {
if (s3Service == null) {
throw new AwsException("ServiceUnavailableException", "S3 service not available", 503);
@@ -3,7 +3,7 @@ package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.services.lambda.launcher.ContainerHandle;
import io.github.hectorvent.floci.services.lambda.launcher.ContainerLauncher;
import io.github.hectorvent.floci.services.lambda.launcher.LambdaRuntimeLauncher;
import io.github.hectorvent.floci.services.lambda.model.ContainerState;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import jakarta.annotation.PostConstruct;
@@ -40,7 +40,7 @@ public class WarmPool implements ContainerTeardown {
private static final int DEFAULT_MAX_POOL_SIZE = Math.max(4, Runtime.getRuntime().availableProcessors());
private final ContainerLauncher containerLauncher;
private final LambdaRuntimeLauncher lambdaRuntimeLauncher;
private final EmulatorConfig config;
private final int maxPoolSizePerFunction;
private final ConcurrentHashMap<String, PoolState> poolStates = new ConcurrentHashMap<>();
@@ -57,15 +57,15 @@ public class WarmPool implements ContainerTeardown {
}
@Inject
public WarmPool(ContainerLauncher containerLauncher, EmulatorConfig config) {
this.containerLauncher = containerLauncher;
public WarmPool(LambdaRuntimeLauncher lambdaRuntimeLauncher, EmulatorConfig config) {
this.lambdaRuntimeLauncher = lambdaRuntimeLauncher;
this.config = config;
this.maxPoolSizePerFunction = DEFAULT_MAX_POOL_SIZE;
}
/** Package-private constructor for testing (empty pool, no containers to drain). */
WarmPool() {
this.containerLauncher = null;
this.lambdaRuntimeLauncher = null;
this.config = null;
this.maxPoolSizePerFunction = DEFAULT_MAX_POOL_SIZE;
}
@@ -144,7 +144,7 @@ public class WarmPool implements ContainerTeardown {
stopQuietly(candidate);
continue;
}
if (containerLauncher.isAlive(candidate)) {
if (lambdaRuntimeLauncher.isAlive(candidate)) {
handle = candidate;
break;
}
@@ -157,7 +157,7 @@ public class WarmPool implements ContainerTeardown {
if (handle == null) {
LOG.debugv(ephemeral ? "Ephemeral start for function: {0}" : "Cold start for function: {0}",
fn.getFunctionName());
handle = containerLauncher.launch(fn);
handle = lambdaRuntimeLauncher.launch(fn);
} else {
LOG.debugv("Reusing warm container for function: {0}", fn.getFunctionName());
}
@@ -367,7 +367,7 @@ public class WarmPool implements ContainerTeardown {
private void stopQuietly(ContainerHandle handle) {
try {
containerLauncher.stop(handle);
lambdaRuntimeLauncher.stop(handle);
} catch (Exception e) {
LOG.warnv("Error stopping container {0}: {1}", handle.getContainerId(), e.getMessage());
}
@@ -22,6 +22,7 @@ import com.github.dockerjava.api.exception.NotFoundException;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Typed;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -48,6 +49,7 @@ import java.util.ArrayList;
import java.util.HexFormat;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -62,7 +64,8 @@ import java.util.concurrent.atomic.AtomicReference;
* rather than a bind mount, so it works when Floci itself runs inside Docker.
*/
@ApplicationScoped
public class ContainerLauncher {
@Typed(ContainerLauncher.class)
public class ContainerLauncher implements LambdaRuntimeLauncher {
private static final Logger LOG = Logger.getLogger(ContainerLauncher.class);
private static final String TASK_DIR = "/var/task";
@@ -101,7 +104,8 @@ public class ContainerLauncher {
*/
private static final String FLOCI_CA_DIR = "/etc";
private static final String FLOCI_CA_FILE_NAME = "floci-ca.crt";
private static final String FLOCI_CA_CONTAINER_PATH = FLOCI_CA_DIR + "/" + FLOCI_CA_FILE_NAME;
/** Shared with the kubernetes executor, which mounts the CA ConfigMap at the same path. */
public static final String FLOCI_CA_CONTAINER_PATH = FLOCI_CA_DIR + "/" + FLOCI_CA_FILE_NAME;
/** Self-signed cert filename produced by {@code TlsConfigSource} under {persistent-path}/tls/. */
private static final String SELF_SIGNED_CERT_NAME = "floci-selfsigned.crt";
@@ -502,6 +506,16 @@ public class ContainerLauncher {
// null by then); otherwise the failure happened before Docker ever saw the volume, so
// its in-flight reference must be released here or cleanup would wait on it forever.
releaseCodeVolumeReference(reservedCodeVolume);
// Stop the server before releasing its port, or the still-listening Vert.x server
// makes a later cold start that reuses the port serve this runtime too.
try {
runtimeApiServer.stop().get(5, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
} catch (Exception stopFailure) {
LOG.debugv("Runtime API server stop failed during launch cleanup: {0}",
stopFailure.getMessage());
}
try {
runtimeApiServerFactory.release(runtimeApiServer);
} catch (Exception cleanupError) {
@@ -547,7 +561,9 @@ public class ContainerLauncher {
server.close().get(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException | TimeoutException e) {
} catch (ExecutionException | TimeoutException | CancellationException e) {
// CancellationException is unchecked; catch it so a cancelled close future
// is logged here instead of propagating out of stop().
LOG.warnv(e, "RuntimeApiServer did not close cleanly for container {0}",
handle.getContainerId());
} finally {
@@ -1307,8 +1323,8 @@ public class ContainerLauncher {
* {@code floci.tls.cert-path} that points at a leaf/server certificate is accepted but only pins
* that exact certificate (it cannot validate a chain it signs), so a warning is logged.
*/
static Optional<Path> resolveFlociCaCertPath(boolean tlsEnabled, Optional<String> userCertPath,
String persistentPath) {
public static Optional<Path> resolveFlociCaCertPath(boolean tlsEnabled, Optional<String> userCertPath,
String persistentPath) {
if (!tlsEnabled) {
return Optional.empty();
}
@@ -1364,7 +1380,7 @@ public class ContainerLauncher {
* which breaks every external HTTPS call (curl, openssl, requests/botocore) the Lambda makes.
* Returns an empty list when no CA cert is available (TLS off).
*/
static List<String> flociCaEnv(Optional<Path> caCert) {
public static List<String> flociCaEnv(Optional<Path> caCert) {
if (caCert.isEmpty()) {
return List.of();
}
@@ -0,0 +1,18 @@
package io.github.hectorvent.floci.services.lambda.launcher;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
/**
* Starts and stops Lambda execution environments. Implementations back each
* environment with a concrete runtime: a Docker container ({@link ContainerLauncher})
* or a Kubernetes pod ({@code KubernetesPodLauncher}). Selected at startup by
* {@code floci.services.lambda.executor}.
*/
public interface LambdaRuntimeLauncher {
ContainerHandle launch(LambdaFunction fn);
void stop(ContainerHandle handle);
boolean isAlive(ContainerHandle handle);
}
@@ -0,0 +1,63 @@
package io.github.hectorvent.floci.services.lambda.launcher;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.lambda.launcher.kubernetes.KubernetesPodLauncher;
import io.quarkus.runtime.StartupEvent;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.Produces;
import org.jboss.logging.Logger;
import java.util.Locale;
/**
* Selects the Lambda execution backend at startup. {@code Instance<>} lookups keep
* the unselected launcher (and its Docker/Kubernetes client) uninstantiated, so a
* Docker-less cluster deployment never touches docker.sock and vice versa.
*/
@ApplicationScoped
public class LambdaRuntimeLauncherProducer {
private static final Logger LOG = Logger.getLogger(LambdaRuntimeLauncherProducer.class);
/**
* The producer itself is lazy, so a bad value would otherwise surface only on the
* first invocation. This makes a misconfigured executor fail startup instead.
*
* <p>For the kubernetes executor the client is also initialized here, on the
* startup thread. Left to the first cold start, the Fabric8 client's Vert.x
* HTTP client would be created on a request's Vert.x context and closed with
* the HTTP server verticles at shutdown, so the WarmPool drain could no longer
* delete pods ("Client is closed").
*/
void validateExecutor(@Observes StartupEvent event, EmulatorConfig config,
Instance<KubernetesPodLauncher> kubernetes) {
if (requireValidExecutor(config).equals("kubernetes")) {
kubernetes.get().initializeClient();
}
}
@Produces
@ApplicationScoped
LambdaRuntimeLauncher launcher(EmulatorConfig config,
Instance<ContainerLauncher> docker,
Instance<KubernetesPodLauncher> kubernetes) {
var executor = requireValidExecutor(config);
if (executor.equals("kubernetes")) {
LOG.info("Lambda executor: kubernetes (environments run as pods)");
return kubernetes.get();
}
return docker.get();
}
private static String requireValidExecutor(EmulatorConfig config) {
var executor = config.services().lambda().executor().trim().toLowerCase(Locale.ROOT);
if (!executor.equals("docker") && !executor.equals("kubernetes")) {
throw new IllegalArgumentException(
"Unknown floci.services.lambda.executor '" + executor
+ "'. Valid values: docker, kubernetes");
}
return executor;
}
}
@@ -0,0 +1,148 @@
package io.github.hectorvent.floci.services.lambda.launcher.kubernetes;
import io.github.hectorvent.floci.config.EmulatorConfig;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* Resolves the address Lambda pods use to reach the Floci process: the Runtime API
* port range (runtime-api-base-port..max-port) and the main emulator port. Pods talk
* straight to this address; there is no Docker-style host gateway or embedded DNS,
* so a wrong value surfaces only as {@code Function.TimedOut}. That is why an
* unresolvable address fails fast instead of guessing.
*/
@ApplicationScoped
public class KubernetesFlociAddressResolver {
private static final Logger LOG = Logger.getLogger(KubernetesFlociAddressResolver.class);
private static final Path SERVICE_ACCOUNT_TOKEN =
Path.of("/var/run/secrets/kubernetes.io/serviceaccount/token");
private final EmulatorConfig config;
private String cachedAddress;
@Inject
public KubernetesFlociAddressResolver(EmulatorConfig config) {
this.config = config;
}
/** Host or IP only, e.g. {@code 10.42.0.7}. */
public synchronized String resolve() {
if (cachedAddress == null) {
cachedAddress = doResolve();
// Ports go in as strings: MessageFormat would render 9200 as "9,200".
LOG.infov("Lambda pods will reach Floci at {0} (runtime API ports {1}-{2}, main port {3})",
cachedAddress,
Integer.toString(config.services().lambda().runtimeApiBasePort()),
Integer.toString(config.services().lambda().runtimeApiMaxPort()),
Integer.toString(mainPort()));
}
return cachedAddress;
}
/**
* Base URL for FLOCI_ENDPOINT / AWS_ENDPOINT_URL inside the pod. Built from
* {@link #resolve()} rather than {@code ContainerReachableEndpoint}, which is
* Docker/embedded-DNS specific.
*/
public String flociBaseUrl() {
var scheme = config.tls().enabled() ? "https" : "http";
return scheme + "://" + resolve() + ":" + mainPort();
}
/**
* Base URL for init-container code and layer downloads: always plain HTTP.
* busybox wget's built-in TLS cannot complete a handshake with Floci's TLS
* stack, and Floci's TLS proxy serves both protocols on the emulator port.
* The runtime API traffic on this same pod network is plain HTTP too, so
* TLS here would add no trust the executor doesn't already assume.
*/
public String downloadBaseUrl() {
return "http://" + resolve() + ":" + mainPort();
}
private int mainPort() {
// The port Floci actually listens on. base-url may describe a proxied
// client-facing endpoint; pods talk to this process directly.
return config.port();
}
private String doResolve() {
var override = config.services().lambda().kubernetes().flociAddress()
.filter(s -> !s.isBlank());
if (override.isPresent()) {
var address = override.get().trim();
// Reachability can only be judged from the pods' network, not from here,
// but anything beyond hostname/IP characters is always a mistake worth
// failing on: the value is embedded in URLs and in single-quoted
// init-container shell commands.
if (!address.matches("[A-Za-z0-9.-]+")) {
throw new IllegalStateException(
"floci.services.lambda.kubernetes.floci-address must be a bare host or IP "
+ "(no scheme, port or path), got '" + address + "'. Ports are "
+ "derived from floci.port and the runtime-api port range.");
}
return address;
}
if (!isRunningInCluster()) {
throw new IllegalStateException(
"The kubernetes Lambda executor needs an address that pods can use to reach "
+ "Floci, and Floci is not running inside the cluster. Set "
+ "FLOCI_SERVICES_LAMBDA_KUBERNETES_FLOCI_ADDRESS "
+ "(floci.services.lambda.kubernetes.floci-address) to a host/IP "
+ "reachable from the cluster's pods, e.g. this machine's LAN IP.");
}
return ownPodAddress();
}
private boolean isRunningInCluster() {
return System.getenv("KUBERNETES_SERVICE_HOST") != null
|| Files.exists(SERVICE_ACCOUNT_TOKEN);
}
/**
* Inside a pod the hostname resolves to the pod IP, so getLocalHost is authoritative.
* The interface scan is a fallback for images with an unresolvable hostname.
*/
private String ownPodAddress() {
// IPv4 only: an IPv6 literal would need bracketing in every URL this address
// feeds, and busybox wget cannot fetch from unbracketed IPv6 hosts.
try {
var local = InetAddress.getLocalHost();
if (local instanceof Inet4Address && !local.isLoopbackAddress()) {
return local.getHostAddress();
}
} catch (Exception e) {
LOG.debugv("getLocalHost failed, scanning interfaces: {0}", e.getMessage());
}
try {
var interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
var nic = interfaces.nextElement();
if (!nic.isUp() || nic.isLoopback()) {
continue;
}
var addresses = nic.getInetAddresses();
while (addresses.hasMoreElements()) {
var address = addresses.nextElement();
if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
return address.getHostAddress();
}
}
}
} catch (Exception e) {
LOG.warnv("Interface scan for pod IP failed: {0}", e.getMessage());
}
throw new IllegalStateException(
"Could not determine an IPv4 address of this pod for Lambda pods to connect "
+ "back to (IPv6-only clusters are not supported yet). Set "
+ "FLOCI_SERVICES_LAMBDA_KUBERNETES_FLOCI_ADDRESS explicitly.");
}
}
@@ -0,0 +1,578 @@
package io.github.hectorvent.floci.services.lambda.launcher.kubernetes;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ContainerStatus;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.NonDeletingOperation;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
import io.github.hectorvent.floci.services.lambda.LambdaLayerService;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.launcher.ContainerHandle;
import io.github.hectorvent.floci.services.lambda.launcher.ContainerLauncher;
import io.github.hectorvent.floci.services.lambda.launcher.ImageResolver;
import io.github.hectorvent.floci.services.lambda.launcher.LambdaRuntimeLauncher;
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.runtime.RuntimeApiServerFactory;
import io.github.hectorvent.floci.services.s3.S3Service;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Typed;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Runs each Lambda execution environment as a Kubernetes pod. Mirrors
* {@link ContainerLauncher}'s contract: the Runtime API server is started before the
* pod so the runtime connects on boot, and any launch failure after the server is
* allocated releases its port and reaps the half-built pod.
*
* <p>Pods reach Floci over the network only (no bind mounts, no host gateway):
* an init container downloads the deployment package from Floci's S3, and the
* runtime polls the Runtime API at the address {@link KubernetesFlociAddressResolver}
* advertises. Hot reload is therefore unsupported here.
*/
@ApplicationScoped
@Typed(KubernetesPodLauncher.class)
public class KubernetesPodLauncher implements LambdaRuntimeLauncher {
private static final Logger LOG = Logger.getLogger(KubernetesPodLauncher.class);
static final String CA_CONFIG_MAP_NAME = "floci-lambda-ca";
static final String CA_CONFIG_MAP_KEY = "ca.crt";
/**
* Generous enough for a node's first pull of a multi-hundred-MB runtime image;
* a pod that will never run (bad image, failing init) is detected much earlier
* via its terminal waiting/terminated states.
*/
private static final int POD_STARTUP_TIMEOUT_SECONDS = 300;
/**
* How long a stop waits for a deleted pod to actually disappear before giving up
* and keeping its Runtime API port reserved. A force delete normally clears within
* a second; the cap only bites when the API server or kubelet is stuck.
*/
private static final int POD_DELETE_TIMEOUT_SECONDS = 15;
/**
* Waiting-state reasons treated as fatal for the cold start. ErrImagePull is
* deliberately absent: it appears on the first failed pull attempt, which the
* kubelet retries; only the backoff state marks repeated failures.
*/
private static final Set<String> TERMINAL_WAITING_REASONS = Set.of(
"ImagePullBackOff", "InvalidImageName", "CrashLoopBackOff",
"CreateContainerError", "CreateContainerConfigError", "RunContainerError");
private final KubernetesClient client;
private final EmulatorConfig config;
private final RuntimeApiServerFactory runtimeApiServerFactory;
private final ImageResolver imageResolver;
private final KubernetesFlociAddressResolver addressResolver;
private final LaunchedContainerAwsEnv awsEnv;
private final LambdaLayerService layerService;
private final LambdaPodSpecFactory podSpecFactory;
private final KubernetesPodLogStreamer logStreamer;
private final S3Service s3Service;
private final Object orphanSweepLock = new Object();
private volatile boolean orphansSwept = false;
/** Names of pods created by this process, so a retried orphan sweep never deletes them. */
private final Set<String> ownPodNames = ConcurrentHashMap.newKeySet();
private final AtomicBoolean caConfigMapApplied = new AtomicBoolean(false);
private final AtomicBoolean awsConfigPathWarned = new AtomicBoolean(false);
@Inject
public KubernetesPodLauncher(KubernetesClient client,
EmulatorConfig config,
RuntimeApiServerFactory runtimeApiServerFactory,
ImageResolver imageResolver,
KubernetesFlociAddressResolver addressResolver,
LaunchedContainerAwsEnv awsEnv,
LambdaLayerService layerService,
LambdaPodSpecFactory podSpecFactory,
KubernetesPodLogStreamer logStreamer,
S3Service s3Service) {
this.client = client;
this.config = config;
this.runtimeApiServerFactory = runtimeApiServerFactory;
this.imageResolver = imageResolver;
this.addressResolver = addressResolver;
this.awsEnv = awsEnv;
this.layerService = layerService;
this.podSpecFactory = podSpecFactory;
this.logStreamer = logStreamer;
this.s3Service = s3Service;
}
/**
* Forces the lazy client bean (and its underlying Vert.x HTTP client) to be
* built on the caller's thread. Called from the startup observer so the HTTP
* client is not born on a request's Vert.x context, whose close at shutdown
* would leave every later Kubernetes API call failing with "Client is closed".
*/
public void initializeClient() {
client.getConfiguration();
// Resolve the address pods use to reach Floci now, so the most common
// misconfiguration (running outside the cluster without a floci-address)
// fails startup instead of every later cold start.
addressResolver.resolve();
}
@Override
public ContainerHandle launch(LambdaFunction fn) {
if (fn.isHotReload()) {
throw new RuntimeException("Hot reload requires a bind mount and is not supported by the "
+ "kubernetes Lambda executor. Use the docker executor for hot-reload functions.");
}
if (config.services().lambda().awsConfigPath().filter(s -> !s.isBlank()).isPresent()
&& awsConfigPathWarned.compareAndSet(false, true)) {
LOG.warn("floci.services.lambda.aws-config-path is a bind mount and is ignored by the "
+ "kubernetes executor; pods receive placeholder credentials instead");
}
var namespace = namespace();
sweepOrphansOnce(namespace);
LOG.infov("Launching pod for function: {0}", fn.getFunctionName());
var runtimeApiServer = runtimeApiServerFactory.create();
// Mirrors ContainerLauncher: any failure after the runtime-api server is allocated
// must release its port and delete a half-created pod, or cold-start bursts leak
// ports until the pool is exhausted.
String podName = null;
try {
var imagePackage = "Image".equals(fn.getPackageType()) && fn.getImageUri() != null;
// No emulated-ECR rewrite here: the kubelet pulls images, and Floci's loopback
// registry is not reachable from cluster nodes. URIs pass through unchanged.
var image = imagePackage ? fn.getImageUri() : imageResolver.resolve(fn.getRuntime());
var shortId = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
podName = LambdaPodSpecFactory.podName(fn.getFunctionName(), shortId);
var region = AwsArnUtils.regionOrDefault(fn.getFunctionArn(), config.defaultRegion());
var cwLogGroup = "/aws/lambda/" + fn.getFunctionName();
var cwLogStream = logStreamer.logStreamName(shortId);
var codeDownloadUrl = imagePackage ? null : codeDownloadUrl(fn, region);
var layerUrls = imagePackage ? List.<String>of() : layerDownloadUrls(fn, region);
var caCert = config.tls().enabled()
? ContainerLauncher.resolveFlociCaCertPath(true, config.tls().certPath(),
config.storage().persistentPath())
: Optional.<Path>empty();
var caConfigMap = caCert.map(cert -> ensureCaConfigMap(namespace, cert));
var env = new ArrayList<String>();
env.add("AWS_LAMBDA_RUNTIME_API=" + addressResolver.resolve() + ":" + runtimeApiServer.getPort());
env.add("AWS_LAMBDA_FUNCTION_NAME=" + fn.getFunctionName());
env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=" + fn.getMemorySize());
env.add("AWS_LAMBDA_FUNCTION_TIMEOUT=" + fn.getTimeout());
env.add("AWS_LAMBDA_FUNCTION_VERSION=$LATEST");
env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + cwLogGroup);
env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + cwLogStream);
if (fn.getHandler() != null && !fn.getHandler().isBlank()) {
env.add("_HANDLER=" + fn.getHandler());
}
env.addAll(awsEnv.sdkBaselineEnv(region, Optional.empty(), addressResolver.flociBaseUrl()));
env.addAll(ContainerLauncher.flociCaEnv(caCert));
if (fn.getEnvironment() != null) {
fn.getEnvironment().forEach((k, v) -> env.add(k + "=" + v));
}
var imageConfig = imagePackage
? new LambdaPodSpecFactory.ImageConfig(fn.getImageConfigEntryPoint(),
fn.getImageConfigCommand(), fn.getImageConfigWorkingDirectory())
: null;
var pod = podSpecFactory.buildPod(podName, fn.getFunctionName(), image, env,
codeDownloadUrl, layerUrls, isProvidedRuntime(fn.getRuntime()),
imagePackage ? null : fn.getHandler(), imageConfig, fn.getMemorySize(), caConfigMap);
ownPodNames.add(podName);
client.pods().inNamespace(namespace).resource(pod).create();
LOG.infov("Created pod {0} for function {1}", podName, fn.getFunctionName());
awaitRunning(namespace, podName, fn.getFunctionName());
var handle = new ContainerHandle(podName, fn.getFunctionName(),
runtimeApiServer, ContainerState.WARM, false);
// Log streaming is non-essential and runs after the pod is already Running,
// so a failure here (e.g. a missing pods/log RBAC verb) must not tear down a
// healthy execution environment.
try {
var logHandle = logStreamer.attach(namespace, podName, cwLogGroup, cwLogStream,
region, "lambda:" + fn.getFunctionName());
handle.setLogStream(logHandle);
} catch (Exception logFailure) {
LOG.warnv("Log streaming for pod {0} could not start; the environment still "
+ "runs but its logs will not reach CloudWatch: {1}", podName,
logFailure.getMessage());
}
return handle;
} catch (RuntimeException e) {
LOG.errorv(e, "Pod launch failed for function {0}; cleaning up", fn.getFunctionName());
boolean podGone = podName == null || deletePod(namespace, podName);
// Stop the server before releasing its port number, or the still-listening
// Vert.x server makes the port unusable for every later cold start.
try {
runtimeApiServer.stop().get(5, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
} catch (Exception stopFailure) {
LOG.debugv("Runtime API server stop failed during launch cleanup: {0}",
stopFailure.getMessage());
}
// Same rule as stop(): the port number goes back to the pool only once the pod
// is confirmed gone. A half-created pod that later reaches Running polls
// AWS_LAMBDA_RUNTIME_API on this port, and a released port could by then be
// serving a different environment.
if (podGone) {
try {
runtimeApiServerFactory.release(runtimeApiServer);
} catch (Exception ignore) {
LOG.debugv("Runtime API port release failed during launch cleanup: {0}", ignore.getMessage());
}
} else {
LOG.warnv("Keeping the Runtime API port reserved for pod {0} because its delete "
+ "did not succeed.", podName);
}
throw e;
}
}
@Override
public void stop(ContainerHandle handle) {
LOG.infov("Stopping pod {0}", handle.getContainerId());
handle.setState(ContainerState.STOPPED);
try {
handle.getRuntimeApiServer().stop().get(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException | TimeoutException | CancellationException e) {
// CancellationException is unchecked; catch it here so a cancelled stop future
// cannot skip the log-stream close, pod deletion, and port release below.
LOG.warnv(e, "RuntimeApiServer did not close cleanly for pod {0}", handle.getContainerId());
}
if (handle.getLogStream() != null) {
try {
handle.getLogStream().close();
} catch (Exception e) {
LOG.debugv("Closing log stream for pod {0} failed: {1}",
handle.getContainerId(), e.getMessage());
}
}
// Return the Runtime API port to the pool only once the pod is gone. If the delete
// did not succeed, the pod's runtime may still be polling that port, and reusing it
// for another cold start could hand that runtime a different function's invocation,
// so keep the port reserved until a later sweep reaps the pod.
if (deletePod(namespace(), handle.getContainerId())) {
try {
runtimeApiServerFactory.release(handle.getRuntimeApiServer());
} catch (Exception e) {
LOG.warnv("Runtime API port release failed for pod {0}: {1}",
handle.getContainerId(), e.getMessage());
}
} else {
LOG.warnv("Keeping the Runtime API port reserved for pod {0} because its delete "
+ "did not succeed.", handle.getContainerId());
}
}
@Override
public boolean isAlive(ContainerHandle handle) {
try {
var pod = client.pods().inNamespace(namespace()).withName(handle.getContainerId()).get();
return pod != null
&& pod.getStatus() != null
&& "Running".equals(pod.getStatus().getPhase())
&& pod.getMetadata().getDeletionTimestamp() == null;
} catch (Exception e) {
// A missing pod comes back as null above; reaching here means the API server
// itself was unreachable. Unlike docker's local socket the API server is remote,
// so a transient blip must not read as "dead" — that would cull the whole warm
// pool at once. Assume alive; a genuinely dead pod fails the next invocation.
LOG.warnv("Liveness probe for pod {0} could not reach the API server, assuming "
+ "alive: {1}", handle.getContainerId(), e.getMessage());
return true;
}
}
private String namespace() {
return config.services().lambda().kubernetes().namespace();
}
/**
* Deletes pods left behind by a previous Floci process. They can never be adopted:
* their runtimes poll Runtime API ports that died with that process.
*
* <p>Every launch blocks here until one sweep has succeeded; a failed sweep is
* retried by the next launch. Launches proceed after a failed sweep and their
* pods carry the same labels, so a retried sweep skips every name in
* {@link #ownPodNames} and deletion targets the listed pod names rather than
* the label selector. Both are required so a retried or concurrent sweep can
* never kill a fresh pod of this process.
*/
private void sweepOrphansOnce(String namespace) {
if (orphansSwept) {
return;
}
synchronized (orphanSweepLock) {
if (orphansSwept) {
return;
}
try {
var orphans = client.pods().inNamespace(namespace)
.withLabels(LambdaPodSpecFactory.managedPodSelector())
.list().getItems().stream()
.filter(pod -> !ownPodNames.contains(pod.getMetadata().getName()))
.toList();
if (!orphans.isEmpty()) {
LOG.infov("Deleting {0} orphaned Lambda pod(s) from a previous run in namespace {1}",
orphans.size(), namespace);
for (var orphan : orphans) {
client.pods().inNamespace(namespace)
.withName(orphan.getMetadata().getName())
.withGracePeriod(0)
.delete();
}
}
orphansSwept = true;
} catch (KubernetesClientException e) {
if (e.getCode() == 401 || e.getCode() == 403) {
// A permission gap never heals within this process, so stop retrying
// it on every cold start; the ServiceAccount simply cannot sweep.
LOG.warnv("Orphaned Lambda pod sweep is not permitted in namespace {0} "
+ "(the ServiceAccount lacks pod list/delete); skipping it for this "
+ "process: {1}", namespace, e.getMessage());
orphansSwept = true;
} else {
LOG.warnv("Orphaned Lambda pod sweep failed in namespace {0} and will be "
+ "retried on the next launch: {1}", namespace, e.getMessage());
}
} catch (Exception e) {
LOG.warnv("Orphaned Lambda pod sweep failed in namespace {0} and will be retried "
+ "on the next launch: {1}", namespace, e.getMessage());
}
}
}
private String codeDownloadUrl(LambdaFunction fn, String region) {
var bucket = LambdaService.tasksBucketName(region);
var key = LambdaService.codeObjectKey(fn);
try {
s3Service.headObject(bucket, key);
} catch (AwsException e) {
throw new RuntimeException("Deployment package for function '" + fn.getFunctionName()
+ "' is not available at s3://" + bucket + "/" + key
+ " — the kubernetes executor downloads code from Floci's S3, and the copy "
+ "stored at deploy time is missing. Re-deploy the function code.", e);
}
var account = fn.getAccountId() != null ? fn.getAccountId() : config.defaultAccountId();
return downloadUrl(bucket, key, account, region);
}
private List<String> layerDownloadUrls(LambdaFunction fn, String functionRegion) {
if (fn.getLayers() == null || fn.getLayers().isEmpty()) {
return List.of();
}
var urls = new ArrayList<String>();
for (var layerArn : fn.getLayers()) {
var layer = layerService.resolveLayerByArn(layerArn);
if (layer == null) {
LOG.warnv("Could not resolve layer ARN {0} for function {1}", layerArn, fn.getFunctionName());
continue;
}
// The archive lives under the layer's own region and account (the canonical
// ARN assigned at publish), which may differ from the function's.
var arn = layer.getLayerVersionArn();
var layerRegion = AwsArnUtils.regionOrDefault(arn, functionRegion);
var account = AwsArnUtils.accountOrDefault(arn, config.defaultAccountId());
var bucket = LambdaService.tasksBucketName(layerRegion);
var key = LambdaService.layerObjectKey(account, layer.getLayerName(), layer.getVersion());
try {
s3Service.headObject(bucket, key);
} catch (AwsException e) {
throw new RuntimeException("Layer zip for '" + layerArn + "' is not available at s3://"
+ bucket + "/" + key + " — layers published before kubernetes executor support "
+ "have no stored archive. Re-publish the layer version.", e);
}
urls.add(downloadUrl(bucket, key, account, layerRegion));
}
return urls;
}
/**
* Download URL for a tasks-bucket object. The init container fetches it over plain
* HTTP with no SigV4 header, so an {@code X-Amz-Credential} query steers Floci's
* account filter to the object's owning account. Without it a function or layer
* owned by a non-default account resolves the default-account prefix and 404s. A
* 12-digit account id doubles as its own access key id (LocalStack convention).
*/
private String downloadUrl(String bucket, String key, String account, String region) {
return addressResolver.downloadBaseUrl() + "/" + bucket + "/"
+ LambdaService.encodeObjectPath(key)
+ "?X-Amz-Credential=" + account + "%2F00010101%2F" + region + "%2Fs3%2Faws4_request";
}
private void awaitRunning(String namespace, String podName, String functionName) {
var timeoutSeconds = POD_STARTUP_TIMEOUT_SECONDS;
Pod pod;
try {
// A null pod (deleted out-of-band) is terminal too, otherwise the wait
// would block the invocation for the full timeout.
pod = client.pods().inNamespace(namespace).withName(podName)
.waitUntilCondition(p -> p == null
|| (p.getStatus() != null && (isRunning(p) || hasTerminalFailure(p))),
timeoutSeconds, TimeUnit.SECONDS);
} catch (Exception e) {
throw new RuntimeException("Pod " + podName + " for function '" + functionName
+ "' did not reach Running within " + timeoutSeconds + "s: " + e.getMessage(), e);
}
if (pod == null || !isRunning(pod)) {
throw new RuntimeException("Pod " + podName + " for function '" + functionName
+ "' failed to start: " + describeFailure(pod));
}
}
private static boolean isRunning(Pod pod) {
return "Running".equals(pod.getStatus().getPhase());
}
private static boolean hasTerminalFailure(Pod pod) {
var phase = pod.getStatus().getPhase();
// Succeeded means the runtime exited before serving — terminal for a server pod.
if ("Failed".equals(phase) || "Succeeded".equals(phase)) {
return true;
}
return unschedulableReason(pod) != null || describeTerminalReason(pod) != null;
}
private static String unschedulableReason(Pod pod) {
if (pod.getStatus().getConditions() == null) {
return null;
}
for (var condition : pod.getStatus().getConditions()) {
// A pod the scheduler cannot place — e.g. a memory request above every node's
// capacity — stays Pending with no container statuses; fail the cold start
// instead of blocking the invoker for the full startup timeout.
if ("PodScheduled".equals(condition.getType()) && "False".equals(condition.getStatus())
&& "Unschedulable".equals(condition.getReason())) {
return "Unschedulable: " + condition.getMessage();
}
}
return null;
}
private static String describeTerminalReason(Pod pod) {
var statuses = new ArrayList<ContainerStatus>();
if (pod.getStatus().getInitContainerStatuses() != null) {
statuses.addAll(pod.getStatus().getInitContainerStatuses());
}
if (pod.getStatus().getContainerStatuses() != null) {
statuses.addAll(pod.getStatus().getContainerStatuses());
}
for (var status : statuses) {
if (status.getState() == null) {
continue;
}
var waiting = status.getState().getWaiting();
// getReason() is null while a reason is absent; Set.of(...).contains(null)
// throws, which would abort an otherwise healthy cold start.
if (waiting != null && waiting.getReason() != null
&& TERMINAL_WAITING_REASONS.contains(waiting.getReason())) {
return status.getName() + ": " + waiting.getReason()
+ " (" + waiting.getMessage() + ")";
}
if (status.getState().getTerminated() != null
&& status.getState().getTerminated().getExitCode() != null
&& status.getState().getTerminated().getExitCode() != 0) {
return status.getName() + ": exited with code "
+ status.getState().getTerminated().getExitCode();
}
}
return null;
}
private static String describeFailure(Pod pod) {
if (pod == null || pod.getStatus() == null) {
return "pod no longer exists";
}
var reason = describeTerminalReason(pod);
if (reason == null) {
reason = unschedulableReason(pod);
}
return reason != null ? reason : "phase=" + pod.getStatus().getPhase();
}
/**
* Publishes Floci's CA cert as a ConfigMap so pods can trust Floci's HTTPS endpoint.
* Applied once per process; the cert cannot rotate within a Floci lifetime.
*/
private String ensureCaConfigMap(String namespace, Path caCertPath) {
if (caConfigMapApplied.get()) {
return CA_CONFIG_MAP_NAME;
}
try {
var pem = Files.readString(caCertPath);
var configMap = new ConfigMapBuilder()
.withNewMetadata()
.withName(CA_CONFIG_MAP_NAME)
.withLabels(LambdaPodSpecFactory.managedPodSelector())
.endMetadata()
.addToData(CA_CONFIG_MAP_KEY, pem)
.build();
client.configMaps().inNamespace(namespace).resource(configMap)
.createOr(NonDeletingOperation::update);
caConfigMapApplied.set(true);
return CA_CONFIG_MAP_NAME;
} catch (Exception e) {
throw new RuntimeException("Could not publish Floci CA cert ConfigMap '" + CA_CONFIG_MAP_NAME
+ "' in namespace " + namespace + ": " + e.getMessage(), e);
}
}
/**
* Force-deletes the pod and waits for it to actually disappear. Returns whether the
* pod is confirmed gone: only then is it safe to reuse resources tied to it (its
* Runtime API port), because until the object vanishes the runtime container may
* still be polling that port. A rejected delete or a wait that times out returns
* false so the caller keeps the port reserved.
*/
private boolean deletePod(String namespace, String podName) {
// Dropped from the own-pod set even when the delete fails: the pod is
// abandoned either way, and a retried orphan sweep may still collect it.
ownPodNames.remove(podName);
try {
client.pods().inNamespace(namespace).withName(podName).withGracePeriod(0).delete();
client.pods().inNamespace(namespace).withName(podName)
.waitUntilCondition(p -> p == null, POD_DELETE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
LOG.warnv("Pod {0} was not confirmed deleted within {1}s: {2}",
podName, POD_DELETE_TIMEOUT_SECONDS, e.getMessage());
return false;
}
}
private static boolean isProvidedRuntime(String runtime) {
return runtime != null && runtime.startsWith("provided");
}
}
@@ -0,0 +1,78 @@
package io.github.hectorvent.floci.services.lambda.launcher.kubernetes;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
* Streams a Lambda pod's runtime-container logs to CloudWatch Logs and the console,
* matching what {@link ContainerLogStreamer} does for Docker containers. The CloudWatch
* write path is delegated to {@code ContainerLogStreamer} so both executors produce
* identical log groups, streams and events.
*/
@ApplicationScoped
public class KubernetesPodLogStreamer {
private static final Logger LOG = Logger.getLogger(KubernetesPodLogStreamer.class);
private final KubernetesClient client;
private final ContainerLogStreamer cloudWatchWriter;
@Inject
public KubernetesPodLogStreamer(KubernetesClient client, ContainerLogStreamer cloudWatchWriter) {
this.client = client;
this.cloudWatchWriter = cloudWatchWriter;
}
/** CloudWatch log stream name for one execution environment, same shape as the docker path. */
public String logStreamName(String shortId) {
return cloudWatchWriter.generateLogStreamName("[$LATEST]" + shortId);
}
public Closeable attach(String namespace, String podName, String logGroup,
String logStream, String region, String logPrefix) {
cloudWatchWriter.ensureLogGroupAndStream(logGroup, logStream, region);
var watch = client.pods().inNamespace(namespace).withName(podName)
.inContainer("runtime")
.watchLog();
// Virtual thread: one blocking reader per warm pod is near-free this way.
var reader = Thread.ofVirtual().name("lambda-pod-logs-" + podName).start(() -> {
try (var lines = new BufferedReader(
new InputStreamReader(watch.getOutput(), StandardCharsets.UTF_8))) {
String line;
while ((line = lines.readLine()) != null) {
// Match the docker log path: trim trailing whitespace and drop blank
// lines so both executors write identical CloudWatch events.
var trimmed = line.stripTrailing();
if (trimmed.isEmpty()) {
continue;
}
LOG.infov("[{0}] {1}", logPrefix, trimmed);
cloudWatchWriter.streamToCloudWatchLogs(logGroup, logStream, region, trimmed);
}
} catch (IOException e) {
// Normal on pod deletion: the log stream just ends.
LOG.debugv("Log stream for pod {0} ended: {1}", podName, e.getMessage());
}
});
return () -> {
try {
watch.close();
} catch (Exception e) {
LOG.debugv("Closing log watch for pod {0} failed: {1}", podName, e.getMessage());
}
reader.interrupt();
};
}
}
@@ -0,0 +1,296 @@
package io.github.hectorvent.floci.services.lambda.launcher.kubernetes;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeBuilder;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.lambda.launcher.ContainerLauncher;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
/**
* Builds the Pod manifest for one Lambda execution environment. Code cannot be
* copied into a pod before it starts (no docker-cp equivalent), so an init
* container downloads the deployment package (and layers) from Floci's S3 over
* HTTP into an emptyDir mounted at /var/task before the runtime starts.
*/
@ApplicationScoped
public class LambdaPodSpecFactory {
private static final Logger LOG = Logger.getLogger(LambdaPodSpecFactory.class);
/** Label key: optional DNS-subdomain prefix (dot-separated valid segments), then a 63-char-max name. */
private static final java.util.regex.Pattern LABEL_KEY =
java.util.regex.Pattern.compile("([a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?[a-zA-Z0-9]([-a-zA-Z0-9_.]{0,61}[a-zA-Z0-9])?");
/** Label value: empty, or 63-char-max alphanumeric-bounded. */
private static final java.util.regex.Pattern LABEL_VALUE =
java.util.regex.Pattern.compile("([a-zA-Z0-9]([-a-zA-Z0-9_.]{0,61}[a-zA-Z0-9])?)?");
static final String MANAGED_BY_LABEL = "app.kubernetes.io/managed-by";
static final String MANAGED_BY_VALUE = "floci";
static final String SERVICE_LABEL = "floci.io/service";
static final String SERVICE_VALUE = "lambda";
static final String FUNCTION_LABEL = "floci.io/function-name";
private static final String TASK_DIR = "/var/task";
private static final String OPT_DIR = "/opt";
private static final String RUNTIME_DIR = "/var/runtime";
private static final int MAX_NAME_LENGTH = 63;
private final EmulatorConfig config;
@Inject
public LambdaPodSpecFactory(EmulatorConfig config) {
this.config = config;
}
/**
* @param podName DNS-safe pod name (see {@link #podName})
* @param image resolved runtime image
* @param env {@code KEY=value} entries, same shape the Docker launcher builds
* @param codeDownloadUrl URL of the function's deployment zip; null for Image package type
* @param layerDownloadUrls layer zips to unpack into /opt, in merge order
* @param providedRuntime whether the runtime is provided.* (bootstrap needs an exec bit)
* @param handlerOrNull function handler used as the runtime container arg (Zip functions)
* @param imageConfig Image-package-type entrypoint/command/workingdir; empty lists/null when unset
* @param caConfigMapName ConfigMap holding Floci's CA cert, mounted at /etc/floci-ca.crt when present
*/
public Pod buildPod(String podName,
String functionName,
String image,
List<String> env,
String codeDownloadUrl,
List<String> layerDownloadUrls,
boolean providedRuntime,
String handlerOrNull,
ImageConfig imageConfig,
int memoryMb,
Optional<String> caConfigMapName) {
var hasCode = codeDownloadUrl != null;
var hasLayers = !layerDownloadUrls.isEmpty();
var volumes = new ArrayList<Volume>();
var runtimeMounts = new ArrayList<VolumeMount>();
if (hasCode) {
volumes.add(new VolumeBuilder().withName("task").withNewEmptyDir().endEmptyDir().build());
runtimeMounts.add(mount("task", TASK_DIR));
}
if (hasLayers) {
volumes.add(new VolumeBuilder().withName("opt").withNewEmptyDir().endEmptyDir().build());
runtimeMounts.add(mount("opt", OPT_DIR));
}
// 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.
var needsRuntimeDir = providedRuntime && hasCode;
if (needsRuntimeDir) {
volumes.add(new VolumeBuilder().withName("runtime").withNewEmptyDir().endEmptyDir().build());
runtimeMounts.add(mount("runtime", RUNTIME_DIR));
}
caConfigMapName.ifPresent(cm -> {
volumes.add(new VolumeBuilder().withName("floci-ca")
.withNewConfigMap().withName(cm).endConfigMap().build());
runtimeMounts.add(new VolumeMountBuilder()
.withName("floci-ca")
.withMountPath(ContainerLauncher.FLOCI_CA_CONTAINER_PATH)
.withSubPath(KubernetesPodLauncher.CA_CONFIG_MAP_KEY)
.withReadOnly(true)
.build());
});
var runtime = new ContainerBuilder()
.withName("runtime")
.withImage(image)
// Explicit IfNotPresent: the default for :latest/untagged is Always,
// which breaks images pre-loaded onto nodes (kind load docker-image).
.withImagePullPolicy("IfNotPresent")
.withEnv(toEnvVars(env))
.withVolumeMounts(runtimeMounts)
.withNewResources()
.addToRequests("memory", new Quantity(memoryMb + "Mi"))
.addToLimits("memory", new Quantity(memoryMb + "Mi"))
.endResources();
if (imageConfig != null && !imageConfig.entryPoint().isEmpty()) {
runtime.withCommand(escapeDollars(imageConfig.entryPoint()));
}
if (imageConfig != null && !imageConfig.command().isEmpty()) {
runtime.withArgs(escapeDollars(imageConfig.command()));
} else if (handlerOrNull != null && !handlerOrNull.isBlank()) {
runtime.withArgs(escapeDollar(handlerOrNull));
}
if (imageConfig != null && imageConfig.workingDirectory() != null
&& !imageConfig.workingDirectory().isBlank()) {
runtime.withWorkingDir(imageConfig.workingDirectory());
}
var initContainers = new ArrayList<Container>();
if (hasCode) {
var initMounts = new ArrayList<VolumeMount>();
initMounts.add(mount("task", TASK_DIR));
if (hasLayers) {
initMounts.add(mount("opt", OPT_DIR));
}
if (needsRuntimeDir) {
initMounts.add(mount("runtime", RUNTIME_DIR));
}
initContainers.add(new ContainerBuilder()
.withName("code-download")
.withImage(config.services().lambda().kubernetes().initImage())
.withImagePullPolicy("IfNotPresent")
.withCommand("sh", "-c", initScript(codeDownloadUrl, layerDownloadUrls, providedRuntime))
.withVolumeMounts(initMounts)
.build());
}
return new PodBuilder()
.withNewMetadata()
.withName(podName)
.withLabels(podLabels(functionName))
.endMetadata()
.withNewSpec()
.withRestartPolicy("Never")
.withTerminationGracePeriodSeconds(5L)
.withInitContainers(initContainers)
.withContainers(runtime.build())
.withVolumes(volumes)
.endSpec()
.build();
}
/** Entrypoint/command/workingdir of an Image-package-type function. */
public record ImageConfig(List<String> entryPoint, List<String> command, String workingDirectory) {
public ImageConfig {
entryPoint = entryPoint == null ? List.of() : entryPoint;
command = command == null ? List.of() : command;
}
}
String initScript(String codeDownloadUrl, List<String> layerDownloadUrls, boolean providedRuntime) {
// Download URLs are always plain HTTP (see KubernetesFlociAddressResolver
// .downloadBaseUrl): busybox wget's built-in TLS cannot handshake with Floci.
var wget = "wget -q";
var script = new StringBuilder("set -e\n");
script.append(wget).append(" -O /tmp/code.zip '").append(codeDownloadUrl).append("'\n");
script.append("unzip -oq /tmp/code.zip -d ").append(TASK_DIR).append("\n");
for (var i = 0; i < layerDownloadUrls.size(); i++) {
script.append(wget).append(" -O /tmp/layer").append(i).append(".zip '")
.append(layerDownloadUrls.get(i)).append("'\n");
script.append("unzip -oq /tmp/layer").append(i).append(".zip -d ").append(OPT_DIR).append("\n");
}
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");
}
return script.toString();
}
Map<String, String> podLabels(String functionName) {
var labels = new LinkedHashMap<String, String>();
config.services().lambda().kubernetes().labels().orElse(List.of()).forEach(entry -> {
var eq = entry.indexOf('=');
var key = eq > 0 ? entry.substring(0, eq).trim() : "";
var value = eq > 0 ? entry.substring(eq + 1).trim() : "";
// Invalid entries are dropped, not sanitized: a silently rewritten label
// would no longer match the NetworkPolicy/selector the user wrote it for,
// and the API server would reject the pod at cold start otherwise.
if (LABEL_KEY.matcher(key).matches() && LABEL_VALUE.matcher(value).matches()) {
labels.put(key, value);
} else {
LOG.warnv("Ignoring invalid Lambda pod label entry ''{0}'' "
+ "(expected key=value with Kubernetes label syntax)", entry);
}
});
// Managed labels go last so user entries can never overwrite them — the
// orphan sweep selects on these, and an overwritten label leaks pods forever.
labels.put(MANAGED_BY_LABEL, MANAGED_BY_VALUE);
labels.put(SERVICE_LABEL, SERVICE_VALUE);
labels.put(FUNCTION_LABEL, sanitizeLabelValue(functionName));
return labels;
}
/** Selector matching every pod this Floci instance's Lambda executor manages. */
static Map<String, String> managedPodSelector() {
return Map.of(MANAGED_BY_LABEL, MANAGED_BY_VALUE, SERVICE_LABEL, SERVICE_VALUE);
}
/**
* DNS-1123 pod name: lowercase alphanumerics and dashes, max 63 chars. The random
* suffix stays intact; the function name is truncated to fit.
*/
static String podName(String functionName, String shortId) {
var prefix = "floci-lambda-";
var sanitized = functionName.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9-]", "-");
var budget = MAX_NAME_LENGTH - prefix.length() - shortId.length() - 1;
if (sanitized.length() > budget) {
sanitized = sanitized.substring(0, budget);
}
sanitized = sanitized.replaceAll("^-+|-+$", "");
if (sanitized.isEmpty()) {
sanitized = "fn";
}
return prefix + sanitized + "-" + shortId;
}
/** Label values allow [a-zA-Z0-9-_.], max 63 chars, alphanumeric at both ends. */
static String sanitizeLabelValue(String value) {
var sanitized = value.replaceAll("[^a-zA-Z0-9-_.]", "-");
if (sanitized.length() > MAX_NAME_LENGTH) {
sanitized = sanitized.substring(0, MAX_NAME_LENGTH);
}
sanitized = sanitized.replaceAll("^[^a-zA-Z0-9]+", "").replaceAll("[^a-zA-Z0-9]+$", "");
return sanitized;
}
private static VolumeMount mount(String name, String path) {
return new VolumeMountBuilder().withName(name).withMountPath(path).build();
}
private static List<String> escapeDollars(List<String> values) {
return values.stream().map(LambdaPodSpecFactory::escapeDollar).toList();
}
private static String escapeDollar(String value) {
// Kubernetes expands $(VAR) and collapses $$ in command/args exactly as in env
// values, so escape $ to deliver them byte-for-byte like the docker executor.
return value.replace("$", "$$");
}
private static List<EnvVar> toEnvVars(List<String> env) {
var vars = new ArrayList<EnvVar>(env.size());
for (var entry : env) {
var eq = entry.indexOf('=');
var key = eq >= 0 ? entry.substring(0, eq) : entry;
var value = eq >= 0 ? entry.substring(eq + 1) : "";
// The API server rejects a pod whose env var has an empty name.
if (key.isEmpty()) {
LOG.warnv("Skipping env entry with an empty name in the pod spec: ''{0}''", entry);
continue;
}
// Kubernetes expands $(VAR) in env values and collapses $$ to $.
// Escaping every $ delivers values byte-for-byte like the docker executor.
vars.add(new EnvVar(key, value.replace("$", "$$"), null));
}
return vars;
}
}
@@ -24,10 +24,14 @@ public class LambdaLayerVersion {
private long codeSizeBytes;
private String codeSha256;
private String codeLocalPath;
private boolean archiveStored;
public LambdaLayerVersion() {
}
public boolean isArchiveStored() { return archiveStored; }
public void setArchiveStored(boolean archiveStored) { this.archiveStored = archiveStored; }
public String getLayerName() { return layerName; }
public void setLayerName(String layerName) { this.layerName = layerName; }