diff --git a/docs/services/eks.md b/docs/services/eks.md index 657f87a398..359f2dfd92 100644 --- a/docs/services/eks.md +++ b/docs/services/eks.md @@ -497,9 +497,12 @@ launches nodes on AWS. Floci checks every 10 seconds. every joined instance (see [Node metadata](#node-metadata-and-aws-outcome-alignment)), and pods are scheduled to it but don't start. - **Node.** The instance joins the cluster through its EKS bootstrap user data (a nodeadm `NodeConfig`), as any - instance does. Its kubelet flags (`--node-labels`, `--register-with-taints`) register the node with its pool's - labels and taints, so no pod the taints keep off lands there first; Floci's own Ready node for a joined instance - honours those flags too. The labels are: `karpenter.sh/nodepool`, + instance does. Its `apiServerEndpoint` is the API server's address in the cluster's VPC (the cluster node's + address in its subnet, port 6443), as an EKS private endpoint answers inside the VPC: the endpoint + `DescribeCluster` returns is the one Floci publishes for clients outside it, which the instance can't reach. Its + kubelet flags (`--node-labels`, `--register-with-taints`) register the node with its pool's labels and taints, + so no pod the taints keep off lands there first; Floci's own Ready node for a joined instance honours those flags + too. The labels are: `karpenter.sh/nodepool`, `eks.amazonaws.com/compute-type: auto`, `node.kubernetes.io/instance-type`, `karpenter.sh/capacity-type`, and the `eks.amazonaws.com/instance-*` category, generation, family, size, CPU and memory labels. The node is named by its instance ID (`i-0123456789abcdef0`), as are its `kubernetes.io/hostname` label, as AWS names Auto Mode's nodes; its diff --git a/src/main/java/io/floci/aws/services/eks/EksAutoModeCompute.java b/src/main/java/io/floci/aws/services/eks/EksAutoModeCompute.java index dd9fa5ed41..0fb946ef48 100644 --- a/src/main/java/io/floci/aws/services/eks/EksAutoModeCompute.java +++ b/src/main/java/io/floci/aws/services/eks/EksAutoModeCompute.java @@ -452,7 +452,9 @@ public class EksAutoModeCompute implements EksAutoModeCleanup { return; } String profileArn = instanceProfile(cluster, region, launch.pool().nodeClass(), role).getArn(); - String userData = nodeConfig(cluster, launch); + // The instance joins over the cluster's VPC, where the endpoint published on the host isn't reachable. + String userData = nodeConfig(cluster, launch, + clusterManager.vpcApiServerEndpoint(cluster).orElse(cluster.getEndpoint())); Reservation reservation = ec2.runInstances(region, imageId, launch.instanceType().instanceType, 1, 1, null, ids(nodeClass.path("securityGroupSelectorTerms")), subnet, UUID.randomUUID().toString(), tags(cluster, launch.pool()), userData, profileArn, null, null, 0, null, null, null, @@ -1005,9 +1007,10 @@ public class EksAutoModeCompute implements EksAutoModeCleanup { /** * nodeadm's {@code NodeConfig} for the cluster, the user data an instance joins it with, its kubelet registering * the node under the instance ID with its pool's labels and taints, so a pod the pool's taints keep off never - * lands there first. + * lands there first. Its {@code apiServerEndpoint} is the one given, the API server's address in the cluster's + * VPC when it has one. */ - static String nodeConfig(Cluster cluster, Launch launch) { + static String nodeConfig(Cluster cluster, Launch launch, String apiServerEndpoint) { StringBuilder yaml = new StringBuilder(""" apiVersion: node.eks.aws/v1alpha1 kind: NodeConfig @@ -1015,8 +1018,8 @@ public class EksAutoModeCompute implements EksAutoModeCleanup { cluster: """); yaml.append(" name: ").append(cluster.getName()).append('\n'); - if (cluster.getEndpoint() != null) { - yaml.append(" apiServerEndpoint: ").append(cluster.getEndpoint()).append('\n'); + if (apiServerEndpoint != null) { + yaml.append(" apiServerEndpoint: ").append(apiServerEndpoint).append('\n'); } if (cluster.getCertificateAuthority() != null && cluster.getCertificateAuthority().getData() != null) { yaml.append(" certificateAuthority: ").append(cluster.getCertificateAuthority().getData()).append('\n'); diff --git a/src/main/java/io/floci/aws/services/eks/EksClusterManager.java b/src/main/java/io/floci/aws/services/eks/EksClusterManager.java index abca8e7040..def820ff83 100644 --- a/src/main/java/io/floci/aws/services/eks/EksClusterManager.java +++ b/src/main/java/io/floci/aws/services/eks/EksClusterManager.java @@ -4451,6 +4451,18 @@ public class EksClusterManager ? regionResolver.getDefaultRegion() : "us-east-1"); // partition-literal: fallback for instance region } + /** + * The API server's address in the cluster's VPC, as an EKS private endpoint answers inside it: the node's address + * in its subnet, which its serving certificate names, on the API server's port. Empty for a node without one. + */ + Optional vpcApiServerEndpoint(Cluster cluster) { + String address = cluster.getNodePrivateIp(); + if (address == null || address.isBlank()) { + return Optional.empty(); + } + return Optional.of("https://" + address + ":" + K3S_API_SERVER_PORT); + } + String deriveInstanceNodeProviderId(Instance instance, String region) { String safeRegion = resolveInstanceRegion(instance, region); String az = (instance != null && instance.getPlacement() != null 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 9d78cfb707..bdaceb6ac4 100644 --- a/src/test/java/io/floci/aws/services/eks/EksAutoModeComputeTest.java +++ b/src/test/java/io/floci/aws/services/eks/EksAutoModeComputeTest.java @@ -116,6 +116,25 @@ class EksAutoModeComputeTest { anyString(), anyBoolean(), any(), any(), any(), anyBoolean(), any(), any())).thenReturn(reservation); } + /** + * The instance joins the API server at its address in the cluster's VPC, where the endpoint published on the + * host isn't reachable, as an EKS private endpoint answers inside the VPC. + */ + @Test + void anInstanceJoinsTheApiServerAtItsVpcAddress() { + Cluster cluster = cluster("prod"); + when(clusterManager.vpcApiServerEndpoint(cluster)).thenReturn(Optional.of("https://10.0.1.10:6443")); + + compute.reconcile(cluster, REGION); + + ArgumentCaptor userData = ArgumentCaptor.forClass(String.class); + verify(ec2).runInstances(eq(REGION), anyString(), anyString(), anyInt(), anyInt(), any(), anyList(), + anyString(), anyString(), anyList(), userData.capture(), anyString(), any(), any(), anyInt(), any(), + any(), any(), anyString(), anyBoolean(), any(), any(), any(), anyBoolean(), any(), any()); + assertEquals(List.of(" apiServerEndpoint: https://10.0.1.10:6443"), userData.getValue().lines() + .filter(line -> line.contains("apiServerEndpoint")).toList()); + } + @Test void anUnschedulablePodGetsAnInstanceOfItsPoolInTheNodeClassesSubnetWithItsProfileTagsAndBootstrap() { Cluster cluster = cluster("prod"); diff --git a/src/test/java/io/floci/aws/services/eks/EksClusterManagerTest.java b/src/test/java/io/floci/aws/services/eks/EksClusterManagerTest.java index 13ff8c8b75..7ba493e0a5 100644 --- a/src/test/java/io/floci/aws/services/eks/EksClusterManagerTest.java +++ b/src/test/java/io/floci/aws/services/eks/EksClusterManagerTest.java @@ -5701,6 +5701,17 @@ class EksClusterManagerTest { return c; } + /** The API server's VPC address is the node's address in its subnet, on the API server's port. */ + @Test + void theApiServersVpcEndpointIsTheNodesSubnetAddress() { + Cluster cluster = new Cluster(); + assertEquals(Optional.empty(), manager.vpcApiServerEndpoint(cluster)); + cluster.setNodePrivateIp(" "); + assertEquals(Optional.empty(), manager.vpcApiServerEndpoint(cluster)); + cluster.setNodePrivateIp("10.0.1.10"); + assertEquals(Optional.of("https://10.0.1.10:6443"), manager.vpcApiServerEndpoint(cluster)); + } + @Test void deriveInstanceNodeNameAndProviderId() { Instance inst = createInstance("i-0123456789abcdef0", "us-east-1b", null, null, null);