← Back to team overview

wordpress-charmers team mailing list archive

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

 

Thomas Cuthbert has proposed merging ~tcuthbert/charm-k8s-wordpress:additional_hostnames into charm-k8s-wordpress:master.

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

Requested reviews:
  Wordpress Charmers (wordpress-charmers)

For more details, see:
https://code.launchpad.net/~tcuthbert/charm-k8s-wordpress/+git/charm-k8s-wordpress-1/+merge/395661
-- 
Your team Wordpress Charmers is requested to review the proposed merge of ~tcuthbert/charm-k8s-wordpress:additional_hostnames into charm-k8s-wordpress:master.
diff --git a/config.yaml b/config.yaml
index 4a4604e..3571c3a 100644
--- a/config.yaml
+++ b/config.yaml
@@ -37,6 +37,10 @@ options:
     type: string
     description: "MySQL database user's password"
     default: "wordpress"
+  additional_hostnames:
+    type: string
+    description: "Space separated list of aditional hostnames for the site."
+    default: ""
   container_config:
     type: string
     description: >
diff --git a/src/charm.py b/src/charm.py
index f1a2252..5d9f4e9 100755
--- a/src/charm.py
+++ b/src/charm.py
@@ -2,6 +2,7 @@
 
 import io
 import logging
+import re
 import subprocess
 from pprint import pprint
 from yaml import safe_load
@@ -85,6 +86,10 @@ def gather_wordpress_secrets():
     return rv
 
 
+def split_additional_hostnames(hostnames):
+    return hostnames.split(" ")
+
+
 class WordpressInitialiseEvent(EventBase):
     """Custom event for signalling Wordpress initialisation.
 
@@ -274,6 +279,20 @@ class WordpressCharm(CharmBase):
             },
         }
 
+        if self.model.config["additional_hostnames"]:
+            additional_hostnames = split_additional_hostnames(self.model.config["additional_hostnames"])
+            rules = resources["kubernetesResources"]["ingressResources"][0]["spec"]["rules"]
+            for hostname in additional_hostnames:
+                rule = {
+                    "host": hostname,
+                    "http": {
+                        "paths": [
+                            {"path": "/", "backend": {"serviceName": self.app.name, "servicePort": 80}}
+                        ]
+                    },
+                }
+                rules.append(rule)
+
         ingress = resources["kubernetesResources"]["ingressResources"][0]
         if self.model.config["tls_secret_name"]:
             ingress["spec"]["tls"] = [
@@ -332,7 +351,7 @@ class WordpressCharm(CharmBase):
 
         return spec
 
-    def is_valid_config(self):
+    def is_valid_config(self):  # If this grows anymore consider breaking up into smaller functions.
         is_valid = True
         config = self.model.config
 
@@ -358,6 +377,16 @@ class WordpressCharm(CharmBase):
             self.model.unit.status = BlockedStatus(message)
             is_valid = False
 
+        if config["additional_hostnames"]:
+            split_hostnames = split_additional_hostnames(config["additional_hostnames"])
+            valid_domain_name_pattern = re.compile(r"^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$")
+            valid = [re.match(valid_domain_name_pattern, h) for h in split_hostnames]
+            if not all(valid):
+                message = "Invalid additional hostnames supplied: {}".format(config["additional_hostnames"])
+                logger.info(message)
+                self.model.unit.status = BlockedStatus(message)
+                is_valid = False
+
         return is_valid
 
     def get_service_ip(self):
diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
index acf406c..6a9c1c2 100644
--- a/tests/unit/test_charm.py
+++ b/tests/unit/test_charm.py
@@ -65,6 +65,15 @@ class TestWordpressCharm(unittest.TestCase):
         self.assertEqual(self.harness.charm.unit.status.message, expected_msg)
         self.assertLogs(expected_msg, level="INFO")
 
+        # Test for invalid additional hostnames.
+        invalid_additional_hostnames = "forgot-my-tld invalid+character.com"
+        expected_msg = "Invalid additional hostnames supplied: {}".format(invalid_additional_hostnames)
+        self.harness.update_config({"additional_hostnames": invalid_additional_hostnames})
+        self.harness.charm.is_valid_config()
+        self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
+        self.assertEqual(self.harness.charm.unit.status.message, expected_msg)
+        self.assertLogs(expected_msg, level="INFO")
+
     @mock.patch("charm._leader_set")
     @mock.patch("charm._leader_get")
     def test_create_wordpress_secrets(self, _leader_get_func, _leader_set_func):
@@ -117,6 +126,28 @@ class TestWordpressCharm(unittest.TestCase):
                                             }
                                         ]
                                     },
+                                },
+                                {
+                                    'host': 'cool-newsite.org',
+                                    'http': {
+                                        'paths': [
+                                            {
+                                                'path': '/',
+                                                'backend': {'serviceName': 'wordpress', 'servicePort': 80},
+                                            }
+                                        ]
+                                    },
+                                },
+                                {
+                                    'host': 'blog.test.com',
+                                    'http': {
+                                        'paths': [
+                                            {
+                                                'path': '/',
+                                                'backend': {'serviceName': 'wordpress', 'servicePort': 80},
+                                            }
+                                        ]
+                                    },
                                 }
                             ],
                             'tls': [{'hosts': ['blog.example.com'], 'secretName': 'blog-example-com-tls'}],
@@ -150,6 +181,28 @@ class TestWordpressCharm(unittest.TestCase):
                                             }
                                         ]
                                     },
+                                },
+                                {
+                                    'host': 'cool-newsite.org',
+                                    'http': {
+                                        'paths': [
+                                            {
+                                                'path': '/',
+                                                'backend': {'serviceName': 'wordpress', 'servicePort': 80},
+                                            }
+                                        ]
+                                    },
+                                },
+                                {
+                                    'host': 'blog.test.com',
+                                    'http': {
+                                        'paths': [
+                                            {
+                                                'path': '/',
+                                                'backend': {'serviceName': 'wordpress', 'servicePort': 80},
+                                            }
+                                        ]
+                                    },
                                 }
                             ],
                         },
diff --git a/tests/unit/test_wordpress.py b/tests/unit/test_wordpress.py
index 92712e7..54982b1 100644
--- a/tests/unit/test_wordpress.py
+++ b/tests/unit/test_wordpress.py
@@ -17,6 +17,7 @@ TEST_MODEL_CONFIG = {
     "db_name": "wordpress",
     "db_user": "admin",
     "db_password": "letmein123",
+    "additional_hostnames": "cool-newsite.org blog.test.com",
     "wp_plugin_openid_team_map": True,
     "wp_plugin_akismet_key": "somerandomstring",
     "container_config": "test-key: test",

Follow ups