Merge ~cjwatson/squid-reverseproxy-charm:services-only-from-config into squid-reverseproxy-charm:master

Proposed by Colin Watson
Status: Merged
Approved by: Tom Haddon
Approved revision: 8666351a0937bc1b5f0373127c2eed3b76f7f066
Merged at revision: d5133d08cae926fd89b9a7434fdbf6a66817004e
Proposed branch: ~cjwatson/squid-reverseproxy-charm:services-only-from-config
Merge into: squid-reverseproxy-charm:master
Diff against target: 70 lines (+36/-2)
3 files modified
config.yaml (+4/-0)
hooks/hooks.py (+6/-2)
hooks/tests/test_website_hooks.py (+26/-0)
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Canonical IS Reviewers Pending
Review via email: mp+447787@code.launchpad.net

Commit message

Add services_only_from_config config setting

Description of the change

In some circumstances it is desirable for the charm to only re-export a subset of the services it consumes. This allows that.

I grabbed this from the snap store's fork of this charm: the store uses it as part of an arrangement to cache some names and search requests with different rate-limiting. I also need it for Launchpad in order to be able to write Apache virtual host configurations that send some requests through to haproxy via Squid but some requests directly to haproxy, for the same service (in our case we use this to selectively cache things based on whether the header indicate an attempt to authenticate). With two use cases, I think that makes it worth trying to get it into the mainline charm.

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

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

Change successfully merged at revision d5133d08cae926fd89b9a7434fdbf6a66817004e

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/config.yaml b/config.yaml
2index 99267bb..f0fea05 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -171,6 +171,10 @@ options:
6 servers:
7 - [foo.internal, 80]
8 - [bar.internal, 80]
9+ services_only_from_config:
10+ default: false
11+ type: boolean
12+ description: Ignore services from relations unless configured in 'services'.
13 enable_forward_proxy:
14 default: false
15 type: boolean
16diff --git a/hooks/hooks.py b/hooks/hooks.py
17index 73672a0..dad3b84 100755
18--- a/hooks/hooks.py
19+++ b/hooks/hooks.py
20@@ -142,10 +142,14 @@ def get_reverse_sites():
21 if len(all_sites) == 0:
22 return
23
24+ final_sites = {}
25 for site, servers in all_sites.items():
26- servers.sort()
27+ if (config_data.get("services_only_from_config")
28+ and site not in server_options_by_site):
29+ continue
30+ final_sites[site] = sorted(servers)
31
32- return all_sites
33+ return final_sites
34
35
36 def get_auth_params():
37diff --git a/hooks/tests/test_website_hooks.py b/hooks/tests/test_website_hooks.py
38index 6f22bb7..e9a115f 100644
39--- a/hooks/tests/test_website_hooks.py
40+++ b/hooks/tests/test_website_hooks.py
41@@ -266,3 +266,29 @@ class WebsiteRelationTest(TestCase):
42 self.config_get.return_value = Serializable({
43 "services": ""})
44 self.assertEqual(None, hooks.get_reverse_sites())
45+
46+ def test_services_only_from_config(self):
47+ self.relations_of_type.return_value = [
48+ {"private-address": "1.2.3.4",
49+ "__unit__": "foo/1",
50+ "__relid__": "website:1",
51+ "all_services": yaml.dump([
52+ {"service_name": "foo.internal",
53+ "service_port": 4242},
54+ {"service_name": "bar.internal",
55+ "service_port": 4243}
56+ ]),
57+ },
58+ ]
59+ self.config_get.return_value = Serializable({
60+ "services": yaml.dump([
61+ {"service_name": "foo.internal", "servers": []},
62+ ]),
63+ "services_only_from_config": True,
64+ })
65+ expected = {
66+ "foo.internal": [
67+ ("foo_internal__website_1__foo_1", "1.2.3.4", 4242, ""),
68+ ],
69+ }
70+ self.assertEqual(expected, hooks.get_reverse_sites())

Subscribers

People subscribed via source and target branches

to all changes: