mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elbv2): route only to healthy targets while any target is healthy (#8452)
An ALB routed to targets still in the initial state beside healthy ones, so a newly registered target that was not ready took requests before its first health check passed. While a target group has a healthy target, the proxy now routes only to its healthy targets; when none is healthy it still fails open to every registered target, matching the ALB guide's "Before a target can receive requests from the load balancer, it must pass the initial health checks". The integration tests share their target group and health state helpers. Closes #8451
This commit is contained in:
1 parent
4fc41cc683
commit
f930634fa8
3 files changed
+94
-28
No files matched your search
@@ -3,17 +3,25 @@ package io.floci.aws.services.elbv2;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.testing.RealElbV2DataPlaneProfile;
|
||||
import io.floci.aws.testutil.FreePorts;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static io.floci.aws.testing.AlbFixtures.createListener;
|
||||
import static io.floci.aws.testing.AlbFixtures.createLoadBalancer;
|
||||
import static io.floci.aws.testing.AlbFixtures.deleteLoadBalancer;
|
||||
import static io.floci.aws.testing.AlbFixtures.deleteTargetGroup;
|
||||
import static io.floci.aws.testing.AlbFixtures.registerTarget;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
@@ -35,19 +43,8 @@ class ElbV2InitialHealthIntegrationTest {
|
||||
backend.start();
|
||||
String groupArn = null;
|
||||
try {
|
||||
groupArn = given()
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "CreateTargetGroup")
|
||||
.formParam("Name", "initial-health-wire")
|
||||
.formParam("Protocol", "HTTP")
|
||||
.formParam("Port", backend.getAddress().getPort())
|
||||
.formParam("VpcId", Ec2Service.defaultVpcId("us-east-1"))
|
||||
.formParam("TargetType", "ip")
|
||||
.formParam("HealthCheckIntervalSeconds", 300)
|
||||
.formParam("HealthyThresholdCount", 10)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("CreateTargetGroupResponse.CreateTargetGroupResult.TargetGroups.member.TargetGroupArn");
|
||||
groupArn = createTargetGroup("initial-health-wire", backend.getAddress().getPort(),
|
||||
"HealthyThresholdCount", 10);
|
||||
given()
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "RegisterTargets")
|
||||
@@ -58,16 +55,10 @@ class ElbV2InitialHealthIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
|
||||
String registeredGroupArn = groupArn;
|
||||
await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> given()
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "DescribeTargetHealth")
|
||||
.formParam("TargetGroupArn", registeredGroupArn)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("DescribeTargetHealthResponse.DescribeTargetHealthResult.TargetHealthDescriptions.member.TargetHealth.State",
|
||||
equalTo("healthy"))
|
||||
.body("DescribeTargetHealthResponse.DescribeTargetHealthResult.TargetHealthDescriptions.member.TargetHealth.Reason.size()",
|
||||
equalTo(0)));
|
||||
await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
|
||||
assertEquals(List.of("healthy"), targetHealth(registeredGroupArn, "State"));
|
||||
assertEquals(List.of(), targetHealth(registeredGroupArn, "Reason"));
|
||||
});
|
||||
assertEquals(1, probes.get(), "Initial admission must not wait for the recovery threshold");
|
||||
} finally {
|
||||
try {
|
||||
@@ -84,4 +75,77 @@ class ElbV2InitialHealthIntegrationTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialTargetGetsNoRequestsWhileAnotherTargetIsHealthy() throws Exception {
|
||||
HttpServer serving = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
serving.createContext("/", exchange -> {
|
||||
exchange.sendResponseHeaders(200, -1);
|
||||
exchange.close();
|
||||
});
|
||||
serving.start();
|
||||
int notListening = FreePorts.loopbackIpv4Port();
|
||||
int listenerPort = FreePorts.anyInterfacePort();
|
||||
String loadBalancerArn = null;
|
||||
String groupArn = null;
|
||||
try {
|
||||
loadBalancerArn = createLoadBalancer("initial-health-routing");
|
||||
groupArn = createTargetGroup("initial-health-routing", serving.getAddress().getPort(),
|
||||
"UnhealthyThresholdCount", 10);
|
||||
registerTarget(groupArn, "127.0.0.1", serving.getAddress().getPort());
|
||||
createListener(loadBalancerArn, groupArn, listenerPort);
|
||||
String registeredGroupArn = groupArn;
|
||||
await().atMost(Duration.ofSeconds(5)).untilAsserted(() ->
|
||||
assertEquals(List.of("healthy"), targetHealth(registeredGroupArn, "State")));
|
||||
|
||||
// A replacement that is not listening yet stays initial: one failed probe is below the threshold.
|
||||
registerTarget(groupArn, "127.0.0.1", notListening);
|
||||
assertThat(targetHealth(groupArn, "State"), containsInAnyOrder("healthy", "initial"));
|
||||
for (int i = 0; i < 6; i++) {
|
||||
given().baseUri("http://127.0.0.1").port(listenerPort)
|
||||
.when().get("/")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if (loadBalancerArn != null) {
|
||||
deleteLoadBalancer(loadBalancerArn);
|
||||
}
|
||||
if (groupArn != null) {
|
||||
deleteTargetGroup(groupArn);
|
||||
}
|
||||
} finally {
|
||||
serving.stop(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** An HTTP ip target group probed every 300 s, with {@code threshold} set to {@code value}. */
|
||||
private static String createTargetGroup(String name, int port, String threshold, int value) {
|
||||
return given()
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "CreateTargetGroup")
|
||||
.formParam("Name", name)
|
||||
.formParam("Protocol", "HTTP")
|
||||
.formParam("Port", port)
|
||||
.formParam("VpcId", Ec2Service.defaultVpcId("us-east-1"))
|
||||
.formParam("TargetType", "ip")
|
||||
.formParam("HealthCheckIntervalSeconds", 300)
|
||||
.formParam(threshold, value)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("CreateTargetGroupResponse.CreateTargetGroupResult.TargetGroups.member.TargetGroupArn");
|
||||
}
|
||||
|
||||
/** One {@code TargetHealth} field, such as {@code State} or {@code Reason}, for each registered target. */
|
||||
private static List<String> targetHealth(String groupArn, String field) {
|
||||
return given()
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "DescribeTargetHealth")
|
||||
.formParam("TargetGroupArn", groupArn)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath().getList("DescribeTargetHealthResponse.DescribeTargetHealthResult"
|
||||
+ ".TargetHealthDescriptions.member.TargetHealth." + field);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user