mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(eks): apply nodegroup labels, taints and capacity type to the node (#5063)
* feat(eks): apply nodegroup labels, taints and capacity type to the node * fix(eks): address review feedback on nodegroup metadata injection * fix(docs): sync IAM operation count in service matrix * fix(eks): use single string literals without concatenation in log messages * chore(eks): resolve phantom nodegroup review finding * docs(eks): document CAPACITY_BLOCK capacity type for nodegroups
This commit is contained in:
1 parent
c6304a26c8
commit
383c32443d
7 files changed
+670
-9
No files matched your search
@@ -61,7 +61,8 @@ public final class EksClusterArgs {
|
||||
"nodelabels",
|
||||
"systemreserved",
|
||||
"kubereserved",
|
||||
"evictionhard"
|
||||
"evictionhard",
|
||||
"registerwithtaints"
|
||||
);
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
|
||||
import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
@@ -28,6 +29,9 @@ import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.PortAllocator;
|
||||
import io.github.hectorvent.floci.core.common.docker.RetryingTarCopier;
|
||||
import io.github.hectorvent.floci.core.common.docker.UserDataPipeline;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ec2.ClusterNodeInstanceProvider;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2InstanceTypeCatalog;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2InstanceTypeCatalog.CatalogInstanceType;
|
||||
@@ -37,6 +41,7 @@ import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.VpcRouteTableListener;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.InstanceState;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Placement;
|
||||
import io.github.hectorvent.floci.services.ec2.model.RouteTable;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
@@ -46,6 +51,8 @@ import io.github.hectorvent.floci.services.eks.model.Cluster;
|
||||
import io.github.hectorvent.floci.services.eks.model.ClusterIdentity;
|
||||
import io.github.hectorvent.floci.services.eks.model.ClusterOidcKey;
|
||||
import io.github.hectorvent.floci.services.eks.model.LogSetup;
|
||||
import io.github.hectorvent.floci.services.eks.model.Nodegroup;
|
||||
import io.github.hectorvent.floci.services.eks.model.NodegroupStatus;
|
||||
import io.github.hectorvent.floci.services.eks.model.OidcIdentity;
|
||||
import io.github.hectorvent.floci.services.eks.model.ResourcesVpcConfig;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -73,6 +80,7 @@ import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -83,6 +91,7 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -167,6 +176,7 @@ public class EksClusterManager
|
||||
@Inject
|
||||
jakarta.enterprise.inject.Instance<Ec2Service> ec2ServiceInstance;
|
||||
private Ec2Service ec2Service;
|
||||
private final StorageBackend<String, Nodegroup> nodeGroupStorage;
|
||||
private final Map<String, Set<String>> programmedClusterRoutes = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, Object> clusterRouteLocks = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -365,7 +375,6 @@ public class EksClusterManager
|
||||
null, logStreamer);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public EksClusterManager(ContainerBuilder containerBuilder,
|
||||
ContainerLifecycleManager lifecycleManager,
|
||||
ContainerDetector containerDetector,
|
||||
@@ -378,6 +387,24 @@ public class EksClusterManager
|
||||
EksOidcService oidcService,
|
||||
FlociCertificateAuthority certificateAuthority,
|
||||
ContainerLogStreamer logStreamer) {
|
||||
this(containerBuilder, lifecycleManager, containerDetector, portAllocator,
|
||||
dockerHostResolver, ecrRegistryManager, config, regionResolver, metadataServer,
|
||||
oidcService, certificateAuthority, logStreamer, (StorageBackend<String, Nodegroup>) null);
|
||||
}
|
||||
|
||||
public EksClusterManager(ContainerBuilder containerBuilder,
|
||||
ContainerLifecycleManager lifecycleManager,
|
||||
ContainerDetector containerDetector,
|
||||
PortAllocator portAllocator,
|
||||
DockerHostResolver dockerHostResolver,
|
||||
EcrRegistryManager ecrRegistryManager,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver,
|
||||
Ec2MetadataServer metadataServer,
|
||||
EksOidcService oidcService,
|
||||
FlociCertificateAuthority certificateAuthority,
|
||||
ContainerLogStreamer logStreamer,
|
||||
StorageBackend<String, Nodegroup> nodeGroupStorage) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.containerDetector = containerDetector;
|
||||
@@ -390,6 +417,30 @@ public class EksClusterManager
|
||||
this.oidcService = oidcService;
|
||||
this.certificateAuthority = certificateAuthority;
|
||||
this.logStreamer = logStreamer;
|
||||
this.nodeGroupStorage = nodeGroupStorage;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public EksClusterManager(ContainerBuilder containerBuilder,
|
||||
ContainerLifecycleManager lifecycleManager,
|
||||
ContainerDetector containerDetector,
|
||||
PortAllocator portAllocator,
|
||||
DockerHostResolver dockerHostResolver,
|
||||
EcrRegistryManager ecrRegistryManager,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver,
|
||||
Ec2MetadataServer metadataServer,
|
||||
EksOidcService oidcService,
|
||||
FlociCertificateAuthority certificateAuthority,
|
||||
ContainerLogStreamer logStreamer,
|
||||
StorageFactory storageFactory) {
|
||||
this(containerBuilder, lifecycleManager, containerDetector, portAllocator,
|
||||
dockerHostResolver, ecrRegistryManager, config, regionResolver, metadataServer,
|
||||
oidcService, certificateAuthority, logStreamer,
|
||||
storageFactory != null
|
||||
? storageFactory.create("eks", "eks-nodegroups.json",
|
||||
new TypeReference<Map<String, Nodegroup>>() {})
|
||||
: null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -530,8 +581,15 @@ public class EksClusterManager
|
||||
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);
|
||||
Nodegroup nodegroup = selectFirstNodegroup(cluster);
|
||||
String nodeLabels = buildNodeLabels(cluster, nodegroup, az, region);
|
||||
if (nodeLabels != null && !nodeLabels.isBlank()) {
|
||||
serverArgs.add("--kubelet-arg=node-labels=" + nodeLabels);
|
||||
}
|
||||
String taints = buildRegisterWithTaints(nodegroup);
|
||||
if (taints != null && !taints.isBlank()) {
|
||||
serverArgs.add("--kubelet-arg=register-with-taints=" + taints);
|
||||
}
|
||||
} 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}",
|
||||
@@ -2434,6 +2492,202 @@ public class EksClusterManager
|
||||
return "us-east-1"; // partition-literal: unreachable under CDI, where the resolver or config answers
|
||||
}
|
||||
|
||||
List<Nodegroup> listActiveNodegroups(Cluster cluster) {
|
||||
if (nodeGroupStorage == null || cluster == null || cluster.getName() == null) {
|
||||
return List.of();
|
||||
}
|
||||
String prefix = cluster.getName() + "/";
|
||||
String accountId = resolveClusterAccountId(cluster);
|
||||
List<Nodegroup> groups;
|
||||
if (nodeGroupStorage instanceof AccountAwareStorageBackend<Nodegroup> aware) {
|
||||
groups = aware.scanAllAccountEntries(key -> key.startsWith(prefix)).stream()
|
||||
.filter(entry -> accountId == null || accountId.equals(entry.accountId()))
|
||||
.map(AccountAwareStorageBackend.AccountEntry::value)
|
||||
.toList();
|
||||
} else {
|
||||
groups = nodeGroupStorage.scan(key -> key.startsWith(prefix));
|
||||
}
|
||||
return groups.stream()
|
||||
.filter(g -> g.getStatus() == null || g.getStatus() == NodegroupStatus.ACTIVE)
|
||||
.sorted(Comparator.comparing(Nodegroup::getCreatedAt,
|
||||
Comparator.nullsLast(Comparator.naturalOrder()))
|
||||
.thenComparing(Nodegroup::getNodegroupName,
|
||||
Comparator.nullsLast(Comparator.naturalOrder())))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Nodegroup selectFirstNodegroup(Cluster cluster) {
|
||||
List<Nodegroup> active = listActiveNodegroups(cluster);
|
||||
if (active.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Nodegroup first = active.getFirst();
|
||||
if (active.size() > 1) {
|
||||
String clusterName = cluster != null ? cluster.getName() : "unknown";
|
||||
for (int i = 1; i < active.size(); i++) {
|
||||
Nodegroup later = active.get(i);
|
||||
LOG.warnv(
|
||||
"EKS cluster {0} has one shared node; nodegroup {1} metadata (labels/taints) is not applied to the node (already represented by nodegroup {2})",
|
||||
clusterName, later.getNodegroupName(), first.getNodegroupName());
|
||||
}
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
String buildNodeLabels(Cluster cluster, Nodegroup nodegroup, String az, String region) {
|
||||
Map<String, String> labels = new LinkedHashMap<>();
|
||||
if (az != null && !az.isBlank()) {
|
||||
labels.put("topology.kubernetes.io/zone", az);
|
||||
}
|
||||
if (region != null && !region.isBlank()) {
|
||||
labels.put("topology.kubernetes.io/region", region);
|
||||
}
|
||||
|
||||
if (nodegroup != null) {
|
||||
String capacityType = nodegroup.getCapacityType() != null && !nodegroup.getCapacityType().isBlank()
|
||||
? nodegroup.getCapacityType() : "ON_DEMAND";
|
||||
labels.put("eks.amazonaws.com/capacityType", capacityType); // partition-literal: Kubernetes node label key is partition-invariant
|
||||
|
||||
if (nodegroup.getNodegroupName() != null && !nodegroup.getNodegroupName().isBlank()) {
|
||||
labels.put("eks.amazonaws.com/nodegroup", nodegroup.getNodegroupName()); // partition-literal: Kubernetes node label key is partition-invariant
|
||||
}
|
||||
|
||||
String imageId = resolveNodegroupImageId(nodegroup);
|
||||
if (imageId != null && !imageId.isBlank()) {
|
||||
labels.put("eks.amazonaws.com/nodegroup-image", imageId); // partition-literal: Kubernetes node label key is partition-invariant
|
||||
}
|
||||
|
||||
String instanceType = resolveNodegroupInstanceType(cluster, nodegroup);
|
||||
if (instanceType != null && !instanceType.isBlank()) {
|
||||
labels.put("node.kubernetes.io/instance-type", instanceType);
|
||||
}
|
||||
|
||||
if (nodegroup.getLabels() != null) {
|
||||
for (Map.Entry<String, String> entry : nodegroup.getLabels().entrySet()) {
|
||||
String k = entry.getKey();
|
||||
String v = entry.getValue();
|
||||
if (k != null && !k.isBlank() && v != null && !isReservedNodeLabelKey(k)) {
|
||||
labels.put(k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (labels.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Map.Entry<String, String> entry : labels.entrySet()) {
|
||||
if (!sb.isEmpty()) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(entry.getKey()).append("=").append(entry.getValue());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static boolean isReservedNodeLabelKey(String key) {
|
||||
return key.startsWith("topology.kubernetes.io/")
|
||||
|| key.startsWith("eks.amazonaws.com/") // partition-literal: Kubernetes label prefix is partition-invariant
|
||||
|| "node.kubernetes.io/instance-type".equals(key);
|
||||
}
|
||||
|
||||
private String resolveNodegroupInstanceType(Cluster cluster, Nodegroup nodegroup) {
|
||||
if (nodegroup != null && nodegroup.getInstanceTypes() != null && !nodegroup.getInstanceTypes().isEmpty()) {
|
||||
String first = nodegroup.getInstanceTypes().getFirst();
|
||||
if (first != null && !first.isBlank()) {
|
||||
return selectNodeInstanceType(first.trim());
|
||||
}
|
||||
}
|
||||
return nodeInstanceType(cluster);
|
||||
}
|
||||
|
||||
private String resolveNodegroupImageId(Nodegroup nodegroup) {
|
||||
if (nodegroup != null) {
|
||||
if (nodegroup.getReleaseVersion() != null && !nodegroup.getReleaseVersion().isBlank()) {
|
||||
return nodegroup.getReleaseVersion().trim();
|
||||
}
|
||||
if (nodegroup.getLaunchTemplate() instanceof Map<?, ?> map) {
|
||||
Object directImage = map.get("imageId");
|
||||
if (directImage != null && !directImage.toString().isBlank()) {
|
||||
return directImage.toString().trim();
|
||||
}
|
||||
Ec2Service ec2 = ec2Service();
|
||||
if (ec2 != null) {
|
||||
String id = map.get("id") != null ? map.get("id").toString().trim() : null;
|
||||
String name = map.get("name") != null ? map.get("name").toString().trim() : null;
|
||||
String version = map.get("version") != null ? map.get("version").toString().trim() : null;
|
||||
if ((id != null && !id.isBlank()) || (name != null && !name.isBlank())) {
|
||||
try {
|
||||
LaunchTemplateData data = ec2.resolveLaunchTemplateData(null, id, name, version);
|
||||
if (data != null && data.getImageId() != null && !data.getImageId().isBlank()) {
|
||||
return data.getImageId().trim();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// Swallowing is safe because falling back to default image id is standard
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "ami-eks-k3s";
|
||||
}
|
||||
|
||||
String buildRegisterWithTaints(Nodegroup nodegroup) {
|
||||
if (nodegroup == null || nodegroup.getTaints() == null || nodegroup.getTaints().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
List<String> taints = new ArrayList<>();
|
||||
for (Object taintObj : nodegroup.getTaints()) {
|
||||
String formatted = formatTaint(taintObj);
|
||||
if (formatted != null && !formatted.isBlank()) {
|
||||
taints.add(formatted);
|
||||
}
|
||||
}
|
||||
if (taints.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return String.join(",", taints);
|
||||
}
|
||||
|
||||
static String formatTaint(Object taintObj) {
|
||||
if (taintObj == null) {
|
||||
return null;
|
||||
}
|
||||
if (taintObj instanceof Map<?, ?> map) {
|
||||
Object k = map.get("key");
|
||||
if (k == null || k.toString().isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String key = k.toString().trim();
|
||||
Object v = map.get("value");
|
||||
String val = (v != null) ? v.toString().trim() : null;
|
||||
Object eff = map.get("effect");
|
||||
String effect = mapTaintEffect(eff != null ? eff.toString() : null);
|
||||
if (val != null && !val.isEmpty()) {
|
||||
return key + "=" + val + ":" + effect;
|
||||
} else {
|
||||
return key + ":" + effect;
|
||||
}
|
||||
} else if (taintObj instanceof String str && !str.isBlank()) {
|
||||
return str.trim();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static String mapTaintEffect(String awsEffect) {
|
||||
if (awsEffect == null || awsEffect.isBlank()) {
|
||||
return "NoSchedule";
|
||||
}
|
||||
String normalized = awsEffect.trim().toUpperCase(Locale.ROOT);
|
||||
return switch (normalized) {
|
||||
case "NO_SCHEDULE", "NOSCHEDULE" -> "NoSchedule";
|
||||
case "NO_EXECUTE", "NOEXECUTE" -> "NoExecute";
|
||||
case "PREFER_NO_SCHEDULE", "PREFERNOSCHEDULE" -> "PreferNoSchedule";
|
||||
default -> awsEffect.trim();
|
||||
};
|
||||
}
|
||||
|
||||
String deriveClusterNodeAvailabilityZone(Cluster cluster, String region) {
|
||||
String safeRegion = (region != null && !region.isBlank()) ? region : clusterRegion(cluster);
|
||||
return safeRegion + "a";
|
||||
|
||||
@@ -259,6 +259,16 @@ public class EksService implements TagHandler, ResourceProvider {
|
||||
.thenComparing(Nodegroup::getNodegroupName));
|
||||
}
|
||||
|
||||
List<Nodegroup> listNodeGroupsForCluster(String clusterName, String accountId) {
|
||||
String prefix = clusterName + "/";
|
||||
if (nodeGroupStorage instanceof AccountAwareStorageBackend<Nodegroup> aware) {
|
||||
return aware.scanAllAccountEntries(key -> key.startsWith(prefix)).stream()
|
||||
.filter(entry -> accountId == null || accountId.equals(entry.accountId()))
|
||||
.map(AccountAwareStorageBackend.AccountEntry::value).toList();
|
||||
}
|
||||
return nodeGroupStorage.scan(key -> key.startsWith(prefix));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives clusters persisted before IRSA support an OIDC issuer and signing key. Without this,
|
||||
* a cluster restored from {@code eks-clusters.json} would report no
|
||||
@@ -790,6 +800,10 @@ public class EksService implements TagHandler, ResourceProvider {
|
||||
boolean firstGroup = firstNodeGroup(clusterName, accountId).isEmpty() && pendingFirst == null;
|
||||
if (firstGroup && !config.services().eks().mock() && clusterManager != null) {
|
||||
applyFirstNodeGroupCapacity(clusterName, nodegroupName, currentCluster, nodeGroup);
|
||||
if (currentCluster.getContainerId() != null && currentCluster.getStatus() == ClusterStatus.ACTIVE) {
|
||||
LOG.infov("EKS cluster {0} is already running; nodegroup {1} node labels and taints are not applied to the running node",
|
||||
clusterName, nodegroupName);
|
||||
}
|
||||
} else if (!firstGroup && pendingFirst == null
|
||||
&& !config.services().eks().mock() && clusterManager != null) {
|
||||
if (currentCluster.getNodeInstanceType() == null) {
|
||||
@@ -802,6 +816,13 @@ public class EksService implements TagHandler, ResourceProvider {
|
||||
LOG.warnv("EKS cluster {0} has one shared node; nodegroup {1} cannot change its capacity from {2}",
|
||||
clusterName, nodegroupName, currentCluster.getNodeInstanceType());
|
||||
}
|
||||
firstNodeGroup(clusterName, accountId).ifPresent(first -> {
|
||||
if (!nodegroupName.equals(first.getNodegroupName())) {
|
||||
LOG.warnv(
|
||||
"EKS cluster {0} has one shared node; nodegroup {1} metadata (labels/taints) is not applied to the node (already represented by nodegroup {2})",
|
||||
clusterName, nodegroupName, first.getNodegroupName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (nodeGroup.getStatus() == NodegroupStatus.ACTIVE && (hasUserData || pendingFirst != null)) {
|
||||
|
||||
Reference in new issue
Block a user