Merge ~hloeung/content-cache-charm:nginx-config into content-cache-charm:master

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 5ca272f35b3f9cff9c68f340f12e4b25fce6957f
Merged at revision: 5f5a1f7e20b74277696ffab710320d890a96e031
Proposed branch: ~hloeung/content-cache-charm:nginx-config
Merge into: content-cache-charm:master
Diff against target: 137 lines (+33/-7)
6 files modified
lib/nginx.py (+17/-2)
templates/nginx_cfg.tmpl (+7/-1)
tests/unit/files/config_test_config.txt (+3/-0)
tests/unit/files/nginx_config_rendered_test_output-site3.local.txt (+1/-1)
tests/unit/files/nginx_config_rendered_test_output-site8.local.txt (+3/-2)
tests/unit/files/nginx_config_rendered_test_output-site9.local.txt (+2/-1)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Content Cache Charmers Pending
Review via email: mp+382661@code.launchpad.net

Commit message

Add support for multiple cache validities - LP:1873116

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
Stuart Bishop (stub) wrote :

Code all looks fine.

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

Change successfully merged at revision 5f5a1f7e20b74277696ffab710320d890a96e031

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/nginx.py b/lib/nginx.py
2index e583107..e05f727 100644
3--- a/lib/nginx.py
4+++ b/lib/nginx.py
5@@ -19,7 +19,7 @@ PROXY_CACHE_DEFAULTS = {
6 'min-uses': 1,
7 'revalidate': 'on',
8 'use-stale': 'error timeout updating http_500 http_502 http_503 http_504',
9- 'valid': '200 1d',
10+ 'valid': ['200 1d'],
11 }
12
13
14@@ -105,11 +105,26 @@ class NginxConf:
15 lc['backend'] = utils.generate_uri('localhost', backend_port, backend_path)
16 for k, v in self.proxy_cache_configs.items():
17 cache_key = 'cache-{}'.format(k)
18+ if cache_key == 'cache-valid':
19+ # Skip and set the default later.
20+ continue
21 lc.setdefault(cache_key, v)
22+
23+ cache_val = self.proxy_cache_configs['valid']
24 # Backwards compatibility
25 if 'cache-validity' in lc:
26- lc['cache-valid'] = lc.get('cache-validity', self.proxy_cache_configs['valid'])
27+ cache_val = lc['cache-validity']
28 lc.pop('cache-validity')
29+ elif 'cache-valid' in lc:
30+ cache_val = lc['cache-valid']
31+
32+ lc['cache-valid'] = []
33+ # Support multiple cache-validities per LP:1873116.
34+ if isinstance(cache_val, str):
35+ lc['cache-valid'].append(cache_val)
36+ else:
37+ lc['cache-valid'] += cache_val
38+
39 lc['force_ranges'] = 'on'
40 extra_config = lc.get('extra-config', [])
41 for ext in extra_config:
42diff --git a/templates/nginx_cfg.tmpl b/templates/nginx_cfg.tmpl
43index 78888f7..70be061 100644
44--- a/templates/nginx_cfg.tmpl
45+++ b/templates/nginx_cfg.tmpl
46@@ -16,9 +16,15 @@ server {
47 add_header X-Cache-Status "$upstream_cache_status from {{juju_unit}}";
48 proxy_force_ranges {{conf['force_ranges']}};
49 proxy_cache {{keys_zone}};
50-{%- for key, value in conf.items() -%}
51+{%- for key, value in conf.items()|sort(attribute='0') -%}
52 {%- if key.startswith('cache-') and value != 'None' %}
53+ {%- if key == 'cache-valid' %}
54+ {%- for cache_valid in value %}
55+ proxy_cache_valid {{cache_valid}};
56+ {%- endfor -%}
57+ {%- else %}
58 proxy_{{key|replace("-", "_")}} {{value}};
59+ {%- endif %}
60 {%- endif %}
61 {%- endfor -%}
62 {%- endif %}
63diff --git a/tests/unit/files/config_test_config.txt b/tests/unit/files/config_test_config.txt
64index ed4f6c7..fb0e4fb 100644
65--- a/tests/unit/files/config_test_config.txt
66+++ b/tests/unit/files/config_test_config.txt
67@@ -101,10 +101,12 @@ site8.local:
68 backend-tls: False
69 backends:
70 - 127.0.1.10:80
71+ cache-valid: '200 6h'
72 /auth:
73 backend-tls: True
74 backends:
75 - 127.0.1.10:443
76+ cache-valid: ['200 1h', '401 15m']
77 site-name: auth.site8.local
78
79 # Test 9: Custom backend-inter-time
80@@ -115,6 +117,7 @@ site9.local:
81 backend-inter-time: '1m'
82 backends:
83 - 127.0.1.15:80
84+ cache-validity: ['200 1h', '401 30m']
85 extra-config:
86 - proxy_force_ranges off
87 /private/content:
88diff --git a/tests/unit/files/nginx_config_rendered_test_output-site3.local.txt b/tests/unit/files/nginx_config_rendered_test_output-site3.local.txt
89index 32bcd50..fba7847 100644
90--- a/tests/unit/files/nginx_config_rendered_test_output-site3.local.txt
91+++ b/tests/unit/files/nginx_config_rendered_test_output-site3.local.txt
92@@ -13,9 +13,9 @@ server {
93 add_header X-Cache-Status "$upstream_cache_status from mock-content-cache/0";
94 proxy_force_ranges on;
95 proxy_cache d515cd6e60b1-cache;
96- proxy_cache_maxconn 4096;
97 proxy_cache_background_update on;
98 proxy_cache_lock on;
99+ proxy_cache_maxconn 4096;
100 proxy_cache_min_uses 1;
101 proxy_cache_revalidate on;
102 proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
103diff --git a/tests/unit/files/nginx_config_rendered_test_output-site8.local.txt b/tests/unit/files/nginx_config_rendered_test_output-site8.local.txt
104index 986504b..5e324e8 100644
105--- a/tests/unit/files/nginx_config_rendered_test_output-site8.local.txt
106+++ b/tests/unit/files/nginx_config_rendered_test_output-site8.local.txt
107@@ -18,7 +18,7 @@ server {
108 proxy_cache_min_uses 1;
109 proxy_cache_revalidate on;
110 proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
111- proxy_cache_valid 200 1d;
112+ proxy_cache_valid 200 6h;
113 }
114
115
116@@ -33,7 +33,8 @@ server {
117 proxy_cache_min_uses 1;
118 proxy_cache_revalidate on;
119 proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
120- proxy_cache_valid 200 1d;
121+ proxy_cache_valid 200 1h;
122+ proxy_cache_valid 401 15m;
123 }
124
125
126diff --git a/tests/unit/files/nginx_config_rendered_test_output-site9.local.txt b/tests/unit/files/nginx_config_rendered_test_output-site9.local.txt
127index f664e9e..80dbdfa 100644
128--- a/tests/unit/files/nginx_config_rendered_test_output-site9.local.txt
129+++ b/tests/unit/files/nginx_config_rendered_test_output-site9.local.txt
130@@ -18,7 +18,8 @@ server {
131 proxy_cache_min_uses 1;
132 proxy_cache_revalidate on;
133 proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
134- proxy_cache_valid 200 1d;
135+ proxy_cache_valid 200 1h;
136+ proxy_cache_valid 401 30m;
137 }
138
139

Subscribers

People subscribed via source and target branches