feat(eks): label cluster nodes with their topology zone and region (#4308)

This commit is contained in:
Simone Primarosa authored and GitHub committed 2026-09-24 15:06:57 -04:00
1 parent 06ec4b563a
commit 6de5e40f54
4 files changed
+123 -1

No files matched your search

+8 -1
View File
@@ -369,7 +369,7 @@ A restored cluster reports `CREATING` until its API server answers again, then r
back (for example Docker is unavailable), the cluster is marked `FAILED` instead of appearing
`ACTIVE` while unreachable.
#### Cluster node provider ID
#### Cluster node provider ID and topology labels
In real mode, cluster nodes carry a Kubernetes `spec.providerID` matching the AWS format:
@@ -379,6 +379,13 @@ aws:///<availability-zone>/<instance-id>
For example, `aws:///us-east-1a/i-0123456789abcdef0`. Floci derives this identifier deterministically before container startup and passes `--kubelet-arg=provider-id=<providerId>` to k3s.
Cluster nodes also carry standard Kubernetes topology labels:
- `topology.kubernetes.io/zone`
- `topology.kubernetes.io/region`
Floci derives the availability zone from the cluster region (for example, `<region>a` for `us-east-1`, yielding `us-east-1a`), rather than from node group subnets as real EKS does. The zone matches the availability zone in the node's `spec.providerID` and synthetic EC2 node instance. These labels enable topology-aware scheduling and allow controllers such as the AWS EBS CSI driver to discover the node's availability zone.
The derived availability zone and instance ID match the synthetic EC2 node instance created for link-local IMDS, ensuring consistent identity across node metadata and kubelet registration. The provider ID enables controllers that reconcile nodes against EC2 (such as CSI drivers) to extract the instance ID. Note that nothing yet resolves that synthetic instance through the EC2 API.
## Configuration
@@ -328,6 +328,17 @@ public class EksClusterManager implements ClusterNodeInstanceProvider {
clusterName, e.getMessage());
}
try {
String region = clusterRegion(cluster);
String az = deriveClusterNodeAvailabilityZone(cluster, region);
serverArgs.add("--kubelet-arg=node-labels=topology.kubernetes.io/zone=" + az
+ ",topology.kubernetes.io/region=" + region);
} catch (Exception e) {
String clusterName = cluster != null ? cluster.getName() : "unknown";
LOG.warnv("EKS node topology labels injection disabled for cluster {0}: could not derive topology labels: {1}",
clusterName, e.getMessage());
}
// The account label comes from the cluster record when set (restore runs with no request
// context); regionResolver is the fallback for the create path.
String labelAccountId = resolveClusterAccountId(cluster);
@@ -1771,6 +1782,10 @@ public class EksClusterManager implements ClusterNodeInstanceProvider {
return safeRegion + "a";
}
String deriveClusterNodeAvailabilityZone(Cluster cluster) {
return deriveClusterNodeAvailabilityZone(cluster, clusterRegion(cluster));
}
String deriveClusterNodeInstanceId(Cluster cluster, String region, String accountId) {
String safeClusterName = (cluster != null && cluster.getName() != null && !cluster.getName().isBlank())
? cluster.getName()
@@ -874,6 +874,13 @@ class EksClusterManagerTest {
assertEquals(instance.getPlacement().getAvailabilityZone(), manager.deriveClusterNodeAvailabilityZone(cluster, "us-west-2"));
}
@Test
void deriveClusterNodeAvailabilityZoneDerivesFromClusterArn() {
Cluster cluster = new Cluster();
cluster.setArn("arn:aws:eks:ap-southeast-1:123456789012:cluster/test-cluster");
assertEquals("ap-southeast-1a", manager.deriveClusterNodeAvailabilityZone(cluster));
}
@Test
void configuresMetadataProxyWhenEnabled() {
Cluster cluster = new Cluster();
@@ -1407,6 +1414,85 @@ class EksClusterManagerTest {
assertFalse(cmd.stream().anyMatch(arg -> arg.startsWith("--kubelet-arg=provider-id=")));
}
@Test
void startClusterConfiguresKubeletTopologyLabelsArg() {
Cluster cluster = new Cluster();
cluster.setName("prod-cluster");
cluster.setAccountId("123456789012");
cluster.setArn("arn:aws:eks:us-west-2:123456789012:cluster/prod-cluster");
manager.startCluster(cluster);
@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
verify(builder).withCmd(cmdCaptor.capture());
List<String> cmd = cmdCaptor.getValue();
String expectedAz = manager.deriveClusterNodeAvailabilityZone(cluster, "us-west-2");
assertEquals("us-west-2a", expectedAz);
assertTrue(cmd.contains("--kubelet-arg=node-labels=topology.kubernetes.io/zone=" + expectedAz
+ ",topology.kubernetes.io/region=us-west-2"));
}
@Test
void topologyLabelZoneMatchesProviderIdZone() {
Cluster cluster = new Cluster();
cluster.setName("prod-cluster");
cluster.setAccountId("123456789012");
cluster.setArn("arn:aws:eks:eu-central-1:123456789012:cluster/prod-cluster");
manager.startCluster(cluster);
@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
verify(builder).withCmd(cmdCaptor.capture());
List<String> cmd = cmdCaptor.getValue();
String providerIdArg = cmd.stream()
.filter(arg -> arg.startsWith("--kubelet-arg=provider-id="))
.findFirst()
.orElseThrow(() -> new AssertionError("provider-id arg missing"));
String providerId = providerIdArg.substring("--kubelet-arg=provider-id=".length());
String[] parts = providerId.split("/");
assertTrue(parts.length >= 4, "Provider ID should have format aws:///<zone>/<instance-id>");
String providerZone = parts[3];
String nodeLabelsArg = cmd.stream()
.filter(arg -> arg.startsWith("--kubelet-arg=node-labels="))
.findFirst()
.orElseThrow(() -> new AssertionError("node-labels arg missing"));
String labelZone = null;
String labelsPart = nodeLabelsArg.substring("--kubelet-arg=node-labels=".length());
for (String pair : labelsPart.split(",")) {
String[] kv = pair.split("=", 2);
if ("topology.kubernetes.io/zone".equals(kv[0])) {
labelZone = kv[1];
break;
}
}
assertNotNull(labelZone, "topology.kubernetes.io/zone must be present in node-labels arg");
assertEquals(providerZone, labelZone);
}
@Test
void startClusterContinuesWhenTopologyLabelsDerivationFails() {
EksClusterManager spyManager = Mockito.spy(manager);
Mockito.doThrow(new RuntimeException("derivation failure"))
.when(spyManager).deriveClusterNodeAvailabilityZone(any(), any());
Cluster cluster = new Cluster();
cluster.setName("fail-cluster");
assertDoesNotThrow(() -> spyManager.startCluster(cluster));
@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> cmdCaptor = ArgumentCaptor.forClass(List.class);
verify(builder).withCmd(cmdCaptor.capture());
List<String> cmd = cmdCaptor.getValue();
assertFalse(cmd.stream().anyMatch(arg -> arg.startsWith("--kubelet-arg=node-labels=")));
}
@Test
void signingKeyFilesWrittenWithRestrictivePermissions(@TempDir Path tempDir) throws Exception {
when(eks.irsaSigningKey()).thenReturn(true);
@@ -203,6 +203,20 @@ class EksIrsaDockerIntegrationTest {
assertEquals(eksClusterManager.deriveClusterNodeProviderId(cluster), nodeResult.stdout().trim(),
"Node providerID must match the derived AWS provider ID");
ExecResult zoneResult = execInContainerWithExitCode(containerId,
new String[]{"kubectl", "get", "nodes", "-o",
"jsonpath={.items[0].metadata.labels.topology\\.kubernetes\\.io/zone}"});
assertEquals(0, zoneResult.exitCode(), "kubectl get nodes failed");
assertEquals(eksClusterManager.deriveClusterNodeAvailabilityZone(cluster), zoneResult.stdout().trim(),
"Node topology zone label must match the derived availability zone");
ExecResult regionResult = execInContainerWithExitCode(containerId,
new String[]{"kubectl", "get", "nodes", "-o",
"jsonpath={.items[0].metadata.labels.topology\\.kubernetes\\.io/region}"});
assertEquals(0, regionResult.exitCode(), "kubectl get nodes failed");
assertEquals(eksClusterManager.clusterRegion(cluster), regionResult.stdout().trim(),
"Node topology region label must match the cluster region");
// Extract the projected token from the pod volume
String token = execInContainer(containerId,
new String[]{"kubectl", "exec", "irsa-workload-pod", "--", "cat", "/var/run/secrets/tokens/token"}).trim();