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

Proposed by Haw Loeung
Status: Merged
Approved by: Tom Haddon
Approved revision: 7175c0232478cb63221403f71eb00fd499cc1a2b
Merged at revision: 307ffcb581de26b080df940a37712801f7001e96
Proposed branch: ~hloeung/charm-k8s-content-cache:non-charm
Merge into: charm-k8s-content-cache:master
Diff against target: 312 lines (+209/-15)
12 files modified
.gitignore (+8/-0)
.jujuignore (+12/-2)
COPYRIGHT (+16/-0)
Makefile (+24/-0)
README.md (+22/-12)
docker/Dockerfile (+27/-0)
docker/entrypoint.sh (+22/-0)
docker/files/nginx-logging-format.conf (+4/-0)
docker/templates/nginx_cfg.tmpl (+32/-0)
metadata.yaml (+1/-1)
tests/requirements.txt (+4/-0)
tox.ini (+37/-0)
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Content Cache Charmers Pending
Review via email: mp+390392@code.launchpad.net

Commit message

Non-charm related changes

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 :

This LGTM, but can we also change the metadata.yaml from "name: charm-k8s-content-cache" to "name: content-cache". This is used by charmcraft build to determine the name of the file to build, and this also makes it consistent with our other k8s charms.

Feel free to merge with that change.

review: Approve
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 must be approved by a member of one of the following teams:
Setting status to needs review.

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

