diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ImageCatalog.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ImageCatalog.java index 93d6a61f6e..758010cdc4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ImageCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ImageCatalog.java @@ -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 images; - private final Map 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 images() { - return images; + return loaded().images; } public Optional 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 images; + private final Map 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 readResource(String resourceName, Class type) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e6a971e4ac..084e9a43bd 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -19,11 +19,12 @@ quarkus: delay-enabled: true native: resources: - # The Pricing service reads its bundled snapshot at runtime via + # The Pricing service reads its bundled snapshot, and EC2 reads its image + # catalog (ec2/image-catalog.yaml), at runtime via # ClassLoader#getResourceAsStream. Quarkus's static resource analysis - # cannot detect dynamically-built paths, so the tree is registered + # cannot detect these dynamically-built paths, so they are registered # explicitly to ensure GraalVM includes every file in the native image. - includes: pricing-snapshots/** + includes: pricing-snapshots/**,ec2/*.yaml additional-build-args: - --report-unsupported-elements-at-runtime - --allow-incomplete-classpath