From 01564bd94cd1799efa9b8ccc48ec1c45307327ad Mon Sep 17 00:00:00 2001 From: mrfelek <17274927+mrfelek@users.noreply.github.com> Date: Sat, 28 Nov 2026 21:09:47 +0100 Subject: [PATCH] fix(eks): expire EKS Auto Mode NodeClaims at Auto Mode's 21-day maximum node lifetime (#9670) An EKS Auto Mode NodeClaim whose expireAfter was Never, longer than 21 days, missing or unreadable never expired, where Auto Mode terminates an instance once it has lived its 21-day maximum lifetime. A claim now expires at the earlier of its expireAfter and 21 days from its creation. A claim whose creationTimestamp can't be read still doesn't expire, and an unreadable expireAfter is logged. --- docs/services/eks.md | 3 +- .../services/eks/EksAutoModeNodeClaims.java | 34 ++++++++++++++----- .../services/eks/EksAutoModeComputeTest.java | 15 ++++++-- .../eks/EksAutoModeNodeClaimsTest.java | 18 ++++++---- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/docs/services/eks.md b/docs/services/eks.md index 5cac767e75..c97947cc1d 100644 --- a/docs/services/eks.md +++ b/docs/services/eks.md @@ -531,7 +531,8 @@ launches nodes on AWS. Floci checks every 10 seconds. with its instance, Karpenter's finalizer taken off (other finalizers stay). Once it has lived its `expireAfter`, counted from its creation, it is deleted, whatever the pool's disruption budgets, as AWS expired a `5m` pool's claims five minutes in, a `nodes: "0"` budget aside; its node then goes as a deleted claim's does, and its pods get - another. A claim that couldn't be made is made + another. No claim lives more than 21 days, Auto Mode's documented maximum node lifetime, which `expireAfter` can + only shorten: a pool's `Never`, or an `expireAfter` longer than that, still has its claims go at 21 days. A claim that couldn't be made is made on a later pass. Floci touches only the claims labelled `app.kubernetes.io/managed-by: eks`. `kubectl get nodeclaims` shows the type, capacity type, zone, node and readiness, as Karpenter's columns do. - **Pool status.** Each pool's `status.resources` (`cpu`, `memory`, `pods`, `nodes`) and `status.nodes` say what its diff --git a/src/main/java/io/floci/aws/services/eks/EksAutoModeNodeClaims.java b/src/main/java/io/floci/aws/services/eks/EksAutoModeNodeClaims.java index 1f0725c6b7..dacda7b99e 100644 --- a/src/main/java/io/floci/aws/services/eks/EksAutoModeNodeClaims.java +++ b/src/main/java/io/floci/aws/services/eks/EksAutoModeNodeClaims.java @@ -32,6 +32,11 @@ final class EksAutoModeNodeClaims { static final String DEFAULT_EXPIRE_AFTER = "336h"; /** What Auto Mode puts in {@code terminationGracePeriod} when the pool doesn't say: 24 hours. */ static final String DEFAULT_TERMINATION_GRACE_PERIOD = "24h0m0s"; + /** + * The longest an Auto Mode node lives, as AWS documents it: 21 days, whatever the pool's {@code expireAfter}, which + * can only shorten it. + */ + static final Duration MAX_NODE_LIFETIME = Duration.ofDays(21); static final String MANAGED_BY = "app.kubernetes.io/managed-by"; static final String MANAGED_BY_VALUE = "eks"; private static final Logger LOG = Logger.getLogger(EksAutoModeNodeClaims.class); @@ -188,26 +193,39 @@ final class EksAutoModeNodeClaims { /** * Whether a NodeClaim has lived its {@code expireAfter}, counted from its creation, as Karpenter expires a claim - * (deleting it, whatever the pool's disruption budgets). One whose {@code expireAfter} is {@code Never}, missing - * or unreadable doesn't expire. + * (deleting it, whatever the pool's disruption budgets), or Auto Mode's {@link #MAX_NODE_LIFETIME}, which an + * {@code expireAfter} can only shorten: one that is {@code Never}, longer, missing or unreadable expires then. One + * whose creation can't be read doesn't expire. */ static boolean expired(JsonNode claim, Instant now) { String after = claim.path("spec").path("expireAfter").asText(""); String created = claim.path("metadata").path("creationTimestamp").asText(""); - if (after.isEmpty() || created.isEmpty()) { + if (created.isEmpty()) { return false; } try { - Duration life = EksAutoModeNodes.duration(after); - return life != null && !Instant.parse(created).plus(life).isAfter(now); + return !Instant.parse(created).plus(lifetime(claim, after)).isAfter(now); } catch (RuntimeException unreadable) { - LOG.warnv("EKS Auto Mode NodeClaim {0} has an expireAfter ({1}) or creationTimestamp ({2}) that can''t be " - + "read; it doesn''t expire: {3}", claim.path("metadata").path("name").asText(), after, created, - unreadable.getMessage()); + LOG.warnv("EKS Auto Mode NodeClaim {0} has a creationTimestamp ({1}) that can''t be read; it doesn''t " + + "expire: {2}", claim.path("metadata").path("name").asText(), created, unreadable.getMessage()); return false; } } + /** How long a claim lives: its {@code expireAfter}, held to {@link #MAX_NODE_LIFETIME}. */ + private static Duration lifetime(JsonNode claim, String after) { + Duration life = null; + if (!after.isEmpty()) { + try { + life = EksAutoModeNodes.duration(after); + } catch (IllegalArgumentException unreadable) { + LOG.warnv("EKS Auto Mode NodeClaim {0} has an expireAfter ({1}) that can''t be read; it expires after " + + "Auto Mode''s maximum node lifetime", claim.path("metadata").path("name").asText(), after); + } + } + return life == null || life.compareTo(MAX_NODE_LIFETIME) > 0 ? MAX_NODE_LIFETIME : life; + } + /** Whether Auto Mode manages a NodeClaim, as its {@code app.kubernetes.io/managed-by: eks} label says. */ static boolean managed(JsonNode claim) { return MANAGED_BY_VALUE.equals(claim.path("metadata").path("labels").path(MANAGED_BY).asText()); diff --git a/src/test/java/io/floci/aws/services/eks/EksAutoModeComputeTest.java b/src/test/java/io/floci/aws/services/eks/EksAutoModeComputeTest.java index fd3bd001ac..2841a1c714 100644 --- a/src/test/java/io/floci/aws/services/eks/EksAutoModeComputeTest.java +++ b/src/test/java/io/floci/aws/services/eks/EksAutoModeComputeTest.java @@ -716,7 +716,10 @@ class EksAutoModeComputeTest { order.verify(ec2).terminateInstances(REGION, List.of("i-node")); } - /** A claim younger than its expireAfter, or whose expireAfter is Never, isn't deleted. */ + /** + * A claim younger than its expireAfter, or whose expireAfter is Never and younger than Auto Mode's 21 days, isn't + * deleted; a Never claim past them is. + */ @Test void aClaimNotYetExpiredStays() { joinedNode("i-node", "node-1"); @@ -727,13 +730,19 @@ class EksAutoModeComputeTest { compute.reconcile(cluster("prod"), REGION); - // A claim whose expireAfter is Never stays however old it is. - lists.put("nodeclaims", "{\"items\": [" + claimOfAge(claim, "Never", Duration.ofDays(30)) + "]}"); + // A claim whose expireAfter is Never stays until Auto Mode's 21 days are up. + lists.put("nodeclaims", "{\"items\": [" + claimOfAge(claim, "Never", Duration.ofDays(20)) + "]}"); compute.reconcile(cluster("prod"), REGION); verify(clusterManager, never()).execInContainerForResult("k3s-1", new String[]{"kubectl", "delete", "nodeclaim", claim, "--wait=false"}, 15); verify(ec2, never()).terminateInstances(anyString(), anyList()); + + lists.put("nodeclaims", "{\"items\": [" + claimOfAge(claim, "Never", Duration.ofDays(21)) + "]}"); + compute.reconcile(cluster("prod"), REGION); + + verify(clusterManager).execInContainerForResult("k3s-1", new String[]{"kubectl", "delete", "nodeclaim", claim, + "--wait=false"}, 15); } /** A deleted claim whose instance was stopped has its node deleted, without a drain, and its instance ended. */ diff --git a/src/test/java/io/floci/aws/services/eks/EksAutoModeNodeClaimsTest.java b/src/test/java/io/floci/aws/services/eks/EksAutoModeNodeClaimsTest.java index 6b09d962af..a4edc5f617 100644 --- a/src/test/java/io/floci/aws/services/eks/EksAutoModeNodeClaimsTest.java +++ b/src/test/java/io/floci/aws/services/eks/EksAutoModeNodeClaimsTest.java @@ -150,7 +150,7 @@ class EksAutoModeNodeClaimsTest { /** * A claim expires once it has lived its expireAfter from its creation, as AWS deleted claims of a 5m pool five - * minutes after they were made; Never, none or one unreadable doesn't. + * minutes after they were made; Never, a longer one, none or one unreadable once Auto Mode's 21 days are up. */ @Test void aClaimExpiresAfterItsExpireAfter() throws Exception { @@ -162,13 +162,19 @@ class EksAutoModeNodeClaimsTest { {"metadata": {"creationTimestamp": "2026-11-01T00:00:00Z"}, "spec": {"expireAfter": "336h"}}"""); assertFalse(EksAutoModeNodeClaims.expired(defaulted, Instant.parse("2026-11-14T23:59:59Z"))); assertTrue(EksAutoModeNodeClaims.expired(defaulted, Instant.parse("2026-11-15T00:00:00Z"))); - Instant later = Instant.parse("2027-01-01T00:00:00Z"); - for (String after : List.of("\"Never\"", "\"soon\"")) { - assertFalse(EksAutoModeNodeClaims.expired(MAPPER.readTree("{\"metadata\": {\"creationTimestamp\": " - + "\"2026-11-01T00:00:00Z\"}, \"spec\": {\"expireAfter\": " + after + "}}"), later), after); + // Created 2026-11-01, so Auto Mode's 21 days are up at 2026-11-22. + Instant justBefore = Instant.parse("2026-11-21T23:59:59Z"); + Instant twentyOneDays = Instant.parse("2026-11-22T00:00:00Z"); + for (String spec : List.of("{\"expireAfter\": \"Never\"}", "{\"expireAfter\": \"720h\"}", + "{\"expireAfter\": \"soon\"}", "{}")) { + JsonNode claim = MAPPER.readTree("{\"metadata\": {\"creationTimestamp\": \"2026-11-01T00:00:00Z\"}, " + + "\"spec\": " + spec + "}"); + assertFalse(EksAutoModeNodeClaims.expired(claim, justBefore), spec); + assertTrue(EksAutoModeNodeClaims.expired(claim, twentyOneDays), spec); } assertFalse(EksAutoModeNodeClaims.expired(MAPPER.readTree( - "{\"metadata\": {\"creationTimestamp\": \"2026-11-01T00:00:00Z\"}, \"spec\": {}}"), later)); + "{\"metadata\": {\"creationTimestamp\": \"yesterday\"}, \"spec\": {\"expireAfter\": \"5m\"}}"), + twentyOneDays)); } /** Only the claims Auto Mode manages are its own; ending one takes Karpenter's finalizer off and keeps others. */