1
0
Fork 0
mirror of https://github.com/ansible-collections/hetzner.hcloud.git synced 2026-02-04 08:01:49 +00:00

fix: check label_selector child targets with load_balancer_status filter (#552)

##### SUMMARY

The previous implementation did not take into consideration label
selectors targets, and their child targets.

This change implements a recursive function that traverse all the
targets.

Related to #467 #550
This commit is contained in:
Jonas L. 2024-08-16 11:09:20 +02:00 committed by GitHub
parent fce8bc9bb9
commit abdf72212b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 62 additions and 53 deletions

View file

@ -11,29 +11,38 @@ def load_balancer_status(load_balancer: dict, *args, **kwargs) -> Literal["unkno
"""
Return the status of a Load Balancer based on its targets.
"""
try:
def targets_status(targets: list) -> Literal["unknown", "unhealthy", "healthy"]:
result = "healthy"
for target in load_balancer["targets"]:
target_health_status = target.get("health_status")
# Report missing health status as unknown
if not target_health_status:
result = "unknown"
continue
for health_status in target_health_status:
status = health_status.get("status")
if status == "healthy":
continue
if status in (None, "unknown"):
result = "unknown"
continue
for target in targets:
# Label selector targets have child targets that must be checked
if target["type"] == "label_selector":
status = targets_status(target["targets"])
if status == "unhealthy":
return "unhealthy"
if status in (None, "unknown"):
result = "unknown"
continue
# Report missing health status as unknown
if not target.get("health_status"):
return "unknown"
for health_status in target.get("health_status"):
status = health_status.get("status")
if status == "unhealthy":
return "unhealthy"
if status in (None, "unknown"):
result = "unknown"
return result
try:
return targets_status(load_balancer["targets"])
except Exception as exc:
raise AnsibleFilterError(f"load_balancer_status - {to_native(exc)}", orig_exc=exc) from exc