fix(ec2): embed image catalog in native image and load it lazily (#1308)

RunInstances 500d on the native image because ec2/image-catalog.yaml was not
embedded: the app-local resource-config.json pattern is not reliably honored by
Quarkus' native build. Register the catalog via quarkus.native.resources.includes,
mirroring the pricing-snapshots entry.

Also load the catalog lazily instead of in the bean constructor so a missing or
unparseable resource no longer fails EC2 bean creation, which keeps mock mode
(which never reads the catalog) working.

Refs #1296
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-06-13 01:48:34 -05:00
1 parent 161a6ba7e0
commit 2c057db396
2 files changed
+41 -16

No files matched your search

@@ -22,36 +22,60 @@ public class Ec2ImageCatalog {
private static final String CATALOG_RESOURCE_NAME = "ec2/image-catalog.yaml";
private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());
private final String defaultDockerImage;
private final List<CatalogImage> images;
private final Map<String, CatalogImage> imagesByIdOrAlias;
private volatile Loaded loaded;
public Ec2ImageCatalog() {
this(readResource(CATALOG_RESOURCE_NAME, Catalog.class));
// The catalog is parsed lazily on first access rather than at bean
// construction. Mock mode never reads it, so eager loading would fail
// EC2 bean creation needlessly when the resource is unavailable.
}
Ec2ImageCatalog(Catalog catalog) {
this.defaultDockerImage = require(catalog.defaultDockerImage, "defaultDockerImage");
this.images = List.copyOf(catalog.images == null ? List.of() : catalog.images);
if (this.images.isEmpty()) {
throw new IllegalStateException("EC2 image catalog has no images: " + CATALOG_RESOURCE_NAME);
}
this.imagesByIdOrAlias = indexImages(this.images);
this.loaded = new Loaded(catalog);
}
public String defaultDockerImage() {
return defaultDockerImage;
return loaded().defaultDockerImage;
}
public List<CatalogImage> images() {
return images;
return loaded().images;
}
public Optional<CatalogImage> findByIdOrAlias(String imageId) {
if (imageId == null || imageId.isBlank()) {
return Optional.empty();
}
return Optional.ofNullable(imagesByIdOrAlias.get(imageId));
return Optional.ofNullable(loaded().imagesByIdOrAlias.get(imageId));
}
private Loaded loaded() {
Loaded result = loaded;
if (result == null) {
synchronized (this) {
result = loaded;
if (result == null) {
result = new Loaded(readResource(CATALOG_RESOURCE_NAME, Catalog.class));
loaded = result;
}
}
}
return result;
}
private static final class Loaded {
private final String defaultDockerImage;
private final List<CatalogImage> images;
private final Map<String, CatalogImage> imagesByIdOrAlias;
Loaded(Catalog catalog) {
this.defaultDockerImage = require(catalog.defaultDockerImage, "defaultDockerImage");
this.images = List.copyOf(catalog.images == null ? List.of() : catalog.images);
if (this.images.isEmpty()) {
throw new IllegalStateException("EC2 image catalog has no images: " + CATALOG_RESOURCE_NAME);
}
this.imagesByIdOrAlias = indexImages(this.images);
}
}
private static <T> T readResource(String resourceName, Class<T> type) {