Change successfully merged at revision 307ffcb581de26b080df940a37712801f7001e96

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.gitignore b/.gitignore
2new file mode 100644
3index 0000000..9759e36
4--- /dev/null
5+++ b/.gitignore
6@@ -0,0 +1,8 @@
7+*~
8+*.charm
9+*.py[cod]
10+*.swp
11+.coverage
12+.tox/
13+build/
14+__pycache__/
15diff --git a/.jujuignore b/.jujuignore
16index 6ccd559..4365792 100644
17--- a/.jujuignore
18+++ b/.jujuignore
19@@ -1,3 +1,13 @@
20-/venv
21-*.py[cod]
22+/venv/
23+*~
24 *.charm
25+*.py[cod]
26+*.swp
27+.coverage
28+.gitignore
29+.tox/
30+Makefile
31+build/
32+docker/
33+tests/
34+__pycache__/
35diff --git a/COPYRIGHT b/COPYRIGHT
36new file mode 100644
37index 0000000..1889423
38--- /dev/null
39+++ b/COPYRIGHT
40@@ -0,0 +1,16 @@
41+Format: http://dep.debian.net/deps/dep5/
42+
43+Files: *
44+Copyright: Copyright 2020, Canonical Ltd.
45+License: GPL-3
46+ This program is free software: you can redistribute it and/or modify
47+ it under the terms of the GNU General Public License version 3, as
48+ published by the Free Software Foundation.
49+ .
50+ This program is distributed in the hope that it will be useful,
51+ but WITHOUT ANY WARRANTY; without even the implied warranties of
52+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
53+ PURPOSE. See the GNU General Public License for more details.
54+ .
55+ You should have received a copy of the GNU General Public License
56+ along with this program. If not, see <http://www.gnu.org/licenses/>.
57diff --git a/Makefile b/Makefile
58new file mode 100644
59index 0000000..af2c5d1
60--- /dev/null
61+++ b/Makefile
62@@ -0,0 +1,24 @@
63+# Copyright (C) 2020 Canonical Ltd.
64+# See LICENSE file for licensing details.
65+
66+lint:
67+ @echo "Normalising python layout with black."
68+ @tox -e black
69+ @echo "Running flake8"
70+ @tox -e lint
71+
72+# We actually use the build directory created by charmcraft,
73+# but the .charm file makes a much more convenient sentinel.
74+unittest: content-cache.charm
75+ @tox -e unit
76+
77+test: lint unittest
78+
79+clean:
80+ @echo "Cleaning files"
81+ @git clean -fXd
82+
83+content-cache.charm: src/*.py requirements.txt
84+ charmcraft build
85+
86+.PHONY: lint test unittest clean
87diff --git a/README.md b/README.md
88index aabaf43..aaaae75 100644
89--- a/README.md
90+++ b/README.md
91@@ -2,27 +2,37 @@
92
93 ## Description
94
95-TODO: fill out the description
96+Deploy content caching layer into K8s.
97
98 ## Usage
99
100-TODO: explain how to use the charm
101+Build the docker image:
102+
103+ `cd docker`
104+ `docker build . -t myimage:v<revision>`
105+ `docker tag myimage:v<revision> localhost:32000/myimage:v<revision>`
106+ `docker push localhost:32000/myimage:v<revision>`
107+
108+Build the charm:
109+
110+ `charmcraft build`
111+
112+Deploy:
113+
114+ `juju deploy ./content-cache.charm --config image_path=localhost:32000/myimage:v<revision> --config site=mysite.local --config backend=http://mybackend.local:80`
115+
116+### Test Deployment
117+
118+`curl --resolve mysite.local:80:<ingress IP> http://mysite.local`
119
120 ### Scale Out Usage
121
122-...
123+Just run `juju add-unit <application name>`.
124
125 ## Developing
126
127-Create and activate a virtualenv,
128-and install the development requirements,
129-
130- virtualenv -p python3 venv
131- source venv/bin/activate
132- pip install -r requirements-dev.txt
133+Just run `make lint`.
134
135 ## Testing
136
137-Just run `run_tests`:
138-
139- ./run_tests
140+Just run `make unittest`.
141diff --git a/docker/Dockerfile b/docker/Dockerfile
142new file mode 100644
143index 0000000..7f89b21
144--- /dev/null
145+++ b/docker/Dockerfile
146@@ -0,0 +1,27 @@
147+# Copyright (C) 2020 Canonical Ltd.
148+# See LICENSE file for licensing details.
149+
150+FROM ubuntu:latest
151+
152+ENV LANG C.UTF-8
153+ENV DEBIAN_FRONTEND noninteractive
154+
155+# Don't install recommends to keep image small and avoid nasty surprises
156+# e.g. rpcbind being pulled in by nrpe.
157+RUN apt-get -qy update && \
158+ apt-get -qy dist-upgrade --no-install-recommends && \
159+ apt-get -qy install --no-install-recommends nginx-light && \
160+ apt-get -qy clean && \
161+ rm -f /var/lib/apt/lists/*_*
162+
163+RUN mkdir -p /srv/content-cache
164+
165+COPY entrypoint.sh /srv/content-cache
166+COPY files /srv/content-cache/files/
167+COPY templates /srv/content-cache/templates/
168+
169+ENTRYPOINT ["/srv/content-cache/entrypoint.sh"]
170+
171+CMD ["nginx", "-g", "daemon off;"]
172+
173+EXPOSE 80
174diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh
175new file mode 100755
176index 0000000..1f3fb10
177--- /dev/null
178+++ b/docker/entrypoint.sh
179@@ -0,0 +1,22 @@
180+#!/bin/sh
181+
182+# Copyright (C) 2020 Canonical Ltd.
183+# See LICENSE file for licensing details.
184+
185+set -eu
186+
187+# https://pempek.net/articles/2013/07/08/bash-sh-as-template-engine/
188+render_template() {
189+ eval "echo \"$(cat "$1")\""
190+}
191+
192+cp /srv/content-cache/files/nginx-logging-format.conf /etc/nginx/conf.d/nginx-logging-format.conf
193+
194+# https://bugs.launchpad.net/juju/+bug/1894782
195+JUJU_UNIT=$(basename /var/lib/juju/tools/unit-* | sed -e 's/^unit-//' -e 's/-\([0-9]\+\)$/\/\1/')
196+export JUJU_UNIT
197+
198+render_template /srv/content-cache/templates/nginx_cfg.tmpl > /etc/nginx/sites-available/default
199+
200+# Run the real command
201+exec "$@"
202diff --git a/docker/files/nginx-logging-format.conf b/docker/files/nginx-logging-format.conf
203new file mode 100644
204index 0000000..8591b3e
205--- /dev/null
206+++ b/docker/files/nginx-logging-format.conf
207@@ -0,0 +1,4 @@
208+log_format content_cache '$http_x_forwarded_for - $remote_user [$time_local] '
209+ '"$request" $status $bytes_sent '
210+ '"$http_referer" "$http_user_agent" $request_time '
211+ '$upstream_cache_status $upstream_response_time';
212diff --git a/docker/templates/nginx_cfg.tmpl b/docker/templates/nginx_cfg.tmpl
213new file mode 100644
214index 0000000..4e1ceb3
215--- /dev/null
216+++ b/docker/templates/nginx_cfg.tmpl
217@@ -0,0 +1,32 @@
218+proxy_cache_path ${NGINX_CACHE_PATH} use_temp_path=off levels=1:2 keys_zone=${NGINX_KEYS_ZONE}:10m inactive=${NGINX_CACHE_INACTIVE_TIME} max_size=${NGINX_CACHE_MAX_SIZE};
219+
220+server {
221+ server_name ${NGINX_SITE_NAME};
222+ listen *:80;
223+
224+ client_max_body_size ${NGINX_CLIENT_MAX_BODY_SIZE};
225+
226+ port_in_redirect off;
227+ absolute_redirect off;
228+
229+ location / {
230+ proxy_pass \"${NGINX_BACKEND}\";
231+ proxy_set_header Host \"${NGINX_SITE_NAME}\";
232+ # Removed the following headers to avoid cache poisoning.
233+ proxy_set_header Forwarded \"\";
234+ proxy_set_header X-Forwarded-Host \"\";
235+ proxy_set_header X-Forwarded-Port \"\";
236+ proxy_set_header X-Forwarded-Proto \"\";
237+ proxy_set_header X-Forwarded-Scheme \"\";
238+
239+ add_header X-Cache-Status \"\$upstream_cache_status from ${JUJU_NODE_NAME} (${JUJU_POD_NAME})\";
240+
241+ proxy_force_ranges on;
242+ proxy_cache ${NGINX_KEYS_ZONE};
243+ proxy_cache_use_stale ${NGINX_CACHE_USE_STALE};
244+ proxy_cache_valid ${NGINX_CACHE_VALID};
245+ }
246+
247+ access_log /dev/stdout content_cache;
248+ error_log /dev/stdout info;
249+}
250diff --git a/metadata.yaml b/metadata.yaml
251index 2874a21..dacd17e 100644
252--- a/metadata.yaml
253+++ b/metadata.yaml
254@@ -1,4 +1,4 @@
255-name: charm-k8s-content-cache
256+name: content-cache
257 description: |
258 Useful for providing local mirrors of HTTP servers and building
259 content delivery networks (CDN).
260diff --git a/tests/requirements.txt b/tests/requirements.txt
261new file mode 100644
262index 0000000..f6c26b8
263--- /dev/null
264+++ b/tests/requirements.txt
265@@ -0,0 +1,4 @@
266+freezegun
267+mock
268+pytest
269+pytest-cov
270diff --git a/tox.ini b/tox.ini
271new file mode 100644
272index 0000000..2daecb5
273--- /dev/null
274+++ b/tox.ini
275@@ -0,0 +1,37 @@
276+[tox]
277+skipsdist=True
278+envlist = unit
279+skip_missing_interpreters = True
280+
281+[testenv]
282+basepython = python3
283+setenv =
284+ PYTHONPATH = {toxinidir}/src:{toxinidir}/build/lib:{toxinidir}/build/venv
285+
286+[testenv:unit]
287+commands =
288+ pytest \
289+ {posargs:-v --cov=src --cov-report=term-missing --cov-branch}
290+deps = -r{toxinidir}/tests/requirements.txt
291+ -r{toxinidir}/requirements.txt
292+setenv =
293+ PYTHONPATH={toxinidir}/src:{toxinidir}/build/lib:{toxinidir}/build/venv
294+ TZ=UTC
295+
296+[testenv:black]
297+commands = black --skip-string-normalization --line-length=120 src/ tests/
298+deps = black
299+
300+[testenv:lint]
301+commands = flake8 src/ tests/
302+# Pin flake8 to 3.7.9 to match focal
303+deps =
304+ flake8==3.7.9
305+
306+[flake8]
307+exclude =
308+ .git,
309+ __pycache__,
310+ .tox,
311+max-line-length = 120
312+max-complexity = 10

Subscribers

People subscribed via source and target branches