mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ec2): honor AMI architecture for containers (#3096)
* fix(ec2): honor AMI architecture for containers * fix(ec2): preserve created AMI architecture
This commit is contained in:
1 parent
18044d966f
commit
43cdbe234f
7 files changed
+120
-21
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -37,16 +38,33 @@ public class AmiImageResolver {
|
||||
}
|
||||
|
||||
return imageCatalog.findByIdOrAlias(imageId)
|
||||
.map(image -> new ResolvedAmiImage(
|
||||
image.dockerImage,
|
||||
image.guestRuntime == null || image.guestRuntime.isBlank()
|
||||
? ResolvedAmiImage.DEFAULT_RUNTIME
|
||||
: image.guestRuntime,
|
||||
Boolean.TRUE.equals(image.cloudInit)))
|
||||
.map(image -> resolveCatalogImage(image))
|
||||
.orElseGet(() -> {
|
||||
LOG.warnv("Unknown AMI ID {0}; falling back to default image {1}",
|
||||
imageId, imageCatalog.defaultDockerImage());
|
||||
return ResolvedAmiImage.minimal(imageCatalog.defaultDockerImage());
|
||||
});
|
||||
}
|
||||
|
||||
private ResolvedAmiImage resolveCatalogImage(Ec2ImageCatalog.CatalogImage image) {
|
||||
if ("windows".equalsIgnoreCase(image.platform)) {
|
||||
throw new AwsException("UnsupportedOperation",
|
||||
"Windows AMI execution is not supported by Floci's Linux container runtime.", 400);
|
||||
}
|
||||
return new ResolvedAmiImage(
|
||||
image.dockerImage,
|
||||
image.guestRuntime == null || image.guestRuntime.isBlank()
|
||||
? ResolvedAmiImage.DEFAULT_RUNTIME
|
||||
: image.guestRuntime,
|
||||
Boolean.TRUE.equals(image.cloudInit),
|
||||
dockerPlatform(image.architecture));
|
||||
}
|
||||
|
||||
private static String dockerPlatform(String architecture) {
|
||||
return switch (architecture) {
|
||||
case "arm64" -> "linux/arm64";
|
||||
case "x86_64" -> "linux/amd64";
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -298,7 +298,9 @@ public class Ec2ContainerManager {
|
||||
String containerId = null;
|
||||
boolean recorded = false;
|
||||
try {
|
||||
containerId = lifecycleManager.create(spec);
|
||||
containerId = image.dockerPlatform() == null
|
||||
? lifecycleManager.create(spec)
|
||||
: lifecycleManager.create(spec, image.dockerPlatform());
|
||||
if (!recordCreatedContainer(instance, containerId, sshHostPort)) {
|
||||
lifecycleManager.removeIfExists(containerId);
|
||||
portAllocator.release(sshHostPort);
|
||||
|
||||
@@ -2318,9 +2318,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
reservation.setOwnerId(accountId);
|
||||
|
||||
String effectiveInstanceType = instanceType != null ? instanceType : "t2.micro";
|
||||
validateArchitectureCompatibility(imageId, effectiveInstanceType);
|
||||
validateArchitectureCompatibility(region, imageId, effectiveInstanceType);
|
||||
int count = Math.min(maxCount, Math.max(minCount, 1));
|
||||
String architecture = architectureFor(imageId, effectiveInstanceType);
|
||||
String architecture = architectureFor(region, imageId, effectiveInstanceType);
|
||||
ResolvedAmiImage dockerImage = null;
|
||||
if (!config.services().ec2().mock()) {
|
||||
// A CreateImage AMI is not in the catalog, so resolve through its source.
|
||||
dockerImage = amiImageResolver.resolveImage(resolveLaunchableImageId(region, imageId));
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
String instanceId = "i-" + randomHex(17);
|
||||
String privateIp = suppliedEni != null
|
||||
@@ -2420,9 +2425,6 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
reservation.getInstances().add(inst);
|
||||
|
||||
if (!config.services().ec2().mock()) {
|
||||
// A CreateImage AMI is not in the catalog, so resolve through its source.
|
||||
ResolvedAmiImage dockerImage =
|
||||
amiImageResolver.resolveImage(resolveLaunchableImageId(region, imageId));
|
||||
String publicKey = null;
|
||||
if (keyName != null) {
|
||||
KeyPair kp = findKeyPair(region, keyName);
|
||||
@@ -2531,9 +2533,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private void validateArchitectureCompatibility(String imageId, String instanceType) {
|
||||
Optional<String> imageArchitecture = imageCatalog.findByIdOrAlias(imageId)
|
||||
.map(image -> image.architecture)
|
||||
private void validateArchitectureCompatibility(String region, String imageId, String instanceType) {
|
||||
Optional<String> imageArchitecture = registeredImages.get(key(region, imageId))
|
||||
.map(Image::getArchitecture)
|
||||
.or(() -> imageCatalog.findByIdOrAlias(imageId).map(image -> image.architecture))
|
||||
.filter(value -> !value.isBlank());
|
||||
if (imageArchitecture.isEmpty()) {
|
||||
return;
|
||||
@@ -2550,9 +2553,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
|
||||
});
|
||||
}
|
||||
|
||||
private String architectureFor(String imageId, String instanceType) {
|
||||
Optional<Ec2ImageCatalog.CatalogImage> image = imageCatalog.findByIdOrAlias(imageId);
|
||||
return image.map(catalogImage -> catalogImage.architecture)
|
||||
private String architectureFor(String region, String imageId, String instanceType) {
|
||||
Optional<String> registeredArchitecture = registeredImages.get(key(region, imageId))
|
||||
.map(Image::getArchitecture);
|
||||
return registeredArchitecture
|
||||
.filter(value -> !value.isBlank())
|
||||
.or(() -> imageCatalog.findByIdOrAlias(imageId).map(image -> image.architecture))
|
||||
.filter(value -> !value.isBlank())
|
||||
.or(() -> instanceTypeCatalog.find(instanceType)
|
||||
.flatMap(type -> type.supportedArchitectures.stream()
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
public record ResolvedAmiImage(String dockerImage, String guestRuntime, boolean cloudInit) {
|
||||
public record ResolvedAmiImage(String dockerImage, String guestRuntime, boolean cloudInit,
|
||||
String dockerPlatform) {
|
||||
public static final String DEFAULT_RUNTIME = "minimal";
|
||||
public static final String SYSTEMD_RUNTIME = "systemd";
|
||||
|
||||
public static ResolvedAmiImage minimal(String dockerImage) {
|
||||
return new ResolvedAmiImage(dockerImage, DEFAULT_RUNTIME, false);
|
||||
return new ResolvedAmiImage(dockerImage, DEFAULT_RUNTIME, false, null);
|
||||
}
|
||||
|
||||
public boolean systemd() {
|
||||
|
||||
Reference in new issue
Block a user