mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lambda): support node selectors and tolerations for Kubernetes Lambda pods (#8967)
The Kubernetes Lambda executor built every pod without a nodeSelector or tolerations, so on a cluster whose node groups are all tainted the pods stayed Pending until the cold-start timeout. FLOCI_SERVICES_LAMBDA_KUBERNETES_NODE_SELECTOR takes key=value entries with label syntax for spec.nodeSelector, and an invalid entry fails the cold start rather than letting the pod land on nodes it was meant to avoid. FLOCI_SERVICES_LAMBDA_KUBERNETES_TOLERATIONS takes the tolerated taints in kubectl taint syntax (key=value:Effect, key:Effect, or a key alone for every effect), and an invalid entry is dropped with a warning, as an invalid label is. A repeated key now logs which value replaced which. The Helm chart, now 0.2.0, renders both from floci.lambda.kubernetes.nodeSelector and tolerations. Closes #8992
This commit is contained in:
1 parent
a8cb033a98
commit
9795119ce0
11 files changed
+275
-16
No files matched your search
@@ -3719,6 +3719,25 @@ public interface EmulatorConfig {
|
||||
*/
|
||||
Optional<List<String>> labels();
|
||||
|
||||
/**
|
||||
* Node selector of Lambda pods, as {@code key=value} entries with label syntax.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_LAMBDA_KUBERNETES_NODE_SELECTOR (comma-separated)
|
||||
*/
|
||||
Optional<List<String>> nodeSelector();
|
||||
|
||||
/**
|
||||
* Tolerations of Lambda pods, each written as the taint it tolerates in
|
||||
* {@code kubectl taint} syntax: {@code key=value:Effect} tolerates that taint
|
||||
* (operator {@code Equal}), {@code key:Effect} tolerates every value of the key
|
||||
* (operator {@code Exists}), and an entry without {@code :Effect} tolerates every
|
||||
* effect. Effects are {@code NoSchedule}, {@code PreferNoSchedule} and
|
||||
* {@code NoExecute}.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_LAMBDA_KUBERNETES_TOLERATIONS (comma-separated)
|
||||
*/
|
||||
Optional<List<String>> tolerations();
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
+98
-15
@@ -15,6 +15,8 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -31,9 +33,12 @@ public class LambdaPodSpecFactory {
|
||||
/** Label key: optional DNS-subdomain prefix (dot-separated valid segments), then a 63-char-max name. */
|
||||
private static final Pattern LABEL_KEY =
|
||||
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])?");
|
||||
/** A label key's DNS-subdomain prefix is at most 253 characters */
|
||||
private static final int MAX_LABEL_KEY_PREFIX_LENGTH = 253;
|
||||
/** Label value: empty, or 63-char-max alphanumeric-bounded. */
|
||||
private static final Pattern LABEL_VALUE =
|
||||
Pattern.compile("([a-zA-Z0-9]([-a-zA-Z0-9_.]{0,61}[a-zA-Z0-9])?)?");
|
||||
private static final Set<String> TAINT_EFFECTS = Set.of("NoSchedule", "PreferNoSchedule", "NoExecute");
|
||||
|
||||
static final String MANAGED_BY_LABEL = "app.kubernetes.io/managed-by";
|
||||
static final String MANAGED_BY_VALUE = "floci";
|
||||
@@ -163,6 +168,19 @@ public class LambdaPodSpecFactory {
|
||||
ObjectNode spec = nodes.objectNode()
|
||||
.put("restartPolicy", "Never")
|
||||
.put("terminationGracePeriodSeconds", 5);
|
||||
|
||||
Map<String, String> nodeSelector = podNodeSelector();
|
||||
if (!nodeSelector.isEmpty()) {
|
||||
ObjectNode selector = nodes.objectNode();
|
||||
nodeSelector.forEach(selector::put);
|
||||
spec.set("nodeSelector", selector);
|
||||
}
|
||||
|
||||
ArrayNode tolerations = podTolerations(nodes);
|
||||
if (!tolerations.isEmpty()) {
|
||||
spec.set("tolerations", tolerations);
|
||||
}
|
||||
|
||||
spec.set("initContainers", initContainers);
|
||||
spec.set("containers", nodes.arrayNode().add(runtime));
|
||||
spec.set("volumes", volumes);
|
||||
@@ -212,21 +230,11 @@ public class LambdaPodSpecFactory {
|
||||
}
|
||||
|
||||
Map<String, String> podLabels(String functionName) {
|
||||
LinkedHashMap<String, String> labels = new LinkedHashMap<>();
|
||||
config.services().lambda().kubernetes().labels().orElse(List.of()).forEach(entry -> {
|
||||
int eq = entry.indexOf('=');
|
||||
String key = eq > 0 ? entry.substring(0, eq).trim() : "";
|
||||
String 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);
|
||||
}
|
||||
});
|
||||
LinkedHashMap<String, String> labels =
|
||||
keyValues(config.services().lambda().kubernetes().labels(),
|
||||
"FLOCI_SERVICES_LAMBDA_KUBERNETES_LABELS", entry -> 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);
|
||||
@@ -235,6 +243,81 @@ public class LambdaPodSpecFactory {
|
||||
return labels;
|
||||
}
|
||||
|
||||
Map<String, String> podNodeSelector() {
|
||||
return keyValues(config.services().lambda().kubernetes().nodeSelector(),
|
||||
"FLOCI_SERVICES_LAMBDA_KUBERNETES_NODE_SELECTOR", entry -> {
|
||||
throw new IllegalStateException("Invalid Lambda pod node selector entry '" + entry
|
||||
+ "' in FLOCI_SERVICES_LAMBDA_KUBERNETES_NODE_SELECTOR (expected key=value with Kubernetes label syntax)");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Each entry is the taint it tolerates in {@code kubectl taint} syntax:
|
||||
* {@code key=value:Effect} (Equal), {@code key:Effect} (Exists), with {@code :Effect}
|
||||
* optional to tolerate every effect.
|
||||
*/
|
||||
ArrayNode podTolerations(JsonNodeFactory nodes) {
|
||||
ArrayNode tolerations = nodes.arrayNode();
|
||||
config.services().lambda().kubernetes().tolerations().orElse(List.of()).forEach(entry -> {
|
||||
String taint = entry.trim();
|
||||
String effect = null;
|
||||
int colon = taint.lastIndexOf(':');
|
||||
if (colon >= 0) {
|
||||
effect = taint.substring(colon + 1).trim();
|
||||
taint = taint.substring(0, colon).trim();
|
||||
}
|
||||
int eq = taint.indexOf('=');
|
||||
String key = eq >= 0 ? taint.substring(0, eq).trim() : taint;
|
||||
String value = eq >= 0 ? taint.substring(eq + 1).trim() : null;
|
||||
if (!isValidLabelKey(key)
|
||||
|| (value != null && !LABEL_VALUE.matcher(value).matches())
|
||||
|| (effect != null && !TAINT_EFFECTS.contains(effect))) {
|
||||
LOG.warnv("Ignoring invalid Lambda pod toleration entry ''{0}'' "
|
||||
+ "(expected key[=value][:NoSchedule|PreferNoSchedule|NoExecute])", entry);
|
||||
return;
|
||||
}
|
||||
ObjectNode toleration = nodes.objectNode().put("key", key);
|
||||
if (value == null) {
|
||||
toleration.put("operator", "Exists");
|
||||
} else {
|
||||
toleration.put("operator", "Equal").put("value", value);
|
||||
}
|
||||
if (effect != null) {
|
||||
toleration.put("effect", effect);
|
||||
}
|
||||
tolerations.add(toleration);
|
||||
});
|
||||
return tolerations;
|
||||
}
|
||||
|
||||
private static LinkedHashMap<String, String> keyValues(Optional<List<String>> entries, String setting,
|
||||
Consumer<String> onInvalid) {
|
||||
LinkedHashMap<String, String> result = new LinkedHashMap<>();
|
||||
entries.orElse(List.of()).forEach(entry -> {
|
||||
int eq = entry.indexOf('=');
|
||||
String key = eq > 0 ? entry.substring(0, eq).trim() : "";
|
||||
String value = eq > 0 ? entry.substring(eq + 1).trim() : "";
|
||||
// Invalid entries are dropped, not sanitized: a silently rewritten entry
|
||||
// would no longer match the NetworkPolicy/selector or node it was written
|
||||
// for, and the API server would reject the pod at cold start otherwise.
|
||||
if (isValidLabelKey(key) && LABEL_VALUE.matcher(value).matches()) {
|
||||
String previous = result.put(key, value);
|
||||
if (previous != null && !previous.equals(value)) {
|
||||
LOG.warnv("Entry ''{0}'' in {1} replaces the earlier value ''{2}'' for key ''{3}'' "
|
||||
+ "(a Kubernetes label key holds one value)", entry, setting, previous, key);
|
||||
}
|
||||
} else {
|
||||
onInvalid.accept(entry);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean isValidLabelKey(String key) {
|
||||
int slash = key.indexOf('/');
|
||||
return LABEL_KEY.matcher(key).matches() && slash <= MAX_LABEL_KEY_PREFIX_LENGTH;
|
||||
}
|
||||
|
||||
/** 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);
|
||||
|
||||
Reference in new issue
Block a user