mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ec2): add instance type metadata catalog (#1534)
This commit is contained in:
1 parent
b9da266b16
commit
1a319551cc
7 files changed
+394
-41
No files matched your search
@@ -0,0 +1,136 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@ApplicationScoped
|
||||
public class Ec2InstanceTypeCatalog {
|
||||
|
||||
private static final String CATALOG_RESOURCE_NAME = "ec2/instance-type-catalog.yaml";
|
||||
private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());
|
||||
|
||||
private volatile Loaded loaded;
|
||||
|
||||
public Ec2InstanceTypeCatalog() {
|
||||
// Load lazily so tests and mock-mode paths that do not need instance type
|
||||
// metadata are not coupled to resource loading during bean construction.
|
||||
}
|
||||
|
||||
Ec2InstanceTypeCatalog(Catalog catalog) {
|
||||
this.loaded = new Loaded(catalog);
|
||||
}
|
||||
|
||||
public List<CatalogInstanceType> instanceTypes() {
|
||||
return loaded().instanceTypes;
|
||||
}
|
||||
|
||||
public Optional<CatalogInstanceType> find(String instanceType) {
|
||||
if (instanceType == null || instanceType.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.ofNullable(loaded().instanceTypesByName.get(instanceType));
|
||||
}
|
||||
|
||||
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 <T> T readResource(String resourceName, Class<T> type) {
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
try (InputStream input = loader.getResourceAsStream(resourceName)) {
|
||||
if (input == null) {
|
||||
throw new IllegalStateException("Missing EC2 instance type catalog resource: " + resourceName);
|
||||
}
|
||||
return YAML_MAPPER.readValue(input, type);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to load EC2 instance type catalog resource: " + resourceName, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Loaded {
|
||||
private final List<CatalogInstanceType> instanceTypes;
|
||||
private final Map<String, CatalogInstanceType> instanceTypesByName;
|
||||
|
||||
Loaded(Catalog catalog) {
|
||||
this.instanceTypes = List.copyOf(catalog.instanceTypes == null ? List.of() : catalog.instanceTypes);
|
||||
if (this.instanceTypes.isEmpty()) {
|
||||
throw new IllegalStateException("EC2 instance type catalog has no instance types: " + CATALOG_RESOURCE_NAME);
|
||||
}
|
||||
this.instanceTypesByName = indexInstanceTypes(this.instanceTypes);
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, CatalogInstanceType> indexInstanceTypes(List<CatalogInstanceType> instanceTypes) {
|
||||
Map<String, CatalogInstanceType> index = new LinkedHashMap<>();
|
||||
for (CatalogInstanceType instanceType : instanceTypes) {
|
||||
String name = require(instanceType.instanceType, "instanceType");
|
||||
if (instanceType.vcpu <= 0) {
|
||||
throw new IllegalStateException("EC2 instance type catalog entry has invalid vcpu: " + name);
|
||||
}
|
||||
if (instanceType.memoryMib <= 0) {
|
||||
throw new IllegalStateException("EC2 instance type catalog entry has invalid memoryMib: " + name);
|
||||
}
|
||||
if (instanceType.supportedArchitectures == null || instanceType.supportedArchitectures.isEmpty()) {
|
||||
throw new IllegalStateException("EC2 instance type catalog entry is missing supportedArchitectures: " + name);
|
||||
}
|
||||
CatalogInstanceType previous = index.putIfAbsent(name, instanceType);
|
||||
if (previous != null) {
|
||||
throw new IllegalStateException("Duplicate EC2 instance type catalog entry: " + name);
|
||||
}
|
||||
}
|
||||
return Map.copyOf(index);
|
||||
}
|
||||
|
||||
private static String require(String value, String field) {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new IllegalStateException("EC2 instance type catalog entry is missing required field: " + field);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@RegisterForReflection
|
||||
public static final class Catalog {
|
||||
public List<CatalogInstanceType> instanceTypes = List.of();
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@RegisterForReflection
|
||||
public static final class CatalogInstanceType {
|
||||
public String instanceType;
|
||||
public int vcpu;
|
||||
public int memoryMib;
|
||||
public List<String> supportedArchitectures = List.of();
|
||||
public Boolean currentGeneration;
|
||||
|
||||
public Map<String, Object> toResponseMap() {
|
||||
Map<String, Object> type = new LinkedHashMap<>();
|
||||
type.put("instanceType", instanceType);
|
||||
type.put("vcpu", vcpu);
|
||||
type.put("memoryMib", memoryMib);
|
||||
type.put("supportedArchitectures", List.copyOf(supportedArchitectures));
|
||||
type.put("currentGeneration", currentGeneration == null || currentGeneration);
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,6 +83,7 @@ public class Ec2Service {
|
||||
private final Ec2ContainerManager containerManager;
|
||||
private final AmiImageResolver amiImageResolver;
|
||||
private final Ec2ImageCatalog imageCatalog;
|
||||
private final Ec2InstanceTypeCatalog instanceTypeCatalog;
|
||||
|
||||
// region::id → resource (persisted via StorageFactory so state survives a restart in
|
||||
// persistent/hybrid/wal modes; see #1297 — CloudFormation persists stacks/exports that
|
||||
@@ -111,8 +112,8 @@ public class Ec2Service {
|
||||
@Inject
|
||||
public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,
|
||||
AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,
|
||||
StorageFactory storageFactory) {
|
||||
this(config, containerManager, amiImageResolver, imageCatalog,
|
||||
Ec2InstanceTypeCatalog instanceTypeCatalog, StorageFactory storageFactory) {
|
||||
this(config, containerManager, amiImageResolver, imageCatalog, instanceTypeCatalog,
|
||||
storageFactory.create("ec2", "ec2-vpcs.json", new TypeReference<Map<String, Vpc>>() {}),
|
||||
storageFactory.create("ec2", "ec2-subnets.json", new TypeReference<Map<String, Subnet>>() {}),
|
||||
storageFactory.create("ec2", "ec2-security-groups.json", new TypeReference<Map<String, SecurityGroup>>() {}),
|
||||
@@ -134,6 +135,7 @@ public class Ec2Service {
|
||||
// Package-private for hermetic tests (pass in-memory or temp-dir-backed StorageBackends directly).
|
||||
Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,
|
||||
AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,
|
||||
Ec2InstanceTypeCatalog instanceTypeCatalog,
|
||||
StorageBackend<String, Vpc> vpcs,
|
||||
StorageBackend<String, Subnet> subnets,
|
||||
StorageBackend<String, SecurityGroup> securityGroups,
|
||||
@@ -155,6 +157,7 @@ public class Ec2Service {
|
||||
this.containerManager = containerManager;
|
||||
this.amiImageResolver = amiImageResolver;
|
||||
this.imageCatalog = imageCatalog;
|
||||
this.instanceTypeCatalog = instanceTypeCatalog;
|
||||
this.vpcs = vpcs;
|
||||
this.subnets = subnets;
|
||||
this.securityGroups = securityGroups;
|
||||
@@ -2059,43 +2062,19 @@ public class Ec2Service {
|
||||
// ─── Instance Types ────────────────────────────────────────────────────────
|
||||
|
||||
public List<Map<String, Object>> describeInstanceTypes(List<String> instanceTypeNames) {
|
||||
List<Map<String, Object>> allTypes = new ArrayList<>();
|
||||
allTypes.add(buildInstanceType("t2.micro", 1, 1024));
|
||||
allTypes.add(buildInstanceType("t3.micro", 2, 1024));
|
||||
allTypes.add(buildInstanceType("t3.small", 2, 2048));
|
||||
allTypes.add(buildInstanceType("t3.medium", 2, 4096));
|
||||
allTypes.add(buildInstanceType("m5.large", 2, 8192));
|
||||
allTypes.add(buildInstanceType("t4g.micro", 2, 1024, List.of("arm64")));
|
||||
allTypes.add(buildInstanceType("t4g.small", 2, 2048, List.of("arm64")));
|
||||
allTypes.add(buildInstanceType("t4g.medium", 2, 4096, List.of("arm64")));
|
||||
allTypes.add(buildInstanceType("m6gd.2xlarge", 8, 32768, List.of("arm64")));
|
||||
allTypes.add(buildInstanceType("m7gd.2xlarge", 8, 32768, List.of("arm64")));
|
||||
allTypes.add(buildInstanceType("m8gd.medium", 1, 4096, List.of("arm64")));
|
||||
allTypes.add(buildInstanceType("m8gd.2xlarge", 8, 32768, List.of("arm64")));
|
||||
|
||||
if (instanceTypeNames.isEmpty()) {
|
||||
return allTypes;
|
||||
return instanceTypeCatalog.instanceTypes().stream()
|
||||
.map(Ec2InstanceTypeCatalog.CatalogInstanceType::toResponseMap)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return allTypes.stream()
|
||||
.filter(t -> instanceTypeNames.contains(t.get("instanceType")))
|
||||
return instanceTypeNames.stream()
|
||||
.distinct()
|
||||
.map(instanceTypeCatalog::find)
|
||||
.flatMap(Optional::stream)
|
||||
.map(Ec2InstanceTypeCatalog.CatalogInstanceType::toResponseMap)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private Map<String, Object> buildInstanceType(String name, int vcpu, int memMib) {
|
||||
return buildInstanceType(name, vcpu, memMib, List.of("x86_64"));
|
||||
}
|
||||
|
||||
private Map<String, Object> buildInstanceType(String name, int vcpu, int memMib,
|
||||
List<String> supportedArchitectures) {
|
||||
Map<String, Object> t = new LinkedHashMap<>();
|
||||
t.put("instanceType", name);
|
||||
t.put("vcpu", vcpu);
|
||||
t.put("memoryMib", memMib);
|
||||
t.put("supportedArchitectures", supportedArchitectures);
|
||||
t.put("currentGeneration", true);
|
||||
return t;
|
||||
}
|
||||
|
||||
public List<Map<String, String>> describeInstanceTypeOfferings(String region, List<String> instanceTypeNames,
|
||||
String locationType,
|
||||
Map<String, List<String>> filters) {
|
||||
|
||||
Reference in new issue
Block a user