mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(scheduler): dispatch ECS targets to RunTask (#1733)
* feat(scheduler): dispatch ECS targets to RunTask * fix(scheduler): preserve ECS target parameters
This commit is contained in:
1 parent
3a31439a84
commit
c3980ebab0
9 files changed
+563
-16
No files matched your search
@@ -4,9 +4,12 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.services.ecs.EcsService;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.EcsParameters;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Target;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
@@ -35,6 +38,7 @@ public class ScheduleInvoker {
|
||||
private final LambdaService lambdaService;
|
||||
private final SnsService snsService;
|
||||
private final EventBridgeService eventBridgeService;
|
||||
private final EcsService ecsService;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final String baseUrl;
|
||||
|
||||
@@ -43,12 +47,14 @@ public class ScheduleInvoker {
|
||||
LambdaService lambdaService,
|
||||
SnsService snsService,
|
||||
EventBridgeService eventBridgeService,
|
||||
EcsService ecsService,
|
||||
ObjectMapper objectMapper,
|
||||
EmulatorConfig config) {
|
||||
this.sqsService = sqsService;
|
||||
this.lambdaService = lambdaService;
|
||||
this.snsService = snsService;
|
||||
this.eventBridgeService = eventBridgeService;
|
||||
this.ecsService = ecsService;
|
||||
this.objectMapper = objectMapper;
|
||||
this.baseUrl = config.baseUrl();
|
||||
}
|
||||
@@ -84,6 +90,9 @@ public class ScheduleInvoker {
|
||||
} else if (arn.contains(":sns:")) {
|
||||
snsService.publish(arn, null, payload, "Scheduler", targetRegion);
|
||||
LOG.debugv("Scheduler delivered to SNS: {0}", arn);
|
||||
} else if (arn.contains(":ecs:") && target.getEcsParameters() != null) {
|
||||
deliverToEcsRunTask(target, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to ECS RunTask: {0}", arn);
|
||||
} else if (isEventBridgePutEventsArn(arn)) {
|
||||
deliverToEventBridge(arn, payload, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to EventBridge: {0}", arn);
|
||||
@@ -92,6 +101,50 @@ public class ScheduleInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
private void deliverToEcsRunTask(Target target, String region) {
|
||||
EcsParameters ecs = target.getEcsParameters();
|
||||
ecsService.runTask(
|
||||
target.getArn(),
|
||||
ecs.getTaskDefinitionArn(),
|
||||
ecs.getTaskCount() != null ? ecs.getTaskCount() : 1,
|
||||
parseLaunchType(ecs.getLaunchType()),
|
||||
null,
|
||||
ecs.getGroup() != null ? ecs.getGroup() : "scheduler",
|
||||
List.of(),
|
||||
ecsNetworkConfiguration(ecs.getNetworkConfiguration()),
|
||||
region);
|
||||
}
|
||||
|
||||
private static LaunchType parseLaunchType(String launchType) {
|
||||
if (launchType == null || launchType.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return LaunchType.valueOf(launchType);
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.warnv("Scheduler: unsupported ECS LaunchType: {0}", launchType);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration ecsNetworkConfiguration(
|
||||
io.github.hectorvent.floci.services.scheduler.model.NetworkConfiguration source) {
|
||||
if (source == null || source.getAwsvpcConfiguration() == null) {
|
||||
return null;
|
||||
}
|
||||
io.github.hectorvent.floci.services.scheduler.model.AwsVpcConfiguration sourceVpc = source.getAwsvpcConfiguration();
|
||||
io.github.hectorvent.floci.services.ecs.model.AwsVpcConfiguration targetVpc =
|
||||
new io.github.hectorvent.floci.services.ecs.model.AwsVpcConfiguration();
|
||||
targetVpc.setSubnets(sourceVpc.getSubnets());
|
||||
targetVpc.setSecurityGroups(sourceVpc.getSecurityGroups());
|
||||
targetVpc.setAssignPublicIp(sourceVpc.getAssignPublicIp());
|
||||
|
||||
io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration target =
|
||||
new io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration();
|
||||
target.setAwsvpcConfiguration(targetVpc);
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches an EventBridge Scheduler universal target ({@code aws-sdk:<service>:<action>}),
|
||||
* reading the call parameters from the target's {@code Input} payload. Supports the
|
||||
|
||||
@@ -2,8 +2,11 @@ package io.github.hectorvent.floci.services.scheduler;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.AwsVpcConfiguration;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.DeadLetterConfig;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.EcsParameters;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.FlexibleTimeWindow;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.NetworkConfiguration;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.RetryPolicy;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
|
||||
import io.github.hectorvent.floci.services.scheduler.model.ScheduleGroup;
|
||||
@@ -267,6 +270,12 @@ public class SchedulerController {
|
||||
sp.put("MessageGroupId", s.getTarget().getSqsParameters().getMessageGroupId());
|
||||
t.put("SqsParameters", sp);
|
||||
}
|
||||
if (s.getTarget().getEcsParameters() != null) {
|
||||
Map<String, Object> ep = buildEcsParametersResponse(s.getTarget().getEcsParameters());
|
||||
if (!ep.isEmpty()) {
|
||||
t.put("EcsParameters", ep);
|
||||
}
|
||||
}
|
||||
r.put("Target", t);
|
||||
}
|
||||
if (s.getDescription() != null) {
|
||||
@@ -402,9 +411,156 @@ public class SchedulerController {
|
||||
}
|
||||
target.setSqsParameters(sp);
|
||||
}
|
||||
if (node.has("EcsParameters") && !node.get("EcsParameters").isNull()) {
|
||||
target.setEcsParameters(parseEcsParameters(node.get("EcsParameters")));
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
private EcsParameters parseEcsParameters(JsonNode node) {
|
||||
EcsParameters ecs = new EcsParameters();
|
||||
if (node.has("CapacityProviderStrategy") && node.get("CapacityProviderStrategy").isArray()) {
|
||||
ecs.setCapacityProviderStrategy(mapList(node.get("CapacityProviderStrategy")));
|
||||
}
|
||||
if (node.has("EnableECSManagedTags") && !node.get("EnableECSManagedTags").isNull()) {
|
||||
ecs.setEnableECSManagedTags(node.get("EnableECSManagedTags").asBoolean());
|
||||
}
|
||||
if (node.has("EnableExecuteCommand") && !node.get("EnableExecuteCommand").isNull()) {
|
||||
ecs.setEnableExecuteCommand(node.get("EnableExecuteCommand").asBoolean());
|
||||
}
|
||||
if (node.has("Group") && !node.get("Group").isNull()) {
|
||||
ecs.setGroup(node.get("Group").asText());
|
||||
}
|
||||
if (node.has("TaskDefinitionArn") && !node.get("TaskDefinitionArn").isNull()) {
|
||||
ecs.setTaskDefinitionArn(node.get("TaskDefinitionArn").asText());
|
||||
}
|
||||
if (node.has("LaunchType") && !node.get("LaunchType").isNull()) {
|
||||
ecs.setLaunchType(node.get("LaunchType").asText());
|
||||
}
|
||||
if (node.has("PlacementConstraints") && node.get("PlacementConstraints").isArray()) {
|
||||
ecs.setPlacementConstraints(mapList(node.get("PlacementConstraints")));
|
||||
}
|
||||
if (node.has("PlacementStrategy") && node.get("PlacementStrategy").isArray()) {
|
||||
ecs.setPlacementStrategy(mapList(node.get("PlacementStrategy")));
|
||||
}
|
||||
if (node.has("TaskCount") && !node.get("TaskCount").isNull()) {
|
||||
ecs.setTaskCount(node.get("TaskCount").asInt());
|
||||
}
|
||||
if (node.has("PlatformVersion") && !node.get("PlatformVersion").isNull()) {
|
||||
ecs.setPlatformVersion(node.get("PlatformVersion").asText());
|
||||
}
|
||||
if (node.has("PropagateTags") && !node.get("PropagateTags").isNull()) {
|
||||
ecs.setPropagateTags(node.get("PropagateTags").asText());
|
||||
}
|
||||
if (node.has("ReferenceId") && !node.get("ReferenceId").isNull()) {
|
||||
ecs.setReferenceId(node.get("ReferenceId").asText());
|
||||
}
|
||||
if (node.has("Tags") && node.get("Tags").isArray()) {
|
||||
ecs.setTags(mapList(node.get("Tags")));
|
||||
}
|
||||
if (node.has("NetworkConfiguration") && !node.get("NetworkConfiguration").isNull()) {
|
||||
ecs.setNetworkConfiguration(parseNetworkConfiguration(node.get("NetworkConfiguration")));
|
||||
}
|
||||
return ecs;
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> mapList(JsonNode node) {
|
||||
return objectMapper.convertValue(node,
|
||||
objectMapper.getTypeFactory().constructCollectionType(List.class,
|
||||
objectMapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class)));
|
||||
}
|
||||
|
||||
private NetworkConfiguration parseNetworkConfiguration(JsonNode node) {
|
||||
NetworkConfiguration network = new NetworkConfiguration();
|
||||
if (node.has("awsvpcConfiguration") && !node.get("awsvpcConfiguration").isNull()) {
|
||||
network.setAwsvpcConfiguration(parseAwsVpcConfiguration(node.get("awsvpcConfiguration")));
|
||||
}
|
||||
return network;
|
||||
}
|
||||
|
||||
private AwsVpcConfiguration parseAwsVpcConfiguration(JsonNode node) {
|
||||
AwsVpcConfiguration vpc = new AwsVpcConfiguration();
|
||||
if (node.has("Subnets") && node.get("Subnets").isArray()) {
|
||||
vpc.setSubnets(objectMapper.convertValue(node.get("Subnets"), objectMapper.getTypeFactory().constructCollectionType(List.class, String.class)));
|
||||
}
|
||||
if (node.has("SecurityGroups") && node.get("SecurityGroups").isArray()) {
|
||||
vpc.setSecurityGroups(objectMapper.convertValue(node.get("SecurityGroups"), objectMapper.getTypeFactory().constructCollectionType(List.class, String.class)));
|
||||
}
|
||||
if (node.has("AssignPublicIp") && !node.get("AssignPublicIp").isNull()) {
|
||||
vpc.setAssignPublicIp(node.get("AssignPublicIp").asText());
|
||||
}
|
||||
return vpc;
|
||||
}
|
||||
|
||||
private Map<String, Object> buildEcsParametersResponse(EcsParameters ecs) {
|
||||
Map<String, Object> ep = new HashMap<>();
|
||||
if (ecs.getCapacityProviderStrategy() != null && !ecs.getCapacityProviderStrategy().isEmpty()) {
|
||||
ep.put("CapacityProviderStrategy", ecs.getCapacityProviderStrategy());
|
||||
}
|
||||
if (ecs.getEnableECSManagedTags() != null) {
|
||||
ep.put("EnableECSManagedTags", ecs.getEnableECSManagedTags());
|
||||
}
|
||||
if (ecs.getEnableExecuteCommand() != null) {
|
||||
ep.put("EnableExecuteCommand", ecs.getEnableExecuteCommand());
|
||||
}
|
||||
if (ecs.getGroup() != null) {
|
||||
ep.put("Group", ecs.getGroup());
|
||||
}
|
||||
if (ecs.getTaskDefinitionArn() != null) {
|
||||
ep.put("TaskDefinitionArn", ecs.getTaskDefinitionArn());
|
||||
}
|
||||
if (ecs.getLaunchType() != null) {
|
||||
ep.put("LaunchType", ecs.getLaunchType());
|
||||
}
|
||||
if (ecs.getPlacementConstraints() != null && !ecs.getPlacementConstraints().isEmpty()) {
|
||||
ep.put("PlacementConstraints", ecs.getPlacementConstraints());
|
||||
}
|
||||
if (ecs.getPlacementStrategy() != null && !ecs.getPlacementStrategy().isEmpty()) {
|
||||
ep.put("PlacementStrategy", ecs.getPlacementStrategy());
|
||||
}
|
||||
if (ecs.getTaskCount() != null) {
|
||||
ep.put("TaskCount", ecs.getTaskCount());
|
||||
}
|
||||
if (ecs.getPlatformVersion() != null) {
|
||||
ep.put("PlatformVersion", ecs.getPlatformVersion());
|
||||
}
|
||||
if (ecs.getPropagateTags() != null) {
|
||||
ep.put("PropagateTags", ecs.getPropagateTags());
|
||||
}
|
||||
if (ecs.getReferenceId() != null) {
|
||||
ep.put("ReferenceId", ecs.getReferenceId());
|
||||
}
|
||||
if (ecs.getTags() != null && !ecs.getTags().isEmpty()) {
|
||||
ep.put("Tags", ecs.getTags());
|
||||
}
|
||||
if (ecs.getNetworkConfiguration() != null) {
|
||||
Map<String, Object> network = buildNetworkConfigurationResponse(ecs.getNetworkConfiguration());
|
||||
if (!network.isEmpty()) {
|
||||
ep.put("NetworkConfiguration", network);
|
||||
}
|
||||
}
|
||||
return ep;
|
||||
}
|
||||
|
||||
private Map<String, Object> buildNetworkConfigurationResponse(NetworkConfiguration network) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
if (network.getAwsvpcConfiguration() != null) {
|
||||
AwsVpcConfiguration vpc = network.getAwsvpcConfiguration();
|
||||
Map<String, Object> awsvpc = new HashMap<>();
|
||||
if (vpc.getSubnets() != null && !vpc.getSubnets().isEmpty()) {
|
||||
awsvpc.put("Subnets", vpc.getSubnets());
|
||||
}
|
||||
if (vpc.getSecurityGroups() != null && !vpc.getSecurityGroups().isEmpty()) {
|
||||
awsvpc.put("SecurityGroups", vpc.getSecurityGroups());
|
||||
}
|
||||
if (vpc.getAssignPublicIp() != null) {
|
||||
awsvpc.put("AssignPublicIp", vpc.getAssignPublicIp());
|
||||
}
|
||||
result.put("awsvpcConfiguration", awsvpc);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, String> parseTags(String body) throws JsonProcessingException {
|
||||
if (body == null || body.isBlank()) {
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package io.github.hectorvent.floci.services.scheduler.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RegisterForReflection
|
||||
public class AwsVpcConfiguration {
|
||||
|
||||
private List<String> subnets = new ArrayList<>();
|
||||
private List<String> securityGroups = new ArrayList<>();
|
||||
private String assignPublicIp;
|
||||
|
||||
public List<String> getSubnets() { return subnets; }
|
||||
public void setSubnets(List<String> subnets) { this.subnets = subnets; }
|
||||
|
||||
public List<String> getSecurityGroups() { return securityGroups; }
|
||||
public void setSecurityGroups(List<String> securityGroups) { this.securityGroups = securityGroups; }
|
||||
|
||||
public String getAssignPublicIp() { return assignPublicIp; }
|
||||
public void setAssignPublicIp(String assignPublicIp) { this.assignPublicIp = assignPublicIp; }
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package io.github.hectorvent.floci.services.scheduler.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
public class EcsParameters {
|
||||
|
||||
private List<Map<String, Object>> capacityProviderStrategy;
|
||||
private Boolean enableECSManagedTags;
|
||||
private Boolean enableExecuteCommand;
|
||||
private String group;
|
||||
private String taskDefinitionArn;
|
||||
private String launchType;
|
||||
private List<Map<String, Object>> placementConstraints;
|
||||
private List<Map<String, Object>> placementStrategy;
|
||||
private Integer taskCount;
|
||||
private String platformVersion;
|
||||
private String propagateTags;
|
||||
private String referenceId;
|
||||
private List<Map<String, Object>> tags;
|
||||
private NetworkConfiguration networkConfiguration;
|
||||
|
||||
public List<Map<String, Object>> getCapacityProviderStrategy() { return capacityProviderStrategy; }
|
||||
public void setCapacityProviderStrategy(List<Map<String, Object>> capacityProviderStrategy) { this.capacityProviderStrategy = capacityProviderStrategy; }
|
||||
|
||||
public Boolean getEnableECSManagedTags() { return enableECSManagedTags; }
|
||||
public void setEnableECSManagedTags(Boolean enableECSManagedTags) { this.enableECSManagedTags = enableECSManagedTags; }
|
||||
|
||||
public Boolean getEnableExecuteCommand() { return enableExecuteCommand; }
|
||||
public void setEnableExecuteCommand(Boolean enableExecuteCommand) { this.enableExecuteCommand = enableExecuteCommand; }
|
||||
|
||||
public String getGroup() { return group; }
|
||||
public void setGroup(String group) { this.group = group; }
|
||||
|
||||
public String getTaskDefinitionArn() { return taskDefinitionArn; }
|
||||
public void setTaskDefinitionArn(String taskDefinitionArn) { this.taskDefinitionArn = taskDefinitionArn; }
|
||||
|
||||
public String getLaunchType() { return launchType; }
|
||||
public void setLaunchType(String launchType) { this.launchType = launchType; }
|
||||
|
||||
public List<Map<String, Object>> getPlacementConstraints() { return placementConstraints; }
|
||||
public void setPlacementConstraints(List<Map<String, Object>> placementConstraints) { this.placementConstraints = placementConstraints; }
|
||||
|
||||
public List<Map<String, Object>> getPlacementStrategy() { return placementStrategy; }
|
||||
public void setPlacementStrategy(List<Map<String, Object>> placementStrategy) { this.placementStrategy = placementStrategy; }
|
||||
|
||||
public Integer getTaskCount() { return taskCount; }
|
||||
public void setTaskCount(Integer taskCount) { this.taskCount = taskCount; }
|
||||
|
||||
public String getPlatformVersion() { return platformVersion; }
|
||||
public void setPlatformVersion(String platformVersion) { this.platformVersion = platformVersion; }
|
||||
|
||||
public String getPropagateTags() { return propagateTags; }
|
||||
public void setPropagateTags(String propagateTags) { this.propagateTags = propagateTags; }
|
||||
|
||||
public String getReferenceId() { return referenceId; }
|
||||
public void setReferenceId(String referenceId) { this.referenceId = referenceId; }
|
||||
|
||||
public List<Map<String, Object>> getTags() { return tags; }
|
||||
public void setTags(List<Map<String, Object>> tags) { this.tags = tags; }
|
||||
|
||||
public NetworkConfiguration getNetworkConfiguration() { return networkConfiguration; }
|
||||
public void setNetworkConfiguration(NetworkConfiguration networkConfiguration) { this.networkConfiguration = networkConfiguration; }
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.scheduler.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public class NetworkConfiguration {
|
||||
|
||||
private AwsVpcConfiguration awsvpcConfiguration;
|
||||
|
||||
public AwsVpcConfiguration getAwsvpcConfiguration() { return awsvpcConfiguration; }
|
||||
public void setAwsvpcConfiguration(AwsVpcConfiguration awsvpcConfiguration) { this.awsvpcConfiguration = awsvpcConfiguration; }
|
||||
}
|
||||
@@ -11,6 +11,7 @@ public class Target {
|
||||
private RetryPolicy retryPolicy;
|
||||
private DeadLetterConfig deadLetterConfig;
|
||||
private SqsParameters sqsParameters;
|
||||
private EcsParameters ecsParameters;
|
||||
|
||||
public Target() {}
|
||||
|
||||
@@ -38,4 +39,7 @@ public class Target {
|
||||
|
||||
public SqsParameters getSqsParameters() { return sqsParameters; }
|
||||
public void setSqsParameters(SqsParameters sqsParameters) { this.sqsParameters = sqsParameters; }
|
||||
|
||||
public EcsParameters getEcsParameters() { return ecsParameters; }
|
||||
public void setEcsParameters(EcsParameters ecsParameters) { this.ecsParameters = ecsParameters; }
|
||||
}
|
||||
Reference in new issue
Block a user