mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ecs): list the services that joined a namespace in ListServicesByNamespace (#8774)
* fix(ecs): list the services that joined a namespace in ListServicesByNamespace * fix(ecs): keep the namespace a service joined and refuse a negative page token * docs(ecs): name ListServicesByNamespace among the ten-at-a-time listings in paginate
This commit is contained in:
1 parent
f0c234485b
commit
6a92129c2f
6 files changed
+239
-12
No files matched your search
@@ -749,13 +749,18 @@ public class EcsJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleListServicesByNamespace(JsonNode req, String region) {
|
||||
String namespace = req.path("namespace").asText();
|
||||
List<String> arns = service.listServicesByNamespace(namespace, region);
|
||||
String namespace = req.hasNonNull("namespace") ? req.path("namespace").asText() : null;
|
||||
Integer maxResults = req.hasNonNull("maxResults") ? req.path("maxResults").asInt() : null;
|
||||
String nextToken = req.hasNonNull("nextToken") ? req.path("nextToken").asText() : null;
|
||||
EcsService.ListPage page = service.listServicesByNamespace(namespace, maxResults, nextToken, region);
|
||||
|
||||
ObjectNode resp = objectMapper.createObjectNode();
|
||||
ArrayNode arr = objectMapper.createArrayNode();
|
||||
arns.forEach(arr::add);
|
||||
page.arns().forEach(arr::add);
|
||||
resp.set("serviceArns", arr);
|
||||
if (page.nextToken() != null) {
|
||||
resp.put("nextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(resp).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -2905,7 +2905,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
/**
|
||||
* @param defaultPageSize the page size when the request names none, which is not the same for
|
||||
* every listing: ListServices answers with ten, the rest with a hundred.
|
||||
* every listing: ListServices and ListServicesByNamespace answer with ten,
|
||||
* the rest with a hundred.
|
||||
*/
|
||||
private static ListPage paginate(List<String> arns, Integer maxResults, String nextToken,
|
||||
int defaultPageSize) {
|
||||
@@ -2926,11 +2927,16 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
if (nextToken == null || nextToken.isBlank()) {
|
||||
return 0;
|
||||
}
|
||||
int offset;
|
||||
try {
|
||||
return Integer.parseInt(Base64Url.decode(nextToken));
|
||||
offset = Integer.parseInt(Base64Url.decode(nextToken));
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidParameterException", "The nextToken is not valid.", 400);
|
||||
}
|
||||
if (offset < 0) {
|
||||
throw new AwsException("InvalidParameterException", "The nextToken is not valid.", 400);
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
// ── Task Protection ───────────────────────────────────────────────────────
|
||||
@@ -3127,6 +3133,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
svc.setAvailabilityZoneRebalancing(request.getAvailabilityZoneRebalancing() != null
|
||||
? request.getAvailabilityZoneRebalancing() : DEFAULT_AZ_REBALANCING_ON_CREATE);
|
||||
svc.setServiceConnectConfiguration(request.getServiceConnectConfiguration());
|
||||
svc.setServiceConnectNamespaceArn(serviceConnectNamespaceArn(svc, cluster, region));
|
||||
svc.setDeploymentConfiguration(request.getDeploymentConfiguration());
|
||||
svc.setEnableExecuteCommand(request.isEnableExecuteCommand());
|
||||
svc.setEnableECSManagedTags(request.isEnableECSManagedTags());
|
||||
@@ -3319,6 +3326,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
&& !serviceConnectConfiguration.equals(svc.getServiceConnectConfiguration());
|
||||
if (serviceConnectConfiguration != null) {
|
||||
svc.setServiceConnectConfiguration(serviceConnectConfiguration);
|
||||
svc.setServiceConnectNamespaceArn(serviceConnectNamespaceArn(svc, cluster, region));
|
||||
}
|
||||
boolean taskDefChanged = false;
|
||||
String platformVersionBefore = resolvePlatformVersion(svc.getPlatformVersion());
|
||||
@@ -3615,12 +3623,57 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return paginate(arns, maxResults, nextToken, DEFAULT_SERVICE_PAGE_SIZE);
|
||||
}
|
||||
|
||||
public List<String> listServicesByNamespace(String namespace, String region) {
|
||||
return services.values().stream()
|
||||
.filter(s -> namespace.equals(s.getNamespace()))
|
||||
.filter(s -> !"INACTIVE".equals(s.getStatus()))
|
||||
/**
|
||||
* Lists the Region's services whose Service Connect configuration joined the namespace a name or ARN gives, its
|
||||
* own or, when it names none, its cluster's default. Pages ten at a time when the request names no
|
||||
* {@code maxResults}, as ListServices does.
|
||||
*/
|
||||
public ListPage listServicesByNamespace(String namespace, Integer maxResults, String nextToken, String region) {
|
||||
if (namespace == null || namespace.isBlank()) {
|
||||
throw new AwsException("InvalidParameterException", "namespace can not be blank.", 400);
|
||||
}
|
||||
if (AwsArnUtils.looksLikeArn(namespace) && !isNamespaceArn(namespace)) {
|
||||
throw new AwsException("InvalidParameterException", "Namespace is not a valid namespace ARN.", 400);
|
||||
}
|
||||
String namespaceArn = discoveryRegistrar == null ? null : discoveryRegistrar.namespaceArn(namespace, region);
|
||||
if (namespaceArn == null) {
|
||||
throw new AwsException("NamespaceNotFoundException", "Failed to retrieve namespace for " + namespace, 400);
|
||||
}
|
||||
List<String> arns = services.entrySet().stream()
|
||||
.filter(e -> e.getKey().startsWith(region + "::"))
|
||||
.map(Map.Entry::getValue)
|
||||
.filter(svc -> !"INACTIVE".equals(svc.getStatus()))
|
||||
.filter(svc -> namespaceArn.equals(svc.getServiceConnectNamespaceArn()))
|
||||
.map(EcsServiceModel::getServiceArn)
|
||||
.toList();
|
||||
return paginate(arns, maxResults, nextToken, DEFAULT_SERVICE_PAGE_SIZE);
|
||||
}
|
||||
|
||||
private static boolean isNamespaceArn(String value) {
|
||||
try {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(value);
|
||||
return "servicediscovery".equals(arn.service()) && arn.resource().startsWith("namespace/");
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN of the namespace the service's enabled Service Connect configuration joins, its own or its cluster's
|
||||
* default; null when it joins none.
|
||||
*/
|
||||
private String serviceConnectNamespaceArn(EcsServiceModel svc, EcsCluster cluster, String region) {
|
||||
Map<String, Object> config = svc.getServiceConnectConfiguration();
|
||||
if (discoveryRegistrar == null || config == null || !Boolean.TRUE.equals(config.get("enabled"))) {
|
||||
return null;
|
||||
}
|
||||
Object namespace = config.get("namespace");
|
||||
if (namespace == null || namespace.toString().isBlank()) {
|
||||
Map<String, Object> defaults = cluster == null ? null : cluster.getServiceConnectDefaults();
|
||||
namespace = defaults == null ? null : defaults.get("namespace");
|
||||
}
|
||||
return namespace == null || namespace.toString().isBlank()
|
||||
? null : discoveryRegistrar.namespaceArn(namespace.toString(), region);
|
||||
}
|
||||
|
||||
// ── Tags ──────────────────────────────────────────────────────────────────
|
||||
@@ -5332,6 +5385,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
svc.setServiceRegistries(revision.getServiceRegistries());
|
||||
svc.setNetworkConfiguration(revision.getNetworkConfiguration());
|
||||
svc.setServiceConnectConfiguration(revision.getServiceConnectConfiguration());
|
||||
svc.setServiceConnectNamespaceArn(serviceConnectNamespaceArn(svc, resolveClusterByArn(svc.getClusterArn()),
|
||||
serviceRegion(svc)));
|
||||
svc.setDeploymentId("ecs-svc/" + taskSetId);
|
||||
svc.setLastDeploymentAt(revision.getCreatedAt());
|
||||
svc.setLastCompletedDeploymentId(null);
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.cloudmap.CloudMapService;
|
||||
import io.floci.aws.services.cloudmap.model.Instance;
|
||||
import io.floci.aws.services.cloudmap.model.Namespace;
|
||||
import io.floci.aws.services.ecs.container.EcsContainerManager;
|
||||
import io.floci.aws.services.ecs.model.Container;
|
||||
import io.floci.aws.services.ecs.model.EcsRegisteredInstances;
|
||||
@@ -213,6 +214,16 @@ public class EcsServiceDiscoveryRegistrar {
|
||||
}
|
||||
}
|
||||
|
||||
/** The ARN of the Region's Cloud Map namespace that a name or ARN gives; null when there is none. */
|
||||
public String namespaceArn(String nameOrArn, String region) {
|
||||
for (Namespace namespace : cloudMapService.listNamespaces(region)) {
|
||||
if (nameOrArn.equals(namespace.getName()) || nameOrArn.equals(namespace.getArn())) {
|
||||
return namespace.getArn();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean hasRegistries(EcsServiceModel svc) {
|
||||
return !registries(svc).isEmpty();
|
||||
}
|
||||
|
||||
@@ -47,6 +47,11 @@ public class EcsServiceModel {
|
||||
* it; the Service shape itself has no member for it.
|
||||
*/
|
||||
private Map<String, Object> serviceConnectConfiguration;
|
||||
/**
|
||||
* The ARN of the Cloud Map namespace the Service Connect configuration joined, its own or its cluster's default,
|
||||
* resolved when the configuration was set so a later change to the cluster default does not move the service.
|
||||
*/
|
||||
private String serviceConnectNamespaceArn;
|
||||
private String platformVersion;
|
||||
private String platformFamily;
|
||||
private List<CapacityProviderStrategyItem> capacityProviderStrategy;
|
||||
@@ -118,6 +123,9 @@ public class EcsServiceModel {
|
||||
public String getFailedDeploymentId() { return failedDeploymentId; }
|
||||
public void setFailedDeploymentId(String failedDeploymentId) { this.failedDeploymentId = failedDeploymentId; }
|
||||
|
||||
public String getServiceConnectNamespaceArn() { return serviceConnectNamespaceArn; }
|
||||
public void setServiceConnectNamespaceArn(String arn) { this.serviceConnectNamespaceArn = arn; }
|
||||
|
||||
public String getNamespace() { return namespace; }
|
||||
public void setNamespace(String namespace) { this.namespace = namespace; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user