Merge ~hloeung/charm-k8s-content-cache:master into charm-k8s-content-cache:master

Proposed by Haw Loeung
Status: Merged
Approved by: Tom Haddon
Approved revision: 8f134c71f7a29d7fb0e237a364b07b33e9a1327f
Merged at revision: 527b86c405c14cebee3d884e64d238ef2035da62
Proposed branch: ~hloeung/charm-k8s-content-cache:master
Merge into: charm-k8s-content-cache:master
Diff against target: 107 lines (+42/-2)
4 files modified
config.yaml (+6/-0)
docker/templates/nginx_cfg.tmpl (+1/-1)
src/charm.py (+8/-1)
tests/test_charm.py (+27/-0)
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Canonical IS Reviewers Pending
Review via email: mp+395576@code.launchpad.net

Commit message

Fixed to set correct 'Host' header for backend and allow overriding

Description of the change

To post a comment you must log in.
Revision history for this message
Tom Haddon (mthaddon) wrote :

One comment inline

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
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.

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 527b86c405c14cebee3d884e64d238ef2035da62

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 d7dcb7c..62c80f4 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -29,6 +29,12 @@ options:
6 The backend to use for site, e.g. "http://mybackend.local:80"
7
8 This setting is required.
9+ backend_site_name:
10+ type: string
11+ description: >-
12+ Backend site name, e.g. mybackend.local. If none given, will
13+ work out from the configured 'backend' config above.
14+ default: ""
15 cache_inactive_time:
16 type: string
17 description: >-
18diff --git a/docker/templates/nginx_cfg.tmpl b/docker/templates/nginx_cfg.tmpl
19index 4e1ceb3..2e777e9 100644
20--- a/docker/templates/nginx_cfg.tmpl
21+++ b/docker/templates/nginx_cfg.tmpl
22@@ -11,7 +11,7 @@ server {
23
24 location / {
25 proxy_pass \"${NGINX_BACKEND}\";
26- proxy_set_header Host \"${NGINX_SITE_NAME}\";
27+ proxy_set_header Host \"${NGINX_BACKEND_SITE_NAME}\";
28 # Removed the following headers to avoid cache poisoning.
29 proxy_set_header Forwarded \"\";
30 proxy_set_header X-Forwarded-Host \"\";
31diff --git a/src/charm.py b/src/charm.py
32index 171e521..e8decc4 100755
33--- a/src/charm.py
34+++ b/src/charm.py
35@@ -5,6 +5,7 @@
36
37 import hashlib
38 import logging
39+from urllib.parse import urlparse
40
41 from ops.charm import CharmBase
42 from ops.main import main
43@@ -183,12 +184,18 @@ class ContentCacheCharm(CharmBase):
44 """Return dict to be used as pod spec's envConfig."""
45 config = self.model.config
46
47+ backend = config['backend']
48+ backend_site_name = config.get('backend_site_name')
49+ if not backend_site_name:
50+ backend_site_name = urlparse(backend).hostname
51+
52 client_max_body_size = '1m'
53 if config.get('client_max_body_size'):
54 client_max_body_size = config.get('client_max_body_size')
55
56 pod_config = {
57- 'NGINX_BACKEND': config['backend'],
58+ 'NGINX_BACKEND': backend,
59+ 'NGINX_BACKEND_SITE_NAME': backend_site_name,
60 'NGINX_CACHE_INACTIVE_TIME': config.get('cache_inactive_time', '10m'),
61 'NGINX_CACHE_MAX_SIZE': config.get('cache_max_size', '10G'),
62 'NGINX_CACHE_PATH': CACHE_PATH,
63diff --git a/tests/test_charm.py b/tests/test_charm.py
64index 02466ed..bd918fd 100644
65--- a/tests/test_charm.py
66+++ b/tests/test_charm.py
67@@ -340,6 +340,32 @@ class TestCharm(unittest.TestCase):
68 harness.update_config(config)
69 expected = {
70 'NGINX_BACKEND': 'http://mybackend.local:80',
71+ 'NGINX_BACKEND_SITE_NAME': 'mybackend.local',
72+ 'NGINX_CACHE_INACTIVE_TIME': '10m',
73+ 'NGINX_CACHE_MAX_SIZE': '10G',
74+ 'NGINX_CACHE_PATH': CACHE_PATH,
75+ 'NGINX_CACHE_USE_STALE': 'error timeout updating http_500 http_502 http_503 http_504',
76+ 'NGINX_CACHE_VALID': '200 1h',
77+ 'NGINX_CLIENT_MAX_BODY_SIZE': '1m',
78+ 'NGINX_KEYS_ZONE': '39c631ffb52d-cache',
79+ 'NGINX_SITE_NAME': 'mysite.local',
80+ }
81+ expected.update(JUJU_ENV_CONFIG)
82+ self.assertEqual(harness.charm._make_pod_config(), expected)
83+
84+ def test_make_pod_config_backend_site_name(self):
85+ """Test make_pod_config with charm config backend_site_config, ensure envConfig returned is correct."""
86+ harness = self.harness
87+
88+ harness.disable_hooks()
89+ harness.begin()
90+
91+ config = copy.deepcopy(BASE_CONFIG)
92+ config['backend_site_name'] = 'myoverridebackendsitename.local'
93+ harness.update_config(config)
94+ expected = {
95+ 'NGINX_BACKEND': 'http://mybackend.local:80',
96+ 'NGINX_BACKEND_SITE_NAME': 'myoverridebackendsitename.local',
97 'NGINX_CACHE_INACTIVE_TIME': '10m',
98 'NGINX_CACHE_MAX_SIZE': '10G',
99 'NGINX_CACHE_PATH': CACHE_PATH,
100@@ -364,6 +390,7 @@ class TestCharm(unittest.TestCase):
101 harness.update_config(config)
102 expected = {
103 'NGINX_BACKEND': 'http://mybackend.local:80',
104+ 'NGINX_BACKEND_SITE_NAME': 'mybackend.local',
105 'NGINX_CACHE_INACTIVE_TIME': '10m',
106 'NGINX_CACHE_MAX_SIZE': '10G',
107 'NGINX_CACHE_PATH': CACHE_PATH,

Subscribers

People subscribed via source and target branches