Skip to content

Commit 831e35a

Browse files
committed
examples: Add cache-mirror sandbox
Signed-off-by: Ryan Northey <[email protected]>
1 parent 04cd72e commit 831e35a

File tree

8 files changed

+195
-3
lines changed

8 files changed

+195
-3
lines changed
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
.. _install_sandboxes_cache_mirror:
2+
3+
Caching mirror
4+
==============
5+
6+
.. sidebar:: Requirements
7+
8+
.. include:: _include/docker-env-setup-link.rst
9+
10+
:ref:`curl <start_sandboxes_setup_curl>`
11+
Used to make ``HTTP`` requests.
12+
13+
This is a simple example to demonstrate caching resources from an upstream, validating their contents against known checksums using the ``ChecksumFilter``.
14+
15+
This can be useful, for example, to cache build assets for improved speed or reliability.
16+
17+
The example uses a backend that serves 2 endpoints ``/foo`` and ``/bar``, that are expected to return ``FOO`` and ``BAR`` respectively.
18+
19+
In the latter case it does not and therefore fails validation and is not cached or proxied.
20+
21+
The cache mirror inserts a cache-forever header in any validated resources to ensure they are cached without expiry. (TODO)
22+
23+
Step 1: Start all of our containers
24+
***********************************
25+
26+
Change to the ``examples/cache-mirror`` directory.
27+
28+
.. code-block:: console
29+
30+
$ pwd
31+
envoy/examples/cache-mirror
32+
$ docker compose pull
33+
$ docker compose up --build -d
34+
$ docker compose ps
35+
36+
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
37+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
38+
cache-mirror-backend-1 cache-mirror-backend "/docker-entrypoint.…" backend 6 seconds ago Up 5 seconds 10000/tcp
39+
cache-mirror-mirror-proxy-1 cache-mirror-mirror-proxy "/docker-entrypoint.…" mirror-proxy 6 seconds ago Up 5 seconds 0.0.0.0:10000->10000/tcp, :::10000->10000/tcp
40+
41+
42+
Step 2: Fetch the cached asset
43+
******************************
44+
45+
Step 3: Check the stats
46+
***********************
47+
48+
Step 4: Fetch the cached asset again
49+
************************************
50+
51+
Step 5: Confirm the asset was served from cache
52+
***********************************************
53+
54+
Step 6: Fetch cached asset with invalid checksum
55+
************************************************
56+
57+
Step 7: Confirm the asset was not cached
58+
****************************************
59+
60+
61+
.. seealso::
62+
63+
:ref:`Envoy Cache filter configuration <config_http_filters_cache>`
64+
Learn more about configuring the Envoy Cache filter.

docs/root/start/sandboxes/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The following sandboxes are available:
4747

4848
brotli
4949
cache
50+
cache-mirror
5051
cors
5152
csrf
5253
double-proxy

