From 43490ba16156a63dc38bcb85a5febfa4a0e2a741 Mon Sep 17 00:00:00 2001 From: AnsibleGuy Date: Wed, 23 Oct 2024 21:07:46 +0200 Subject: [PATCH] added option to split statics traffic from default route-backend --- defaults/main/1_main.yml | 5 ++++ filter_plugins/utils.py | 49 ++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/defaults/main/1_main.yml b/defaults/main/1_main.yml index baa8e43..677c31d 100644 --- a/defaults/main/1_main.yml +++ b/defaults/main/1_main.yml @@ -196,3 +196,8 @@ defaults_frontend_route: filter_not_acl: [] filter_match_or: false # only one filter needs to match (pe: country or IP => [domain AND country] OR [domain AND ip]) + + # route statics-locations to other backend (to separate app services from those 'file-services') + statics: + locations: [] + backend: diff --git a/filter_plugins/utils.py b/filter_plugins/utils.py index ec52f81..758b757 100644 --- a/filter_plugins/utils.py +++ b/filter_plugins/utils.py @@ -126,23 +126,44 @@ def build_route(cls, fe_cnf: dict, be_cnf: dict, be_name: str) -> list: ) to_match.append(f'!{var_prefix}_not_asn') - if len(to_match) > 0: - if cls.is_truthy(be_cnf['filter_match_or']): - to_match_or = [] - if len(be_cnf['domains']) == 0: - to_match_or = to_match + be_statics_name = None + statics_to_match = [] + if 'statics' in be_cnf and 'locations' in be_cnf['statics'] \ + and len(be_cnf['statics']['locations']) > 0 and str(be_cnf['statics']['backend']).strip() != '': + be_statics_name = be_cnf['statics']['backend'] + statics_to_match = to_match.copy() - else: - d = to_match[0] - for m in to_match[1:]: - to_match_or.append(f'{d} {m}') + lines.append( + f"acl {var_prefix}_statics path " + f"-i -m beg {' '.join(cls.ensure_list(be_cnf['statics']['locations']))}" + ) + to_match.append(f'!{var_prefix}_statics') + statics_to_match.append(f'{var_prefix}_statics') + + for loop_be, loop_match in { + be_name: to_match, + be_statics_name: statics_to_match + }.items(): + if loop_be is None: + continue - lines.append(f"use_backend {be_name} if {' || '.join(to_match_or)}") + if len(loop_match) > 0: + if cls.is_truthy(be_cnf['filter_match_or']): + loop_match_or = [] + if len(be_cnf['domains']) == 0: + loop_match_or = loop_match - else: - lines.append(f"use_backend {be_name} if {' '.join(to_match)}") + else: + d = loop_match[0] + for m in loop_match[1:]: + loop_match_or.append(f'{d} {m}') - else: - lines.append(f"use_backend {be_name}") + lines.append(f"use_backend {loop_be} if {' || '.join(loop_match_or)}") + + else: + lines.append(f"use_backend {loop_be} if {' '.join(loop_match)}") + + else: + lines.append(f"use_backend {loop_be}") return lines