Merge ~hloeung/content-cache-charm:sites-persistent-ports into content-cache-charm:master

Proposed by Haw Loeung
Status: Rejected
Rejected by: Haw Loeung
Proposed branch: ~hloeung/content-cache-charm:sites-persistent-ports
Merge into: content-cache-charm:master
Prerequisite: ~hloeung/content-cache-charm:master
Diff against target: 234 lines (+187/-6)
3 files modified
reactive/content_cache.py (+17/-6)
tests/unit/files/config_test_sites_map.txt (+127/-0)
tests/unit/test_content_cache.py (+43/-0)
Reviewer Review Type Date Requested Status
Content Cache Charmers Pending
Review via email: mp+380807@code.launchpad.net

Commit message

Don't shuffling sites and port-pair mappings - LP:1865945

To post a comment you must log in.
Revision history for this message
Haw Loeung (hloeung) wrote :

Rejected in favor of keeping the mappings in unitdata.kv(). See:

https://code.launchpad.net/~hloeung/content-cache-charm/+git/content-cache-charm/+merge/380871

Unmerged commits

e0fa10b... by Haw Loeung

WIP: Don't shuffling sites and port-pair mappings - LP:1865945

3b55472... by Haw Loeung

Fixed based on reviews

ff8557e... by Haw Loeung

Add map_sites_ports for persistent ports - LP:1865945

