Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(eks): set the cluster node providerID (#4255)

Simone Primarosa766166fc3514
diff --git a/docs/services/eks.md b/docs/services/eks.md
index 06165990c1..f18ff060db 100644
--- a/docs/services/eks.md
+++ b/docs/services/eks.md
@@ -369,6 +369,18 @@ 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
+
+In real mode, cluster nodes carry a Kubernetes `spec.providerID` matching the AWS format:
+
+```text
+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.
+
+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
| Variable | Default | Description |
diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
index 6a0025278a..ff1fa74dc1 100644
--- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java
@@ -315,6 +315,15 @@ public class EksClusterManager {
List<String> serverArgs = buildServerArgs(config.services().eks().disableCni(), serviceCidr, clusterCidr);
+ try {
+ String providerId = deriveClusterNodeProviderId(cluster);
+ serverArgs.add("--kubelet-arg=provider-id=" + providerId);
+ } catch (Exception e) {
+ String clusterName = cluster != null ? cluster.getName() : "unknown";
+ LOG.warnv("EKS node provider ID injection disabled for cluster {0}: could not derive provider ID: {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);
@@ -1720,20 +1729,58 @@ public class EksClusterManager {
return "us-east-1";
}
- Instance synthesizeClusterNodeInstance(Cluster cluster, String containerIp, String region, String accountId) {
- Instance inst = new Instance();
- String safeClusterName = cluster.getName() != null ? cluster.getName() : "eks-cluster";
- String safeAccountId = accountId != null ? accountId : config.defaultAccountId();
- String safeRegion = region != null ? region : clusterRegion(cluster);
+ String deriveClusterNodeAvailabilityZone(Cluster cluster, String region) {
+ String safeRegion = (region != null && !region.isBlank()) ? region : clusterRegion(cluster);
+ return safeRegion + "a";
+ }
+
+ String deriveClusterNodeInstanceId(Cluster cluster, String region, String accountId) {
+ String safeClusterName = (cluster != null && cluster.getName() != null && !cluster.getName().isBlank())
+ ? cluster.getName()
+ : "eks-cluster";
+ String safeAccountId = (accountId != null && !accountId.isBlank())
+ ? accountId
+ : resolveClusterAccountId(cluster);
+ String safeRegion = (region != null && !region.isBlank())
+ ? region
+ : clusterRegion(cluster);
String seed = safeClusterName + "-" + safeAccountId + "-" + safeRegion;
String hex = UUID.nameUUIDFromBytes(seed.getBytes(StandardCharsets.UTF_8)).toString().replace("-", "");
- String instanceId = "i-" + (hex.length() >= 17 ? hex.substring(0, 17) : (hex + "00000000000000000").substring(0, 17));
+ return "i-" + (hex.length() >= 17 ? hex.substring(0, 17) : (hex + "00000000000000000").substring(0, 17));
+ }
+
+ String deriveClusterNodeProviderId(Cluster cluster, String region, String accountId) {
+ String az = deriveClusterNodeAvailabilityZone(cluster, region);
+ String instanceId = deriveClusterNodeInstanceId(cluster, region, accountId);
+ return "aws:///" + az + "/" + instanceId;
+ }
+
+ String deriveClusterNodeProviderId(Cluster cluster) {
+ String accountId = resolveClusterAccountId(cluster);
+ String region = clusterRegion(cluster);
+ return deriveClusterNodeProviderId(cluster, region, accountId);
+ }
+
+ Instance synthesizeClusterNodeInstance(Cluster cluster, String containerIp, String region, String accountId) {
+ Instance inst = new Instance();
+ String safeClusterName = (cluster != null && cluster.getName() != null && !cluster.getName().isBlank())
+ ? cluster.getName()
+ : "eks-cluster";
+ String safeAccountId = (accountId != null && !accountId.isBlank())
+ ? accountId
+ : resolveClusterAccountId(cluster);
+ String safeRegion = (region != null && !region.isBlank())
+ ? region
+ : clusterRegion(cluster);
+
+ String instanceId = deriveClusterNodeInstanceId(cluster, safeRegion, safeAccountId);
+ String az = deriveClusterNodeAvailabilityZone(cluster, safeRegion);
inst.setInstanceId(instanceId);
inst.setImageId("ami-eks-k3s");
inst.setInstanceType("m5.large");
- inst.setPlacement(new Placement(safeRegion + "a"));
+ inst.setPlacement(new Placement(az));
inst.setRegion(safeRegion);
inst.setState(InstanceState.running());
diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java
index 6fe9fbf781..fd2f52f8f5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java
@@ -849,6 +849,30 @@ class EksClusterManagerTest {
assertNotEquals(inst1.getInstanceId(), inst2.getInstanceId());
}
+ @Test
+ void derivesClusterNodeProviderIdMatchingExpectedFormat() {
+ Cluster cluster = new Cluster();
+ cluster.setName("prod-cluster");
+
+ String providerId = manager.deriveClusterNodeProviderId(cluster, "us-west-2", "123456789012");
+ assertEquals("aws:///us-west-2a/i-0e413a1bfb5c3cd79", providerId);
+ }
+
+ @Test
+ void derivedProviderIdAgreesWithSynthesizedClusterNodeInstance() {
+ Cluster cluster = new Cluster();
+ cluster.setName("prod-cluster");
+ cluster.setRoleArn("arn:aws:iam::123456789012:role/eks-node-role");
+
+ Instance instance = manager.synthesizeClusterNodeInstance(cluster, "172.17.0.2", "us-west-2", "123456789012");
+ String providerId = manager.deriveClusterNodeProviderId(cluster, "us-west-2", "123456789012");
+
+ String expectedProviderId = "aws:///" + instance.getPlacement().getAvailabilityZone() + "/" + instance.getInstanceId();
+ assertEquals(expectedProviderId, providerId);
+ assertEquals(instance.getInstanceId(), manager.deriveClusterNodeInstanceId(cluster, "us-west-2", "123456789012"));
+ assertEquals(instance.getPlacement().getAvailabilityZone(), manager.deriveClusterNodeAvailabilityZone(cluster, "us-west-2"));
+ }
+
@Test
void configuresMetadataProxyWhenEnabled() {
Cluster cluster = new Cluster();
@@ -1296,6 +1320,44 @@ class EksClusterManagerTest {
verify(copyCmd).withHostResource(pubFile.toString());
}
+ @Test
+ void startClusterConfiguresKubeletProviderIdArg() {
+ 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 expectedProviderId = manager.deriveClusterNodeProviderId(cluster);
+ assertEquals("aws:///us-west-2a/i-0e413a1bfb5c3cd79", expectedProviderId);
+ assertTrue(cmd.contains("--kubelet-arg=provider-id=" + expectedProviderId));
+ }
+
+ @Test
+ void startClusterContinuesWhenProviderIdDerivationFails() {
+ EksClusterManager spyManager = Mockito.spy(manager);
+ Mockito.doThrow(new RuntimeException("derivation failure"))
+ .when(spyManager).deriveClusterNodeProviderId(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=provider-id=")));
+ }
+
@Test
void signingKeyFilesWrittenWithRestrictivePermissions(@TempDir Path tempDir) throws Exception {
when(eks.irsaSigningKey()).thenReturn(true);
diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaDockerIntegrationTest.java
index 1bbc7a4a52..764b2ee0d0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksIrsaDockerIntegrationTest.java
@@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -194,6 +195,14 @@ class EksIrsaDockerIntegrationTest {
}
assertTrue(podRunning, "irsa-workload-pod must reach Running state");
+ // The pod is Running, so it was scheduled onto the node, which the kubelet
+ // registered with its provider ID: the node is guaranteed to exist here.
+ ExecResult nodeResult = execInContainerWithExitCode(containerId,
+ new String[]{"kubectl", "get", "nodes", "-o", "jsonpath={.items[0].spec.providerID}"});
+ assertEquals(0, nodeResult.exitCode(), "kubectl get nodes failed");
+ assertEquals(eksClusterManager.deriveClusterNodeProviderId(cluster), nodeResult.stdout().trim(),
+ "Node providerID must match the derived AWS provider ID");
+
// 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();