examples/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ filegroup(
1616
exclude = [
1717
"cache/ci-responses.yaml",
1818
"cache/responses.yaml",
19+
"cache-mirror/envoy.yaml",
1920
"dynamic-config-fs/**/*",
2021
"jaeger-native-tracing/*",
2122
"opentelemetry/otel-collector-config.yaml",
@@ -34,6 +35,7 @@ filegroup(
3435
name = "contrib_configs",
3536
srcs = glob(
3637
[
38+
"cache-mirror/envoy.yaml",
3739
"golang-http/*.yaml",
3840
"golang-network/*.yaml",
3941
"mysql/*.yaml",
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
mirror-proxy:
3+
build:
4+
context: .
5+
dockerfile: ../shared/envoy/Dockerfile
6+
args:
7+
ENVOY_VARIANT: contrib-dev
8+
ports:
9+
- "${PORT_PROXY:-10000}:10000"
10+
11+
backend:
12+
build:
13+
context: .
14+
dockerfile: ../shared/envoy/Dockerfile
15+
args:
16+
ENVOY_CONFIG: envoy-backend.yaml
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
static_resources:
2+
listeners:
3+
- name: listener_0
4+
address:
5+
socket_address:
6+
protocol: TCP
7+
address: 0.0.0.0
8+
port_value: 10000
9+
filter_chains:
10+
- filters:
11+
- name: envoy.filters.network.http_connection_manager
12+
typed_config:
13+
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
14+
stat_prefix: ingress_http
15+
route_config:
16+
name: local_route
17+
virtual_hosts:
18+
- name: local_service
19+
domains: ["*"]
20+
routes:
21+
- match:
22+
prefix: "/foo"
23+
direct_response:
24+
status: 200
25+
body:
26+
inline_string: "FOO"
27+
- match:
28+
prefix: "/bar"
29+
direct_response:
30+
status: 200
31+
body:
32+
inline_string: "BAZ"
33+
http_filters:
34+
- name: envoy.filters.http.router
35+
typed_config:
36+
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

examples/cache-mirror/envoy.yaml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
static_resources:
2+
listeners:
3+
- name: listener_0
4+
address:
5+
socket_address:
6+
protocol: TCP
7+
address: 0.0.0.0
8+
port_value: 10000
9+
filter_chains:
10+
- filters:
11+
- name: envoy.filters.network.http_connection_manager
12+
typed_config:
13+
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
14+
stat_prefix: ingress_http
15+
access_log:
16+
- name: envoy.access_loggers.stdout
17+
typed_config:
18+
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
19+
route_config:
20+
name: local_route
21+
virtual_hosts:
22+
- name: local_service
23+
domains: ["*"]
24+
routes:
25+
- match:
26+
prefix: "/"
27+
route:
28+
host_rewrite_literal: github.com
29+
cluster: github_com
30+
http_filters:
31+
- name: envoy.filters.http.checksum
32+
typed_config:
33+
"@type": type.googleapis.com/envoy.extensions.filters.http.checksum.v3alpha.ChecksumConfig
34+
checksums:
35+
- path_matcher:
36+
exact: /foo
37+
sha256: 1706b5c18e4358041b463995efc30f8f721766fab0e018d50d85978b46df013c
38+
- path_matcher:
39+
exact: /badfoo
40+
sha256: e629cbae1acb296c138795f38149a3efc0eb894e041f2dc588864c8103bc5843
41+
- name: envoy.filters.http.router
42+
typed_config:
43+
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
44+
clusters:
45+
- name: github_com
46+
type: LOGICAL_DNS
47+
# Comment out the following line to test on v6 networks
48+
dns_lookup_family: V4_ONLY
49+
lb_policy: ROUND_ROBIN
50+
load_assignment:
51+
cluster_name: service_github_com
52+
endpoints:
53+
- lb_endpoints:
54+
- endpoint:
55+
address:
56+
socket_address:
57+
address: github.com
58+
port_value: 443
59+
transport_socket:
60+
name: envoy.transport_sockets.tls
61+
typed_config:
62+
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
63+
sni: github.com

examples/cache-mirror/verify.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash -e
2+
3+
export NAME=caching-mirror
4+
5+
export PORT_PROXY="${CACHE_MIRROR_PORT_PROXY:-10330}"
6+
7+
8+
# shellcheck source=examples/verify-common.sh
9+
. "$(dirname "${BASH_SOURCE[0]}")/../verify-common.sh"

examples/shared/envoy/Dockerfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ ARG ENVOY_VARIANT="${ENVOY_VARIANT:-dev}"
33

44

55
FROM ${ENVOY_IMAGE}:${ENVOY_VARIANT} as envoy-base
6-
ARG ENVOY_CONFIG=envoy.yaml
7-
ENV ENVOY_CONFIG="$ENVOY_CONFIG"
86
ENV DEBIAN_FRONTEND=noninteractive
97
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
108
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
119
rm -f /etc/apt/apt.conf.d/docker-clean \
1210
&& echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' | tee /etc/apt/apt.conf.d/keep-cache \
1311
&& apt-get -qq update -y \
1412
&& apt-get -qq install --no-install-recommends -y curl
15-
COPY --chmod=777 "$ENVOY_CONFIG" /etc/envoy.yaml
1613
CMD ["/usr/local/bin/envoy", "-c", "/etc/envoy.yaml"]
14+
ARG ENVOY_CONFIG=envoy.yaml
15+
ENV ENVOY_CONFIG="$ENVOY_CONFIG"
16+
COPY --chmod=777 "$ENVOY_CONFIG" /etc/envoy.yaml
17+
1718

1819
FROM envoy-base as envoy-admin
1920
ARG ENVOY_ADMIN_PORT=10001

0 commit comments

Comments
 (0)