Merge ~nick-moffitt/content-cache-charm:per-location-listen-stanzas into content-cache-charm:master

Proposed by Nick Moffitt
Status: Merged
Approved by: Tom Haddon
Approved revision: ddbcef14979d575ce6da6f3f2b80c10404bdaca1
Merged at revision: bcad84d4df6f5a869b1b3ca76ba26aa7f4871a3e
Proposed branch: ~nick-moffitt/content-cache-charm:per-location-listen-stanzas
Merge into: content-cache-charm:master
Diff against target: 85 lines (+28/-1)
3 files modified
lib/haproxy.py (+18/-1)
reactive/content_cache.py (+2/-0)
tests/unit/files/content_cache_rendered_haproxy_test_output.txt (+8/-0)
Reviewer Review Type Date Requested Status
Tom Haddon (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+367846@code.launchpad.net

Commit message

Create haproxy listen stanzas to match ALL nginx back-end statements

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Tom Haddon (mthaddon) wrote :

LGTM, thx

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision bcad84d4df6f5a869b1b3ca76ba26aa7f4871a3e

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/haproxy.py b/lib/haproxy.py
2index 8c315e1..6b787eb 100644
3--- a/lib/haproxy.py
4+++ b/lib/haproxy.py
5@@ -63,6 +63,21 @@ class HAProxyConf:
6 new[name] = {}
7 new[name][site] = config[site]
8 new[name][site]['port'] = port
9+
10+ for location, loc_conf in config[site].get('locations', {}).items():
11+ if 'backend_port' in loc_conf:
12+ port = loc_conf['backend_port']
13+ name = '{}:{}'.format(listen_address, port)
14+ if name not in new:
15+ new[name] = {}
16+ count = 2
17+ new_site = site
18+ while new_site in new[name]:
19+ new_site = '{}-{}'.format(site, count)
20+ count += 1
21+ if site not in new[name] or new[name][site]['port'] != port:
22+ new[name][new_site] = config[site]
23+ new[name][new_site]['port'] = port
24 return new
25
26 def render_stanza_listen(self, config):
27@@ -73,6 +88,7 @@ listen {name}
28 backend_conf = '{indent}use_backend backend-{backend} if {{ hdr(Host) -i {site_name} }}\n'
29
30 rendered_output = []
31+ stanza_names = []
32
33 # For listen stanzas, we need to merge them and use 'use_backend' with
34 # the 'Host' header to direct to the correct backends.
35@@ -84,9 +100,10 @@ listen {name}
36 site_name = site_conf.get('site-name', site)
37
38 if len(config[address_port].keys()) == 1:
39- name = self._generate_stanza_name(site)
40+ name = self._generate_stanza_name(site, stanza_names)
41 else:
42 name = 'combined-{}'.format(address_port.split(':')[1])
43+ stanza_names.append(name)
44
45 tls_path = site_conf.get('tls-cert-bundle-path')
46 if tls_path:
47diff --git a/reactive/content_cache.py b/reactive/content_cache.py
48index 82d255c..45e1c07 100644
49--- a/reactive/content_cache.py
50+++ b/reactive/content_cache.py
51@@ -210,6 +210,8 @@ def configure_haproxy():
52 }
53
54 new_loc_conf = new_conf[site]['locations'][location] = {'backends': loc_conf['backends']}
55+ if 'backend_port' in loc_conf:
56+ new_loc_conf['backend_port'] = loc_conf['backend_port']
57
58 backend_check_method = loc_conf.get('backend-check-method')
59 if backend_check_method:
60diff --git a/tests/unit/files/content_cache_rendered_haproxy_test_output.txt b/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
61index f2077ab..1d38f70 100644
62--- a/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
63+++ b/tests/unit/files/content_cache_rendered_haproxy_test_output.txt
64@@ -80,6 +80,10 @@ listen site5
65 bind 127.0.0.1:8083
66 default_backend backend-site5
67
68+listen site5-2
69+ bind 127.0.0.1:8084
70+ default_backend backend-site5
71+
72 listen site6-local
73 bind 127.0.0.1:8085
74 default_backend backend-site6-local
75@@ -97,6 +101,10 @@ listen site8-local
76 bind 127.0.0.1:8087
77 default_backend backend-site8-local
78
79+listen site8-local-2
80+ bind 127.0.0.1:8088
81+ default_backend backend-site8-local
82+
83 backend backend-cached-site1-local
84 option forwardfor
85 option httpchk HEAD /?token=1868572800_4eb30fc94f247635f7ed445083a4783862ad58de HTTP/1.0\r\nHost:\ site1.local\r\nUser-Agent:\ haproxy/httpchk

Subscribers

People subscribed via source and target branches