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

Proposed by Haw Loeung
Status: Rejected
Rejected by: Haw Loeung
Proposed branch: ~hloeung/charm-k8s-content-cache:cache-all
Merge into: charm-k8s-content-cache:master
Diff against target: 98 lines (+41/-0)
4 files modified
config.yaml (+8/-0)
docker/templates/nginx_cfg.tmpl (+1/-0)
src/charm.py (+5/-0)
tests/test_charm.py (+27/-0)
Reviewer Review Type Date Requested Status
Content Cache Charmers Pending
Review via email: mp+395512@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Haw Loeung (hloeung) wrote :

As suggested by Thomas, we could make this more flexible supporting a supplied pattern and using Location to add no caching headers for requests matching said pattern.

Revision history for this message
Haw Loeung (hloeung) wrote :

Err, that should be to remove the no caching headers forcing cache rather than 'add no caching headers'

Unmerged commits

c4cbb11... by Haw Loeung

Add option to always cache ignoring upstream/origin Cache-Control and Expires headers

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..ec8793c 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -29,6 +29,14 @@ options:
6 The backend to use for site, e.g. "http://mybackend.local:80"
7
8 This setting is required.
9+ cache_all:
10+ type: boolean
11+ description: >-
12+
13+ By default, we respect upstream cache headers so Cache-Control
14+ and Expires. This overrides that and enables caching even if
15+ upstream tells us not to.
16+ default: False
17 cache_inactive_time:
18 type: string
19 description: >-
20diff --git a/docker/templates/nginx_cfg.tmpl b/docker/templates/nginx_cfg.tmpl
21index 4e1ceb3..2523a37 100644
22--- a/docker/templates/nginx_cfg.tmpl
23+++ b/docker/templates/nginx_cfg.tmpl
24@@ -25,6 +25,7 @@ server {
25 proxy_cache ${NGINX_KEYS_ZONE};
26 proxy_cache_use_stale ${NGINX_CACHE_USE_STALE};
27 proxy_cache_valid ${NGINX_CACHE_VALID};
28+ ${NGINX_CACHE_ALL}
29 }
30
31 access_log /dev/stdout content_cache;
32diff --git a/src/charm.py b/src/charm.py
33index 171e521..9045994 100755
34--- a/src/charm.py
35+++ b/src/charm.py
36@@ -183,12 +183,17 @@ class ContentCacheCharm(CharmBase):
37 """Return dict to be used as pod spec's envConfig."""
38 config = self.model.config
39
40+ cache_all_configs = ''
41+ if config.get('cache_all', False):
42+ cache_all_configs = "proxy_ignore_headers Cache-Control Expires;"
43+
44 client_max_body_size = '1m'
45 if config.get('client_max_body_size'):
46 client_max_body_size = config.get('client_max_body_size')
47
48 pod_config = {
49 'NGINX_BACKEND': config['backend'],
50+ 'NGINX_CACHE_ALL': cache_all_configs,
51 'NGINX_CACHE_INACTIVE_TIME': config.get('cache_inactive_time', '10m'),
52 'NGINX_CACHE_MAX_SIZE': config.get('cache_max_size', '10G'),
53 'NGINX_CACHE_PATH': CACHE_PATH,
54diff --git a/tests/test_charm.py b/tests/test_charm.py
55index 02466ed..153b6d0 100644
56--- a/tests/test_charm.py
57+++ b/tests/test_charm.py
58@@ -340,6 +340,32 @@ class TestCharm(unittest.TestCase):
59 harness.update_config(config)
60 expected = {
61 'NGINX_BACKEND': 'http://mybackend.local:80',
62+ 'NGINX_CACHE_ALL': '',
63+ 'NGINX_CACHE_INACTIVE_TIME': '10m',
64+ 'NGINX_CACHE_MAX_SIZE': '10G',
65+ 'NGINX_CACHE_PATH': CACHE_PATH,
66+ 'NGINX_CACHE_USE_STALE': 'error timeout updating http_500 http_502 http_503 http_504',
67+ 'NGINX_CACHE_VALID': '200 1h',
68+ 'NGINX_CLIENT_MAX_BODY_SIZE': '1m',
69+ 'NGINX_KEYS_ZONE': '39c631ffb52d-cache',
70+ 'NGINX_SITE_NAME': 'mysite.local',
71+ }
72+ expected.update(JUJU_ENV_CONFIG)
73+ self.assertEqual(harness.charm._make_pod_config(), expected)
74+
75+ def test_make_pod_config_cache_all(self):
76+ """Test make_pod_config with charm config cache_all, ensure envConfig returned is correct."""
77+ harness = self.harness
78+
79+ harness.disable_hooks()
80+ harness.begin()
81+
82+ config = copy.deepcopy(BASE_CONFIG)
83+ config['cache_all'] = True
84+ harness.update_config(config)
85+ expected = {
86+ 'NGINX_BACKEND': 'http://mybackend.local:80',
87+ 'NGINX_CACHE_ALL': 'proxy_ignore_headers Cache-Control Expires;',
88 'NGINX_CACHE_INACTIVE_TIME': '10m',
89 'NGINX_CACHE_MAX_SIZE': '10G',
90 'NGINX_CACHE_PATH': CACHE_PATH,
91@@ -364,6 +390,7 @@ class TestCharm(unittest.TestCase):
92 harness.update_config(config)
93 expected = {
94 'NGINX_BACKEND': 'http://mybackend.local:80',
95+ 'NGINX_CACHE_ALL': '',
96 'NGINX_CACHE_INACTIVE_TIME': '10m',
97 'NGINX_CACHE_MAX_SIZE': '10G',
98 'NGINX_CACHE_PATH': CACHE_PATH,

Subscribers

People subscribed via source and target branches