mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(elbv2): probe the target group's HealthCheckPort instead of always the traffic port (#4432)
* fix(elbv2): probe the target group's HealthCheckPort instead of always the traffic port The health checker always probed each target on its traffic port, so a target group whose HealthCheckPort names a separate port (an app serving traffic on one port and its health endpoint on another) was marked unhealthy even though its health endpoint answered. AWS probes HealthCheckPort when it is a port number and the target's own port when it is traffic-port, the default. Health state stays keyed by the traffic port either way. * fix(elbv2): reject a HealthCheckPort that is not traffic-port or a port from 1 to 65535
This commit is contained in:
1 parent
b4d8483348
commit
16a6c24e15
5 files changed
+115
-1
No files matched your search
@@ -89,6 +89,7 @@ Floci supports Application Load Balancers (ALB) and Network Load Balancers (NLB)
|
||||
- HTTP listener sockets are preserved when listener actions change and are restarted only when socket-level settings such as port change.
|
||||
- Instance targets are resolved through EC2 instance private addresses so local load balancer traffic can reach containers.
|
||||
- Target health starts in `initial` state with reason `Elb.RegistrationInProgress` and is updated by Floci's health checker when monitoring is active.
|
||||
- Health checks probe the target group's `HealthCheckPort` when it is a port number, and each target's own port when it is `traffic-port` (the default).
|
||||
- Each `CreateListener` automatically creates an immutable default rule (`priority=default`, `isDefault=true`). This rule cannot be deleted; use `ModifyListener` to change its action.
|
||||
- Rule priorities are validated for uniqueness. `SetRulePriorities` is atomic: all priority assignments are validated before any change is committed.
|
||||
- `DeleteTargetGroup` is rejected with `ResourceInUse` while the target group is referenced by any listener or rule.
|
||||
|
||||
@@ -151,9 +151,10 @@ public class ElbV2HealthChecker implements Resettable {
|
||||
int timeout = tg.getHealthCheckTimeoutSeconds() != null ? tg.getHealthCheckTimeoutSeconds() : 5;
|
||||
int healthyThreshold = tg.getHealthyThresholdCount() != null ? tg.getHealthyThresholdCount() : 5;
|
||||
int unhealthyThreshold = tg.getUnhealthyThresholdCount() != null ? tg.getUnhealthyThresholdCount() : 2;
|
||||
int probePort = healthCheckPort(tg, port);
|
||||
|
||||
vertx.executeBlocking(() -> {
|
||||
return probe(host, port, path, timeout);
|
||||
return probe(host, probePort, path, timeout);
|
||||
}).onSuccess(statusCode -> {
|
||||
boolean success = matchesStatusCode(statusCode, matcher);
|
||||
if (success) {
|
||||
@@ -209,6 +210,37 @@ public class ElbV2HealthChecker implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS probes {@code HealthCheckPort} when it is a number and the target's own port when it is
|
||||
* {@code traffic-port} or unset. Health state stays keyed by the traffic port either way.
|
||||
* A stored value that is not a valid port, which the API accepted before it validated the
|
||||
* field, keeps probing the traffic port as before.
|
||||
*/
|
||||
static int healthCheckPort(TargetGroup tg, int trafficPort) {
|
||||
String configured = tg.getHealthCheckPort();
|
||||
if (configured == null || configured.isBlank() || "traffic-port".equals(configured)) {
|
||||
return trafficPort;
|
||||
}
|
||||
Integer port = parsePort(configured);
|
||||
if (port == null) {
|
||||
LOG.debugv("Target group {0} has invalid HealthCheckPort {1}; probing traffic port {2}",
|
||||
tg.getTargetGroupArn(), configured, trafficPort);
|
||||
return trafficPort;
|
||||
}
|
||||
return port;
|
||||
}
|
||||
|
||||
/** Returns the port number {@code value} names, or null when it is not a port from 1 to 65535. */
|
||||
static Integer parsePort(String value) {
|
||||
try {
|
||||
int port = Integer.parseInt(value.trim());
|
||||
return port >= 1 && port <= 65535 ? port : null;
|
||||
} catch (NumberFormatException ignored) {
|
||||
// Not a number: the caller treats it the same as an out-of-range port.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static boolean matchesStatusCode(int code, String matcher) {
|
||||
String codeStr = String.valueOf(code);
|
||||
if (matcher.contains("-")) {
|
||||
|
||||
@@ -357,6 +357,7 @@ public class ElbV2Service implements ResourceProvider {
|
||||
String matcher, String ipAddressType,
|
||||
Map<String, String> initialTags) {
|
||||
validateName(name, "target group");
|
||||
validateHealthCheckPort(healthCheckPort);
|
||||
Map<String, TargetGroup> regionTgs = targetGroups.computeIfAbsent(region, k -> new ConcurrentHashMap<>());
|
||||
boolean duplicate = regionTgs.values().stream()
|
||||
.anyMatch(tg -> tg.getTargetGroupName().equals(name));
|
||||
@@ -450,6 +451,7 @@ public class ElbV2Service implements ResourceProvider {
|
||||
Integer healthCheckTimeout, Integer healthyThreshold,
|
||||
Integer unhealthyThreshold, String matcher) {
|
||||
TargetGroup tg = requireTargetGroup(region, arn);
|
||||
validateHealthCheckPort(healthCheckPort);
|
||||
if (healthCheckProtocol != null) tg.setHealthCheckProtocol(healthCheckProtocol);
|
||||
if (healthCheckPort != null) tg.setHealthCheckPort(healthCheckPort);
|
||||
if (healthCheckEnabled != null) tg.setHealthCheckEnabled(healthCheckEnabled);
|
||||
@@ -1056,6 +1058,17 @@ public class ElbV2Service implements ResourceProvider {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/** {@code HealthCheckPort} is either {@code traffic-port} or a port number from 1 to 65535. */
|
||||
private static void validateHealthCheckPort(String healthCheckPort) {
|
||||
if (healthCheckPort == null || "traffic-port".equals(healthCheckPort)) {
|
||||
return;
|
||||
}
|
||||
if (ElbV2HealthChecker.parsePort(healthCheckPort) == null) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Health check port '" + healthCheckPort + "' must be 'traffic-port' or a port number from 1 to 65535.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateName(String name, String resource) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
throw new AwsException("ValidationError", "Name is required for " + resource + ".", 400);
|
||||
|
||||
@@ -157,6 +157,42 @@ class ElbV2HealthCheckerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void numericHealthCheckPortIsProbedInsteadOfTrafficPort() throws Exception {
|
||||
HttpServer traffic = startServer(404);
|
||||
HttpServer health = startServer(200);
|
||||
try {
|
||||
ElbV2HealthChecker checker = healthChecker();
|
||||
TargetGroup targetGroup = targetGroup(traffic.actualPort(), "200", 1, 1);
|
||||
targetGroup.setHealthCheckPort(String.valueOf(health.actualPort()));
|
||||
TargetDescription target = target("127.0.0.1", traffic.actualPort());
|
||||
|
||||
checker.addTargets(targetGroup.getTargetGroupArn(), List.of(target), targetGroup);
|
||||
|
||||
await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> assertEquals(
|
||||
"healthy",
|
||||
checker.getHealth(targetGroup.getTargetGroupArn(), target.getId(), traffic.actualPort()).state()));
|
||||
} finally {
|
||||
close(traffic);
|
||||
close(health);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void trafficPortKeywordOrInvalidPortProbesTargetPort() {
|
||||
TargetGroup targetGroup = new TargetGroup();
|
||||
targetGroup.setHealthCheckPort("traffic-port");
|
||||
assertEquals(50051, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051));
|
||||
targetGroup.setHealthCheckPort(null);
|
||||
assertEquals(50051, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051));
|
||||
targetGroup.setHealthCheckPort("8080");
|
||||
assertEquals(8080, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051));
|
||||
targetGroup.setHealthCheckPort("70000");
|
||||
assertEquals(50051, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051));
|
||||
targetGroup.setHealthCheckPort("not-a-port");
|
||||
assertEquals(50051, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registrationStateCarriesAwsReasonUntilFirstProbeCompletes() {
|
||||
ElbV2HealthChecker checker = healthChecker();
|
||||
|
||||
@@ -599,6 +599,38 @@ class ElbV2IntegrationTest {
|
||||
.body("ErrorResponse.Error.Code", equalTo("DuplicateTargetGroupName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(22)
|
||||
void createTargetGroupRejectsOutOfRangeHealthCheckPort() {
|
||||
given()
|
||||
.formParam("Action", "CreateTargetGroup")
|
||||
.formParam("Name", "bad-hc-port-tg")
|
||||
.formParam("Protocol", "HTTP")
|
||||
.formParam("Port", "80")
|
||||
.formParam("HealthCheckPort", "70000")
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(22)
|
||||
void modifyTargetGroupRejectsNonNumericHealthCheckPort() {
|
||||
given()
|
||||
.formParam("Action", "ModifyTargetGroup")
|
||||
.formParam("TargetGroupArn", tgArn)
|
||||
.formParam("HealthCheckPort", "not-a-port")
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(23)
|
||||
void modifyTargetGroupAttributes() {
|
||||
|
||||
Reference in new issue
Block a user