mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): report NotFound for launch templates requested by name (#3246)
* fix(ec2): report NotFound for launch templates requested by name DescribeLaunchTemplates filtered the launch-template collection and returned whatever matched, so an explicitly requested LaunchTemplateName that does not exist came back as an empty success. Karpenter creates a missing launch template only when the lookup fails with InvalidLaunchTemplateName.NotFoundException, so the empty list read as "already there" and left EC2NodeClass validation stuck at launch-template-count=0. An explicit name or id is now checked against the region's templates before filtering, mirroring DescribeKeyPairs. Unqualified listings and filter queries that match nothing still return an empty list. The lenient scan stays available as matchingLaunchTemplates for the callers that treat "absent" as an answer: DescribeLaunchTemplateVersions feeds AutoScaling's own ValidationError for an unresolved template, and the reconciler must not abort a pass over a group whose template was deleted. Fixes #3234 * fix(autoscaling): only treat NotFound as a deleted launch template The reconciler's launch-template lookup caught every AwsException and returned null, so any other failure from DescribeLaunchTemplates would have read as "the template is gone" and silently skipped the group's scaling instead of surfacing. Only InvalidLaunchTemplateName.NotFoundException and InvalidLaunchTemplateId.NotFound mean the template is absent; anything else now propagates, which reconcileAll already logs per group. * fix(ec2): use InvalidLaunchTemplateId.NotFound on the mutating paths DescribeLaunchTemplates reports a missing explicit id as InvalidLaunchTemplateId.NotFound, while findLaunchTemplate — the lookup behind ModifyLaunchTemplate, DeleteLaunchTemplate, CreateLaunchTemplateVersion and resolveLaunchTemplateData — spelled the identical condition InvalidLaunchTemplateId.NotFoundException, so the two disagreed on the wire code for the same missing template. EC2 has no Exception suffix on the id code, only on the name one; that asymmetry is now noted where it is easy to "correct" by mistake.
This commit is contained in:
1 parent
1a804ae547
commit
8bc4891a9a
5 files changed
+222
-16
No files matched your search
+44
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.autoscaling;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
|
||||
import io.github.hectorvent.floci.services.autoscaling.model.LaunchConfiguration;
|
||||
@@ -25,6 +26,7 @@ import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.anyList;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -467,6 +469,48 @@ class AutoScalingReconcilerTest {
|
||||
verify(asgService, times(2)).saveAutoScalingGroup(asg);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletedLaunchTemplateDoesNotAbortTheReconcilePass() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
AutoScalingGroup asg = asgWhoseLaunchTemplateLookupFails(ec2Service,
|
||||
new AwsException("InvalidLaunchTemplateName.NotFoundException",
|
||||
"The launch template 'app-lt' does not exist.", 400));
|
||||
AutoScalingReconciler reconciler =
|
||||
new AutoScalingReconciler(asgService, ec2Service, mock(ElbV2Service.class));
|
||||
|
||||
reconciler.reconcile(asg);
|
||||
|
||||
assertEquals(0, asg.getInstances().size());
|
||||
verify(asgService).completeInstanceRefreshIfSettled("us-east-1", "app-asg");
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherLaunchTemplateErrorsSurfaceRatherThanSilentlySkippingScaling() {
|
||||
AutoScalingService asgService = mock(AutoScalingService.class);
|
||||
Ec2Service ec2Service = mock(Ec2Service.class);
|
||||
AutoScalingGroup asg = asgWhoseLaunchTemplateLookupFails(ec2Service,
|
||||
new AwsException("RequestLimitExceeded", "Request limit exceeded.", 503));
|
||||
AutoScalingReconciler reconciler =
|
||||
new AutoScalingReconciler(asgService, ec2Service, mock(ElbV2Service.class));
|
||||
|
||||
AwsException thrown = assertThrows(AwsException.class, () -> reconciler.reconcile(asg));
|
||||
|
||||
assertEquals("RequestLimitExceeded", thrown.getErrorCode());
|
||||
}
|
||||
|
||||
private static AutoScalingGroup asgWhoseLaunchTemplateLookupFails(Ec2Service ec2Service,
|
||||
AwsException failure) {
|
||||
AutoScalingGroup asg = new AutoScalingGroup();
|
||||
asg.setRegion("us-east-1");
|
||||
asg.setAutoScalingGroupName("app-asg");
|
||||
asg.setDesiredCapacity(1);
|
||||
asg.setLaunchTemplateName("app-lt");
|
||||
when(ec2Service.describeLaunchTemplates("us-east-1", List.of(), List.of("app-lt"), Map.of()))
|
||||
.thenThrow(failure);
|
||||
return asg;
|
||||
}
|
||||
|
||||
private static AsgInstance instance(String lifecycleState) {
|
||||
AsgInstance instance = new AsgInstance();
|
||||
instance.setLifecycleState(lifecycleState);
|
||||
|
||||
@@ -2015,6 +2015,35 @@ class Ec2IntegrationTest {
|
||||
equalTo("sample-template"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(44)
|
||||
void describeLaunchTemplatesRejectsMissingName() {
|
||||
given()
|
||||
.formParam("Action", "DescribeLaunchTemplates")
|
||||
.formParam("LaunchTemplateName.1", "no-such-template")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidLaunchTemplateName.NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(44)
|
||||
void describeLaunchTemplatesWithNoMatchingFilterStaysAnEmptyList() {
|
||||
given()
|
||||
.formParam("Action", "DescribeLaunchTemplates")
|
||||
.formParam("Filter.1.Name", "launch-template-name")
|
||||
.formParam("Filter.1.Value.1", "no-such-template")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("<item>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(45)
|
||||
void describeLaunchTemplateVersionsById() {
|
||||
|
||||
@@ -379,6 +379,81 @@ class Ec2ServiceTest {
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeLaunchTemplatesRejectsMissingExplicitNamesAndIds() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
|
||||
// An unfiltered describe on an account with no templates is not an error.
|
||||
assertTrue(service.describeLaunchTemplates("us-east-1", List.of(), List.of(), Map.of()).isEmpty());
|
||||
|
||||
LaunchTemplateData data = new LaunchTemplateData();
|
||||
data.setImageId("ami-present");
|
||||
LaunchTemplate present = service.createLaunchTemplate("us-east-1", "present-template", data, List.of());
|
||||
|
||||
assertEquals(1, service.describeLaunchTemplates(
|
||||
"us-east-1", List.of(), List.of("present-template"), Map.of()).size());
|
||||
|
||||
// Karpenter creates a launch template only when the lookup fails with this code; an empty
|
||||
// success reads as "already there" and leaves it waiting on a template nothing will create.
|
||||
AwsException byName = assertThrows(AwsException.class, () -> service.describeLaunchTemplates(
|
||||
"us-east-1", List.of(), List.of("absent-template"), Map.of()));
|
||||
assertEquals("InvalidLaunchTemplateName.NotFoundException", byName.getErrorCode());
|
||||
assertEquals(400, byName.getHttpStatus());
|
||||
|
||||
// A missing name is not masked by a present one in the same request.
|
||||
AwsException mixed = assertThrows(AwsException.class, () -> service.describeLaunchTemplates(
|
||||
"us-east-1", List.of(), List.of("present-template", "absent-template"), Map.of()));
|
||||
assertEquals("InvalidLaunchTemplateName.NotFoundException", mixed.getErrorCode());
|
||||
|
||||
AwsException byId = assertThrows(AwsException.class, () -> service.describeLaunchTemplates(
|
||||
"us-east-1", List.of("lt-0000000000000dead"), List.of(), Map.of()));
|
||||
assertEquals("InvalidLaunchTemplateId.NotFound", byId.getErrorCode());
|
||||
|
||||
// Another region does not hold the template, so the same name is missing there.
|
||||
AwsException otherRegion = assertThrows(AwsException.class, () -> service.describeLaunchTemplates(
|
||||
"eu-west-1", List.of(), List.of("present-template"), Map.of()));
|
||||
assertEquals("InvalidLaunchTemplateName.NotFoundException", otherRegion.getErrorCode());
|
||||
|
||||
// A filter that matches nothing still returns an empty list rather than an error.
|
||||
assertTrue(service.describeLaunchTemplates("us-east-1", List.of(), List.of(),
|
||||
Map.of("launch-template-name", List.of("no-such-template"))).isEmpty());
|
||||
assertTrue(service.describeLaunchTemplates("us-east-1", List.of(present.getLaunchTemplateId()),
|
||||
List.of(), Map.of("launch-template-name", List.of("no-such-template"))).isEmpty());
|
||||
|
||||
// AutoScaling reports its own ValidationError for an unresolved template, so the version
|
||||
// lookup it goes through keeps answering with an empty list instead of raising NotFound.
|
||||
assertTrue(service.describeLaunchTemplateVersions(
|
||||
"us-east-1", null, "absent-template", List.of()).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingLaunchTemplateIdReportsTheSameCodeWhicheverCallLooksItUp() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
|
||||
// The mutating paths resolve the template through findLaunchTemplate rather than
|
||||
// describeLaunchTemplates, and used to spell the same condition
|
||||
// InvalidLaunchTemplateId.NotFoundException.
|
||||
AwsException modify = assertThrows(AwsException.class, () -> service.modifyLaunchTemplate(
|
||||
"us-east-1", "lt-0000000000000dead", null, "1"));
|
||||
assertEquals("InvalidLaunchTemplateId.NotFound", modify.getErrorCode());
|
||||
assertEquals(400, modify.getHttpStatus());
|
||||
|
||||
AwsException delete = assertThrows(AwsException.class, () -> service.deleteLaunchTemplate(
|
||||
"us-east-1", "lt-0000000000000dead", null));
|
||||
assertEquals("InvalidLaunchTemplateId.NotFound", delete.getErrorCode());
|
||||
|
||||
// A missing name keeps the Exception suffix EC2 uses on that one.
|
||||
AwsException byName = assertThrows(AwsException.class, () -> service.deleteLaunchTemplate(
|
||||
"us-east-1", null, "absent-template"));
|
||||
assertEquals("InvalidLaunchTemplateName.NotFoundException", byName.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchTemplateVersionInheritsOmittedFieldsFromRequestedSourceVersion() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
|
||||
Reference in new issue
Block a user