mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ec2): add Java-built Ubuntu AMI guest image (#1542)
This commit is contained in:
1 parent
e5a646d40c
commit
ad785f95f6
18 files changed
+822
-16
No files matched your search
@@ -87,6 +87,17 @@ class ContainerBuilderTest {
|
||||
assertEquals(List.of(), spec.dnsServers());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCgroupnsModeRecordsDockerNamespaceMode() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
|
||||
ContainerSpec spec = fixture.builder.newContainer("alpine")
|
||||
.withCgroupnsMode("host")
|
||||
.build();
|
||||
|
||||
assertEquals("host", spec.cgroupnsMode());
|
||||
}
|
||||
|
||||
private static class TestFixture {
|
||||
final EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
final EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
|
||||
@@ -245,6 +245,29 @@ class Ec2ContainerManagerTest {
|
||||
"http://floci:9169"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchSystemdGuestUsesInitInsteadOfTail() throws Exception {
|
||||
Ec2ContainerManager.containerBridgeIpAttempts = 1;
|
||||
Ec2ContainerManager.containerBridgeIpPollMillis = 1;
|
||||
LaunchHarness harness = launchHarness();
|
||||
InspectContainerCmd inspect = mock(InspectContainerCmd.class);
|
||||
InspectContainerResponse withIp = inspectResponse("172.18.0.11");
|
||||
when(harness.dockerClient.inspectContainerCmd(TEST_CONTAINER_ID)).thenReturn(inspect);
|
||||
when(inspect.exec()).thenReturn(withIp);
|
||||
harness.stubSuccessfulExecs(new CountDownLatch(0), new CountDownLatch(0));
|
||||
Instance instance = instance("i-systemd");
|
||||
|
||||
harness.manager.launch(instance,
|
||||
new ResolvedAmiImage("floci/ami-ubuntu:24.04-arm64", ResolvedAmiImage.SYSTEMD_RUNTIME, true),
|
||||
null,
|
||||
"us-west-2");
|
||||
|
||||
awaitUntil(() -> "running".equals(instance.getState().getName()), Duration.ofSeconds(2));
|
||||
verify(harness.builder).withCmd(List.of("/sbin/init"));
|
||||
verify(harness.builder).withCgroupnsMode("host");
|
||||
verify(harness.builder).withBind("/sys/fs/cgroup", "/sys/fs/cgroup");
|
||||
}
|
||||
|
||||
@Test
|
||||
void preferredMetadataSourceIpUsesConfiguredNetworkBeforeBridge() {
|
||||
ContainerNetwork bridge = new ContainerNetwork();
|
||||
@@ -380,7 +403,7 @@ class Ec2ContainerManagerTest {
|
||||
portAllocator,
|
||||
config,
|
||||
metadataServer);
|
||||
return new LaunchHarness(manager, dockerClient, metadataServer, logStreamer);
|
||||
return new LaunchHarness(manager, dockerClient, metadataServer, logStreamer, builder);
|
||||
}
|
||||
|
||||
private static Instance instance(String instanceId) {
|
||||
@@ -414,7 +437,8 @@ class Ec2ContainerManagerTest {
|
||||
private record LaunchHarness(Ec2ContainerManager manager,
|
||||
DockerClient dockerClient,
|
||||
Ec2MetadataServer metadataServer,
|
||||
ContainerLogStreamer logStreamer) {
|
||||
ContainerLogStreamer logStreamer,
|
||||
ContainerBuilder.Builder builder) {
|
||||
void stubSuccessfulExecs(CountDownLatch userDataStarted, CountDownLatch finishUserData) throws Exception {
|
||||
AtomicReference<String[]> currentCommand = new AtomicReference<>();
|
||||
ExecCreateCmd execCreate = mock(ExecCreateCmd.class, withSettings().defaultAnswer(RETURNS_SELF));
|
||||
|
||||
@@ -35,6 +35,7 @@ class Ec2ImageCatalogTest {
|
||||
"ami-ubuntu2204",
|
||||
"ami-ubuntu2404-arm64",
|
||||
"ami-ubuntu2404-amd64",
|
||||
"ami-ubuntu2404-cloud-arm64",
|
||||
"ami-debian12",
|
||||
"ami-alpine",
|
||||
"ami-0abcdef1234567893"), imageIds);
|
||||
@@ -43,6 +44,7 @@ class Ec2ImageCatalogTest {
|
||||
assertTrue(imageCatalog.findByIdOrAlias("ami-amazonlinux2023").isPresent());
|
||||
assertTrue(imageCatalog.findByIdOrAlias("ami-ubuntu2004").isPresent());
|
||||
assertTrue(imageCatalog.findByIdOrAlias("ami-ubuntu2404").isPresent());
|
||||
assertTrue(imageCatalog.findByIdOrAlias("ami-ubuntu2404-cloud").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,6 +58,16 @@ class Ec2ImageCatalogTest {
|
||||
amiImageResolver.resolve(alias)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolverExposesCloudImageGuestRuntimeMetadata() {
|
||||
ResolvedAmiImage image = amiImageResolver.resolveImage("ami-ubuntu2404-cloud");
|
||||
|
||||
assertEquals("floci/ami-ubuntu:24.04-arm64", image.dockerImage());
|
||||
assertEquals(ResolvedAmiImage.SYSTEMD_RUNTIME, image.guestRuntime());
|
||||
assertTrue(image.cloudInit());
|
||||
assertTrue(image.systemd());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownImageUsesCatalogDefault() {
|
||||
assertFalse(imageCatalog.findByIdOrAlias("ami-unknown").isPresent());
|
||||
|
||||
@@ -223,9 +223,10 @@ class Ec2IntegrationTest {
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(1))
|
||||
.body("DescribeImagesResponse.imagesSet.item.imageId", equalTo("ami-ubuntu2404-arm64"))
|
||||
.body("DescribeImagesResponse.imagesSet.item.architecture", equalTo("arm64"));
|
||||
.body("DescribeImagesResponse.imagesSet.item.size()", equalTo(2))
|
||||
.body("DescribeImagesResponse.imagesSet.item.imageId",
|
||||
containsInAnyOrder("ami-ubuntu2404-arm64", "ami-ubuntu2404-cloud-arm64"))
|
||||
.body("DescribeImagesResponse.imagesSet.item.architecture", everyItem(equalTo("arm64")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -77,6 +77,22 @@ class Ec2ServiceTest {
|
||||
assertEquals("source", version.getInstanceTags().getFirst().getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeImagesAdvertisesCloudGuestWithoutChangingUbuntuDefault() {
|
||||
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
|
||||
AmiImageResolver amiImageResolver = new AmiImageResolver(imageCatalog);
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
amiImageResolver, imageCatalog, new InMemoryStorageFactory());
|
||||
|
||||
assertTrue(service.describeImages("us-east-1", List.of(), List.of()).stream()
|
||||
.anyMatch(image -> "ami-ubuntu2404-cloud-arm64".equals(image.getImageId())));
|
||||
assertEquals("public.ecr.aws/docker/library/ubuntu:24.04", amiImageResolver.resolve("ami-ubuntu2404"));
|
||||
|
||||
ResolvedAmiImage resolved = amiImageResolver.resolveImage("ami-ubuntu2404-cloud");
|
||||
assertEquals("floci/ami-ubuntu:24.04-arm64", resolved.dockerImage());
|
||||
assertTrue(resolved.systemd());
|
||||
}
|
||||
|
||||
private static EmulatorConfig mockConfig(boolean ec2Mock) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
package io.github.hectorvent.floci.tools.ami;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.HexFormat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
class AmiImageToolTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void metadataDrivesGenerationAndCatalogUpdate() throws Exception {
|
||||
Path rootfs = tempDir.resolve("ubuntu-root.tar.xz");
|
||||
Path manifest = tempDir.resolve("ubuntu.manifest");
|
||||
Files.writeString(rootfs, "rootfs", StandardCharsets.UTF_8);
|
||||
Files.writeString(manifest, "systemd\t1\ncloud-init\t1\n", StandardCharsets.UTF_8);
|
||||
Path metadata = tempDir.resolve("image-build-metadata.yaml");
|
||||
Files.writeString(metadata, """
|
||||
releaseId: test-release
|
||||
images:
|
||||
- id: ubuntu-24.04-arm64
|
||||
catalogImageId: ami-ubuntu2404-cloud-arm64
|
||||
catalogAliases: [ami-ubuntu2404-cloud]
|
||||
family: ubuntu
|
||||
version: "24.04"
|
||||
architecture: arm64
|
||||
aws:
|
||||
region: us-east-1
|
||||
ownerId: "099720109477"
|
||||
imageId: ami-source
|
||||
name: ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-arm64-server-20260515
|
||||
creationDate: "2026-05-15T00:00:00.000Z"
|
||||
virtualizationType: hvm
|
||||
rootDeviceType: ebs
|
||||
canonical:
|
||||
baseUrl: %s
|
||||
rootfs: %s
|
||||
rootfsSha256: %s
|
||||
manifest: %s
|
||||
docker:
|
||||
image: floci/ami-ubuntu:24.04-arm64
|
||||
guest:
|
||||
runtime: systemd
|
||||
cloudInit: true
|
||||
smokePackages: [systemd, cloud-init]
|
||||
""".formatted(tempDir.toUri(), rootfs.getFileName(), sha256(rootfs), manifest.getFileName()));
|
||||
|
||||
AmiImageTool.Metadata loaded = AmiImageTool.loadMetadata(metadata);
|
||||
AmiImageTool.ImageSpec image = loaded.images.getFirst();
|
||||
Path context = AmiImageTool.generate(image, tempDir.resolve("out"), true);
|
||||
|
||||
assertTrue(Files.readString(context.resolve("Dockerfile")).contains("FROM scratch"));
|
||||
assertTrue(Files.readString(context.resolve("Dockerfile")).contains("io.floci.ami.catalog-image-id"));
|
||||
assertTrue(Files.readString(context.resolve("Dockerfile")).contains("ADD ubuntu-root.tar.xz /"));
|
||||
assertTrue(Files.readString(context.resolve("provenance.yaml")).contains("manifestSha256"));
|
||||
|
||||
Path catalog = tempDir.resolve("image-catalog.yaml");
|
||||
Files.writeString(catalog, """
|
||||
defaultDockerImage: public.ecr.aws/amazonlinux/amazonlinux:2023
|
||||
images:
|
||||
- imageId: ami-existing
|
||||
dockerImage: public.ecr.aws/docker/library/ubuntu:24.04
|
||||
name: existing
|
||||
description: existing
|
||||
architecture: arm64
|
||||
creationDate: "2026-01-01T00:00:00.000Z"
|
||||
""");
|
||||
Path output = tempDir.resolve("custom-output");
|
||||
AmiImageTool.updateCatalog(image, catalog, output, true);
|
||||
String catalogText = Files.readString(catalog);
|
||||
assertTrue(catalogText.contains("ami-existing"));
|
||||
assertTrue(catalogText.contains("ami-ubuntu2404-cloud-arm64"));
|
||||
assertTrue(catalogText.contains("guestRuntime: \"systemd\""));
|
||||
assertTrue(catalogText.contains("cloudInit: true"));
|
||||
assertTrue(catalogText.contains("custom-output/ubuntu-24.04-arm64/provenance.yaml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void metadataRejectsDockerLibraryUbuntuAsSource() throws Exception {
|
||||
Path badRootfsMetadata = tempDir.resolve("bad-rootfs.yaml");
|
||||
Files.writeString(badRootfsMetadata, """
|
||||
releaseId: bad
|
||||
images:
|
||||
- id: bad
|
||||
catalogImageId: ami-bad
|
||||
family: ubuntu
|
||||
version: "24.04"
|
||||
architecture: arm64
|
||||
aws:
|
||||
region: us-east-1
|
||||
ownerId: "099720109477"
|
||||
imageId: ami-bad-source
|
||||
name: bad
|
||||
creationDate: "2026-05-15T00:00:00.000Z"
|
||||
virtualizationType: hvm
|
||||
rootDeviceType: ebs
|
||||
canonical:
|
||||
baseUrl: https://example.com
|
||||
rootfs: ubuntu:24.04
|
||||
rootfsSha256: abc
|
||||
manifest: manifest
|
||||
docker: { image: floci/ami-ubuntu:24.04-arm64 }
|
||||
guest: { runtime: systemd, cloudInit: true }
|
||||
""");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> AmiImageTool.loadMetadata(badRootfsMetadata));
|
||||
|
||||
Path badDockerMetadata = tempDir.resolve("bad-docker.yaml");
|
||||
Files.writeString(badDockerMetadata, """
|
||||
releaseId: bad
|
||||
images:
|
||||
- id: bad
|
||||
catalogImageId: ami-bad
|
||||
family: ubuntu
|
||||
version: "24.04"
|
||||
architecture: arm64
|
||||
aws:
|
||||
region: us-east-1
|
||||
ownerId: "099720109477"
|
||||
imageId: ami-bad-source
|
||||
name: bad
|
||||
creationDate: "2026-05-15T00:00:00.000Z"
|
||||
virtualizationType: hvm
|
||||
rootDeviceType: ebs
|
||||
canonical:
|
||||
baseUrl: https://example.com
|
||||
rootfs: ubuntu-root.tar.xz
|
||||
rootfsSha256: abc
|
||||
manifest: manifest
|
||||
docker: { image: public.ecr.aws/docker/library/ubuntu:24.04 }
|
||||
guest: { runtime: systemd, cloudInit: true }
|
||||
""");
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> AmiImageTool.loadMetadata(badDockerMetadata));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toolTreeDoesNotContainShellScripts() throws Exception {
|
||||
Path tools = Path.of("src/main/java/io/github/hectorvent/floci/tools/ami");
|
||||
if (Files.isDirectory(tools)) {
|
||||
try (var stream = Files.walk(tools)) {
|
||||
assertFalse(stream.anyMatch(path -> path.getFileName().toString().endsWith(".sh")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String sha256(Path path) throws Exception {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
digest.update(Files.readAllBytes(path));
|
||||
return HexFormat.of().formatHex(digest.digest());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user