Skip to content

Commit

Permalink
added option to split statics traffic from default route-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed Oct 23, 2024
1 parent 0b23ee1 commit 43490ba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
5 changes: 5 additions & 0 deletions defaults/main/1_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
49 changes: 35 additions & 14 deletions filter_plugins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 43490ba

Please sign in to comment.