mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): improve EC2 Instance update fidelity (#4130)
On UpdateStack the EC2 Instance provisioner kept a reused instance unchanged, ignoring mutable property edits and never replacing on a createOnly change. - Reconcile InstanceType and SecurityGroupIds in place on a reused instance via ModifyInstanceAttribute / ModifyInstanceGroups. UserData and IamInstanceProfile have no in-place Ec2Service path yet, so they stay a follow-up. - Force replacement on a changed PrivateIpAddress or AvailabilityZone. Both are createOnly and are not applied to the instance at launch, so the change is detected against the value declared on the previous provision and recorded on the resource, not the instance's auto-assigned value. A property with no recorded prior value is not treated as a change, so adopting this does not replace instances whose template did not actually change.
This commit is contained in:
1 parent
28ca34ad6c
commit
c847a99828
3 files changed
+359
-14
No files matched your search
+111
@@ -8,6 +8,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -83,4 +84,114 @@ class CloudFormationEc2InstanceIntegrationTest {
|
||||
.statusCode(200)
|
||||
.body(containsString(instanceId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateStackResizesInstanceTypeInPlaceWithoutReplacingTheInstance() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-ec2-resize-" + suffix;
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Server": {
|
||||
"Type": "AWS::EC2::Instance",
|
||||
"Properties": {"ImageId": "ami-12345678", "InstanceType": "%s"}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"InstanceId": {"Value": {"Ref": "Server"}}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
cfn("CreateStack", stackName, template.formatted("t3.micro"));
|
||||
String createXml = describeStacks(stackName, "CREATE_COMPLETE");
|
||||
Matcher m = Pattern.compile("<OutputValue>(i-[0-9a-fA-F]+)</OutputValue>").matcher(createXml);
|
||||
assertTrue(m.find(), "expected an instance id in the stack outputs");
|
||||
String instanceId = m.group(1);
|
||||
|
||||
cfn("UpdateStack", stackName, template.formatted("t3.small"));
|
||||
String updateXml = describeStacks(stackName, "UPDATE_COMPLETE");
|
||||
|
||||
// Ref is unchanged: InstanceType is mutable, so the instance was resized in place rather
|
||||
// than replaced with a new one.
|
||||
assertTrue(updateXml.contains("<OutputValue>" + instanceId + "</OutputValue>"),
|
||||
"instance id changed on update, so it was replaced not resized: " + updateXml);
|
||||
|
||||
// DescribeInstances reports the new type on the same instance.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "DescribeInstances")
|
||||
.formParam("InstanceId.1", instanceId)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<instanceType>t3.small</instanceType>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changingPrivateIpAddressReplacesTheInstance() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-ec2-replace-" + suffix;
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"Server": {
|
||||
"Type": "AWS::EC2::Instance",
|
||||
"Properties": {"ImageId": "ami-12345678", "InstanceType": "t3.micro",
|
||||
"PrivateIpAddress": "%s"}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"InstanceId": {"Value": {"Ref": "Server"}}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
cfn("CreateStack", stackName, template.formatted("10.0.0.5"));
|
||||
String firstId = instanceIdFrom(describeStacks(stackName, "CREATE_COMPLETE"));
|
||||
|
||||
// PrivateIpAddress is createOnly, so changing it replaces the instance rather than reusing it.
|
||||
cfn("UpdateStack", stackName, template.formatted("10.0.0.6"));
|
||||
String secondId = instanceIdFrom(describeStacks(stackName, "UPDATE_COMPLETE"));
|
||||
|
||||
assertNotEquals(firstId, secondId, "PrivateIpAddress changed, so the instance must be replaced");
|
||||
|
||||
// The replacement instance really exists in EC2.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "DescribeInstances")
|
||||
.formParam("InstanceId.1", secondId)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString(secondId));
|
||||
}
|
||||
|
||||
private static void cfn(String action, String stackName, String template) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String describeStacks(String stackName, String expectedStatus) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StackStatus>" + expectedStatus + "</StackStatus>"))
|
||||
.extract().asString();
|
||||
}
|
||||
|
||||
private static String instanceIdFrom(String stackXml) {
|
||||
Matcher m = Pattern.compile("<OutputValue>(i-[0-9a-fA-F]+)</OutputValue>").matcher(stackXml);
|
||||
assertTrue(m.find(), "expected an instance id in the stack outputs: " + stackXml);
|
||||
return m.group(1);
|
||||
}
|
||||
}
|
||||
+145
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.InstanceState;
|
||||
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
|
||||
@@ -15,6 +16,7 @@ import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -32,6 +34,7 @@ import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -206,6 +209,92 @@ class Ec2InstanceCfnProvisionerTest {
|
||||
assertEquals("i-1", provisioner.updateCleanupPhysicalId(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changingPrivateIpAddressReplacesTheInstanceAndRecordsTheCleanup() throws Exception {
|
||||
Instance i1 = new Instance();
|
||||
i1.setInstanceId("i-1");
|
||||
i1.setImageId("ami-1");
|
||||
Instance i2 = new Instance();
|
||||
i2.setInstanceId("i-2");
|
||||
i2.setImageId("ami-1");
|
||||
when(ec2.runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(), anyList(),
|
||||
any(), any(), anyList(), any(), any(), any()))
|
||||
.thenReturn(reservationOf(i1), reservationOf(i2));
|
||||
StackResource r = resource("Server");
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"PrivateIpAddress\": \"10.0.0.5\"}"), ctx(null));
|
||||
assertEquals("i-1", r.getPhysicalId());
|
||||
|
||||
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
|
||||
.thenReturn(List.of(reservationOf(i1)));
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"PrivateIpAddress\": \"10.0.0.6\"}"), ctx("i-1"));
|
||||
|
||||
assertEquals("i-2", r.getPhysicalId());
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
assertEquals("i-1", provisioner.updateCleanupPhysicalId(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void changingAvailabilityZoneReplacesTheInstance() throws Exception {
|
||||
Instance i1 = new Instance();
|
||||
i1.setInstanceId("i-1");
|
||||
i1.setImageId("ami-1");
|
||||
Instance i2 = new Instance();
|
||||
i2.setInstanceId("i-2");
|
||||
i2.setImageId("ami-1");
|
||||
when(ec2.runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(), anyList(),
|
||||
any(), any(), anyList(), any(), any(), any()))
|
||||
.thenReturn(reservationOf(i1), reservationOf(i2));
|
||||
StackResource r = resource("Server");
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"AvailabilityZone\": \"us-east-1a\"}"), ctx(null));
|
||||
|
||||
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
|
||||
.thenReturn(List.of(reservationOf(i1)));
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"AvailabilityZone\": \"us-east-1b\"}"), ctx("i-1"));
|
||||
|
||||
assertEquals("i-2", r.getPhysicalId());
|
||||
assertTrue(provisioner.hasReplacementUpdate(r));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unchangedDeclaredCreateOnlyPropertiesReuseTheInstance() throws Exception {
|
||||
Instance i1 = new Instance();
|
||||
i1.setInstanceId("i-1");
|
||||
i1.setImageId("ami-1");
|
||||
stubLaunch(i1);
|
||||
StackResource r = resource("Server");
|
||||
String template = "{\"ImageId\": \"ami-1\", \"PrivateIpAddress\": \"10.0.0.5\", "
|
||||
+ "\"AvailabilityZone\": \"us-east-1a\"}";
|
||||
provisioner.provision(r, props(template), ctx(null));
|
||||
|
||||
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
|
||||
.thenReturn(List.of(reservationOf(i1)));
|
||||
provisioner.provision(r, props(template), ctx("i-1"));
|
||||
|
||||
assertEquals("i-1", r.getPhysicalId());
|
||||
verify(ec2, times(1)).runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(),
|
||||
anyList(), any(), any(), anyList(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNewlyDeclaredCreateOnlyValueWithNoPriorRecordDoesNotReplaceTheInstance() throws Exception {
|
||||
Instance i1 = new Instance();
|
||||
i1.setInstanceId("i-1");
|
||||
i1.setImageId("ami-1");
|
||||
stubLaunch(i1);
|
||||
StackResource r = resource("Server");
|
||||
// The prior provision declared no PrivateIpAddress, so none was recorded; a first declared
|
||||
// value is not treated as a change, only recorded for next time.
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx(null));
|
||||
|
||||
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
|
||||
.thenReturn(List.of(reservationOf(i1)));
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"PrivateIpAddress\": \"10.0.0.5\"}"), ctx("i-1"));
|
||||
|
||||
assertEquals("i-1", r.getPhysicalId());
|
||||
verify(ec2, times(1)).runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(),
|
||||
anyList(), any(), any(), anyList(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void reusedInstanceReconcilesTagsInPlace() throws Exception {
|
||||
Instance i1 = new Instance();
|
||||
@@ -232,6 +321,62 @@ class Ec2InstanceCfnProvisionerTest {
|
||||
assertEquals("old", removed.getValue().get(0).getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void reusedInstanceResizesInstanceTypeInPlace() throws Exception {
|
||||
Instance i1 = new Instance();
|
||||
i1.setInstanceId("i-1");
|
||||
i1.setImageId("ami-1");
|
||||
i1.setInstanceType("t3.micro");
|
||||
stubLaunch(i1);
|
||||
StackResource r = resource("Server");
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"InstanceType\": \"t3.micro\"}"), ctx(null));
|
||||
|
||||
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
|
||||
.thenReturn(List.of(reservationOf(i1)));
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"InstanceType\": \"t3.small\"}"), ctx("i-1"));
|
||||
|
||||
assertEquals("i-1", r.getPhysicalId());
|
||||
verify(ec2).modifyInstanceAttribute("us-east-1", "i-1", "instanceType", "t3.small");
|
||||
}
|
||||
|
||||
@Test
|
||||
void reusedInstanceReattachesChangedSecurityGroups() throws Exception {
|
||||
Instance i1 = new Instance();
|
||||
i1.setInstanceId("i-1");
|
||||
i1.setImageId("ami-1");
|
||||
i1.setSecurityGroups(new ArrayList<>(List.of(new GroupIdentifier("sg-1", "old"))));
|
||||
stubLaunch(i1);
|
||||
StackResource r = resource("Server");
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx(null));
|
||||
|
||||
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
|
||||
.thenReturn(List.of(reservationOf(i1)));
|
||||
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"SecurityGroupIds\": [\"sg-2\"]}"), ctx("i-1"));
|
||||
|
||||
verify(ec2).modifyInstanceGroups("us-east-1", "i-1", List.of("sg-2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reusedInstanceLeavesUnchangedMutablePropertiesAlone() throws Exception {
|
||||
Instance i1 = new Instance();
|
||||
i1.setInstanceId("i-1");
|
||||
i1.setImageId("ami-1");
|
||||
i1.setInstanceType("t3.small");
|
||||
i1.setSecurityGroups(new ArrayList<>(List.of(new GroupIdentifier("sg-1", "keep"))));
|
||||
stubLaunch(i1);
|
||||
StackResource r = resource("Server");
|
||||
String template = "{\"ImageId\": \"ami-1\", \"InstanceType\": \"t3.small\", "
|
||||
+ "\"SecurityGroupIds\": [\"sg-1\"]}";
|
||||
provisioner.provision(r, props(template), ctx(null));
|
||||
|
||||
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
|
||||
.thenReturn(List.of(reservationOf(i1)));
|
||||
provisioner.provision(r, props(template), ctx("i-1"));
|
||||
|
||||
verify(ec2, never()).modifyInstanceAttribute(anyString(), anyString(), anyString(), anyString());
|
||||
verify(ec2, never()).modifyInstanceGroups(anyString(), anyString(), anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateCreatesAFreshInstanceWhenThePriorOneWasTerminatedOutOfBand() throws Exception {
|
||||
Instance created = new Instance();
|
||||
|
||||
Reference in new issue
Block a user