mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eks): tag EKS Auto Mode instances as AWS does, and find their NodeClaims by provider ID (#9737)
AWS's Auto Mode instances carry aws:eks:cluster-name and no karpenter.sh/nodeclaim tag (measured). Floci now writes aws:eks:cluster-name and no longer tags an instance with its claim's name: a pass finds each instance's claim by its status.providerID, as Karpenter matches them, else by the old tag on an instance launched before, else by the derived name, another derivation when an older instance's claim has it. A claim whose launched status write failed keeps the name its launch chose, as claims Auto Mode doesn't manage and claims a provider ID places stay reserved, and with the claims unreadable a pass launches nothing, so a launch can't take a name already in use.
This commit is contained in:
1 parent
2d3a5c045a
commit
a98a43d519
5 files changed
+224
-76
No files matched your search
@@ -487,8 +487,9 @@ launches nodes on AWS. Floci checks every 10 seconds.
|
||||
100m CPU. It is launched in a subnet of the pool's NodeClass, in the zone the pods ask for if they do, with its
|
||||
security groups, the instance
|
||||
profile `eks-<region>-<cluster>-<number>` (path `/eks/<region>/<cluster>/`) holding the NodeClass's role, and the
|
||||
tags Auto Mode sets: `eks:eks-cluster-name`, `eks:kubernetes-node-pool-name`, `eks:kubernetes-node-class-name`,
|
||||
`kubernetes.io/cluster/<cluster>: owned` and `aws:ec2:managed-launch: eks-auto`.
|
||||
tags Auto Mode sets (measured): `eks:eks-cluster-name` and `aws:eks:cluster-name`, `eks:kubernetes-node-pool-name`,
|
||||
`eks:kubernetes-node-class-name`, `kubernetes.io/cluster/<cluster>: owned` and `aws:ec2:managed-launch: eks-auto`.
|
||||
AWS's instance also carries its EC2 Fleet's and launch template's IDs, which Floci doesn't have.
|
||||
- **Image.** The EKS optimized Amazon Linux 2023 image of the cluster's Kubernetes version and the node's
|
||||
architecture, the one a managed node group of that version runs. AWS runs its own EKS Auto Mode image, which Floci
|
||||
doesn't have. When the [image catalog](ec2.md) maps that image to one whose `kubeletRegistersNode` is on, pods
|
||||
@@ -524,8 +525,9 @@ launches nodes on AWS. Floci checks every 10 seconds.
|
||||
terminated, say), by its provider ID, is deleted by the next pass, while EC2 keeps the instance, an hour as on AWS, with compute on or
|
||||
off.
|
||||
- **NodeClaim.** Each node has a `NodeClaim` (`karpenter.sh/v1`), as on AWS: named after its pool with five
|
||||
characters (`general-purpose-kw46r`) no other instance's claim has, kept in its instance's `karpenter.sh/nodeclaim`
|
||||
tag as Karpenter tags its instances, owned by its NodePool and held by the `karpenter.sh/termination` finalizer,
|
||||
characters (`general-purpose-kw46r`) no other instance's claim has, and found again by its `status.providerID`
|
||||
(`aws:///<zone>/<instance-id>`), as Karpenter matches a claim to its instance (an instance launched before by an
|
||||
older Floci keeps the `karpenter.sh/nodeclaim` tag that named its claim, which still does), owned by its NodePool and held by the `karpenter.sh/termination` finalizer,
|
||||
labelled as its node, with the pool's requirements (plus the chosen instance type, the pool and the NodeClass), its
|
||||
pods' requests, and the pool's `expireAfter` and `terminationGracePeriod` (336h and 24h0m0s when the pool doesn't
|
||||
say, as on AWS). Its status says `Launched` once the instance is up, then, once the node joins, `Registered`,
|
||||
|
||||
@@ -79,13 +79,18 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
/** The tags Auto Mode gives its instances (measured). */
|
||||
static final String CLUSTER_TAG = "eks:eks-cluster-name";
|
||||
static final String AWS_CLUSTER_TAG = "aws:eks:cluster-name";
|
||||
static final String POOL_TAG = "eks:kubernetes-node-pool-name";
|
||||
/** The taint Karpenter puts on a node it is removing, so nothing more is scheduled there. */
|
||||
static final String DISRUPTED_KEY = "karpenter.sh/disrupted";
|
||||
static final String DISRUPTED_TAINT = DISRUPTED_KEY + ":NoSchedule";
|
||||
static final String NODE_CLASS_TAG = "eks:kubernetes-node-class-name";
|
||||
/** The tag naming an instance's NodeClaim, as Karpenter tags the instances it launches. */
|
||||
static final String NODECLAIM_TAG = "karpenter.sh/nodeclaim";
|
||||
/**
|
||||
* The tag that named an instance's NodeClaim before Floci found claims by their provider ID. AWS's Auto Mode
|
||||
* instances don't carry it (measured), so Floci no longer writes it; an instance launched before still has it,
|
||||
* and it still names that instance's claim.
|
||||
*/
|
||||
static final String LEGACY_NODECLAIM_TAG = "karpenter.sh/nodeclaim";
|
||||
static final String MANAGED_LAUNCH_TAG = "aws:ec2:managed-launch";
|
||||
static final String MANAGED_LAUNCH = "eks-auto";
|
||||
/** The principal that manages Auto Mode's instances (measured). */
|
||||
@@ -181,8 +186,8 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
}
|
||||
Map<String, JsonNode> nodesByName = nodesByName(cluster);
|
||||
removeStrayNodes(cluster, region, nodesByName);
|
||||
Set<String> claimsDeleted = syncNodeClaims(cluster, region, nodesByName, podList);
|
||||
removeStoppedOfDeletedClaims(cluster, region, claimsDeleted);
|
||||
ClaimSync claims = syncNodeClaims(cluster, region, nodesByName, podList);
|
||||
removeStoppedOfDeletedClaims(cluster, region, claims.deletedInstances());
|
||||
// Read before the pass may stop, so a pool whose last node went shows none.
|
||||
JsonNode poolList = read(cluster, "nodepools");
|
||||
updatePoolStatus(cluster, region, poolList, launched, nodesByName);
|
||||
@@ -221,7 +226,7 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
neverRegistered.add(instance);
|
||||
continue;
|
||||
}
|
||||
if (pool == null || claimsDeleted.contains(claimName(instance))) {
|
||||
if (pool == null || claims.deletedInstances().contains(instance.getInstanceId())) {
|
||||
// Karpenter removes the nodes of a NodePool that was deleted, or that UpdateClusterConfig dropped,
|
||||
// and the node of a NodeClaim that was deleted, as deleting a claim is how a node is removed.
|
||||
poolGone.add(instance);
|
||||
@@ -302,6 +307,12 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
if (withoutRoom.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// Without the claims, a launch couldn't tell which names are taken: it waits for a pass that reads them.
|
||||
if (!claims.read()) {
|
||||
LOG.debugv("EKS Auto Mode compute of cluster {0} launches nothing this pass: its NodeClaims are unread",
|
||||
cluster.getName());
|
||||
return;
|
||||
}
|
||||
Map<String, JsonNode> nodeClasses = new HashMap<>();
|
||||
Set<String> subnetIds = new LinkedHashSet<>();
|
||||
for (JsonNode nodeClass : read(cluster, "nodeclasses").path("items")) {
|
||||
@@ -316,7 +327,7 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
for (Launch launch : EksAutoModeNodes.plan(List.copyOf(pools.values()), withoutRoom,
|
||||
instanceTypes.instanceTypes(), zonesByNodeClass, daemons, region, poolUsed)) {
|
||||
try {
|
||||
launch(cluster, region, launch, nodeClasses.get(launch.pool().nodeClass()), subnetZones,
|
||||
launch(cluster, region, launch, claims, nodeClasses.get(launch.pool().nodeClass()), subnetZones,
|
||||
poolItems.getOrDefault(launch.pool().name(), MAPPER.createObjectNode()));
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv("EKS Auto Mode pool {0} of cluster {1} launched no node for {2}: {3}",
|
||||
@@ -417,7 +428,7 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
}
|
||||
}
|
||||
|
||||
private void launch(Cluster cluster, String region, Launch launch, JsonNode nodeClass,
|
||||
private void launch(Cluster cluster, String region, Launch launch, ClaimSync claims, JsonNode nodeClass,
|
||||
Map<String, String> subnetZones, JsonNode poolItem) {
|
||||
if (nodeClass == null) {
|
||||
throw new IllegalStateException("its NodeClass " + launch.pool().nodeClass() + " isn't there");
|
||||
@@ -459,29 +470,17 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
LOG.infov("EKS Auto Mode pool {0} of cluster {1} launched {2} ({3}) for {4}", launch.pool().name(),
|
||||
cluster.getName(), instanceId, launch.instanceType().instanceType, podNames(launch));
|
||||
Instance instance = reservation.getInstances().getFirst();
|
||||
String claim = uniqueClaimName(cluster, region, launch.pool().name(), instanceId);
|
||||
try {
|
||||
ec2.createTags(region, List.of(instanceId), List.of(new Tag(NODECLAIM_TAG, claim)));
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv("EKS Auto Mode instance {0} of cluster {1} wasn''t tagged with its NodeClaim: {2}", instanceId,
|
||||
cluster.getName(), e.getMessage());
|
||||
}
|
||||
// A name none of the cluster's claims and instances has, as Karpenter's generateName gives one not taken: two
|
||||
// instances of a pool whose derived names are the same would otherwise share one claim, and deleting it
|
||||
// would remove both nodes. Later passes find the claim by its provider ID.
|
||||
String claim = derivedClaimName(launch.pool().name(), instanceId, claims.taken());
|
||||
claims.taken().add(claim);
|
||||
makeNodeClaim(cluster, region, launch, instance, claim, imageId, poolItem,
|
||||
EksAutoModeNodeClaims.requests(launch));
|
||||
}
|
||||
|
||||
/**
|
||||
* A NodeClaim name for a new instance of {@code pool} that none of the cluster's other instances has, as
|
||||
* Karpenter's {@code generateName} gives a name not taken: two instances of a pool whose derived names are the
|
||||
* same would otherwise share one claim, and deleting it would remove both nodes.
|
||||
*/
|
||||
private String uniqueClaimName(Cluster cluster, String region, String pool, String instanceId) {
|
||||
Set<String> taken = new HashSet<>();
|
||||
for (Instance other : instancesOf(cluster, region, "pending", "running", "stopping", "stopped")) {
|
||||
if (!instanceId.equals(other.getInstanceId())) {
|
||||
taken.add(claimName(other));
|
||||
}
|
||||
}
|
||||
/** The name derived from an instance's pool and ID, another derivation while {@code taken} holds it. */
|
||||
private static String derivedClaimName(String pool, String instanceId, Set<String> taken) {
|
||||
String name = EksAutoModeNodeClaims.name(pool, instanceId);
|
||||
for (int attempt = 1; taken.contains(name); attempt++) {
|
||||
name = EksAutoModeNodeClaims.name(pool, instanceId, attempt);
|
||||
@@ -489,6 +488,65 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* What a pass knows of the cluster's NodeClaims: whether it could read them, the names taken (the claims listed
|
||||
* and the instances' claim names, {@link #claimNames}, which a launch adds to), and the instances whose claims are
|
||||
* being deleted while they still run, whose nodes the pass removes.
|
||||
*/
|
||||
record ClaimSync(boolean read, Set<String> taken, Set<String> deletedInstances) {
|
||||
|
||||
/** The claims couldn't be read: no name is known to be free, so the pass launches nothing. */
|
||||
static ClaimSync unread() {
|
||||
return new ClaimSync(false, new HashSet<>(), Set.of());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Each instance's NodeClaim name, as Karpenter matches a claim to its instance: the claim whose
|
||||
* {@code status.providerID} names the instance ({@code aws:///<zone>/<instance-id>}); else, for an instance
|
||||
* launched before Floci found claims so, the claim its {@link #LEGACY_NODECLAIM_TAG} names; else, for an instance
|
||||
* whose claim has no status yet or is missing, the name derived from its pool and ID, another derivation while
|
||||
* that one is taken, as its launch chose: by an older instance's claim, a claim Auto Mode doesn't manage, or one
|
||||
* whose provider ID names an instance. A managed claim with no provider ID is the one its instance's launch made.
|
||||
*/
|
||||
static Map<String, String> claimNames(JsonNode claims, List<Instance> instances) {
|
||||
Map<String, String> byProvider = new HashMap<>();
|
||||
for (JsonNode claim : claims.path("items")) {
|
||||
String providerId = claim.path("status").path("providerID").asText("");
|
||||
int slash = providerId.lastIndexOf('/');
|
||||
if (slash >= 0 && slash < providerId.length() - 1) {
|
||||
byProvider.putIfAbsent(providerId.substring(slash + 1), claim.path("metadata").path("name").asText());
|
||||
}
|
||||
}
|
||||
Map<String, String> names = new HashMap<>();
|
||||
for (Instance instance : instances) {
|
||||
String name = byProvider.get(instance.getInstanceId());
|
||||
if (name == null) {
|
||||
name = tag(instance, LEGACY_NODECLAIM_TAG);
|
||||
}
|
||||
if (name != null) {
|
||||
names.put(instance.getInstanceId(), name);
|
||||
}
|
||||
}
|
||||
Set<String> taken = new HashSet<>(names.values());
|
||||
for (JsonNode claim : claims.path("items")) {
|
||||
if (!EksAutoModeNodeClaims.managed(claim) || !claim.path("status").path("providerID").asText("").isEmpty()) {
|
||||
taken.add(claim.path("metadata").path("name").asText());
|
||||
}
|
||||
}
|
||||
// Older instances first, as each launch chose its name with the older instances' names taken.
|
||||
instances.stream().filter(instance -> !names.containsKey(instance.getInstanceId()))
|
||||
.sorted(Comparator.comparing((Instance instance) -> instance.getLaunchTime(),
|
||||
Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(Instance::getInstanceId))
|
||||
.forEach(instance -> {
|
||||
String name = derivedClaimName(String.valueOf(tag(instance, POOL_TAG)), instance.getInstanceId(),
|
||||
taken);
|
||||
names.put(instance.getInstanceId(), name);
|
||||
taken.add(name);
|
||||
});
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the NodeClaim of an instance, with {@code requests} in its spec ({@code null} for none), and marks it
|
||||
* launched. One that can't be made is logged, and a later pass makes it again.
|
||||
@@ -513,22 +571,24 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
* termination ends it. Only the claims Auto Mode manages ({@code app.kubernetes.io/managed-by: eks}) are touched.
|
||||
* With the cluster's pods ({@code podList}, {@code null} for none, as with compute off), a claim that has lived its
|
||||
* {@code expireAfter} is deleted, and a pending or running instance of a pool whose claim is missing, as when
|
||||
* making it failed, gets it again. Returns the names of the claims being deleted whose
|
||||
* instance still runs, whose nodes the pass then removes.
|
||||
* making it failed, gets it again. Returns each instance's claim name, the names taken, and the instances whose
|
||||
* claims are being deleted while they still run, whose nodes the pass then removes.
|
||||
*/
|
||||
private Set<String> syncNodeClaims(Cluster cluster, String region, Map<String, JsonNode> nodesByName,
|
||||
JsonNode podList) {
|
||||
private ClaimSync syncNodeClaims(Cluster cluster, String region, Map<String, JsonNode> nodesByName,
|
||||
JsonNode podList) {
|
||||
JsonNode claims;
|
||||
try {
|
||||
claims = read(cluster, "nodeclaims");
|
||||
} catch (RuntimeException e) {
|
||||
LOG.debugv("EKS Auto Mode NodeClaims of cluster {0} unreadable: {1}", cluster.getName(), e.getMessage());
|
||||
return Set.of();
|
||||
return ClaimSync.unread();
|
||||
}
|
||||
Set<String> deleted = new HashSet<>();
|
||||
List<Instance> instances = instancesOf(cluster, region, "pending", "running", "stopping", "stopped");
|
||||
Map<String, String> names = claimNames(claims, instances);
|
||||
Map<String, Instance> live = new HashMap<>();
|
||||
for (Instance instance : instancesOf(cluster, region, "pending", "running", "stopping", "stopped")) {
|
||||
live.put(claimName(instance), instance);
|
||||
for (Instance instance : instances) {
|
||||
live.put(names.get(instance.getInstanceId()), instance);
|
||||
}
|
||||
Set<String> listed = new HashSet<>();
|
||||
for (JsonNode claim : claims.path("items")) {
|
||||
@@ -545,7 +605,7 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
}
|
||||
if (claim.path("metadata").has("deletionTimestamp")) {
|
||||
// Karpenter's finalizer holds it while its node is drained and its instance ends.
|
||||
deleted.add(name);
|
||||
deleted.add(instance.getInstanceId());
|
||||
continue;
|
||||
}
|
||||
if (podList != null && EksAutoModeNodeClaims.expired(claim, clock.instant())) {
|
||||
@@ -555,7 +615,7 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
"delete nodeclaim " + name);
|
||||
LOG.infov("EKS Auto Mode NodeClaim {0} of cluster {1} expired after {2}", name, cluster.getName(),
|
||||
claim.path("spec").path("expireAfter").asText());
|
||||
deleted.add(name);
|
||||
deleted.add(instance.getInstanceId());
|
||||
continue;
|
||||
}
|
||||
JsonNode node = nodesByName.get(clusterManager.deriveInstanceNodeName(instance, region));
|
||||
@@ -573,19 +633,21 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
}
|
||||
if (podList != null) {
|
||||
List<Instance> unclaimed = instancesOf(cluster, region, "pending", "running").stream()
|
||||
.filter(i -> !listed.contains(claimName(i))).toList();
|
||||
.filter(i -> !listed.contains(names.get(i.getInstanceId()))).toList();
|
||||
if (!unclaimed.isEmpty()) {
|
||||
remakeNodeClaims(cluster, region, unclaimed, EksAutoModeNodes.requestsByNode(podList));
|
||||
remakeNodeClaims(cluster, region, unclaimed, names, EksAutoModeNodes.requestsByNode(podList));
|
||||
}
|
||||
}
|
||||
return deleted;
|
||||
Set<String> taken = new HashSet<>(listed);
|
||||
taken.addAll(names.values());
|
||||
return new ClaimSync(true, taken, deleted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the NodeClaims of instances of pools that are still there whose claims are missing. A joined node's claim
|
||||
* requests what the pods bound to it request; one whose node hasn't joined requests nothing it can't know.
|
||||
*/
|
||||
private void remakeNodeClaims(Cluster cluster, String region, List<Instance> unclaimed,
|
||||
private void remakeNodeClaims(Cluster cluster, String region, List<Instance> unclaimed, Map<String, String> names,
|
||||
Map<String, long[]> requestsByNode) {
|
||||
Pools pools;
|
||||
try {
|
||||
@@ -602,7 +664,8 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
}
|
||||
String zone = zone(instance);
|
||||
makeNodeClaim(cluster, region, new Launch(pool, type.get(), zone,
|
||||
EksAutoModeNodes.labels(type.get(), pool, zone), List.of()), instance, claimName(instance),
|
||||
EksAutoModeNodes.labels(type.get(), pool, zone), List.of()), instance,
|
||||
names.get(instance.getInstanceId()),
|
||||
instance.getImageId(),
|
||||
pools.items().get(pool.name()),
|
||||
requestsByNode.get(clusterManager.deriveInstanceNodeName(instance, region)));
|
||||
@@ -614,12 +677,12 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
* out, go too: a stopped node serves no pods, so its node is deleted without a drain and its instance terminated.
|
||||
* The next pass then ends the claim.
|
||||
*/
|
||||
private void removeStoppedOfDeletedClaims(Cluster cluster, String region, Set<String> claimsDeleted) {
|
||||
if (claimsDeleted.isEmpty()) {
|
||||
private void removeStoppedOfDeletedClaims(Cluster cluster, String region, Set<String> deletedInstances) {
|
||||
if (deletedInstances.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (Instance instance : instancesOf(cluster, region, "stopping", "stopped")) {
|
||||
if (!claimsDeleted.contains(claimName(instance))) {
|
||||
if (!deletedInstances.contains(instance.getInstanceId())) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
@@ -634,16 +697,6 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of an instance's NodeClaim: its {@code karpenter.sh/nodeclaim} tag, or, for an instance without one
|
||||
* (its tagging failed), the name derived from its pool and instance ID.
|
||||
*/
|
||||
private static String claimName(Instance instance) {
|
||||
String tagged = tag(instance, NODECLAIM_TAG);
|
||||
return tagged != null ? tagged
|
||||
: EksAutoModeNodeClaims.name(String.valueOf(tag(instance, POOL_TAG)), instance.getInstanceId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes Karpenter's finalizer off a NodeClaim, keeping any other, and deletes it, as Karpenter does once its
|
||||
* instance is gone.
|
||||
@@ -943,7 +996,8 @@ public class EksAutoModeCompute implements EksAutoModeCleanup {
|
||||
}
|
||||
|
||||
private static List<Tag> tags(Cluster cluster, NodePool pool) {
|
||||
return List.of(new Tag(CLUSTER_TAG, cluster.getName()), new Tag(POOL_TAG, pool.name()),
|
||||
return List.of(new Tag(CLUSTER_TAG, cluster.getName()), new Tag(AWS_CLUSTER_TAG, cluster.getName()),
|
||||
new Tag(POOL_TAG, pool.name()),
|
||||
new Tag(NODE_CLASS_TAG, pool.nodeClass()), new Tag("kubernetes.io/cluster/" + cluster.getName(), "owned"),
|
||||
new Tag(MANAGED_LAUNCH_TAG, MANAGED_LAUNCH));
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ final class EksAutoModeNodeClaims {
|
||||
|
||||
/**
|
||||
* As above; {@code attempt} past 0 derives another name, for when the first is another instance's already. The
|
||||
* name chosen is kept in the instance's {@code karpenter.sh/nodeclaim} tag.
|
||||
* claim is found again by its provider ID.
|
||||
*/
|
||||
static String name(String pool, String instanceId, int attempt) {
|
||||
String seed = attempt == 0 ? instanceId : instanceId + "#" + attempt;
|
||||
|
||||
@@ -135,7 +135,8 @@ class EksAutoModeComputeTest {
|
||||
// A managed instance, hidden from list and filter calls by default, as Auto Mode's are.
|
||||
verify(ec2).setInstanceOperator(eq(REGION), eq("i-1"), argThat(operator -> operator.isManaged()
|
||||
&& "eks.amazonaws.com".equals(operator.getPrincipal()) && operator.isHiddenByDefault()));
|
||||
assertEquals(List.of("eks:eks-cluster-name=prod", "eks:kubernetes-node-pool-name=general-purpose",
|
||||
assertEquals(List.of("eks:eks-cluster-name=prod", "aws:eks:cluster-name=prod",
|
||||
"eks:kubernetes-node-pool-name=general-purpose",
|
||||
"eks:kubernetes-node-class-name=default", "kubernetes.io/cluster/prod=owned",
|
||||
"aws:ec2:managed-launch=eks-auto"),
|
||||
tags.getValue().stream().map(t -> t.getKey() + "=" + t.getValue()).toList());
|
||||
@@ -160,9 +161,9 @@ class EksAutoModeComputeTest {
|
||||
verify(iam).addRoleToInstanceProfile(profileName.getValue(), "auto-node");
|
||||
assertEquals("arn:aws:iam::000000000000:instance-profile/eks/us-east-1/prod/" + profileName.getValue(),
|
||||
profile.getValue());
|
||||
// Its NodeClaim, named in the instance's tag, then the claim's launched status.
|
||||
// Its NodeClaim, then the claim's launched status; the instance isn't tagged with it, as on AWS.
|
||||
String claim = EksAutoModeNodeClaims.name("general-purpose", "i-1");
|
||||
verifyTaggedWithClaim("i-1", claim);
|
||||
verifyNotTaggedWithClaim();
|
||||
verify(clusterManager).execInContainerForResult(eq("k3s-1"), argThat(cmd -> cmd.length == 3
|
||||
&& "sh".equals(cmd[0]) && cmd[2].contains("\"kind\":\"NodeClaim\"")
|
||||
&& cmd[2].contains("\"name\":\"" + claim + "\"")), anyInt());
|
||||
@@ -204,23 +205,109 @@ class EksAutoModeComputeTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* A launched instance whose derived claim name another instance of its pool has already gets another, so two
|
||||
* nodes never share one claim.
|
||||
* A launched instance whose derived claim name another instance's claim (found by its provider ID) has already gets
|
||||
* another, so two nodes never share one claim.
|
||||
*/
|
||||
@Test
|
||||
void aLaunchedInstanceWhoseClaimNameIsTakenGetsAnother() {
|
||||
Instance other = new Instance();
|
||||
other.setInstanceId("i-other");
|
||||
String taken = EksAutoModeNodeClaims.name("general-purpose", "i-1");
|
||||
other.setTags(List.of(new Tag(EksAutoModeCompute.POOL_TAG, "general-purpose"),
|
||||
new Tag(EksAutoModeCompute.NODECLAIM_TAG, taken)));
|
||||
other.setTags(List.of(new Tag(EksAutoModeCompute.POOL_TAG, "general-purpose")));
|
||||
instancesByState(List.of(), List.of(other), List.of());
|
||||
lists.put("nodeclaims", "{\"items\": [" + claimOf(taken, "i-other") + "]}");
|
||||
|
||||
compute.reconcile(cluster("prod"), REGION);
|
||||
|
||||
String claim = EksAutoModeNodeClaims.name("general-purpose", "i-1", 1);
|
||||
verifyTaggedWithClaim("i-1", claim);
|
||||
verifyClaimMarkedLaunched(claim);
|
||||
verifyNotTaggedWithClaim();
|
||||
}
|
||||
|
||||
/**
|
||||
* A claim whose launched status was never written keeps the name its launch chose, another derivation as a claim
|
||||
* Auto Mode doesn't manage had the first: the next pass finds it by that name, keeps it and makes no other.
|
||||
*/
|
||||
@Test
|
||||
void aClaimWhoseStatusWasNeverWrittenKeepsTheNameItsLaunchChose() {
|
||||
joinedNode("i-node", "node-1");
|
||||
aRunningPodOn("node-1", "");
|
||||
aReadyNode("node-1");
|
||||
String unmanaged = EksAutoModeNodeClaims.name("general-purpose", "i-node");
|
||||
String chosen = EksAutoModeNodeClaims.name("general-purpose", "i-node", 1);
|
||||
lists.put("nodeclaims", "{\"items\": [{\"metadata\": {\"name\": \"" + unmanaged + "\"}, \"status\": {}}, "
|
||||
+ managedClaim(chosen, "") + "]}");
|
||||
|
||||
compute.reconcile(cluster("prod"), REGION);
|
||||
|
||||
verify(clusterManager, never()).execInContainerForResult(eq("k3s-1"), argThat(cmd -> cmd.length > 3
|
||||
&& "nodeclaim".equals(cmd[2]) && chosen.equals(cmd[3]) && ("delete".equals(cmd[1])
|
||||
|| "patch".equals(cmd[1]) && !"--subresource=status".equals(cmd[4]))), anyInt());
|
||||
verify(clusterManager, never()).execInContainerForResult(eq("k3s-1"), argThat(cmd -> cmd.length > 2
|
||||
&& "sh".equals(cmd[0]) && cmd[2].contains("kubectl apply -f -")), anyInt());
|
||||
verify(clusterManager, never()).execInContainerForResult(eq("k3s-1"), argThat(cmd -> cmd.length > 3
|
||||
&& "nodeclaim".equals(cmd[2]) && unmanaged.equals(cmd[3])), anyInt());
|
||||
}
|
||||
|
||||
/** Claims that can't be read leave no name known to be free: the pass launches nothing, and the next one does. */
|
||||
@Test
|
||||
void noNodeIsLaunchedWhileTheClaimsCantBeRead() {
|
||||
lists.put("nodeclaims", "not json");
|
||||
|
||||
compute.reconcile(cluster("prod"), REGION);
|
||||
|
||||
verify(ec2, never()).runInstances(anyString(), anyString(), anyString(), anyInt(), anyInt(), any(), anyList(),
|
||||
anyString(), anyString(), anyList(), anyString(), anyString(), any(), any(), anyInt(), any(), any(),
|
||||
any(), anyString(), anyBoolean(), any(), any(), any(), anyBoolean(), any(), any());
|
||||
|
||||
lists.put("nodeclaims", "{\"items\": []}");
|
||||
compute.reconcile(cluster("prod"), REGION);
|
||||
|
||||
verifyClaimMarkedLaunched(EksAutoModeNodeClaims.name("general-purpose", "i-1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* An instance's claim is the one whose provider ID names it, as Karpenter matches them, whatever its name: it is
|
||||
* kept in step, not deleted, and no other claim is made for the instance.
|
||||
*/
|
||||
@Test
|
||||
void anInstancesClaimIsFoundByItsProviderId() {
|
||||
joinedNode("i-node", "node-1");
|
||||
aRunningPodOn("node-1", "");
|
||||
aReadyNode("node-1");
|
||||
lists.put("nodeclaims", "{\"items\": [" + claimOf("general-purpose-xyzzy", "i-node") + "]}");
|
||||
|
||||
compute.reconcile(cluster("prod"), REGION);
|
||||
|
||||
verify(clusterManager).execInContainerForResult(eq("k3s-1"), argThat(cmd -> cmd.length > 4
|
||||
&& "patch".equals(cmd[1]) && "general-purpose-xyzzy".equals(cmd[3])
|
||||
&& "--subresource=status".equals(cmd[4]) && cmd[cmd.length - 1].contains("\"nodeName\":\"node-1\"")),
|
||||
anyInt());
|
||||
verify(clusterManager, never()).execInContainerForResult("k3s-1", new String[]{"kubectl", "delete",
|
||||
"nodeclaim", "general-purpose-xyzzy", "--ignore-not-found=true", "--wait=false"}, 15);
|
||||
verify(clusterManager, never()).execInContainerForResult(eq("k3s-1"), argThat(cmd -> cmd.length > 2
|
||||
&& "sh".equals(cmd[0]) && cmd[2].contains("kubectl apply -f -")), anyInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* An instance launched before Floci found claims by provider ID still has the karpenter.sh/nodeclaim tag, which
|
||||
* still names its claim, one whose status was never written included.
|
||||
*/
|
||||
@Test
|
||||
void anInstanceLaunchedBeforeStillFindsItsClaimByItsOldTag() {
|
||||
Instance node = joinedNode("i-node", "node-1");
|
||||
node.setTags(List.of(new Tag(EksAutoModeCompute.POOL_TAG, "general-purpose"),
|
||||
new Tag(EksAutoModeCompute.LEGACY_NODECLAIM_TAG, "general-purpose-legcy")));
|
||||
aRunningPodOn("node-1", "");
|
||||
aReadyNode("node-1");
|
||||
lists.put("nodeclaims", "{\"items\": [" + managedClaim("general-purpose-legcy", "") + "]}");
|
||||
|
||||
compute.reconcile(cluster("prod"), REGION);
|
||||
|
||||
verify(clusterManager, never()).execInContainerForResult("k3s-1", new String[]{"kubectl", "delete",
|
||||
"nodeclaim", "general-purpose-legcy", "--ignore-not-found=true", "--wait=false"}, 15);
|
||||
verify(clusterManager, never()).execInContainerForResult(eq("k3s-1"), argThat(cmd -> cmd.length > 2
|
||||
&& "sh".equals(cmd[0]) && cmd[2].contains("kubectl apply -f -")), anyInt());
|
||||
}
|
||||
|
||||
/** A running instance whose claim is missing, as when making it failed, gets it again without a new launch. */
|
||||
@@ -302,11 +389,16 @@ class EksAutoModeComputeTest {
|
||||
+ "\"status\": {\"conditions\": [{\"type\": \"Ready\", \"status\": \"True\"}]}}]}");
|
||||
}
|
||||
|
||||
/** That instance {@code instanceId} was tagged with its NodeClaim's name, {@code claim}. */
|
||||
private void verifyTaggedWithClaim(String instanceId, String claim) {
|
||||
verify(ec2).createTags(eq(REGION), eq(List.of(instanceId)), argThat(tags -> tags.size() == 1
|
||||
&& EksAutoModeCompute.NODECLAIM_TAG.equals(tags.getFirst().getKey())
|
||||
&& claim.equals(tags.getFirst().getValue())));
|
||||
/** That no instance was tagged with its NodeClaim's name, as AWS's Auto Mode instances aren't. */
|
||||
private void verifyNotTaggedWithClaim() {
|
||||
verify(ec2, never()).createTags(anyString(), anyList(), argThat(tags -> tags.stream()
|
||||
.anyMatch(t -> EksAutoModeCompute.LEGACY_NODECLAIM_TAG.equals(t.getKey()))));
|
||||
}
|
||||
|
||||
/** A claim Auto Mode manages whose status names {@code instanceId} as its provider. */
|
||||
private static String claimOf(String name, String instanceId) {
|
||||
return managedClaim(name, "").replace("\"status\": {}",
|
||||
"\"status\": {\"providerID\": \"aws:///us-east-1a/" + instanceId + "\"}");
|
||||
}
|
||||
|
||||
/** That {@code claim}'s status was patched to say it was launched. */
|
||||
|
||||
+4
-4
@@ -210,11 +210,11 @@ class EksAutoModeNodesDockerNightlyIntegrationTest {
|
||||
Instance instance = launched.getFirst();
|
||||
assertEquals(subnet, instance.getSubnetId());
|
||||
Map<String, String> tags = instance.getTags().stream().collect(Collectors.toMap(Tag::getKey, Tag::getValue));
|
||||
// Tagged with its NodeClaim's name too, as Karpenter tags the instances it launches.
|
||||
// Tagged as AWS tags an Auto Mode instance (measured), with no tag naming its NodeClaim.
|
||||
String claim = EksAutoModeNodeClaims.name("general-purpose", instance.getInstanceId());
|
||||
assertEquals(Map.of("eks:eks-cluster-name", cluster.getName(), "eks:kubernetes-node-pool-name", "general-purpose",
|
||||
"eks:kubernetes-node-class-name", "default", "kubernetes.io/cluster/" + cluster.getName(), "owned",
|
||||
"aws:ec2:managed-launch", "eks-auto", EksAutoModeCompute.NODECLAIM_TAG, claim), tags);
|
||||
assertEquals(Map.of("eks:eks-cluster-name", cluster.getName(), "aws:eks:cluster-name", cluster.getName(),
|
||||
"eks:kubernetes-node-pool-name", "general-purpose", "eks:kubernetes-node-class-name", "default",
|
||||
"kubernetes.io/cluster/" + cluster.getName(), "owned", "aws:ec2:managed-launch", "eks-auto"), tags);
|
||||
InstanceProfile profile = RequestScopes.callAs(ACCOUNT, REGION, () -> iam.listInstanceProfiles(
|
||||
"/eks/" + REGION + "/" + cluster.getName() + "/")).getFirst();
|
||||
assertEquals(List.of(role), profile.getRoleNames());
|
||||
|
||||
Reference in new issue
Block a user