mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
fix(cloudformation): wait for a stable ECS service (#4880)
AWS CloudFormation reports an AWS::ECS::Service CREATE_COMPLETE or UPDATE_COMPLETE only once the service is stable: it polls DescribeServices until the desired number of tasks of the current task definition are running. Floci reported the resource complete as soon as CreateService or UpdateService returned. The tasks were started later by the ECS reconciler, every five seconds, and the service's runningCount only caught up a tick after that, so a client reading the service right after the stack operation saw runningCount 0. EcsService.awaitServiceStable now waits, like Ec2Service.awaitContainerLaunch, for runningCount to equal desiredCount, for the current deployment to run that many tasks, and for no task of an earlier deployment to be running. Instead of idling until the next tick it submits up to three reconcile passes to the single reconciler thread, so they never overlap a scheduled tick. After five minutes it fails with "Service <arn> did not stabilize.". The wait ends at once when the service's tasks cannot start. The reconciler now keeps, in memory, why its last attempt to launch a task for a service failed, for example an unknown subnet, and the deployment it was launching for. Only a failure of the deployment being awaited ends the wait, so a pass that began before an update cannot fail the update. The record is cleared on a successful launch, on DeleteService and on a state reset. A task that stops with TaskFailedToStart during the wait ends it too. A task the reconciler launches is stamped with the deployment read before its task definition, not after the launch, so a launch that an update overtakes counts as the earlier deployment's and is replaced. The ECS provisioner calls the wait after an in-place update and after a create. A created service stays marked as owned by the stack while it waits, so the create rollback deletes it. A replacement created by a stack update is deleted when it does not stabilize. A service updated in place that does not stabilize is put back on the task definition, desired count and network configuration it had, and the provisioner waits for that to stabilize too, as CloudFormation's rollback does. A restore that fails or does not stabilize is reported as a rollback failure, so a rolled-back stack does not claim the prior configuration is running when it is not. The ECS service test in CloudFormationIntegrationTest used subnets and a security group that do not exist, so its tasks could never start. It now uses the default VPC's.
This commit is contained in:
1 parent
3b36a4d0d8
commit
0ec64add2d
7 files changed
+1152
-11
No files matched your search
+140
@@ -0,0 +1,140 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
|
||||
import software.amazon.awssdk.services.cloudformation.model.Parameter;
|
||||
import software.amazon.awssdk.services.cloudformation.model.StackStatus;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.Subnet;
|
||||
import software.amazon.awssdk.services.ecs.EcsClient;
|
||||
import software.amazon.awssdk.services.ecs.model.DeploymentRolloutState;
|
||||
import software.amazon.awssdk.services.ecs.model.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* CloudFormation reports an {@code AWS::ECS::Service} CREATE_COMPLETE only once the service has
|
||||
* reached a steady state, so DescribeServices right after the stack completes shows the desired
|
||||
* tasks running.
|
||||
*/
|
||||
@DisplayName("CloudFormation AWS::ECS::Service steady state")
|
||||
class CloudFormationEcsServiceTest {
|
||||
|
||||
private static final String TEMPLATE = """
|
||||
{
|
||||
"Parameters": {
|
||||
"Name": {"Type": "String"},
|
||||
"Subnet": {"Type": "String"}
|
||||
},
|
||||
"Resources": {
|
||||
"Cluster": {
|
||||
"Type": "AWS::ECS::Cluster",
|
||||
"Properties": {"ClusterName": {"Ref": "Name"}}
|
||||
},
|
||||
"TaskDef": {
|
||||
"Type": "AWS::ECS::TaskDefinition",
|
||||
"Properties": {
|
||||
"Family": {"Ref": "Name"},
|
||||
"NetworkMode": "awsvpc",
|
||||
"RequiresCompatibilities": ["FARGATE"],
|
||||
"Cpu": "256",
|
||||
"Memory": "512",
|
||||
"ContainerDefinitions": [
|
||||
{"Name": "web", "Image": "nginx:alpine", "Essential": true}
|
||||
]
|
||||
}
|
||||
},
|
||||
"Service": {
|
||||
"Type": "AWS::ECS::Service",
|
||||
"Properties": {
|
||||
"ServiceName": {"Ref": "Name"},
|
||||
"Cluster": {"Ref": "Cluster"},
|
||||
"TaskDefinition": {"Ref": "TaskDef"},
|
||||
"DesiredCount": 1,
|
||||
"LaunchType": "FARGATE",
|
||||
"NetworkConfiguration": {
|
||||
"AwsvpcConfiguration": {"Subnets": [{"Ref": "Subnet"}], "AssignPublicIp": "ENABLED"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
private static CloudFormationClient cfn;
|
||||
private static EcsClient ecs;
|
||||
private static Ec2Client ec2;
|
||||
private String stackName;
|
||||
private boolean stackCreated;
|
||||
|
||||
@BeforeAll
|
||||
static void clients() {
|
||||
cfn = TestFixtures.cloudFormationClient();
|
||||
ecs = TestFixtures.ecsClient();
|
||||
ec2 = TestFixtures.ec2Client();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
stackName = TestFixtures.uniqueName("compat-cfn-ecs-svc");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
if (stackCreated) {
|
||||
cfn.deleteStack(r -> r.stackName(stackName));
|
||||
cfn.waiter().waitUntilStackDeleteComplete(r -> r.stackName(stackName),
|
||||
w -> w.waitTimeout(Duration.ofMinutes(5)));
|
||||
}
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void closeClients() {
|
||||
cfn.close();
|
||||
ecs.close();
|
||||
ec2.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("DescribeServices right after CREATE_COMPLETE shows the desired task running")
|
||||
void createCompleteMeansTheDesiredTaskIsRunning() {
|
||||
cfn.createStack(r -> r.stackName(stackName).templateBody(TEMPLATE).parameters(
|
||||
parameter("Name", stackName), parameter("Subnet", defaultSubnet())));
|
||||
stackCreated = true;
|
||||
StackStatus status = cfn.waiter().waitUntilStackCreateComplete(r -> r.stackName(stackName),
|
||||
w -> w.waitTimeout(Duration.ofMinutes(10)))
|
||||
.matched().response().orElseThrow().stacks().get(0).stackStatus();
|
||||
assertThat(status).isEqualTo(StackStatus.CREATE_COMPLETE);
|
||||
|
||||
Service service = ecs.describeServices(r -> r.cluster(stackName).services(stackName))
|
||||
.services().get(0);
|
||||
|
||||
assertThat(service.desiredCount()).isEqualTo(1);
|
||||
assertThat(service.runningCount()).as("runningCount right after CREATE_COMPLETE").isEqualTo(1);
|
||||
assertThat(service.deployments()).hasSize(1);
|
||||
assertThat(service.deployments().get(0).rolloutState()).isEqualTo(DeploymentRolloutState.COMPLETED);
|
||||
}
|
||||
|
||||
private static Parameter parameter(String key, String value) {
|
||||
return Parameter.builder().parameterKey(key).parameterValue(value).build();
|
||||
}
|
||||
|
||||
/** A subnet of the default VPC, which the task's network interface goes into. */
|
||||
private static String defaultSubnet() {
|
||||
List<Subnet> subnets = ec2.describeSubnets().subnets();
|
||||
return subnets.stream()
|
||||
.filter(subnet -> Boolean.TRUE.equals(subnet.defaultForAz()))
|
||||
.map(Subnet::subnetId)
|
||||
.sorted()
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalStateException("no default-VPC subnet to place the task in"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user