fix(ec2): align AMI guest and launch template parity (#1597)

* fix(ec2): preserve launch template instance profiles

* fix(ec2): skip networkd wait-online in AMI guests

* feat(docker): add image registry override

* fix(ec2): align launch template AMI parity

* fix(docker): simplify registry base config handling

* fix(docker): normalize registry base once
This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-07-01 22:05:06 -05:00
1 parent 6223fc4a02
commit fef03c6b0f
16 files changed
+273 -21

No files matched your search

@@ -98,6 +98,31 @@ class ContainerBuilderTest {
assertEquals("host", spec.cgroupnsMode());
}
@Test
void imageRegistryBasePrefixesEveryContainerImage() {
TestFixture fixture = new TestFixture();
when(fixture.docker.imageRegistryBase()).thenReturn(Optional.of("ghcr.io/floci-io/mirror/"));
assertEquals(
"ghcr.io/floci-io/mirror/postgres:16-alpine",
fixture.builder.newContainer("postgres:16-alpine").build().image());
assertEquals(
"ghcr.io/floci-io/mirror/public.ecr.aws/docker/library/ubuntu:24.04",
fixture.builder.newContainer("public.ecr.aws/docker/library/ubuntu:24.04").build().image());
}
@Test
void imageRegistryBaseDoesNotDoublePrefixImagesAlreadyUnderBase() {
TestFixture fixture = new TestFixture();
when(fixture.docker.imageRegistryBase()).thenReturn(Optional.of("ghcr.io/floci-io/mirror"));
ContainerSpec spec = fixture.builder
.newContainer("ghcr.io/floci-io/mirror/floci/ami-ubuntu:24.04-arm64")
.build();
assertEquals("ghcr.io/floci-io/mirror/floci/ami-ubuntu:24.04-arm64", spec.image());
}
private static class TestFixture {
final EmulatorConfig config = mock(EmulatorConfig.class);
final EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
@@ -116,6 +141,7 @@ class ContainerBuilderTest {
when(config.docker()).thenReturn(docker);
when(docker.logMaxSize()).thenReturn("10m");
when(docker.logMaxFile()).thenReturn("3");
when(docker.imageRegistryBase()).thenReturn(Optional.empty());
when(config.dns()).thenReturn(dns);
when(dns.containerFallbackEnabled()).thenReturn(true);
when(dns.containerFallbackServers()).thenReturn(List.of("8.8.8.8", "8.8.4.4"));
@@ -75,6 +75,7 @@ class AutoScalingReconcilerTest {
version.setLatestVersionNumber("1");
version.setImageId("ami-version-1");
version.setInstanceType("t3.micro");
version.setIamInstanceProfileArn("arn:aws:iam::000000000000:instance-profile/app-profile");
List<Tag> instanceTags = List.of(new Tag("app.ClusterId", "development"));
version.setInstanceTags(instanceTags);
when(ec2Service.describeLaunchTemplates("us-east-1", List.of("lt-123"), List.of(), Map.of()))
@@ -87,7 +88,7 @@ class AutoScalingReconcilerTest {
reservation.setInstances(List.of(ec2Instance));
when(ec2Service.runInstances(eq("us-east-1"), eq("ami-version-1"), eq("t3.micro"),
eq(1), eq(1), eq(null), eq(List.of()), eq(null), eq(null),
eq(instanceTags), eq(null), eq(null))).thenReturn(reservation);
eq(instanceTags), eq(null), eq("arn:aws:iam::000000000000:instance-profile/app-profile"))).thenReturn(reservation);
reconciler.reconcile(asg);
@@ -97,7 +98,7 @@ class AutoScalingReconcilerTest {
assertEquals("1", asg.getInstances().getFirst().getLaunchTemplateVersion());
verify(ec2Service).runInstances(eq("us-east-1"), eq("ami-version-1"), eq("t3.micro"),
eq(1), eq(1), eq(null), eq(List.of()), eq(null), eq(null),
eq(instanceTags), eq(null), eq(null));
eq(instanceTags), eq(null), eq("arn:aws:iam::000000000000:instance-profile/app-profile"));
}
@Test
@@ -764,13 +764,14 @@ class Ec2IntegrationTest {
.formParam("LaunchTemplateData.ImageId", "ami-0abcdef1234567890")
.formParam("LaunchTemplateData.InstanceType", "t3.micro")
.formParam("LaunchTemplateData.KeyName", "test-key")
.formParam("LaunchTemplateData.IamInstanceProfile.Name", "sample-profile")
.formParam("LaunchTemplateData.SecurityGroupId.1", securityGroupId)
.formParam("LaunchTemplateData.UserData", gzipBase64("#!/bin/sh\necho launch-template\n"))
.formParam("LaunchTemplateData.TagSpecification.1.ResourceType", "instance")
.formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Key", "example:ClusterId")
.formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Value", "sample-template")
.formParam("LaunchTemplateData.TagSpecification.1.Tag.2.Key", "example:NodeType")
.formParam("LaunchTemplateData.TagSpecification.1.Tag.2.Value", "COORDINATOR")
.formParam("LaunchTemplateData.TagSpecification.1.Tag.2.Value", "PRIMARY")
.formParam("TagSpecification.1.ResourceType", "launch-template")
.formParam("TagSpecification.1.Tag.1.Key", "Name")
.formParam("TagSpecification.1.Tag.1.Value", "sample-template")
@@ -822,12 +823,14 @@ class Ec2IntegrationTest {
equalTo("t3.micro"))
.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.userData",
equalTo("#!/bin/sh\necho launch-template\n"))
.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.iamInstanceProfile.arn",
equalTo("arn:aws:iam::000000000000:instance-profile/sample-profile"))
.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.tagSpecificationSet.item.resourceType",
equalTo("instance"))
.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.tagSpecificationSet.item.tagSet.item.find { it.key == 'example:ClusterId' }.value",
equalTo("sample-template"))
.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.tagSpecificationSet.item.tagSet.item.find { it.key == 'example:NodeType' }.value",
equalTo("COORDINATOR"));
equalTo("PRIMARY"));
}
@Test
@@ -842,11 +845,12 @@ class Ec2IntegrationTest {
.formParam("LaunchTemplateData.ImageId", "ami-0abcdef1234567890")
.formParam("LaunchTemplateData.InstanceType", "t3.small")
.formParam("LaunchTemplateData.KeyName", "test-key")
.formParam("LaunchTemplateData.IamInstanceProfile.Name", "sample-profile-v2")
.formParam("LaunchTemplateData.SecurityGroupId.1", securityGroupId)
.formParam("LaunchTemplateData.UserData", gzipBase64("#!/bin/sh\necho launch-template-version\n"))
.formParam("LaunchTemplateData.TagSpecification.1.ResourceType", "instance")
.formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Key", "example:NodeType")
.formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Value", "WORKER")
.formParam("LaunchTemplateData.TagSpecification.1.Tag.1.Value", "SECONDARY")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
@@ -862,8 +866,10 @@ class Ec2IntegrationTest {
equalTo("t3.small"))
.body("CreateLaunchTemplateVersionResponse.launchTemplateVersion.launchTemplateData.userData",
equalTo("#!/bin/sh\necho launch-template-version\n"))
.body("CreateLaunchTemplateVersionResponse.launchTemplateVersion.launchTemplateData.iamInstanceProfile.arn",
equalTo("arn:aws:iam::000000000000:instance-profile/sample-profile-v2"))
.body("CreateLaunchTemplateVersionResponse.launchTemplateVersion.launchTemplateData.tagSpecificationSet.item.tagSet.item.find { it.key == 'example:NodeType' }.value",
equalTo("WORKER"));
equalTo("SECONDARY"));
given()
.formParam("Action", "DescribeLaunchTemplateVersions")
@@ -883,7 +889,7 @@ class Ec2IntegrationTest {
.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.userData",
equalTo("#!/bin/sh\necho launch-template\n"))
.body("DescribeLaunchTemplateVersionsResponse.launchTemplateVersionSet.item.launchTemplateData.tagSpecificationSet.item.tagSet.item.find { it.key == 'example:NodeType' }.value",
equalTo("COORDINATOR"));
equalTo("PRIMARY"));
}
@Test
@@ -928,6 +934,55 @@ class Ec2IntegrationTest {
@Test
@Order(49)
void runInstancesResolvesLaunchTemplateDefaultsWithRequestOverrides() {
String launchedInstanceId = given()
.formParam("Action", "RunInstances")
.formParam("LaunchTemplate.LaunchTemplateId", launchTemplateId)
.formParam("LaunchTemplate.Version", "$Default")
.formParam("ImageId", "ami-sample-override")
.formParam("InstanceType", "c7g.large")
.formParam("MinCount", "1")
.formParam("MaxCount", "1")
.formParam("TagSpecification.1.ResourceType", "instance")
.formParam("TagSpecification.1.Tag.1.Key", "example:NodeType")
.formParam("TagSpecification.1.Tag.1.Value", "REQUEST")
.formParam("TagSpecification.1.Tag.2.Key", "sample-name")
.formParam("TagSpecification.1.Tag.2.Value", "sample-local")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("RunInstancesResponse.instancesSet.item.imageId", equalTo("ami-sample-override"))
.body("RunInstancesResponse.instancesSet.item.instanceType", equalTo("c7g.large"))
.body("RunInstancesResponse.instancesSet.item.keyName", equalTo("test-key"))
.body("RunInstancesResponse.instancesSet.item.iamInstanceProfile.arn",
equalTo("arn:aws:iam::000000000000:instance-profile/sample-profile-v2"))
.body("RunInstancesResponse.instancesSet.item.groupSet.item.groupId", equalTo(securityGroupId))
.body("RunInstancesResponse.instancesSet.item.tagSet.item.find { it.key == 'example:NodeType' }.value",
equalTo("REQUEST"))
.body("RunInstancesResponse.instancesSet.item.tagSet.item.findAll { it.key == 'example:NodeType' }.size()",
equalTo(1))
.body("RunInstancesResponse.instancesSet.item.tagSet.item.find { it.key == 'sample-name' }.value",
equalTo("sample-local"))
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
given()
.formParam("Action", "DescribeInstances")
.formParam("InstanceId.1", launchedInstanceId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.instanceId",
equalTo(launchedInstanceId))
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.iamInstanceProfile.arn",
equalTo("arn:aws:iam::000000000000:instance-profile/sample-profile-v2"));
}
@Test
@Order(50)
void deleteLaunchTemplate() {
given()
.formParam("Action", "DeleteLaunchTemplate")
@@ -1570,8 +1625,7 @@ class Ec2IntegrationTest {
.post("/")
.then()
.statusCode(200)
.body("DescribeTagsResponse.tagSet.item.key", equalTo("Name"))
.body("DescribeTagsResponse.tagSet.item.value", equalTo("test-instance"));
.body("DescribeTagsResponse.tagSet.item.find { it.key == 'Name' }.value", equalTo("test-instance"));
}
@Test
@@ -2,12 +2,16 @@ package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.model.InstanceProfile;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class Ec2MetadataServerTest {
@@ -50,7 +54,7 @@ class Ec2MetadataServerTest {
@Test
void staleContainerUnregisterDoesNotRemoveCurrentRegistration() {
Ec2MetadataServer server = new Ec2MetadataServer(null, null);
Ec2MetadataServer server = new Ec2MetadataServer(null, null, null);
Instance oldInstance = new Instance();
oldInstance.setInstanceId("i-old");
Instance currentInstance = new Instance();
@@ -64,4 +68,18 @@ class Ec2MetadataServerTest {
currentInstance,
server.registeredContainer("192.168.215.7").orElseThrow());
}
@Test
void iamCredentialRoleNameComesFromInstanceProfileRole() {
IamService iamService = mock(IamService.class);
InstanceProfile profile = new InstanceProfile();
profile.setInstanceProfileName("sample-profile");
profile.setRoleNames(List.of("sample-role"));
when(iamService.getInstanceProfile("sample-profile")).thenReturn(profile);
Ec2MetadataServer server = new Ec2MetadataServer(null, null, iamService);
assertEquals("sample-role", server.resolveRoleName(
"arn:aws:iam::000000000000:instance-profile/sample-profile"));
}
}
@@ -62,10 +62,11 @@ class Ec2ServiceTest {
new InMemoryStorageFactory());
LaunchTemplate template = service.createLaunchTemplate("us-east-1", "app-template",
"ami-source", "t3.micro", "app-key", List.of("sg-source"),
"source-user-data", List.of(), List.of(new Tag("Role", "source")));
"source-user-data", "arn:aws:iam::000000000000:instance-profile/app-profile",
List.of(), List.of(new Tag("Role", "source")));
service.createLaunchTemplateVersion("us-east-1", template.getLaunchTemplateId(), null,
"1", null, "t3.small", null, List.of(), null, List.of());
"1", null, "t3.small", null, List.of(), null, null, List.of());
LaunchTemplate version = service.describeLaunchTemplateVersions(
"us-east-1", template.getLaunchTemplateId(), null, List.of("2")).getFirst();
@@ -74,6 +75,7 @@ class Ec2ServiceTest {
assertEquals("app-key", version.getKeyName());
assertEquals(List.of("sg-source"), version.getSecurityGroupIds());
assertEquals("source-user-data", version.getUserData());
assertEquals("arn:aws:iam::000000000000:instance-profile/app-profile", version.getIamInstanceProfileArn());
assertEquals("2", version.getLatestVersionNumber());
assertEquals(1, version.getInstanceTags().size());
assertEquals("Role", version.getInstanceTags().getFirst().getKey());
@@ -62,6 +62,9 @@ class AmiImageToolTest {
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("Dockerfile")).contains("useradd --uid 1000 --gid ubuntu"));
assertTrue(Files.readString(context.resolve("Dockerfile")).contains("systemd-networkd-wait-online.service.d/floci.conf"));
assertTrue(Files.readString(context.resolve("systemd-networkd-wait-online-floci.conf")).contains("ExecStart=/bin/true"));
assertTrue(Files.readString(context.resolve("provenance.yaml")).contains("manifestSha256"));
Path catalog = tempDir.resolve("image-catalog.yaml");