mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(eks): name cluster nodes after their EC2 instance (#4916)
* feat(eks): name cluster nodes after their EC2 instance * fix(eks): prune legacy hostname nodes on finalize and test container recreation * fix(eks): preserve active nodes when pruning legacy cluster nodes * fix(eks): delimit jsonpath node records with newline outside condition range * fix(eks): name nodes using EC2 private DNS name format and align synthetic instance * fix(eks): implement DnsRecordSource to resolve node private DNS names * fix(eks): prevent VPC forwarding rules from bypassing node private DNS resolution * fix(eks): scope node DNS system rules to matching VPC only
This commit is contained in:
1 parent
73b8c6124c
commit
548f4698c5
5 files changed
+461
-6
No files matched your search
@@ -9,8 +9,12 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsAnswer;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsClientVpcSource;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsClientVpcSource.ClientVpc;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsForwardingRule;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsForwardingRuleSource;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsRecordSource;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerExec;
|
||||
@@ -70,6 +74,7 @@ import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -85,7 +90,8 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EksClusterManager
|
||||
implements ClusterNodeInstanceProvider, VpcRouteTableListener, DnsClientVpcSource {
|
||||
implements ClusterNodeInstanceProvider, VpcRouteTableListener, DnsClientVpcSource,
|
||||
DnsRecordSource, DnsForwardingRuleSource {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(EksClusterManager.class);
|
||||
private static final int K3S_API_SERVER_PORT = 6443;
|
||||
@@ -205,6 +211,51 @@ public class EksClusterManager
|
||||
.map(ClusterNodeVpc::clientVpc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DnsAnswer> resolveIpv4(String queryName) {
|
||||
if (queryName == null || queryName.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String name = queryName.toLowerCase(Locale.ROOT);
|
||||
if (name.endsWith(".")) {
|
||||
name = name.substring(0, name.length() - 1);
|
||||
}
|
||||
for (ClusterNodeRecord record : clusterNodeInstances.values()) {
|
||||
Instance inst = record.instance();
|
||||
if (inst != null && inst.getPrivateDnsName() != null && inst.getPrivateIpAddress() != null
|
||||
&& !inst.getPrivateIpAddress().isBlank()) {
|
||||
String nodeDnsName = inst.getPrivateDnsName().toLowerCase(Locale.ROOT);
|
||||
if (name.equals(nodeDnsName)) {
|
||||
return Optional.of(DnsAnswer.records(List.of(inst.getPrivateIpAddress()), DnsAnswer.DEFAULT_TTL_SECONDS));
|
||||
}
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DnsForwardingRule> rulesFor(String accountId, String region, String vpcId) {
|
||||
if (vpcId == null || vpcId.isBlank()) {
|
||||
return List.of();
|
||||
}
|
||||
List<DnsForwardingRule> rules = new ArrayList<>();
|
||||
for (ClusterNodeRecord record : clusterNodeInstances.values()) {
|
||||
if (accountId != null && !accountId.isBlank() && !accountId.equals(record.accountId())) {
|
||||
continue;
|
||||
}
|
||||
if (region != null && !region.isBlank() && !region.equals(record.region())) {
|
||||
continue;
|
||||
}
|
||||
Instance inst = record.instance();
|
||||
if (inst != null && inst.getPrivateDnsName() != null && !inst.getPrivateDnsName().isBlank()) {
|
||||
if (vpcId.equals(inst.getVpcId())) {
|
||||
rules.add(DnsForwardingRule.system(inst.getPrivateDnsName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return List.copyOf(rules);
|
||||
}
|
||||
|
||||
private void registerClusterNodeVpc(Cluster cluster, String accountId, String region,
|
||||
Set<String> addresses) {
|
||||
String clusterKey = clusterResourceName(cluster);
|
||||
@@ -444,6 +495,15 @@ public class EksClusterManager
|
||||
nodeLimits.addKubeletArgs(serverArgs);
|
||||
}
|
||||
|
||||
try {
|
||||
String nodeName = deriveClusterNodePrivateDnsName(cluster);
|
||||
serverArgs.add("--node-name=" + nodeName);
|
||||
} catch (Exception e) {
|
||||
String clusterName = cluster != null ? cluster.getName() : "unknown";
|
||||
LOG.warnv("EKS node name injection disabled for cluster {0}: could not derive node name: {1}",
|
||||
clusterName, e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
String providerId = deriveClusterNodeProviderId(cluster);
|
||||
serverArgs.add("--kubelet-arg=provider-id=" + providerId);
|
||||
@@ -813,6 +873,8 @@ public class EksClusterManager
|
||||
cluster.setCertificateAuthority(new CertificateAuthority(caData.trim()));
|
||||
}
|
||||
|
||||
pruneLegacyClusterNodes(cluster, containerId);
|
||||
|
||||
LOG.infov("Finalized EKS cluster {0} with CA data extracted", cluster.getName());
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Could not extract kubeconfig for cluster {0}: {1}",
|
||||
@@ -820,6 +882,31 @@ public class EksClusterManager
|
||||
}
|
||||
}
|
||||
|
||||
private void pruneLegacyClusterNodes(Cluster cluster, String containerId) {
|
||||
try {
|
||||
String expectedNodeName = deriveClusterNodePrivateDnsName(cluster);
|
||||
ContainerExec.Result nodeResult = execInContainerForResult(containerId,
|
||||
new String[]{"kubectl", "get", "nodes", "-o",
|
||||
"jsonpath={range .items[*]}{.metadata.name}{\" \"}{range .status.conditions[?(@.type==\"Ready\")]}{.status}{end}{\"\\n\"}{end}"}, 10);
|
||||
if (nodeResult.exitCode() == 0 && nodeResult.stdout() != null && !nodeResult.stdout().isBlank()) {
|
||||
for (String line : nodeResult.stdout().split("\\r?\\n")) {
|
||||
String[] parts = line.trim().split("\\s+");
|
||||
if (parts.length >= 1 && !parts[0].isBlank()) {
|
||||
String node = parts[0];
|
||||
String readyStatus = parts.length > 1 ? parts[1] : "";
|
||||
if (!node.equals(expectedNodeName) && !"True".equalsIgnoreCase(readyStatus)) {
|
||||
execInContainerForResult(containerId,
|
||||
new String[]{"kubectl", "delete", "node", node}, 10);
|
||||
LOG.infov("Removed stale legacy node {0} from EKS cluster {1}", node, cluster.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Could not prune legacy nodes for cluster {0}: {1}", cluster.getName(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops and removes the k3s container for the given cluster. The k3s data volume follows the
|
||||
* storage prune policy ({@link ContainerStorageHelper#removeNamedVolume}): it is only removed
|
||||
@@ -2327,6 +2414,12 @@ public class EksClusterManager
|
||||
return "i-" + (hex.length() >= 17 ? hex.substring(0, 17) : (hex + "00000000000000000").substring(0, 17));
|
||||
}
|
||||
|
||||
String deriveClusterNodeInstanceId(Cluster cluster) {
|
||||
String accountId = resolveClusterAccountId(cluster);
|
||||
String region = clusterRegion(cluster);
|
||||
return deriveClusterNodeInstanceId(cluster, region, accountId);
|
||||
}
|
||||
|
||||
String deriveClusterNodeProviderId(Cluster cluster, String region, String accountId) {
|
||||
String az = deriveClusterNodeAvailabilityZone(cluster, region);
|
||||
String instanceId = deriveClusterNodeInstanceId(cluster, region, accountId);
|
||||
@@ -2339,6 +2432,27 @@ public class EksClusterManager
|
||||
return deriveClusterNodeProviderId(cluster, region, accountId);
|
||||
}
|
||||
|
||||
String deriveClusterNodePrivateDnsDomain(String region) {
|
||||
String safeRegion = (region != null && !region.isBlank()) ? region : "us-east-1"; // partition-literal: fallback for domain derivation
|
||||
return "us-east-1".equals(safeRegion) // partition-literal: ec2.internal is us-east-1's own search domain
|
||||
? "ec2.internal"
|
||||
: safeRegion + ".compute.internal";
|
||||
}
|
||||
|
||||
String deriveClusterNodePrivateDnsName(Cluster cluster, String region, String accountId) {
|
||||
String safeRegion = (region != null && !region.isBlank())
|
||||
? region
|
||||
: clusterRegion(cluster);
|
||||
String instanceId = deriveClusterNodeInstanceId(cluster, safeRegion, accountId);
|
||||
return instanceId + "." + deriveClusterNodePrivateDnsDomain(safeRegion);
|
||||
}
|
||||
|
||||
String deriveClusterNodePrivateDnsName(Cluster cluster) {
|
||||
String accountId = resolveClusterAccountId(cluster);
|
||||
String region = clusterRegion(cluster);
|
||||
return deriveClusterNodePrivateDnsName(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())
|
||||
@@ -2353,6 +2467,7 @@ public class EksClusterManager
|
||||
|
||||
String instanceId = deriveClusterNodeInstanceId(cluster, safeRegion, safeAccountId);
|
||||
String az = deriveClusterNodeAvailabilityZone(cluster, safeRegion);
|
||||
String privateDnsName = deriveClusterNodePrivateDnsName(cluster, safeRegion, safeAccountId);
|
||||
|
||||
inst.setInstanceId(instanceId);
|
||||
inst.setImageId("ami-eks-k3s");
|
||||
@@ -2363,7 +2478,7 @@ public class EksClusterManager
|
||||
|
||||
String ip = (containerIp != null && !containerIp.isBlank()) ? containerIp : "10.0.0.1";
|
||||
inst.setPrivateIpAddress(ip);
|
||||
inst.setPrivateDnsName("ip-" + ip.replace('.', '-') + "." + safeRegion + ".compute.internal");
|
||||
inst.setPrivateDnsName(privateDnsName);
|
||||
|
||||
// AWS EKS nodes receive credentials from a node IAM role through an EC2 instance profile,
|
||||
// never from the cluster control-plane role (cluster.getRoleArn()). Synthesize a distinct
|
||||
|
||||
Reference in new issue
Block a user