mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eks): run node group launch template user data in cluster containers (#4149)
This commit is contained in:
1 parent
76c103fd8c
commit
b7a66d4aaf
9 files changed
+1196
-427
No files matched your search
+128
@@ -0,0 +1,128 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
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;
|
||||
|
||||
class UserDataPipelineTest {
|
||||
|
||||
@Test
|
||||
void extractsPlainShellScript() {
|
||||
String script = "#!/bin/bash\necho 'hello world'\n";
|
||||
List<String> scripts = UserDataPipeline.extractShellScripts(script);
|
||||
assertEquals(1, scripts.size());
|
||||
assertEquals(script, scripts.getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractsBase64EncodedScript() {
|
||||
String script = "#!/bin/bash\necho 'from base64'\n";
|
||||
String encoded = Base64.getEncoder().encodeToString(script.getBytes(StandardCharsets.UTF_8));
|
||||
List<String> scripts = UserDataPipeline.extractShellScripts(encoded);
|
||||
assertEquals(1, scripts.size());
|
||||
assertEquals(script, scripts.getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractsGzipBase64EncodedScript() throws IOException {
|
||||
String script = "#!/bin/bash\necho 'from gzip base64'\n";
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
|
||||
gzos.write(script.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
String encoded = Base64.getEncoder().encodeToString(baos.toByteArray());
|
||||
List<String> scripts = UserDataPipeline.extractShellScripts(encoded);
|
||||
assertEquals(1, scripts.size());
|
||||
assertEquals(script, scripts.getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractsOnlyShellScriptPartsFromMultipartMime() {
|
||||
String mime = "Content-Type: multipart/mixed; boundary=\"==BOUNDARY==\"\n"
|
||||
+ "MIME-Version: 1.0\n"
|
||||
+ "\n"
|
||||
+ "--==BOUNDARY==\n"
|
||||
+ "Content-Type: text/cloud-config; charset=\"us-ascii\"\n"
|
||||
+ "\n"
|
||||
+ "#cloud-config\n"
|
||||
+ "package_upgrade: true\n"
|
||||
+ "--==BOUNDARY==\n"
|
||||
+ "Content-Type: text/x-shellscript; charset=\"us-ascii\"\n"
|
||||
+ "\n"
|
||||
+ "#!/bin/bash\n"
|
||||
+ "echo 'from mime'\n"
|
||||
+ "--==BOUNDARY==--\n";
|
||||
|
||||
List<String> scripts = UserDataPipeline.extractShellScripts(mime);
|
||||
assertEquals(1, scripts.size());
|
||||
assertTrue(scripts.getFirst().contains("echo 'from mime'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsEmptyForNullOrBlankOrNonScript() {
|
||||
assertTrue(UserDataPipeline.extractShellScripts(null).isEmpty());
|
||||
assertTrue(UserDataPipeline.extractShellScripts("").isEmpty());
|
||||
assertTrue(UserDataPipeline.extractShellScripts(" ").isEmpty());
|
||||
assertTrue(UserDataPipeline.extractShellScripts("plain text without shebang").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void boundedOutputAppendsAndTruncates() throws IOException {
|
||||
UserDataPipeline.BoundedOutput bounded = new UserDataPipeline.BoundedOutput(20);
|
||||
bounded.write("12345".getBytes(StandardCharsets.UTF_8));
|
||||
bounded.write("67890".getBytes(StandardCharsets.UTF_8));
|
||||
assertEquals("1234567890", bounded.utf8Tail());
|
||||
assertFalse(bounded.truncated());
|
||||
|
||||
bounded.write("1234567890EXTRA".getBytes(StandardCharsets.UTF_8));
|
||||
assertTrue(bounded.truncated());
|
||||
assertEquals(25, bounded.totalBytes());
|
||||
assertTrue(bounded.utf8Tail().endsWith("EXTRA"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void boundedOutputHandlesMultiByteUtf8Splits() throws IOException {
|
||||
UserDataPipeline.BoundedOutput bounded = new UserDataPipeline.BoundedOutput(5);
|
||||
byte[] euro = "\u20AC".getBytes(StandardCharsets.UTF_8); // 3 bytes
|
||||
byte[] smile = "\uD83D\uDE00".getBytes(StandardCharsets.UTF_8); // 4 bytes
|
||||
bounded.write(euro);
|
||||
bounded.write(smile);
|
||||
assertTrue(bounded.truncated());
|
||||
// Tail should safely decode without corrupted leading continuation byte
|
||||
assertNotNull(bounded.utf8Tail());
|
||||
}
|
||||
|
||||
@Test
|
||||
void executionResultFormats() {
|
||||
UserDataPipeline.ExecutionResult success = UserDataPipeline.ExecutionResult.success(0L, "done");
|
||||
assertTrue(success.isSuccess());
|
||||
assertEquals(0L, success.getExitCode());
|
||||
assertFalse(success.isTimedOut());
|
||||
|
||||
UserDataPipeline.ExecutionResult failed = UserDataPipeline.ExecutionResult.failed(1L, 1, 2, "error log");
|
||||
assertFalse(failed.isSuccess());
|
||||
assertEquals(1L, failed.getExitCode());
|
||||
assertNotNull(failed.getFailureMessage());
|
||||
assertTrue(failed.getFailureMessage().contains("part 1/2 failed with exit code 1"));
|
||||
|
||||
UserDataPipeline.ExecutionResult timeout = UserDataPipeline.ExecutionResult.timedOut(2, 2, 30L, "stuck");
|
||||
assertFalse(timeout.isSuccess());
|
||||
assertTrue(timeout.isTimedOut());
|
||||
assertNotNull(timeout.getFailureMessage());
|
||||
assertTrue(timeout.getFailureMessage().contains("part 2/2 timed out after 30 minutes"));
|
||||
|
||||
UserDataPipeline.ExecutionResult skipped = UserDataPipeline.ExecutionResult.skipped("no container");
|
||||
assertTrue(skipped.isSuccess());
|
||||
assertEquals(0L, skipped.getExitCode());
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
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.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.UserDataPipeline;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
@@ -572,7 +573,7 @@ class Ec2ContainerManagerTest {
|
||||
int concurrentLaunches = 12;
|
||||
AtomicInteger active = new AtomicInteger(0);
|
||||
AtomicInteger peakActive = new AtomicInteger(0);
|
||||
Ec2ContainerManager.userDataDecompressionTestHook = () -> {
|
||||
UserDataPipeline.userDataDecompressionTestHook = () -> {
|
||||
int now = active.incrementAndGet();
|
||||
peakActive.accumulateAndGet(now, Math::max);
|
||||
try {
|
||||
@@ -600,7 +601,7 @@ class Ec2ContainerManagerTest {
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
Ec2ContainerManager.userDataDecompressionTestHook = null;
|
||||
UserDataPipeline.userDataDecompressionTestHook = null;
|
||||
}
|
||||
|
||||
assertEquals(concurrentLaunches, results.size());
|
||||
|
||||
+331
@@ -0,0 +1,331 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.UserDataPipeline;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ec2.AmiImageResolver;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2ContainerManager;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2ImageCatalog;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2InstanceTypeCatalog;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import io.github.hectorvent.floci.services.eks.model.Cluster;
|
||||
import io.github.hectorvent.floci.services.eks.model.CreateClusterRequest;
|
||||
import io.github.hectorvent.floci.services.eks.model.CreateNodeGroupRequest;
|
||||
import io.github.hectorvent.floci.services.eks.model.Nodegroup;
|
||||
import io.github.hectorvent.floci.services.eks.model.NodegroupStatus;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class EksLaunchTemplateUserDataTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String CLUSTER_NAME = "userdata-cluster";
|
||||
|
||||
private EksService eksService;
|
||||
private Ec2Service ec2Service;
|
||||
private EksClusterManager clusterManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StorageFactory storageFactory = new StorageFactory(null, null) {
|
||||
@Override
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return AccountAwareStorageBackend.inMemory(ACCOUNT);
|
||||
}
|
||||
};
|
||||
|
||||
EmulatorConfig config = testConfig();
|
||||
clusterManager = mock(EksClusterManager.class);
|
||||
ec2Service = realEc2Service();
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT);
|
||||
|
||||
eksService = new EksService(storageFactory, config, regionResolver, clusterManager, ec2Service,
|
||||
new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class),
|
||||
mock(EksPodIdentityAssociationService.class));
|
||||
|
||||
CreateClusterRequest clusterReq = new CreateClusterRequest();
|
||||
clusterReq.setName(CLUSTER_NAME);
|
||||
clusterReq.setRoleArn("arn:aws:iam::" + ACCOUNT + ":role/eks-role");
|
||||
eksService.createCluster(clusterReq);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nodegroupWithLaunchTemplateExecutesUserData() {
|
||||
String script = "#!/bin/bash\necho 'hello from node group' > /tmp/bootstrap.log\n";
|
||||
String encoded = Base64.getEncoder().encodeToString(script.getBytes(StandardCharsets.UTF_8));
|
||||
String ltId = createLaunchTemplate("lt-test-1", encoded);
|
||||
|
||||
when(clusterManager.executeUserData(any(Cluster.class), eq("ng-1"), eq(encoded)))
|
||||
.thenReturn(UserDataPipeline.ExecutionResult.success(0L, "ok"));
|
||||
|
||||
CreateNodeGroupRequest req = nodeGroupRequest("ng-1");
|
||||
req.setLaunchTemplate(Map.of("id", ltId, "version", "1"));
|
||||
|
||||
Nodegroup ng = eksService.createNodeGroup(CLUSTER_NAME, req);
|
||||
|
||||
assertEquals(NodegroupStatus.ACTIVE, ng.getStatus());
|
||||
verify(clusterManager, times(1)).executeUserData(any(Cluster.class), eq("ng-1"), eq(encoded));
|
||||
Map<?, ?> health = (Map<?, ?>) ng.getHealth();
|
||||
assertNotNull(health);
|
||||
List<?> issues = (List<?>) health.get("issues");
|
||||
assertTrue(issues.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void identicalUserDataSkipsExecutionIdempotently() {
|
||||
String script = "#!/bin/bash\necho 'cluster bootstrap'\n";
|
||||
String encoded = Base64.getEncoder().encodeToString(script.getBytes(StandardCharsets.UTF_8));
|
||||
String ltId = createLaunchTemplate("lt-same", encoded);
|
||||
|
||||
when(clusterManager.executeUserData(any(Cluster.class), eq("ng-1"), eq(encoded)))
|
||||
.thenReturn(UserDataPipeline.ExecutionResult.success(0L, "ok"));
|
||||
|
||||
CreateNodeGroupRequest req1 = nodeGroupRequest("ng-1");
|
||||
req1.setLaunchTemplate(Map.of("id", ltId));
|
||||
Nodegroup ng1 = eksService.createNodeGroup(CLUSTER_NAME, req1);
|
||||
assertEquals(NodegroupStatus.ACTIVE, ng1.getStatus());
|
||||
|
||||
CreateNodeGroupRequest req2 = nodeGroupRequest("ng-2");
|
||||
req2.setLaunchTemplate(Map.of("id", ltId));
|
||||
Nodegroup ng2 = eksService.createNodeGroup(CLUSTER_NAME, req2);
|
||||
assertEquals(NodegroupStatus.ACTIVE, ng2.getStatus());
|
||||
|
||||
// clusterManager.executeUserData must be called only ONCE because ng-2 has identical user data
|
||||
verify(clusterManager, times(1)).executeUserData(any(Cluster.class), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void differingUserDataWarnsAndSkipsWithoutExecuting() {
|
||||
String script1 = "#!/bin/bash\necho 'first'\n";
|
||||
String encoded1 = Base64.getEncoder().encodeToString(script1.getBytes(StandardCharsets.UTF_8));
|
||||
String ltId1 = createLaunchTemplate("lt-first", encoded1);
|
||||
|
||||
String script2 = "#!/bin/bash\necho 'second'\n";
|
||||
String encoded2 = Base64.getEncoder().encodeToString(script2.getBytes(StandardCharsets.UTF_8));
|
||||
String ltId2 = createLaunchTemplate("lt-second", encoded2);
|
||||
|
||||
when(clusterManager.executeUserData(any(Cluster.class), eq("ng-1"), eq(encoded1)))
|
||||
.thenReturn(UserDataPipeline.ExecutionResult.success(0L, "ok"));
|
||||
|
||||
CreateNodeGroupRequest req1 = nodeGroupRequest("ng-1");
|
||||
req1.setLaunchTemplate(Map.of("id", ltId1));
|
||||
Nodegroup ng1 = eksService.createNodeGroup(CLUSTER_NAME, req1);
|
||||
assertEquals(NodegroupStatus.ACTIVE, ng1.getStatus());
|
||||
|
||||
CreateNodeGroupRequest req2 = nodeGroupRequest("ng-2");
|
||||
req2.setLaunchTemplate(Map.of("id", ltId2));
|
||||
Nodegroup ng2 = eksService.createNodeGroup(CLUSTER_NAME, req2);
|
||||
|
||||
// Floci runs a single shared container per cluster; differing user data warns and skips
|
||||
assertEquals(NodegroupStatus.ACTIVE, ng2.getStatus());
|
||||
verify(clusterManager, times(1)).executeUserData(any(Cluster.class), any(), any());
|
||||
verify(clusterManager, never()).executeUserData(any(Cluster.class), eq("ng-2"), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void executionFailureMarksNodegroupCreateFailed() {
|
||||
String script = "#!/bin/bash\nexit 2\n";
|
||||
String encoded = Base64.getEncoder().encodeToString(script.getBytes(StandardCharsets.UTF_8));
|
||||
String ltId = createLaunchTemplate("lt-fail", encoded);
|
||||
|
||||
when(clusterManager.executeUserData(any(Cluster.class), eq("ng-fail"), eq(encoded)))
|
||||
.thenReturn(UserDataPipeline.ExecutionResult.failed(2L, 1, 1, "script failed"));
|
||||
|
||||
CreateNodeGroupRequest req = nodeGroupRequest("ng-fail");
|
||||
req.setLaunchTemplate(Map.of("id", ltId));
|
||||
|
||||
Nodegroup ng = eksService.createNodeGroup(CLUSTER_NAME, req);
|
||||
|
||||
assertEquals(NodegroupStatus.CREATE_FAILED, ng.getStatus());
|
||||
Map<?, ?> health = (Map<?, ?>) ng.getHealth();
|
||||
assertNotNull(health);
|
||||
List<?> issues = (List<?>) health.get("issues");
|
||||
assertEquals(1, issues.size());
|
||||
|
||||
Map<?, ?> issue = (Map<?, ?>) issues.getFirst();
|
||||
assertEquals("NodeCreationFailure", issue.get("code"));
|
||||
assertTrue(issue.get("message").toString().contains("failed with exit code 2"));
|
||||
assertEquals(List.of("ng-fail"), issue.get("resourceIds"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void executionTimeoutMarksNodegroupCreateFailed() {
|
||||
String script = "#!/bin/bash\nsleep 9999\n";
|
||||
String encoded = Base64.getEncoder().encodeToString(script.getBytes(StandardCharsets.UTF_8));
|
||||
String ltId = createLaunchTemplate("lt-timeout", encoded);
|
||||
|
||||
when(clusterManager.executeUserData(any(Cluster.class), eq("ng-timeout"), eq(encoded)))
|
||||
.thenReturn(UserDataPipeline.ExecutionResult.timedOut(1, 1, 30L, "hang"));
|
||||
|
||||
CreateNodeGroupRequest req = nodeGroupRequest("ng-timeout");
|
||||
req.setLaunchTemplate(Map.of("id", ltId));
|
||||
|
||||
Nodegroup ng = eksService.createNodeGroup(CLUSTER_NAME, req);
|
||||
|
||||
assertEquals(NodegroupStatus.CREATE_FAILED, ng.getStatus());
|
||||
Map<?, ?> health = (Map<?, ?>) ng.getHealth();
|
||||
assertNotNull(health);
|
||||
List<?> issues = (List<?>) health.get("issues");
|
||||
assertEquals(1, issues.size());
|
||||
|
||||
Map<?, ?> issue = (Map<?, ?>) issues.getFirst();
|
||||
assertEquals("NodeCreationFailure", issue.get("code"));
|
||||
assertTrue(issue.get("message").toString().contains("timed out after 30 minutes"));
|
||||
assertEquals(List.of("ng-timeout"), issue.get("resourceIds"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterDeletionClearsAppliedUserDataTracker() {
|
||||
String script = "#!/bin/bash\necho 'recreate test'\n";
|
||||
String encoded = Base64.getEncoder().encodeToString(script.getBytes(StandardCharsets.UTF_8));
|
||||
String ltId = createLaunchTemplate("lt-recreate", encoded);
|
||||
|
||||
when(clusterManager.executeUserData(any(Cluster.class), eq("ng-1"), eq(encoded)))
|
||||
.thenReturn(UserDataPipeline.ExecutionResult.success(0L, "ok"));
|
||||
|
||||
CreateNodeGroupRequest req = nodeGroupRequest("ng-1");
|
||||
req.setLaunchTemplate(Map.of("id", ltId));
|
||||
eksService.createNodeGroup(CLUSTER_NAME, req);
|
||||
verify(clusterManager, times(1)).executeUserData(any(Cluster.class), eq("ng-1"), eq(encoded));
|
||||
|
||||
// Delete nodegroup and cluster, then recreate
|
||||
eksService.deleteNodeGroup(CLUSTER_NAME, "ng-1");
|
||||
eksService.deleteCluster(CLUSTER_NAME);
|
||||
|
||||
CreateClusterRequest clusterReq = new CreateClusterRequest();
|
||||
clusterReq.setName(CLUSTER_NAME);
|
||||
clusterReq.setRoleArn("arn:aws:iam::" + ACCOUNT + ":role/eks-role");
|
||||
eksService.createCluster(clusterReq);
|
||||
|
||||
// Nodegroup on recreated cluster should execute user data again
|
||||
eksService.createNodeGroup(CLUSTER_NAME, req);
|
||||
verify(clusterManager, times(2)).executeUserData(any(Cluster.class), eq("ng-1"), eq(encoded));
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterDeletionForAccountClearsAppliedUserDataTracker() {
|
||||
String script = "#!/bin/bash\necho 'recreate account test'\n";
|
||||
String encoded = Base64.getEncoder().encodeToString(script.getBytes(StandardCharsets.UTF_8));
|
||||
String ltId = createLaunchTemplate("lt-recreate-account", encoded);
|
||||
|
||||
when(clusterManager.executeUserData(any(Cluster.class), eq("ng-acc"), eq(encoded)))
|
||||
.thenReturn(UserDataPipeline.ExecutionResult.success(0L, "ok"));
|
||||
|
||||
CreateNodeGroupRequest req = nodeGroupRequest("ng-acc");
|
||||
req.setLaunchTemplate(Map.of("id", ltId));
|
||||
eksService.createNodeGroup(CLUSTER_NAME, req);
|
||||
verify(clusterManager, times(1)).executeUserData(any(Cluster.class), eq("ng-acc"), eq(encoded));
|
||||
|
||||
// Delete nodegroup and cluster via deleteClusterForAccount, then recreate
|
||||
eksService.deleteNodeGroup(CLUSTER_NAME, "ng-acc");
|
||||
eksService.deleteClusterForAccount(ACCOUNT, CLUSTER_NAME);
|
||||
|
||||
CreateClusterRequest clusterReq = new CreateClusterRequest();
|
||||
clusterReq.setName(CLUSTER_NAME);
|
||||
clusterReq.setRoleArn("arn:aws:iam::" + ACCOUNT + ":role/eks-role");
|
||||
eksService.createCluster(clusterReq);
|
||||
|
||||
// Nodegroup on recreated cluster should execute user data again
|
||||
eksService.createNodeGroup(CLUSTER_NAME, req);
|
||||
verify(clusterManager, times(2)).executeUserData(any(Cluster.class), eq("ng-acc"), eq(encoded));
|
||||
}
|
||||
|
||||
private String createLaunchTemplate(String name, String userData) {
|
||||
LaunchTemplateData data = new LaunchTemplateData();
|
||||
data.setUserData(userData);
|
||||
LaunchTemplate lt = ec2Service.createLaunchTemplate(REGION, name, data, List.of());
|
||||
return lt.getLaunchTemplateId();
|
||||
}
|
||||
|
||||
private CreateNodeGroupRequest nodeGroupRequest(String name) {
|
||||
CreateNodeGroupRequest req = new CreateNodeGroupRequest();
|
||||
req.setNodegroupName(name);
|
||||
req.setNodeRole("arn:aws:iam::" + ACCOUNT + ":role/eks-node-role");
|
||||
req.setSubnets(List.of("subnet-12345678"));
|
||||
return req;
|
||||
}
|
||||
|
||||
private Ec2Service realEc2Service() {
|
||||
EmulatorConfig ec2Config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.Ec2ServiceConfig ec2ServiceConfig = mock(EmulatorConfig.Ec2ServiceConfig.class);
|
||||
when(ec2Config.defaultAccountId()).thenReturn(ACCOUNT);
|
||||
when(ec2Config.services()).thenReturn(services);
|
||||
when(services.ec2()).thenReturn(ec2ServiceConfig);
|
||||
when(ec2ServiceConfig.mock()).thenReturn(true);
|
||||
|
||||
StorageFactory storageFactory = new StorageFactory(null, null) {
|
||||
@Override
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return AccountAwareStorageBackend.inMemory(ACCOUNT);
|
||||
}
|
||||
};
|
||||
|
||||
return new Ec2Service(ec2Config, mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class),
|
||||
mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(), storageFactory);
|
||||
}
|
||||
|
||||
private EmulatorConfig testConfig() {
|
||||
EmulatorConfig.EksServiceConfig eksConfig = proxy(EmulatorConfig.EksServiceConfig.class,
|
||||
(p, method, args) -> switch (method.getName()) {
|
||||
case "enabled" -> true;
|
||||
case "mock" -> true;
|
||||
case "apiServerBasePort" -> 6500;
|
||||
default -> defaultValue(method);
|
||||
});
|
||||
|
||||
EmulatorConfig.ServicesConfig services = proxy(EmulatorConfig.ServicesConfig.class,
|
||||
(p, method, args) -> "eks".equals(method.getName()) ? eksConfig : defaultValue(method));
|
||||
|
||||
return proxy(EmulatorConfig.class, (p, method, args) -> switch (method.getName()) {
|
||||
case "defaultRegion" -> REGION;
|
||||
case "defaultAccountId" -> ACCOUNT;
|
||||
case "services" -> services;
|
||||
default -> defaultValue(method);
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T proxy(Class<T> type, InvocationHandler handler) {
|
||||
return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type }, handler);
|
||||
}
|
||||
|
||||
private static Object defaultValue(Method method) {
|
||||
Class<?> r = method.getReturnType();
|
||||
if (r == boolean.class) return false;
|
||||
if (r == int.class) return 0;
|
||||
if (r == long.class) return 0L;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+27
@@ -1,11 +1,19 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -75,6 +83,9 @@ class EksNodegroupIntegrationTest {
|
||||
.body("nodegroup.nodeRole", equalTo(NODE_ROLE));
|
||||
}
|
||||
|
||||
@Inject
|
||||
Ec2Service ec2Service;
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void deleteNodeGroup() {
|
||||
@@ -87,4 +98,20 @@ class EksNodegroupIntegrationTest {
|
||||
.when().get("/clusters/" + CLUSTER + "/node-groups/ng1")
|
||||
.then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void createNodeGroupWithLaunchTemplateUserData() {
|
||||
LaunchTemplateData data = new LaunchTemplateData();
|
||||
data.setUserData(Base64.getEncoder().encodeToString("#!/bin/sh\necho hi".getBytes(StandardCharsets.UTF_8)));
|
||||
LaunchTemplate lt = ec2Service.createLaunchTemplate("us-east-1", "eks-it-lt", data, List.of());
|
||||
|
||||
given().contentType(JSON)
|
||||
.body("{\"nodegroupName\":\"ng-lt\",\"subnets\":[\"subnet-abc\"],\"nodeRole\":\"" + NODE_ROLE + "\","
|
||||
+ "\"launchTemplate\":{\"id\":\"" + lt.getLaunchTemplateId() + "\",\"version\":\"1\"}}")
|
||||
.when().post("/clusters/" + CLUSTER + "/node-groups")
|
||||
.then().statusCode(200)
|
||||
.body("nodegroup.nodegroupName", equalTo("ng-lt"))
|
||||
.body("nodegroup.launchTemplate.id", equalTo(lt.getLaunchTemplateId()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user