mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 08:31:53 -05:00
fix(eks): leave an existing cluster's node unlabelled when EKS Auto Mode turns on, as AWS does (#9716)
This commit is contained in:
1 parent
ba7bfc3ca6
commit
f30e649c7f
7 files changed
+160
-12
No files matched your search
@@ -449,9 +449,11 @@ Floci acts on the configuration as EKS does:
|
||||
the cluster's subnets and security group) and the built-in `general-purpose` and `system` NodePools
|
||||
`computeConfig.nodePools` names, as Auto Mode makes them: on-demand c, m and r instances of generation 5 and later,
|
||||
`amd64` (`system` also `arm64`, with a `CriticalAddonsOnly` `NoSchedule` taint), expiring after 336 hours. A pool
|
||||
`UpdateClusterConfig` drops is removed. The cluster's node carries Auto Mode's `eks.amazonaws.com/compute-type: auto`
|
||||
label, so workloads that select Auto Mode nodes run on it. Pods it can't take get nodes of their own (see
|
||||
[Auto Mode nodes](#auto-mode-nodes)).
|
||||
`UpdateClusterConfig` drops is removed. In a cluster created with Auto Mode compute, the cluster's node carries Auto
|
||||
Mode's `eks.amazonaws.com/compute-type: auto` label, so workloads that select Auto Mode nodes run on it. A cluster
|
||||
that turns compute on later keeps its node as it was, without the label, as Auto Mode leaves the nodes a cluster
|
||||
already has: a DaemonSet kept off Auto Mode nodes, such as the EBS CSI driver's node plugin, keeps running there. Pods
|
||||
it can't take get nodes of their own (see [Auto Mode nodes](#auto-mode-nodes)).
|
||||
- **Block storage.** While it is on, claims of a StorageClass naming `ebs.csi.eks.amazonaws.com` are provisioned
|
||||
(see [Storage classes](#storage-classes)).
|
||||
- **Load balancing.** While it is on, Services get Network Load Balancers (see
|
||||
|
||||
@@ -839,7 +839,7 @@ public class EksClusterManager
|
||||
if (!serviceLb) {
|
||||
serverArgs.add("--disable=servicelb");
|
||||
}
|
||||
if (computeOn(cluster)) {
|
||||
if (nodeIsAutoMode(cluster)) {
|
||||
// Auto Mode's nodes carry this label, which workloads select them by.
|
||||
serverArgs.add("--node-label=" + AUTO_COMPUTE_LABEL);
|
||||
}
|
||||
@@ -3172,8 +3172,8 @@ public class EksClusterManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives the cluster's running node the label EKS Auto Mode's nodes carry while compute is on, and takes it off
|
||||
* once compute is off. k3s's {@code --node-label} adds a label when the node starts but never removes one, and a
|
||||
* Gives the cluster's running node the label EKS Auto Mode's nodes carry while it stands for them
|
||||
* ({@link #nodeIsAutoMode}), and takes it off otherwise. k3s's {@code --node-label} adds a label when the node starts but never removes one, and a
|
||||
* node started before Floci set it has none until it restarts. A cluster that never had Auto Mode compute is left
|
||||
* alone.
|
||||
*/
|
||||
@@ -3181,7 +3181,7 @@ public class EksClusterManager
|
||||
if (cluster.getComputeConfig() == null || containerId == null) {
|
||||
return;
|
||||
}
|
||||
String label = computeOn(cluster) ? AUTO_COMPUTE_LABEL : AUTO_COMPUTE_LABEL_KEY + "-";
|
||||
String label = nodeIsAutoMode(cluster) ? AUTO_COMPUTE_LABEL : AUTO_COMPUTE_LABEL_KEY + "-";
|
||||
try {
|
||||
ContainerExec.Result result = execInContainerForResult(containerId, new String[]{"kubectl", "label",
|
||||
"node", deriveClusterNodePrivateDnsName(cluster), "--overwrite", label}, 15);
|
||||
@@ -3195,6 +3195,15 @@ public class EksClusterManager
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the cluster's node stands for EKS Auto Mode's nodes and so carries their label: while compute is on, for
|
||||
* a cluster created with it. A cluster that turned compute on later keeps its node as it was, as Auto Mode leaves
|
||||
* the nodes a cluster already has. One stored before Floci recorded how it was created keeps the label.
|
||||
*/
|
||||
static boolean nodeIsAutoMode(Cluster cluster) {
|
||||
return computeOn(cluster) && !Boolean.FALSE.equals(cluster.getCreatedWithAutoModeCompute());
|
||||
}
|
||||
|
||||
static boolean computeOn(Cluster cluster) {
|
||||
return cluster != null && cluster.getComputeConfig() != null
|
||||
&& Boolean.TRUE.equals(cluster.getComputeConfig().enabled());
|
||||
|
||||
@@ -143,7 +143,8 @@ public class EksController {
|
||||
&& cluster.getNodePrivateIp() == null && cluster.getNodePodPrefixes() == null
|
||||
&& cluster.getClusterArgs() == null && cluster.getDefaultStorageClass() == null
|
||||
&& cluster.getJoinedInstanceIds() == null && cluster.getDockerNameRegion() == null
|
||||
&& cluster.getCreateRequest() == null && cluster.getClientRequestToken() == null)) {
|
||||
&& cluster.getCreateRequest() == null && cluster.getClientRequestToken() == null
|
||||
&& cluster.getCreatedWithAutoModeCompute() == null)) {
|
||||
return cluster;
|
||||
}
|
||||
Cluster response = cluster.copy();
|
||||
@@ -157,6 +158,7 @@ public class EksController {
|
||||
response.setJoinedInstanceIds(null);
|
||||
response.setCreateRequest(null);
|
||||
response.setClientRequestToken(null);
|
||||
response.setCreatedWithAutoModeCompute(null);
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
@@ -950,6 +950,7 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
cluster.setCreateRequest(requestSnapshot);
|
||||
cluster.setAccessConfig(accessConfig);
|
||||
cluster.setComputeConfig(request.getComputeConfig());
|
||||
cluster.setCreatedWithAutoModeCompute(EksClusterManager.computeOn(cluster));
|
||||
cluster.setStorageConfig(request.getStorageConfig());
|
||||
cluster.setUpgradePolicy(upgradePolicy);
|
||||
cluster.setArn(arn);
|
||||
@@ -1507,6 +1508,11 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
|
||||
/** Stores {@code next}, and returns the Auto Mode members the request names as the update's parameters. */
|
||||
private static List<UpdateParam> applyAutoMode(Cluster cluster, AutoMode next, UpdateClusterConfigRequest request) {
|
||||
if (next.computeOn() && !EksClusterManager.computeOn(cluster)
|
||||
&& cluster.getCreatedWithAutoModeCompute() == null) {
|
||||
// Compute turns on for a cluster stored before Floci recorded how it was created: it had none.
|
||||
cluster.setCreatedWithAutoModeCompute(false);
|
||||
}
|
||||
storeAutoMode(cluster, next);
|
||||
List<UpdateParam> params = new ArrayList<>();
|
||||
if (request.computeConfig() != null) {
|
||||
|
||||
@@ -164,6 +164,16 @@ public class Cluster {
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
private Map<String, String> joinedInstanceIds;
|
||||
|
||||
/**
|
||||
* Whether the cluster was created with EKS Auto Mode compute on, so that its node stands for Auto Mode's own
|
||||
* nodes; false for one that turned compute on later, whose node stands for the nodes it had, which Auto Mode leaves
|
||||
* as they were. Null for a cluster stored before Floci recorded it. Internal metadata omitted from AWS API
|
||||
* responses.
|
||||
*/
|
||||
@JsonProperty("createdWithAutoModeCompute")
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private Boolean createdWithAutoModeCompute;
|
||||
|
||||
private AccessConfig accessConfig;
|
||||
|
||||
@JsonProperty("computeConfig")
|
||||
@@ -212,6 +222,11 @@ public class Cluster {
|
||||
|
||||
public Cluster() {}
|
||||
|
||||
public Boolean getCreatedWithAutoModeCompute() { return createdWithAutoModeCompute; }
|
||||
public void setCreatedWithAutoModeCompute(Boolean createdWithAutoModeCompute) {
|
||||
this.createdWithAutoModeCompute = createdWithAutoModeCompute;
|
||||
}
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
@@ -339,6 +354,7 @@ public class Cluster {
|
||||
c.clusterArgs = this.clusterArgs != null ? new ArrayList<>(this.clusterArgs) : null;
|
||||
c.defaultStorageClass = this.defaultStorageClass;
|
||||
c.joinedInstanceIds = this.joinedInstanceIds != null ? new ConcurrentHashMap<>(this.joinedInstanceIds) : null;
|
||||
c.createdWithAutoModeCompute = this.createdWithAutoModeCompute;
|
||||
c.accessConfig = this.accessConfig;
|
||||
c.computeConfig = this.computeConfig;
|
||||
c.storageConfig = this.storageConfig;
|
||||
|
||||
@@ -599,9 +599,14 @@ class EksClusterManagerTest {
|
||||
assertFalse(manifest.contains("kind: StorageClass"), manifest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto Mode's capabilities are on together; the node carries the compute-type label unless the cluster turned
|
||||
* compute on after it was created (createdWithAutoMode false), or for one stored before Floci recorded it (empty).
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void aNodeOfAClusterWithAutoModeLoadBalancingStartsWithoutServiceLb(boolean loadBalancing) {
|
||||
@CsvSource({"true,", "true,true", "true,false", "false,"})
|
||||
void aNodeOfAClusterWithAutoModeLoadBalancingStartsWithoutServiceLb(boolean loadBalancing,
|
||||
Boolean createdWithAutoMode) {
|
||||
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
when(config.services().eks().defaultImage()).thenReturn("rancher/k3s:v1.30.0-k3s1");
|
||||
ContainerLifecycleManager lifecycleManager = Mockito.mock(ContainerLifecycleManager.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
@@ -620,6 +625,7 @@ class EksClusterManagerTest {
|
||||
network.setElasticLoadBalancing(new ElasticLoadBalancing(loadBalancing));
|
||||
cluster.setKubernetesNetworkConfig(network);
|
||||
cluster.setComputeConfig(new ComputeConfig(loadBalancing, List.of(), null));
|
||||
cluster.setCreatedWithAutoModeCompute(createdWithAutoMode);
|
||||
|
||||
manager.startCluster(cluster);
|
||||
|
||||
@@ -627,8 +633,8 @@ class EksClusterManagerTest {
|
||||
ArgumentCaptor<List<String>> cmd = ArgumentCaptor.forClass(List.class);
|
||||
verify(builder).withCmd(cmd.capture());
|
||||
assertEquals(loadBalancing, cmd.getValue().contains("--disable=servicelb"));
|
||||
// Auto Mode's capabilities are on together, and its nodes carry the compute-type label.
|
||||
assertEquals(loadBalancing, cmd.getValue().contains("--node-label=" + EksClusterManager.AUTO_COMPUTE_LABEL));
|
||||
assertEquals(loadBalancing && !Boolean.FALSE.equals(createdWithAutoMode),
|
||||
cmd.getValue().contains("--node-label=" + EksClusterManager.AUTO_COMPUTE_LABEL));
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, String>> labels = ArgumentCaptor.forClass(Map.class);
|
||||
verify(builder).withLabels(labels.capture());
|
||||
@@ -4545,6 +4551,27 @@ class EksClusterManagerTest {
|
||||
verify(spyManager, times(2)).execInContainerForResult(anyString(), any(), anyInt());
|
||||
}
|
||||
|
||||
/**
|
||||
* A cluster that turned compute on after it was created keeps its node as it was: the running node loses the
|
||||
* label rather than gaining it, and a new node starts without it.
|
||||
*/
|
||||
@Test
|
||||
void aNodeOfAClusterThatTurnedComputeOnLaterIsNotLabelled() {
|
||||
EksClusterManager spyManager = Mockito.spy(manager);
|
||||
doReturn(new ContainerExec.Result(0L, "", "", false)).when(spyManager)
|
||||
.execInContainerForResult(anyString(), any(), anyInt());
|
||||
Cluster cluster = cluster();
|
||||
cluster.setComputeConfig(new ComputeConfig(true, List.of(), null));
|
||||
cluster.setCreatedWithAutoModeCompute(false);
|
||||
String node = spyManager.deriveClusterNodePrivateDnsName(cluster);
|
||||
|
||||
spyManager.applyComputeLabel(cluster, "container-1");
|
||||
|
||||
verify(spyManager).execInContainerForResult("container-1", new String[]{"kubectl", "label", "node",
|
||||
node, "--overwrite", "eks.amazonaws.com/compute-type-"}, 15);
|
||||
verify(spyManager, times(1)).execInContainerForResult(anyString(), any(), anyInt());
|
||||
}
|
||||
|
||||
/** With no subnets or security group known, the terms are empty lists on their keys' lines, which k3s parses. */
|
||||
@Test
|
||||
void aNodeClassWithoutSubnetsOrASecurityGroupHasEmptyTerms() {
|
||||
|
||||
@@ -8,6 +8,12 @@ import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.docker.ContainerBuilder;
|
||||
import io.floci.aws.core.common.docker.ContainerDetector;
|
||||
import io.floci.aws.core.common.docker.ContainerExec;
|
||||
import io.floci.aws.core.common.docker.ContainerLifecycleManager;
|
||||
import io.floci.aws.core.common.docker.DockerHostResolver;
|
||||
import io.floci.aws.core.common.docker.PortAllocator;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
@@ -27,6 +33,7 @@ import io.floci.aws.services.ec2.model.SecurityGroup;
|
||||
import io.floci.aws.services.ec2.model.Tag;
|
||||
import io.floci.aws.services.ec2.model.Vpc;
|
||||
import io.floci.aws.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import io.floci.aws.services.ecr.registry.EcrRegistryManager;
|
||||
import io.floci.aws.services.eks.model.AccessConfig;
|
||||
import io.floci.aws.services.eks.model.BlockStorage;
|
||||
import io.floci.aws.services.eks.model.Cluster;
|
||||
@@ -64,6 +71,7 @@ import org.junit.jupiter.api.parallel.ResourceAccessMode;
|
||||
import org.junit.jupiter.api.parallel.ResourceLock;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -91,10 +99,12 @@ import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -2877,6 +2887,82 @@ class EksServiceTest {
|
||||
assertEquals(new ElasticLoadBalancing(true), described.getKubernetesNetworkConfig().getElasticLoadBalancing());
|
||||
}
|
||||
|
||||
/**
|
||||
* A cluster created with Auto Mode compute has its node labelled as Auto Mode's; a cluster created without it that
|
||||
* turns compute on later through UpdateClusterConfig keeps its node unlabelled, as Auto Mode leaves existing nodes.
|
||||
*/
|
||||
@Test
|
||||
void anExistingClustersNodeIsNotLabelledWhenAutoModeComputeTurnsOn() {
|
||||
ComputeConfig on = new ComputeConfig(true, List.of("general-purpose", "system"), AUTO_MODE_NODE_ROLE);
|
||||
eksService.createCluster(autoModeRequest("born-auto", on, true, true));
|
||||
eksService.createCluster(createTestClusterRequest("turned-auto"));
|
||||
|
||||
assertEquals(AUTO_MODE_LABEL, nodeLabelApplied(eksService.describeCluster("born-auto")));
|
||||
|
||||
Cluster turnedAuto = eksService.describeCluster("turned-auto");
|
||||
turnAutoModeOn(turnedAuto);
|
||||
|
||||
assertTrue(EksClusterManager.computeOn(turnedAuto));
|
||||
assertEquals(AUTO_MODE_LABEL_REMOVED, nodeLabelApplied(turnedAuto));
|
||||
}
|
||||
|
||||
/**
|
||||
* A cluster stored before Floci recorded how it was created: one that turns compute on now had none, so its node
|
||||
* stays unlabelled; one that has compute on already keeps its label.
|
||||
*/
|
||||
@Test
|
||||
void aClusterStoredBeforeTheRecordFollowsWhetherItHadComputeWhenItTurnsOn() {
|
||||
Cluster legacy = activeCluster();
|
||||
legacy.setKubernetesNetworkConfig(new KubernetesNetworkConfig());
|
||||
|
||||
turnAutoModeOn(legacy);
|
||||
|
||||
assertEquals(AUTO_MODE_LABEL_REMOVED, nodeLabelApplied(legacy));
|
||||
|
||||
Cluster legacyOn = activeCluster();
|
||||
legacyOn.setComputeConfig(new ComputeConfig(true, List.of("general-purpose"), AUTO_MODE_NODE_ROLE));
|
||||
assertEquals(AUTO_MODE_LABEL, nodeLabelApplied(legacyOn));
|
||||
}
|
||||
|
||||
private static final String AUTO_MODE_LABEL = "eks.amazonaws.com/compute-type=auto";
|
||||
private static final String AUTO_MODE_LABEL_REMOVED = "eks.amazonaws.com/compute-type-";
|
||||
|
||||
/** The label a cluster manager gives {@code cluster}'s running node: Auto Mode's, or its removal. */
|
||||
private String nodeLabelApplied(Cluster cluster) {
|
||||
EksClusterManager manager = spy(EksClusterManagers.create(mock(ContainerBuilder.class),
|
||||
mock(ContainerLifecycleManager.class), mock(ContainerDetector.class), mock(PortAllocator.class),
|
||||
mock(DockerHostResolver.class), mock(EcrRegistryManager.class),
|
||||
mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS), regionResolver));
|
||||
doReturn(new ContainerExec.Result(0L, "", "", false)).when(manager)
|
||||
.execInContainerForResult(anyString(), any(), anyInt());
|
||||
|
||||
manager.applyComputeLabel(cluster, "container-1");
|
||||
|
||||
ArgumentCaptor<String[]> command = ArgumentCaptor.forClass(String[].class);
|
||||
verify(manager).execInContainerForResult(eq("container-1"), command.capture(), anyInt());
|
||||
return command.getValue()[command.getValue().length - 1];
|
||||
}
|
||||
|
||||
/** Turns Auto Mode on for {@code cluster} through UpdateClusterConfig, in a service that stores it alone. */
|
||||
private void turnAutoModeOn(Cluster cluster) {
|
||||
EksAddonService addons = mock(EksAddonService.class);
|
||||
when(addons.recordUpdate(any(), anyString(), any(), any(), any()))
|
||||
.thenReturn(new Update("u", "Successful", "AutoModeUpdate", List.of(), 0, List.of()));
|
||||
AccountAwareStorageBackend<Cluster> clusterStore =
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, "000000000000");
|
||||
clusterStore.putForAccount("000000000000", EksService.clusterKey("us-east-1", cluster.getName()), cluster);
|
||||
EksService service = new EksService(fixedStorageFactory(clusterStore), testConfig(true),
|
||||
new RegionResolver("us-east-1", "000000000000"), mock(EksClusterManager.class), null,
|
||||
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
|
||||
new ObjectMapper()), mock(EksAccessEntryService.class),
|
||||
mock(EksPodIdentityAssociationService.class), addons);
|
||||
KubernetesNetworkConfig loadBalancing = new KubernetesNetworkConfig();
|
||||
loadBalancing.setElasticLoadBalancing(new ElasticLoadBalancing(true));
|
||||
service.updateClusterConfig(cluster.getName(), new UpdateClusterConfigRequest(null, null, null, null, null,
|
||||
new ComputeConfig(true, List.of("general-purpose"), AUTO_MODE_NODE_ROLE),
|
||||
new StorageConfig(new BlockStorage(true)), loadBalancing));
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoModeIsRefusedUnlessComputeBlockStorageAndLoadBalancingAreEnabledTogether() {
|
||||
ComputeConfig on = new ComputeConfig(true, List.of("general-purpose"), AUTO_MODE_NODE_ROLE);
|
||||
|
||||
Reference in new issue
Block a user