Skip to content

Commit

Permalink
Update NLB Health check parameters to match docs, allow NLB un/health…
Browse files Browse the repository at this point in the history
…y counts to be different
  • Loading branch information
louismollick committed Jan 2, 2024
1 parent 9c3be5b commit e6fbb03
Showing 1 changed file with 2 additions and 56 deletions.
58 changes: 2 additions & 56 deletions ecs_composex/elbv2/elbv2_ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,57 +261,6 @@ def set_new_resource_outputs(self, output_definition):
return value


def validate_tcp_health_counts(props):
healthy_prop = "HealthyThresholdCount"
unhealthy_prop = "UnhealthyThresholdCount"
if (
keyisset(healthy_prop, props)
and keyisset(unhealthy_prop, props)
and not props[unhealthy_prop] == props[healthy_prop]
):
valid_value = max(props[unhealthy_prop], props[healthy_prop])
LOG.warning(
"With NLB your healthy and unhealthy count must be the same. Using the max of the two for cautious: "
f"{valid_value}"
)
props[healthy_prop] = valid_value
props[unhealthy_prop] = valid_value


def fix_nlb_settings(props):
"""
Function to automatically adjust/correct settings for NLB to avoid users cringe on fails
:param dict props:
"""
network_modes = ["TCP", "UDP", "TCP_UDP"]
if (
keyisset("HealthCheckProtocol", props)
and not props["HealthCheckProtocol"] in network_modes
):
return
if keyisset("HealthCheckTimeoutSeconds", props):
LOG.warning("With NLB you cannot set intervals. Resetting")
props["HealthCheckTimeoutSeconds"] = Ref(AWS_NO_VALUE)
if (
keyisset("HealthCheckIntervalSeconds", props)
and not (
props["HealthCheckIntervalSeconds"] == 10
or props["HealthCheckIntervalSeconds"] == 30
)
and not isinstance(props["HealthCheckIntervalSeconds"], Ref)
):
right_value = min(
[10, 30], key=lambda x: abs(x - props["HealthCheckIntervalSeconds"])
)
LOG.warning(
f"Set to {props['HealthCheckIntervalSeconds']} - "
f"The only intervals value valid for NLB are 10 and 30. Closest value is {right_value}"
)
props["HealthCheckIntervalSeconds"] = right_value
validate_tcp_health_counts(props)


def handle_ping_settings(props, ping_raw):
"""
Function to setup the "ping" settings
Expand All @@ -325,16 +274,15 @@ def handle_ping_settings(props, ping_raw):
ping_mapping = (
("HealthyThresholdCount", (2, 10)),
("UnhealthyThresholdCount", (2, 10)),
("HealthCheckIntervalSeconds", (2, 120)),
("HealthCheckTimeoutSeconds", (2, 10)),
("HealthCheckIntervalSeconds", (5, 300)),
("HealthCheckTimeoutSeconds", (2, 120)),
)
for count, value in enumerate(groups):
if not min(ping_mapping[count][1]) <= int(value) <= max(ping_mapping[count][1]):
LOG.error(
f"Value for {ping_mapping[count][0]} is not valid. Must be in range of {ping_mapping[count][1]}"
)
props[ping_mapping[count][0]] = int(value)
fix_nlb_settings(props)


def handle_path_settings(props, path_raw):
Expand Down Expand Up @@ -572,7 +520,6 @@ def define_service_target_group(
if not keyisset("protocol", target_definition)
else target_definition["protocol"]
)
fix_nlb_settings(props)
props["TargetType"] = "ip"
import_target_group_attributes(props, target_definition, resource)
validate_props_and_service_definition(props, service)
Expand Down Expand Up @@ -707,7 +654,6 @@ def handle_target_groups_association(
set_healthcheck_definition(props, _target_def, "HealthCheck")
props["Port"] = _target_def["Port"]
props["Protocol"] = _target_def["Protocol"]
fix_nlb_settings(props)
props["TargetType"] = "ip"
import_target_group_attributes(props, _target_def, load_balancer)
_tgt_group = MergedTargetGroup(
Expand Down

0 comments on commit e6fbb03

Please sign in to comment.