Merge ~tcuthbert/charm-k8s-wordpress:additional_hostnames into charm-k8s-wordpress:master

Proposed by Thomas Cuthbert
Status: Merged
Approved by: Tom Haddon
Approved revision: 53bdaaed6f436eb14f45d46edcbb233f498c116b
Merged at revision: 15142081bbd9396aa0b66ba97fbaec1ae8033877
Proposed branch: ~tcuthbert/charm-k8s-wordpress:additional_hostnames
Merge into: charm-k8s-wordpress:master
Diff against target: 173 lines (+94/-0)
4 files modified
config.yaml (+4/-0)
src/charm.py (+30/-0)
tests/unit/test_charm.py (+59/-0)
tests/unit/test_wordpress.py (+1/-0)
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Canonical IS Reviewers Pending
Review via email: mp+395661@code.launchpad.net

Commit message

Support sites with multiple hostnames ie fridge.u.c / ubuntu-news.org

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 :

Added some comments inline.

Revision history for this message
Thomas Cuthbert (tcuthbert) wrote :

comments inline

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 15142081bbd9396aa0b66ba97fbaec1ae8033877

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 b0ee33b..acb10d3 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -37,6 +37,10 @@ options:
6 type: string
7 description: "MySQL database user's password"
8 default: "wordpress"
9+ additional_hostnames:
10+ type: string
11+ description: "Space separated list of aditional hostnames for the site."
12+ default: ""
13 container_config:
14 type: string
15 description: >
16diff --git a/src/charm.py b/src/charm.py
17index 3dcb33e..19fe468 100755
18--- a/src/charm.py
19+++ b/src/charm.py
20@@ -2,6 +2,7 @@
21
22 import io
23 import logging
24+import re
25 import subprocess
26 from pprint import pprint
27 from yaml import safe_load
28@@ -87,6 +88,11 @@ def gather_wordpress_secrets():
29 return rv
30
31
32+def juju_setting_to_list(config_string, split_char=" "):
33+ "Transforms Juju setting strings into a list, defaults to splitting on whitespace."
34+ return config_string.split(split_char)
35+
36+
37 class WordpressInitialiseEvent(EventBase):
38 """Custom event for signalling Wordpress initialisation.
39
40@@ -281,6 +287,20 @@ class WordpressCharm(CharmBase):
41 },
42 }
43
44+ if self.model.config["additional_hostnames"]:
45+ additional_hostnames = juju_setting_to_list(self.model.config["additional_hostnames"])
46+ rules = resources["kubernetesResources"]["ingressResources"][0]["spec"]["rules"]
47+ for hostname in additional_hostnames:
48+ rule = {
49+ "host": hostname,
50+ "http": {
51+ "paths": [
52+ {"path": "/", "backend": {"serviceName": self.app.name, "servicePort": 80}}
53+ ]
54+ },
55+ }
56+ rules.append(rule)
57+
58 ingress = resources["kubernetesResources"]["ingressResources"][0]
59 if self.model.config["tls_secret_name"]:
60 ingress["spec"]["tls"] = [
61@@ -365,6 +385,16 @@ class WordpressCharm(CharmBase):
62 self.model.unit.status = BlockedStatus(message)
63 is_valid = False
64
65+ if config["additional_hostnames"]:
66+ additional_hostnames = juju_setting_to_list(config["additional_hostnames"])
67+ valid_domain_name_pattern = re.compile(r"^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$")
68+ valid = [re.match(valid_domain_name_pattern, h) for h in additional_hostnames]
69+ if not all(valid):
70+ message = "Invalid additional hostnames supplied: {}".format(config["additional_hostnames"])
71+ logger.info(message)
72+ self.model.unit.status = BlockedStatus(message)
73+ is_valid = False
74+
75 return is_valid
76
77 def get_service_ip(self):
78diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
79index 7f6dc54..155e908 100644
80--- a/tests/unit/test_charm.py
81+++ b/tests/unit/test_charm.py
82@@ -98,6 +98,21 @@ class TestWordpressCharm(unittest.TestCase):
83 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
84 self.assertEqual(self.harness.charm.unit.status.message, expected_msg)
85 self.assertLogs(expected_msg, level="INFO")
86+ self.harness.update_config(copy.deepcopy(self.test_model_config))
87+
88+ # Test for empty additional hostnames string.
89+ self.harness.update_config({"additional_hostnames": ""})
90+ want_true = self.harness.charm.is_valid_config()
91+ self.assertTrue(want_true)
92+
93+ # Test for invalid additional hostnames.
94+ invalid_additional_hostnames = "forgot-my-tld invalid+character.com"
95+ expected_msg = "Invalid additional hostnames supplied: {}".format(invalid_additional_hostnames)
96+ self.harness.update_config({"additional_hostnames": invalid_additional_hostnames})
97+ self.harness.charm.is_valid_config()
98+ self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
99+ self.assertEqual(self.harness.charm.unit.status.message, expected_msg)
100+ self.assertLogs(expected_msg, level="INFO")
101
102 @mock.patch("charm._leader_set")
103 @mock.patch("charm._leader_get")
104@@ -151,6 +166,28 @@ class TestWordpressCharm(unittest.TestCase):
105 }
106 ]
107 },
108+ },
109+ {
110+ 'host': 'cool-newsite.org',
111+ 'http': {
112+ 'paths': [
113+ {
114+ 'path': '/',
115+ 'backend': {'serviceName': 'wordpress', 'servicePort': 80},
116+ }
117+ ]
118+ },
119+ },
120+ {
121+ 'host': 'blog.test.com',
122+ 'http': {
123+ 'paths': [
124+ {
125+ 'path': '/',
126+ 'backend': {'serviceName': 'wordpress', 'servicePort': 80},
127+ }
128+ ]
129+ },
130 }
131 ],
132 'tls': [{'hosts': ['blog.example.com'], 'secretName': 'blog-example-com-tls'}],
133@@ -185,6 +222,28 @@ class TestWordpressCharm(unittest.TestCase):
134 }
135 ]
136 },
137+ },
138+ {
139+ 'host': 'cool-newsite.org',
140+ 'http': {
141+ 'paths': [
142+ {
143+ 'path': '/',
144+ 'backend': {'serviceName': 'wordpress', 'servicePort': 80},
145+ }
146+ ]
147+ },
148+ },
149+ {
150+ 'host': 'blog.test.com',
151+ 'http': {
152+ 'paths': [
153+ {
154+ 'path': '/',
155+ 'backend': {'serviceName': 'wordpress', 'servicePort': 80},
156+ }
157+ ]
158+ },
159 }
160 ],
161 },
162diff --git a/tests/unit/test_wordpress.py b/tests/unit/test_wordpress.py
163index 47cf8be..6083481 100644
164--- a/tests/unit/test_wordpress.py
165+++ b/tests/unit/test_wordpress.py
166@@ -17,6 +17,7 @@ TEST_MODEL_CONFIG = {
167 "db_name": "wordpress",
168 "db_user": "admin",
169 "db_password": "letmein123",
170+ "additional_hostnames": "cool-newsite.org blog.test.com",
171 "wp_plugin_openid_team_map": True,
172 "wp_plugin_akismet_key": "somerandomstring",
173 "container_config": "test-key: test",

Subscribers

People subscribed via source and target branches