map_sites_ports() to parse the on disk HAProxy config and build a list
of site to port-pair mappings.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reactive/content_cache.py b/reactive/content_cache.py
2index 450c63f..4311d78 100644
3--- a/reactive/content_cache.py
4+++ b/reactive/content_cache.py
5@@ -506,18 +506,29 @@ def sites_from_config(sites_yaml, sites_secrets=None, blacklist_ports=None):
6 cache_port = 0
7 backend_port = 0
8 new_sites = {}
9+ existing_site_map = HAProxy.HAProxyConf().map_sites_ports()
10+ if not blacklist_ports:
11+ blacklist_ports = []
12+ blacklist_ports += list(existing_site_map['cache'].values())
13+ blacklist_ports += list(existing_site_map['backend'].values())
14 for site, site_conf in sites.items():
15 if not site_conf:
16 continue
17- (cache_port, unused_backend_port) = utils.next_port_pair(
18- cache_port, backend_port, blacklist_ports=blacklist_ports
19- )
20+ if site in existing_site_map['cache']:
21+ cache_port = existing_site_map['cache'][site]
22+ else:
23+ (cache_port, unused_backend_port) = utils.next_port_pair(
24+ cache_port, backend_port, blacklist_ports=blacklist_ports
25+ )
26 site_conf['cache_port'] = cache_port
27 for location, loc_conf in site_conf.get('locations', {}).items():
28 if loc_conf and loc_conf.get('backends'):
29- (unused_cache_port, backend_port) = utils.next_port_pair(
30- cache_port, backend_port, blacklist_ports=blacklist_ports
31- )
32+ if site in existing_site_map['backend']:
33+ backend_port = existing_site_map['backend'][site]
34+ else:
35+ (unused_cache_port, backend_port) = utils.next_port_pair(
36+ cache_port, backend_port, blacklist_ports=blacklist_ports
37+ )
38 loc_conf['backend_port'] = backend_port
39 new_sites[site] = site_conf
40 return new_sites
41diff --git a/tests/unit/files/config_test_sites_map.txt b/tests/unit/files/config_test_sites_map.txt
42new file mode 100644
43index 0000000..7158e24
44--- /dev/null
45+++ b/tests/unit/files/config_test_sites_map.txt
46@@ -0,0 +1,127 @@
47+site1.local:
48+ cache_port: 6080
49+ locations:
50+ /:
51+ backend_port: 8080
52+ backends:
53+ - 127.0.1.10:80
54+ - 127.0.1.11:80
55+ - 127.0.1.12:80
56+ origin-headers:
57+ - X-Origin-Key: Sae6oob2aethuosh
58+ - X-Some-Header-1: something one two three
59+ - X-Some-Header-2: something:one:two:three
60+ signed-url-hmac-key: SrRorTsImr92B6FfSKBFrSIiR5NYGS+gdjd8oGoVH44=
61+ port: 80
62+site2.local:
63+ cache_port: 6081
64+ locations:
65+ /:
66+ backend-check-method: GET
67+ backend-check-path: /check/
68+ backend-maxconn: 1024
69+ backend-tls: true
70+ backend_port: 8081
71+ backends:
72+ - 127.0.1.10:443
73+ - 127.0.1.11:443
74+ - 127.0.1.12:443
75+ /my-local-content/:
76+ extra-config:
77+ - root /var/www/html
78+ /my-local-content2/:
79+ extra-config:
80+ - root /var/www/html
81+ redirect-http-to-https: true
82+ tls-cert-bundle-path: /etc/haproxy/site2-bundle.crt
83+site3.local:
84+ cache_port: 6082
85+ locations:
86+ /:
87+ backend-options:
88+ - forwardfor except 127.0.0.1
89+ - forceclose
90+ backend_port: 8082
91+ backends:
92+ - 127.0.1.10:80
93+ - 127.0.1.11:80
94+ - 127.0.1.12:80
95+ cache-maxconn: 4096
96+site4.local:
97+ cache_port: 6083
98+ locations:
99+ /:
100+ extra-config:
101+ - autoindex on
102+ /ubuntu/pool/:
103+ extra-config:
104+ - autoindex on
105+ - auth_request /auth
106+site5:
107+ cache_port: 6084
108+ locations:
109+ /:
110+ backend_port: 8083
111+ backends:
112+ - 127.0.1.10:80
113+ /auth:
114+ backend-path: /auth-check/
115+ backend_port: 8084
116+ backends:
117+ - 127.0.1.11:80
118+ cache-background-update: None
119+ cache-min-uses: None
120+ cache-validity: 200 401 1h
121+ modifier: '='
122+ site-name: site5.local
123+site6.local:
124+ cache_port: 6085
125+ locations:
126+ /:
127+ backend-tls: true
128+ backend_port: 8085
129+ backends:
130+ - 127.0.1.10:443
131+site7.local:
132+ cache_port: 6086
133+ locations:
134+ /:
135+ backend-tls: false
136+ backend_port: 8086
137+ backends:
138+ - 127.0.1.10:80
139+ port: 444
140+ tls-cert-bundle-path: /etc/haproxy/site7-bundle.crt
141+site8.local:
142+ cache_port: 6087
143+ locations:
144+ /:
145+ backend-tls: false
146+ backend_port: 8087
147+ backends:
148+ - 127.0.1.10:80
149+ /auth:
150+ backend-tls: true
151+ backend_port: 8088
152+ backends:
153+ - 127.0.1.10:443
154+ site-name: auth.site8.local
155+ port: 444
156+ tls-cert-bundle-path: /etc/haproxy/site8-bundle.crt
157+site9.local:
158+ cache_port: 6088
159+ locations:
160+ /:
161+ backend-inter-time: 1m
162+ backend-tls: false
163+ backend_port: 8089
164+ backends:
165+ - 127.0.1.15:80
166+ extra-config:
167+ - proxy_force_ranges off
168+ /private/content:
169+ extra-config:
170+ - root /srv/example1.com/content/
171+ - autoindex on
172+ - auth_request /auth
173+ nagios-expect: 401 Unauthorized
174diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
175index 4cbbf05..f506063 100644
176--- a/tests/unit/test_content_cache.py
177+++ b/tests/unit/test_content_cache.py
178@@ -7,6 +7,7 @@ from unittest import mock
179
180 import freezegun
181 import jinja2
182+import yaml
183
184 # We also need to mock up charms.layer so we can run unit tests without having
185 # to build the charm and pull in layers such as layer-status.
186@@ -774,6 +775,48 @@ site5.local:
187 }
188 self.assertEqual(want, content_cache.sites_from_config(config_yaml))
189
190+ def test_sites_from_config_no_reshuffling(self):
191+ haproxy_cfg = os.path.join(self.tmpdir, 'haproxy.cfg')
192+ with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
193+ mock_conf_file.return_value = haproxy_cfg
194+ shutil.copyfile('tests/unit/files/content_cache_rendered_haproxy_test_output.txt', haproxy_cfg)
195+
196+ with open('tests/unit/files/config_test_sites_map.txt', 'r', encoding='utf-8') as f:
197+ config_yaml = f.read()
198+ want = yaml.safe_load(config_yaml)
199+ self.assertEqual(want, content_cache.sites_from_config(config_yaml))
200+
201+ sites = yaml.safe_load(config_yaml)
202+ sites_list = list(sites.keys())
203+
204+ # Remove all except second and last site for testing. Check to
205+ # make sure it's correct and ports aren't reshuffled.
206+ new = {}
207+ new[sites_list[1]] = sites[sites_list[1]]
208+ new[sites_list[len(sites_list) - 1]] = sites[sites_list[len(sites_list) - 1]]
209+ want = new
210+ config_yaml = yaml.safe_dump(new, indent=4, default_flow_style=False)
211+ self.assertEqual(want, content_cache.sites_from_config(config_yaml))
212+
213+ # Add two sites back and make sure the existing two aren't reshuffled.
214+ new = {}
215+ new[sites_list[1]] = sites[sites_list[1]]
216+ new[sites_list[len(sites_list) - 1]] = sites[sites_list[len(sites_list) - 1]]
217+ new[sites_list[0]] = sites[sites_list[0]]
218+ new[sites_list[2]] = sites[sites_list[2]]
219+ want = new
220+ config_yaml = yaml.safe_dump(new, indent=4, default_flow_style=False)
221+ self.assertEqual(want, content_cache.sites_from_config(config_yaml))
222+
223+ # Add new site somewhere in the middle
224+ new = {}
225+ new[sites_list[1]] = sites[sites_list[1]]
226+ new[sites_list[len(sites_list) - 1]] = sites[sites_list[len(sites_list) - 1]]
227+ new[sites_list[0]] = sites[sites_list[0]]
228+ new[sites_list[2]] = sites[sites_list[2]]
229+ new['site11'] = {'locations': {'/': {'backend-tls': True, 'backends': ['127.0.1.10:443']}}}
230+ self.assertEqual(want, content_cache.sites_from_config(config_yaml))
231+
232 def test_sites_from_config_blacklist_ports(self):
233 blacklist_ports = [6080, 8080]
234 config_yaml = '''

Subscribers

People subscribed via source and target branches