mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ecs): keep capacity providers and account settings in their region (#9566)
Capacity providers are stored under region::name and resolved through their ARN's region, so DescribeCapacityProviders lists only the region's own and a name is free in each region; a provider stored under its bare name is re-filed on its next write, its legacy key dropped only when the ARN matches. Account settings carry the region in their key, as botocore's PutAccountSetting says they are set per Region, and a row stored without one is the default region's. The CloudFormation capacity provider provisioner passes the stack's region. FARGATE and FARGATE_SPOT stay built in to every region.
This commit is contained in:
1 parent
19162f3423
commit
2536bc1052
7 files changed
+420
-90
No files matched your search
+6
-6
@@ -71,7 +71,7 @@ public class EcsCapacityCfnProvisioner implements CfnResourceProvisioner {
|
||||
return;
|
||||
}
|
||||
switch (resourceType) {
|
||||
case CAPACITY_PROVIDER -> deleteCapacityProvider(physicalId);
|
||||
case CAPACITY_PROVIDER -> deleteCapacityProvider(physicalId, region);
|
||||
// Deleting the association resource leaves the cluster in place with no capacity
|
||||
// providers attached, which is what AWS does: the associations are the resource.
|
||||
case ASSOCIATIONS -> clearAssociations(physicalId, region);
|
||||
@@ -104,7 +104,7 @@ public class EcsCapacityCfnProvisioner implements CfnResourceProvisioner {
|
||||
// which is recreated rather than reported as a failure.
|
||||
CapacityProvider existing = existingName == null || existingName.isBlank()
|
||||
? null
|
||||
: ecsService.describeCapacityProviders(List.of(existingName)).stream()
|
||||
: ecsService.describeCapacityProviders(List.of(existingName), ctx.region()).stream()
|
||||
.findFirst().orElse(null);
|
||||
|
||||
if (existing == null) {
|
||||
@@ -115,7 +115,7 @@ public class EcsCapacityCfnProvisioner implements CfnResourceProvisioner {
|
||||
"Updating AutoScalingGroupArn requires resource replacement, which is not "
|
||||
+ "supported.", 400);
|
||||
}
|
||||
ecsService.updateCapacityProvider(name, asgProvider);
|
||||
ecsService.updateCapacityProvider(name, asgProvider, ctx.region());
|
||||
applyTags(existing, tags);
|
||||
}
|
||||
// Ref returns the capacity provider name; AWS exposes no Fn::GetAtt attributes here.
|
||||
@@ -211,14 +211,14 @@ public class EcsCapacityCfnProvisioner implements CfnResourceProvisioner {
|
||||
return out;
|
||||
}
|
||||
|
||||
private void deleteCapacityProvider(String physicalId) {
|
||||
private void deleteCapacityProvider(String physicalId, String region) {
|
||||
// Idempotent: a provider that never got created (rollback) or was already removed is
|
||||
// delete-complete. describeCapacityProviders resolves both names and ARNs.
|
||||
if (ecsService.describeCapacityProviders(List.of(physicalId)).isEmpty()) {
|
||||
if (ecsService.describeCapacityProviders(List.of(physicalId), region).isEmpty()) {
|
||||
LOG.debugv("ECS capacity provider {0} already gone, treating delete as complete", physicalId);
|
||||
return;
|
||||
}
|
||||
ecsService.deleteCapacityProvider(physicalId);
|
||||
ecsService.deleteCapacityProvider(physicalId, region);
|
||||
}
|
||||
|
||||
private void clearAssociations(String clusterRef, String region) {
|
||||
|
||||
@@ -792,7 +792,7 @@ public class EcsJsonHandler {
|
||||
|
||||
private Response handlePutAccountSetting(JsonNode req, String region) {
|
||||
EcsService.AccountSetting setting = service.putAccountSetting(
|
||||
req.path("name").asText(), req.path("value").asText(), principalArn(req));
|
||||
req.path("name").asText(), req.path("value").asText(), principalArn(req), region);
|
||||
ObjectNode resp = objectMapper.createObjectNode();
|
||||
resp.set("setting", writer.settingNode(setting));
|
||||
return Response.ok(resp).build();
|
||||
@@ -800,7 +800,7 @@ public class EcsJsonHandler {
|
||||
|
||||
private Response handlePutAccountSettingDefault(JsonNode req, String region) {
|
||||
EcsService.AccountSetting setting = service.putAccountSettingDefault(
|
||||
req.path("name").asText(), req.path("value").asText());
|
||||
req.path("name").asText(), req.path("value").asText(), region);
|
||||
ObjectNode resp = objectMapper.createObjectNode();
|
||||
resp.set("setting", writer.settingNode(setting));
|
||||
return Response.ok(resp).build();
|
||||
@@ -808,7 +808,7 @@ public class EcsJsonHandler {
|
||||
|
||||
private Response handleDeleteAccountSetting(JsonNode req, String region) {
|
||||
EcsService.AccountSetting setting =
|
||||
service.deleteAccountSetting(req.path("name").asText(), principalArn(req));
|
||||
service.deleteAccountSetting(req.path("name").asText(), principalArn(req), region);
|
||||
ObjectNode resp = objectMapper.createObjectNode();
|
||||
resp.set("setting", writer.settingNode(setting));
|
||||
return Response.ok(resp).build();
|
||||
@@ -821,7 +821,8 @@ public class EcsJsonHandler {
|
||||
principalArn(req),
|
||||
req.path("effectiveSettings").asBoolean(false),
|
||||
req.hasNonNull("maxResults") ? req.path("maxResults").asInt() : null,
|
||||
req.hasNonNull("nextToken") ? req.path("nextToken").asText() : null);
|
||||
req.hasNonNull("nextToken") ? req.path("nextToken").asText() : null,
|
||||
region);
|
||||
ObjectNode resp = objectMapper.createObjectNode();
|
||||
ArrayNode arr = objectMapper.createArrayNode();
|
||||
page.settings().forEach(s -> arr.add(writer.settingNode(s)));
|
||||
@@ -990,7 +991,7 @@ public class EcsJsonHandler {
|
||||
private Response handleUpdateCapacityProvider(JsonNode req, String region) {
|
||||
String name = req.path("name").asText();
|
||||
Map<String, Object> asgProvider = parseRawObject(req.path("autoScalingGroupProvider"));
|
||||
CapacityProvider cp = service.updateCapacityProvider(name, asgProvider);
|
||||
CapacityProvider cp = service.updateCapacityProvider(name, asgProvider, region);
|
||||
ObjectNode resp = objectMapper.createObjectNode();
|
||||
resp.set("capacityProvider", writer.capacityProviderNode(cp));
|
||||
return Response.ok(resp).build();
|
||||
@@ -998,7 +999,7 @@ public class EcsJsonHandler {
|
||||
|
||||
private Response handleDeleteCapacityProvider(JsonNode req, String region) {
|
||||
String nameOrArn = req.path("capacityProvider").asText();
|
||||
CapacityProvider cp = service.deleteCapacityProvider(nameOrArn);
|
||||
CapacityProvider cp = service.deleteCapacityProvider(nameOrArn, region);
|
||||
ObjectNode resp = objectMapper.createObjectNode();
|
||||
resp.set("capacityProvider", writer.capacityProviderNode(cp));
|
||||
return Response.ok(resp).build();
|
||||
|
||||
@@ -263,7 +263,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
private static final int MAX_CAPACITY_PROVIDER_BASE = 100_000;
|
||||
// region::clusterArn/instanceArn → ContainerInstance
|
||||
private final Map<String, ContainerInstance> containerInstances = new ConcurrentHashMap<>();
|
||||
// name → CapacityProvider (excludes built-ins FARGATE, FARGATE_SPOT)
|
||||
// region::name → CapacityProvider (excludes built-ins FARGATE, FARGATE_SPOT). A provider stored
|
||||
// before the key carried the region sits under its bare name until its next write.
|
||||
private Map<String, CapacityProvider> capacityProviders = new ConcurrentHashMap<>();
|
||||
private AccountAwareStorageBackend<CapacityProvider> capacityProvidersStore;
|
||||
// taskSetArn → TaskSet
|
||||
@@ -282,9 +283,9 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
private final Map<String, InsightsSample> publishedInsights = new ConcurrentHashMap<>();
|
||||
// targetArn → List<Attribute>
|
||||
private Map<String, List<Attribute>> attributes = new ConcurrentHashMap<>();
|
||||
// name → value (account-level settings)
|
||||
// principalArn::name → value. The root principal's row doubles as the account default, which
|
||||
// is how AWS describes it: "the account settings for the root user or the default setting".
|
||||
// region::principalArn::name → value. The root principal's row doubles as the account default,
|
||||
// which is how AWS describes it: "the account settings for the root user or the default setting".
|
||||
// A row stored before the key carried the region (principalArn::name) is the default region's.
|
||||
private Map<String, String> accountSettings = new ConcurrentHashMap<>();
|
||||
// deploymentIds for which SERVICE_DEPLOYMENT_IN_PROGRESS has already been emitted this process.
|
||||
private final Set<String> inProgressEmitted = ConcurrentHashMap.newKeySet();
|
||||
@@ -2047,7 +2048,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
existing.merge(task.getCapacityProviderName(), 1, Integer::sum);
|
||||
}
|
||||
List<Placement> missing = new ArrayList<>();
|
||||
for (Placement placement : distribute(strategy, svc.getDesiredCount())) {
|
||||
for (Placement placement : distribute(strategy, svc.getDesiredCount(),
|
||||
regionResolver.regionOf(svc.getServiceArn()))) {
|
||||
int running = existing.getOrDefault(placement.capacityProviderName(), 0);
|
||||
if (running > 0) {
|
||||
existing.put(placement.capacityProviderName(), running - 1);
|
||||
@@ -2083,7 +2085,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
LaunchType launchType = request.getLaunchType() != null ? request.getLaunchType() : LaunchType.FARGATE;
|
||||
return Collections.nCopies(count, new Placement(launchType, null));
|
||||
}
|
||||
return distribute(strategy, count);
|
||||
return distribute(strategy, count, regionResolver.regionOf(cluster.getClusterArn()));
|
||||
}
|
||||
|
||||
/** The cluster's {@code defaultCapacityProviderStrategy}, which is stored as raw JSON. */
|
||||
@@ -2118,7 +2120,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* CreateService fail on it. A lone entry weighing 0 is left alone: the model scopes that rule
|
||||
* to a strategy naming more than one provider.
|
||||
*/
|
||||
private void validateCapacityProviderStrategy(List<CapacityProviderStrategyItem> strategy) {
|
||||
private void validateCapacityProviderStrategy(List<CapacityProviderStrategyItem> strategy, String region) {
|
||||
if (strategy.size() > MAX_CAPACITY_PROVIDERS_PER_STRATEGY) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"A capacity provider strategy can contain a maximum of "
|
||||
@@ -2145,7 +2147,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
"At least one capacity provider in a capacity provider strategy must have a "
|
||||
+ "weight greater than zero.", 400);
|
||||
}
|
||||
strategy.forEach(item -> requireCapacityProvider(item.capacityProvider()));
|
||||
strategy.forEach(item -> requireCapacityProvider(item.capacityProvider(), region));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2154,8 +2156,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* integer split go to the heaviest entries, so a 1:1 strategy with an odd count still places
|
||||
* every task.
|
||||
*/
|
||||
private List<Placement> distribute(List<CapacityProviderStrategyItem> strategy, int count) {
|
||||
validateCapacityProviderStrategy(strategy);
|
||||
private List<Placement> distribute(List<CapacityProviderStrategyItem> strategy, int count, String region) {
|
||||
validateCapacityProviderStrategy(strategy, region);
|
||||
|
||||
List<Placement> placements = new ArrayList<>();
|
||||
for (CapacityProviderStrategyItem item : strategy) {
|
||||
@@ -2207,18 +2209,13 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return new Placement(launchType, capacityProvider);
|
||||
}
|
||||
|
||||
/** Accepts the two built-in Fargate providers as well as anything CreateCapacityProvider made. */
|
||||
private void requireCapacityProvider(String name) {
|
||||
/** Accepts the two built-in Fargate providers, in every region, and any the region's own CreateCapacityProvider made. */
|
||||
private void requireCapacityProvider(String name, String region) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"A capacity provider strategy entry must name a capacity provider.", 400);
|
||||
}
|
||||
if (FARGATE_CAPACITY_PROVIDERS.contains(name) || capacityProviders.containsKey(name)) {
|
||||
return;
|
||||
}
|
||||
boolean knownByArn = capacityProviders.values().stream()
|
||||
.anyMatch(cp -> name.equals(cp.getCapacityProviderArn()));
|
||||
if (!knownByArn) {
|
||||
if (!FARGATE_CAPACITY_PROVIDERS.contains(name) && findCapacityProvider(name, region) == null) {
|
||||
throw new AwsException("ClientException",
|
||||
"The specified capacity provider '" + name + "' was not found. "
|
||||
+ "Specify a valid capacity provider and try again.", 400);
|
||||
@@ -3512,7 +3509,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
if (strategy == null || strategy.isEmpty()) {
|
||||
return LaunchType.FARGATE;
|
||||
}
|
||||
validateCapacityProviderStrategy(strategy);
|
||||
validateCapacityProviderStrategy(strategy, regionResolver.regionOf(cluster.getClusterArn()));
|
||||
return placementFor(strategy.getFirst().capacityProvider()).launchType();
|
||||
}
|
||||
|
||||
@@ -3822,7 +3819,10 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
: readStored(servicesStore, services, account,
|
||||
serviceKey(region, id.substring(0, idSlash), id.substring(idSlash + 1)))
|
||||
.filter(s -> arn.equals(s.getServiceArn())).map(EcsServiceModel::getTags);
|
||||
case "capacity-provider" -> readStored(capacityProvidersStore, capacityProviders, account, id)
|
||||
// The key capacityProviderKey builds, then the bare name a provider was saved under before.
|
||||
case "capacity-provider" -> readStored(capacityProvidersStore, capacityProviders, account,
|
||||
capacityProviderKey(region, id))
|
||||
.or(() -> readStored(capacityProvidersStore, capacityProviders, account, id))
|
||||
.filter(cp -> arn.equals(cp.getCapacityProviderArn())).map(CapacityProvider::getTags);
|
||||
case "task" -> Optional.ofNullable(tasks.get(arn)).map(EcsTask::getTags);
|
||||
case "task-set" -> Optional.ofNullable(taskSets.get(arn))
|
||||
@@ -3966,10 +3966,11 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* Sets one principal's account setting. A request that names no principal sets it for the
|
||||
* caller, which here is the account root, because Floci does not authenticate a separate user.
|
||||
*/
|
||||
public AccountSetting putAccountSetting(String name, String value, String principalArn) {
|
||||
public AccountSetting putAccountSetting(String name, String value, String principalArn, String region) {
|
||||
validateAccountSetting(name, value);
|
||||
String principal = principalArn != null ? principalArn : rootPrincipalArn();
|
||||
accountSettings.put(accountSettingKey(principal, name), value);
|
||||
removeLegacyAccountSetting(principal, name, region);
|
||||
accountSettings.put(accountSettingKey(region, principal, name), value);
|
||||
return new AccountSetting(name, value, principal, settingType(name));
|
||||
}
|
||||
|
||||
@@ -3978,16 +3979,18 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* stores that on the root user, so setting the default and setting the root user's own value
|
||||
* are the same write.
|
||||
*/
|
||||
public AccountSetting putAccountSettingDefault(String name, String value) {
|
||||
return putAccountSetting(name, value, rootPrincipalArn());
|
||||
public AccountSetting putAccountSettingDefault(String name, String value, String region) {
|
||||
return putAccountSetting(name, value, rootPrincipalArn(), region);
|
||||
}
|
||||
|
||||
public AccountSetting deleteAccountSetting(String name, String principalArn) {
|
||||
public AccountSetting deleteAccountSetting(String name, String principalArn, String region) {
|
||||
requireAccountSettingName(name);
|
||||
String principal = principalArn != null ? principalArn : rootPrincipalArn();
|
||||
String removed = accountSettings.remove(accountSettingKey(principal, name));
|
||||
String legacy = removeLegacyAccountSetting(principal, name, region);
|
||||
String current = accountSettings.remove(accountSettingKey(region, principal, name));
|
||||
String removed = current != null ? current : legacy;
|
||||
return new AccountSetting(name,
|
||||
removed != null ? removed : effectiveAccountSetting(principal, name),
|
||||
removed != null ? removed : effectiveAccountSetting(principal, name, region),
|
||||
principal, settingType(name));
|
||||
}
|
||||
|
||||
@@ -4002,7 +4005,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
*/
|
||||
public AccountSettingPage listAccountSettings(String filterName, String filterValue,
|
||||
String principalArn, boolean effectiveSettings,
|
||||
Integer maxResults, String nextToken) {
|
||||
Integer maxResults, String nextToken, String region) {
|
||||
if (filterName != null) {
|
||||
requireAccountSettingName(filterName);
|
||||
}
|
||||
@@ -4013,11 +4016,11 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
String principal = principalArn != null ? principalArn : rootPrincipalArn();
|
||||
List<AccountSetting> settings = new ArrayList<>();
|
||||
for (String name : ACCOUNT_SETTING_NAMES) {
|
||||
String explicit = accountSettings.get(accountSettingKey(principal, name));
|
||||
String explicit = explicitAccountSetting(principal, name, region);
|
||||
if (explicit != null) {
|
||||
settings.add(new AccountSetting(name, explicit, principal, settingType(name)));
|
||||
} else if (effectiveSettings) {
|
||||
settings.add(new AccountSetting(name, effectiveAccountSetting(principal, name),
|
||||
settings.add(new AccountSetting(name, effectiveAccountSetting(principal, name, region),
|
||||
principal, settingType(name)));
|
||||
}
|
||||
}
|
||||
@@ -4039,20 +4042,42 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return new AccountSettingPage(List.copyOf(filtered.subList(offset, end)), token);
|
||||
}
|
||||
|
||||
private String effectiveAccountSetting(String principal, String name) {
|
||||
String explicit = accountSettings.get(accountSettingKey(principal, name));
|
||||
private String effectiveAccountSetting(String principal, String name, String region) {
|
||||
String explicit = explicitAccountSetting(principal, name, region);
|
||||
if (explicit != null) {
|
||||
return explicit;
|
||||
}
|
||||
String accountDefault = accountSettings.get(accountSettingKey(rootPrincipalArn(), name));
|
||||
String accountDefault = explicitAccountSetting(rootPrincipalArn(), name, region);
|
||||
return accountDefault != null ? accountDefault : ACCOUNT_SETTING_DEFAULTS.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The value a principal set in a region. A row saved before the key carried the region is the
|
||||
* default region's, so only that region reads it.
|
||||
*/
|
||||
private String explicitAccountSetting(String principal, String name, String region) {
|
||||
String value = accountSettings.get(accountSettingKey(region, principal, name));
|
||||
if (value == null && region.equals(regionResolver.getDefaultRegion())) {
|
||||
value = accountSettings.get(legacyAccountSettingKey(principal, name));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Drops the region-less row a write in the default region supersedes, returning its value. */
|
||||
private String removeLegacyAccountSetting(String principal, String name, String region) {
|
||||
return region.equals(regionResolver.getDefaultRegion())
|
||||
? accountSettings.remove(legacyAccountSettingKey(principal, name)) : null;
|
||||
}
|
||||
|
||||
private static String settingType(String name) {
|
||||
return AWS_MANAGED_SETTING.equals(name) ? "aws_managed" : "user";
|
||||
}
|
||||
|
||||
private static String accountSettingKey(String principalArn, String name) {
|
||||
private static String accountSettingKey(String region, String principalArn, String name) {
|
||||
return region + "::" + principalArn + "::" + name;
|
||||
}
|
||||
|
||||
private static String legacyAccountSettingKey(String principalArn, String name) {
|
||||
return principalArn + "::" + name;
|
||||
}
|
||||
|
||||
@@ -4514,7 +4539,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
public CapacityProvider createCapacityProvider(String name, Map<String, Object> asgProvider,
|
||||
Map<String, String> tags, String region) {
|
||||
validateCapacityProviderName(name);
|
||||
if (capacityProviders.containsKey(name)) {
|
||||
if (findCapacityProvider(name, region) != null) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"A capacity provider with name " + name + " already exists.", 400);
|
||||
}
|
||||
@@ -4528,15 +4553,15 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
if (tags != null) {
|
||||
cp.setTags(tags);
|
||||
}
|
||||
capacityProviders.put(name, cp);
|
||||
storeCapacityProvider(cp);
|
||||
return cp;
|
||||
}
|
||||
|
||||
public CapacityProvider updateCapacityProvider(String name, Map<String, Object> asgProvider) {
|
||||
CapacityProvider cp = resolveCapacityProviderOrThrow(name);
|
||||
public CapacityProvider updateCapacityProvider(String name, Map<String, Object> asgProvider, String region) {
|
||||
CapacityProvider cp = resolveCapacityProviderOrThrow(name, region);
|
||||
cp.setAutoScalingGroupProvider(asgProvider);
|
||||
cp.setUpdateStatus("UPDATE_COMPLETE");
|
||||
capacityProviders.put(cp.getName(), cp);
|
||||
storeCapacityProvider(cp);
|
||||
return cp;
|
||||
}
|
||||
|
||||
@@ -4550,20 +4575,20 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* {@code CapacityProviderStatus} takes, so reporting it there hands the SDK an enum it
|
||||
* cannot map.
|
||||
*/
|
||||
public CapacityProvider deleteCapacityProvider(String nameOrArn) {
|
||||
public CapacityProvider deleteCapacityProvider(String nameOrArn, String region) {
|
||||
if (RESERVED_CAPACITY_PROVIDERS.contains(nameOrArn)) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"The " + nameOrArn + " capacity provider is reserved and can't be deleted.", 400);
|
||||
}
|
||||
CapacityProvider cp = resolveCapacityProviderOrThrow(nameOrArn);
|
||||
String attachedTo = clusterUsingCapacityProvider(cp.getName());
|
||||
CapacityProvider cp = resolveCapacityProviderOrThrow(nameOrArn, region);
|
||||
String attachedTo = clusterUsingCapacityProvider(cp.getName(), region);
|
||||
if (attachedTo != null) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"The capacity provider " + cp.getName() + " is associated with cluster "
|
||||
+ attachedTo + " and can't be deleted. Remove it with "
|
||||
+ "PutClusterCapacityProviders or delete the cluster first.", 400);
|
||||
}
|
||||
String usedBy = serviceUsingCapacityProvider(cp);
|
||||
String usedBy = serviceUsingCapacityProvider(cp, region);
|
||||
if (usedBy != null) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"The capacity provider " + cp.getName() + " is in the capacity provider "
|
||||
@@ -4572,16 +4597,16 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
+ "first.", 400);
|
||||
}
|
||||
cp.setUpdateStatus("DELETE_IN_PROGRESS");
|
||||
capacityProviders.remove(cp.getName());
|
||||
removeLegacyCapacityProvider(cp);
|
||||
capacityProviders.remove(capacityProviderKey(region, cp.getName()));
|
||||
return cp;
|
||||
}
|
||||
|
||||
public record DescribeCapacityProvidersResult(List<CapacityProvider> capacityProviders,
|
||||
List<Failure> failures) {}
|
||||
|
||||
public List<CapacityProvider> describeCapacityProviders(List<String> providers) {
|
||||
return describeCapacityProvidersDetailed(providers, regionResolver.getDefaultRegion())
|
||||
.capacityProviders();
|
||||
public List<CapacityProvider> describeCapacityProviders(List<String> providers, String region) {
|
||||
return describeCapacityProvidersDetailed(providers, region).capacityProviders();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4593,7 +4618,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
if (providers == null || providers.isEmpty()) {
|
||||
List<CapacityProvider> result =
|
||||
new ArrayList<>(List.of(builtInFargate(region), builtInFargateSpot(region)));
|
||||
result.addAll(capacityProviders.values());
|
||||
capacityProvidersIn(region).forEach(result::add);
|
||||
return new DescribeCapacityProvidersResult(result, List.of());
|
||||
}
|
||||
List<CapacityProvider> found = new ArrayList<>();
|
||||
@@ -4602,10 +4627,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
CapacityProvider cp = switch (ref) {
|
||||
case "FARGATE" -> builtInFargate(region);
|
||||
case "FARGATE_SPOT" -> builtInFargateSpot(region);
|
||||
default -> capacityProviders.getOrDefault(ref,
|
||||
capacityProviders.values().stream()
|
||||
.filter(p -> ref.equals(p.getCapacityProviderArn()))
|
||||
.findFirst().orElse(null));
|
||||
default -> findCapacityProvider(ref, region);
|
||||
};
|
||||
if (cp == null) {
|
||||
failures.add(Failure.missing(AwsArnUtils.looksLikeArn(ref) ? ref
|
||||
@@ -4617,9 +4639,10 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return new DescribeCapacityProvidersResult(found, failures);
|
||||
}
|
||||
|
||||
/** The cluster a capacity provider is attached to, or {@code null} when none is. */
|
||||
private String clusterUsingCapacityProvider(String name) {
|
||||
/** The cluster of the region a capacity provider is attached to, or {@code null} when none is. */
|
||||
private String clusterUsingCapacityProvider(String name, String region) {
|
||||
return clusters.values().stream()
|
||||
.filter(c -> region.equals(regionResolver.regionOf(c.getClusterArn())))
|
||||
.filter(c -> c.getCapacityProviders() != null && c.getCapacityProviders().contains(name))
|
||||
.map(EcsCluster::getClusterName)
|
||||
.findFirst().orElse(null);
|
||||
@@ -4631,9 +4654,10 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* service's strategy, not only out of every cluster. A deleted service is INACTIVE and holds
|
||||
* nothing back.
|
||||
*/
|
||||
private String serviceUsingCapacityProvider(CapacityProvider cp) {
|
||||
private String serviceUsingCapacityProvider(CapacityProvider cp, String region) {
|
||||
return services.values().stream()
|
||||
.filter(svc -> !"INACTIVE".equals(svc.getStatus()))
|
||||
.filter(svc -> region.equals(regionResolver.regionOf(svc.getServiceArn())))
|
||||
.filter(svc -> svc.getCapacityProviderStrategy() != null
|
||||
&& svc.getCapacityProviderStrategy().stream()
|
||||
.anyMatch(item -> cp.getName().equals(item.capacityProvider())
|
||||
@@ -6722,6 +6746,40 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
taskDefinitions.put(taskDefinitionKey(regionOf(td), td.getFamily(), td.getRevision()), td);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a capacity provider under its region's key. One persisted under its bare name before the
|
||||
* key carried the region is re-filed on its next write, and the old key is dropped only when it
|
||||
* holds this same provider: the same name in another region is a different provider.
|
||||
*/
|
||||
private void storeCapacityProvider(CapacityProvider cp) {
|
||||
removeLegacyCapacityProvider(cp);
|
||||
String region = regionResolver.regionOf(cp.getCapacityProviderArn());
|
||||
capacityProviders.put(capacityProviderKey(region, cp.getName()), cp);
|
||||
}
|
||||
|
||||
private void removeLegacyCapacityProvider(CapacityProvider cp) {
|
||||
CapacityProvider legacy = capacityProviders.get(cp.getName());
|
||||
if (legacy != null && Objects.equals(cp.getCapacityProviderArn(), legacy.getCapacityProviderArn())) {
|
||||
capacityProviders.remove(cp.getName());
|
||||
}
|
||||
}
|
||||
|
||||
/** Capacity providers are regional; the region is read from each one's ARN, whatever its storage key. */
|
||||
private Stream<CapacityProvider> capacityProvidersIn(String region) {
|
||||
return capacityProviders.values().stream()
|
||||
.filter(cp -> region.equals(regionResolver.regionOf(cp.getCapacityProviderArn())));
|
||||
}
|
||||
|
||||
/** The region's created provider with this name or ARN, or {@code null}; FARGATE and FARGATE_SPOT are not stored. */
|
||||
private CapacityProvider findCapacityProvider(String nameOrArn, String region) {
|
||||
if (nameOrArn == null) {
|
||||
return null;
|
||||
}
|
||||
return capacityProvidersIn(region)
|
||||
.filter(cp -> nameOrArn.equals(cp.getName()) || nameOrArn.equals(cp.getCapacityProviderArn()))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/** Task definitions are regional; the region is read from each one's ARN, whatever its storage key. */
|
||||
private Stream<TaskDefinition> taskDefinitionsIn(String region) {
|
||||
return taskDefinitions.values().stream().filter(td -> region.equals(regionOf(td)));
|
||||
@@ -6816,12 +6874,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return instance;
|
||||
}
|
||||
|
||||
private CapacityProvider resolveCapacityProviderOrThrow(String nameOrArn) {
|
||||
CapacityProvider cp = capacityProviders.get(nameOrArn);
|
||||
if (cp != null) { return cp; }
|
||||
cp = capacityProviders.values().stream()
|
||||
.filter(p -> p.getCapacityProviderArn().equals(nameOrArn))
|
||||
.findFirst().orElse(null);
|
||||
private CapacityProvider resolveCapacityProviderOrThrow(String nameOrArn, String region) {
|
||||
CapacityProvider cp = findCapacityProvider(nameOrArn, region);
|
||||
if (cp == null) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"Capacity provider not found: " + nameOrArn, 400);
|
||||
@@ -6852,6 +6906,10 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return region + "::" + clusterName;
|
||||
}
|
||||
|
||||
private static String capacityProviderKey(String region, String name) {
|
||||
return region + "::" + name;
|
||||
}
|
||||
|
||||
private static String familyKey(String region, String family) {
|
||||
return region + "::" + family;
|
||||
}
|
||||
|
||||
+39
-15
@@ -44,6 +44,10 @@ class EcsCapacityCfnProvisionerTest {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private ProvisionContext ctx() {
|
||||
return ctx("us-east-1");
|
||||
}
|
||||
|
||||
private ProvisionContext ctx(String region) {
|
||||
// Scalars only: resolve returns the node's text and resolveNode is structure-preserving,
|
||||
// which is what the engine does for templates without intrinsics.
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
@@ -52,7 +56,7 @@ class EcsCapacityCfnProvisionerTest {
|
||||
return node == null ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
return new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack");
|
||||
return new ProvisionContext(engine, region, "000000000000", "my-stack");
|
||||
}
|
||||
|
||||
private StackResource resource(String type, String logicalId) {
|
||||
@@ -102,7 +106,7 @@ class EcsCapacityCfnProvisionerTest {
|
||||
// createCapacityProvider rejects an existing name, so this used to fail the whole update.
|
||||
StackResource r = resource(PROVIDER_TYPE, "Provider");
|
||||
r.setPhysicalId("the-provider");
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider")))
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider"), "us-east-1"))
|
||||
.thenReturn(List.of(stored("the-provider", ASG_ARN, Map.of())));
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
props.put("Name", "the-provider");
|
||||
@@ -115,30 +119,50 @@ class EcsCapacityCfnProvisionerTest {
|
||||
assertEquals("the-provider", r.getPhysicalId());
|
||||
verify(ecs, never()).createCapacityProvider(anyString(), any(), any(), anyString());
|
||||
ArgumentCaptor<Map<String, Object>> asgCaptor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(ecs).updateCapacityProvider(eq("the-provider"), asgCaptor.capture());
|
||||
verify(ecs).updateCapacityProvider(eq("the-provider"), asgCaptor.capture(), eq("us-east-1"));
|
||||
assertEquals("ENABLED", asgCaptor.getValue().get("managedDraining"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStackOutsideTheDefaultRegionUpdatesAndDeletesItsProviderInItsOwnRegion() {
|
||||
StackResource r = resource(PROVIDER_TYPE, "Provider");
|
||||
r.setPhysicalId("the-provider");
|
||||
CapacityProvider existing = stored("the-provider", ASG_ARN, Map.of());
|
||||
existing.setCapacityProviderArn("arn:aws:ecs:eu-west-1:000000000000:capacity-provider/the-provider");
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider"), "eu-west-1")).thenReturn(List.of(existing));
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
props.put("Name", "the-provider");
|
||||
props.putObject("AutoScalingGroupProvider").put("AutoScalingGroupArn", ASG_ARN);
|
||||
|
||||
provisioner.provision(r, props, ctx("eu-west-1"));
|
||||
provisioner.delete(PROVIDER_TYPE, "the-provider", "eu-west-1");
|
||||
|
||||
verify(ecs, never()).createCapacityProvider(anyString(), any(), any(), anyString());
|
||||
verify(ecs).updateCapacityProvider(eq("the-provider"), any(), eq("eu-west-1"));
|
||||
verify(ecs).deleteCapacityProvider("the-provider", "eu-west-1");
|
||||
verify(ecs, never()).describeCapacityProviders(anyList(), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnnamedProviderKeepsItsGeneratedNameAcrossUpdates() {
|
||||
// A fresh generated name on every pass created a second provider and orphaned the first.
|
||||
StackResource r = resource(PROVIDER_TYPE, "Provider");
|
||||
r.setPhysicalId("my-stack-Provider-abcdef123456");
|
||||
when(ecs.describeCapacityProviders(List.of("my-stack-Provider-abcdef123456")))
|
||||
when(ecs.describeCapacityProviders(List.of("my-stack-Provider-abcdef123456"), "us-east-1"))
|
||||
.thenReturn(List.of(stored("my-stack-Provider-abcdef123456", null, Map.of())));
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode(), ctx());
|
||||
|
||||
assertEquals("my-stack-Provider-abcdef123456", r.getPhysicalId());
|
||||
verify(ecs, never()).createCapacityProvider(anyString(), any(), any(), anyString());
|
||||
verify(ecs).updateCapacityProvider(eq("my-stack-Provider-abcdef123456"), any());
|
||||
verify(ecs).updateCapacityProvider(eq("my-stack-Provider-abcdef123456"), any(), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aProviderRemovedOutOfBandIsRecreated() {
|
||||
StackResource r = resource(PROVIDER_TYPE, "Provider");
|
||||
r.setPhysicalId("the-provider");
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider"))).thenReturn(List.of());
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider"), "us-east-1")).thenReturn(List.of());
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
props.put("Name", "the-provider");
|
||||
props.putObject("AutoScalingGroupProvider").put("AutoScalingGroupArn", ASG_ARN);
|
||||
@@ -146,7 +170,7 @@ class EcsCapacityCfnProvisionerTest {
|
||||
provisioner.provision(r, props, ctx());
|
||||
|
||||
verify(ecs).createCapacityProvider(eq("the-provider"), any(), any(), eq("us-east-1"));
|
||||
verify(ecs, never()).updateCapacityProvider(anyString(), any());
|
||||
verify(ecs, never()).updateCapacityProvider(anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -160,14 +184,14 @@ class EcsCapacityCfnProvisionerTest {
|
||||
|
||||
assertEquals("ValidationError", failure.getErrorCode());
|
||||
verify(ecs, never()).createCapacityProvider(anyString(), any(), any(), anyString());
|
||||
verify(ecs, never()).updateCapacityProvider(anyString(), any());
|
||||
verify(ecs, never()).updateCapacityProvider(anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changingAutoScalingGroupArnIsRejectedAsAReplacement() {
|
||||
StackResource r = resource(PROVIDER_TYPE, "Provider");
|
||||
r.setPhysicalId("the-provider");
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider")))
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider"), "us-east-1"))
|
||||
.thenReturn(List.of(stored("the-provider", ASG_ARN, Map.of())));
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
props.put("Name", "the-provider");
|
||||
@@ -177,7 +201,7 @@ class EcsCapacityCfnProvisionerTest {
|
||||
AwsException failure = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
|
||||
assertEquals("ValidationError", failure.getErrorCode());
|
||||
verify(ecs, never()).updateCapacityProvider(anyString(), any());
|
||||
verify(ecs, never()).updateCapacityProvider(anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -187,7 +211,7 @@ class EcsCapacityCfnProvisionerTest {
|
||||
StackResource r = resource(PROVIDER_TYPE, "Provider");
|
||||
r.setPhysicalId("the-provider");
|
||||
String arn = "arn:aws:ecs:us-east-1:000000000000:capacity-provider/the-provider";
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider")))
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider"), "us-east-1"))
|
||||
.thenReturn(List.of(stored("the-provider", ASG_ARN, Map.of("keep", "1", "drop", "2"))));
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
props.put("Name", "the-provider");
|
||||
@@ -288,20 +312,20 @@ class EcsCapacityCfnProvisionerTest {
|
||||
void deleteCapacityProviderDelegatesToService() {
|
||||
CapacityProvider existing = new CapacityProvider();
|
||||
existing.setName("the-provider");
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider"))).thenReturn(List.of(existing));
|
||||
when(ecs.describeCapacityProviders(List.of("the-provider"), "us-east-1")).thenReturn(List.of(existing));
|
||||
|
||||
provisioner.delete(PROVIDER_TYPE, "the-provider", "us-east-1");
|
||||
|
||||
verify(ecs).deleteCapacityProvider("the-provider");
|
||||
verify(ecs).deleteCapacityProvider("the-provider", "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteCapacityProviderAlreadyGoneIsDeleteComplete() {
|
||||
when(ecs.describeCapacityProviders(anyList())).thenReturn(List.of());
|
||||
when(ecs.describeCapacityProviders(anyList(), anyString())).thenReturn(List.of());
|
||||
|
||||
provisioner.delete(PROVIDER_TYPE, "the-provider", "us-east-1");
|
||||
|
||||
verify(ecs, never()).deleteCapacityProvider(anyString());
|
||||
verify(ecs, never()).deleteCapacityProvider(anyString(), anyString());
|
||||
}
|
||||
|
||||
/** Greptile P1: the cluster kept its associations after the stack was deleted. */
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package io.floci.aws.services.ecs;
|
||||
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.services.ecs.model.CapacityProvider;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Capacity providers stored under their bare name, and account settings stored without a region, from
|
||||
* before their keys carried the region: each stays in the region it belongs to and is re-filed on its
|
||||
* next write.
|
||||
*/
|
||||
class EcsCapacityProviderLegacyKeyTest {
|
||||
|
||||
private static final String REGION = EcsServiceFixtures.REGION;
|
||||
private static final String OTHER = "eu-west-1";
|
||||
private static final String NAME = "legacy-cp";
|
||||
private static final String LEGACY_ARN =
|
||||
"arn:aws:ecs:" + REGION + ":000000000000:capacity-provider/" + NAME;
|
||||
private static final String ROOT = "arn:aws:iam::000000000000:root";
|
||||
private static final String SETTING = "awsvpcTrunking";
|
||||
|
||||
@Test
|
||||
void aLegacyProviderStaysInItsRegionAndIsRefiledOnItsNextWrite() {
|
||||
AccountAwareStorageBackend<CapacityProvider> providers =
|
||||
AccountAwareStorageBackend.inMemory(EcsServiceFixtures.ACCOUNT);
|
||||
providers.put(NAME, legacyProvider());
|
||||
EcsService service = EcsServiceFixtures.builder()
|
||||
.storageFactory(new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT)
|
||||
.withStore("ecs-capacity-providers.json", providers))
|
||||
.build();
|
||||
|
||||
assertEquals(List.of(LEGACY_ARN), arns(service.describeCapacityProviders(List.of(NAME), REGION)));
|
||||
assertTrue(service.describeCapacityProviders(List.of(NAME), OTHER).isEmpty());
|
||||
CapacityProvider other = service.createCapacityProvider(NAME, Map.of(), Map.of(), OTHER);
|
||||
service.updateCapacityProvider(NAME, Map.of(), REGION);
|
||||
|
||||
assertEquals(Set.of(REGION + "::" + NAME, OTHER + "::" + NAME), Set.copyOf(providers.keys()));
|
||||
assertEquals(List.of(LEGACY_ARN), arns(service.describeCapacityProviders(List.of(NAME), REGION)));
|
||||
assertEquals(List.of(other.getCapacityProviderArn()),
|
||||
arns(service.describeCapacityProviders(List.of(NAME), OTHER)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLegacyAccountSettingIsTheDefaultRegionsAndIsRefiledOnItsNextWrite() {
|
||||
AccountAwareStorageBackend<String> settings = AccountAwareStorageBackend.inMemory(EcsServiceFixtures.ACCOUNT);
|
||||
settings.put(ROOT + "::" + SETTING, "enabled");
|
||||
EcsService service = EcsServiceFixtures.builder()
|
||||
.storageFactory(new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT)
|
||||
.withStore("ecs-account-settings.json", settings))
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("enabled"), values(service, REGION));
|
||||
assertTrue(values(service, OTHER).isEmpty());
|
||||
|
||||
service.putAccountSetting(SETTING, "disabled", null, REGION);
|
||||
|
||||
assertEquals(List.of(REGION + "::" + ROOT + "::" + SETTING), List.copyOf(settings.keys()));
|
||||
assertEquals(List.of("disabled"), values(service, REGION));
|
||||
}
|
||||
|
||||
private static CapacityProvider legacyProvider() {
|
||||
CapacityProvider cp = new CapacityProvider();
|
||||
cp.setName(NAME);
|
||||
cp.setCapacityProviderArn(LEGACY_ARN);
|
||||
cp.setStatus("ACTIVE");
|
||||
cp.setType("EC2_AUTOSCALING");
|
||||
return cp;
|
||||
}
|
||||
|
||||
private static List<String> arns(List<CapacityProvider> providers) {
|
||||
return providers.stream().map(CapacityProvider::getCapacityProviderArn).toList();
|
||||
}
|
||||
|
||||
private static List<String> values(EcsService service, String region) {
|
||||
return service.listAccountSettings(SETTING, null, null, false, null, null, region).settings().stream()
|
||||
.map(EcsService.AccountSetting::value)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
package io.floci.aws.services.ecs;
|
||||
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* Capacity providers and account settings are regional: a provider name is taken once per region,
|
||||
* and "account settings are set on a per-Region basis" (botocore ecs PutAccountSetting).
|
||||
*/
|
||||
@QuarkusTest
|
||||
class EcsCapacityProviderRegionIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
private static final String TARGET_PREFIX = "AmazonEC2ContainerServiceV20141113.";
|
||||
private static final String HOME = "eu-west-1";
|
||||
private static final String OTHER = "us-west-2";
|
||||
private static final String ABSENT = "ap-southeast-1";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
private static RequestSpecification ecs(String region, String action) {
|
||||
return given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region
|
||||
+ "/ecs/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
.header("X-Amz-Target", TARGET_PREFIX + action);
|
||||
}
|
||||
|
||||
private static String createProvider(String region, String name) {
|
||||
return ecs(region, "CreateCapacityProvider")
|
||||
.body("""
|
||||
{"name": "%s", "autoScalingGroupProvider": {"autoScalingGroupArn":
|
||||
"arn:aws:autoscaling:%s:000000000000:autoScalingGroup:x:autoScalingGroupName/asg-%s"}}
|
||||
""".formatted(name, region, name))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("capacityProvider.capacityProviderArn", containsString(":" + region + ":"))
|
||||
.extract().path("capacityProvider.capacityProviderArn");
|
||||
}
|
||||
|
||||
@Test
|
||||
void capacityProvidersStayInTheirRegion() {
|
||||
String name = "region-cp-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String homeArn = createProvider(HOME, name);
|
||||
String otherArn = createProvider(OTHER, name);
|
||||
|
||||
ecs(HOME, "DescribeCapacityProviders")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("capacityProviders.capacityProviderArn", hasItems(homeArn))
|
||||
.body("capacityProviders.capacityProviderArn", not(hasItems(otherArn)))
|
||||
.body("capacityProviders.name", hasItems("FARGATE", "FARGATE_SPOT"));
|
||||
ecs(OTHER, "DescribeCapacityProviders")
|
||||
.body("{\"capacityProviders\": [\"" + name + "\"]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("capacityProviders.capacityProviderArn", contains(otherArn));
|
||||
ecs(ABSENT, "DescribeCapacityProviders")
|
||||
.body("{\"capacityProviders\": [\"" + name + "\", \"FARGATE\"]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("capacityProviders.name", contains("FARGATE"))
|
||||
.body("failures[0].reason", equalTo("MISSING"));
|
||||
ecs(ABSENT, "DeleteCapacityProvider")
|
||||
.body("{\"capacityProvider\": \"" + name + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400);
|
||||
|
||||
ecs(HOME, "DeleteCapacityProvider")
|
||||
.body("{\"capacityProvider\": \"" + name + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("capacityProvider.capacityProviderArn", equalTo(homeArn));
|
||||
ecs(OTHER, "DescribeCapacityProviders")
|
||||
.body("{\"capacityProviders\": [\"" + name + "\"]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("capacityProviders.capacityProviderArn", contains(otherArn));
|
||||
ecs(OTHER, "DeleteCapacityProvider")
|
||||
.body("{\"capacityProvider\": \"" + otherArn + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void accountSettingsStayInTheirRegion() {
|
||||
String principal = "arn:aws:iam::000000000000:role/region-setting-"
|
||||
+ UUID.randomUUID().toString().substring(0, 8);
|
||||
ecs(HOME, "PutAccountSetting")
|
||||
.body("{\"name\": \"awsvpcTrunking\", \"value\": \"enabled\", \"principalArn\": \"" + principal + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
ecs(HOME, "ListAccountSettings")
|
||||
.body("{\"name\": \"awsvpcTrunking\", \"principalArn\": \"" + principal + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("settings.value", contains("enabled"));
|
||||
ecs(OTHER, "ListAccountSettings")
|
||||
.body("{\"name\": \"awsvpcTrunking\", \"principalArn\": \"" + principal + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("settings", empty());
|
||||
ecs(OTHER, "ListAccountSettings")
|
||||
.body("{\"name\": \"awsvpcTrunking\", \"principalArn\": \"" + principal
|
||||
+ "\", \"effectiveSettings\": true}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("settings.value", contains("disabled"));
|
||||
|
||||
ecs(HOME, "DeleteAccountSetting")
|
||||
.body("{\"name\": \"awsvpcTrunking\", \"principalArn\": \"" + principal + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("setting.value", equalTo("enabled"));
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,7 @@ class EcsServicePersistenceTest {
|
||||
.getContainerInstanceArn();
|
||||
first.putAttributes("app-cluster",
|
||||
List.of(new Attribute("stack", "prod", "container-instance", instanceArn)), REGION);
|
||||
first.putAccountSetting("containerInsights", "enabled", null);
|
||||
first.putAccountSetting("containerInsights", "enabled", null, REGION);
|
||||
|
||||
// Simulate restart: a fresh instance reloading from the same shared store.
|
||||
EcsService reloaded = serviceWithStorage(storage);
|
||||
@@ -80,7 +80,7 @@ class EcsServicePersistenceTest {
|
||||
assertEquals("prod", reloaded.listAttributes("app-cluster", "container-instance", "stack",
|
||||
null, null, null, REGION).attributes().getFirst().value());
|
||||
assertEquals("enabled", reloaded.listAccountSettings("containerInsights", null, null,
|
||||
false, null, null).settings().getFirst().value());
|
||||
false, null, null, REGION).settings().getFirst().value());
|
||||
|
||||
// latestRevisions persisted: registering the same family again yields the next revision.
|
||||
assertEquals(2, reloaded.registerTaskDefinition("web", List.of(container("app", "nginx:latest")),
|
||||
|
||||
Reference in new issue
Block a user