-
Notifications
You must be signed in to change notification settings - Fork 0
/
varnish-vcl-configMap.yaml
176 lines (147 loc) · 5.48 KB
/
varnish-vcl-configMap.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
apiVersion: v1
kind: ConfigMap
metadata:
name: vcl-config
data:
# A templated file as has the `.tmpl` extension
backends.vcl.tmpl: |-
import directors;
{{ if .Backends -}}
{{ range .Backends }}
backend {{ .PodName }} {
// backend {{ .PodName }} labels:
{{- range $item, $key := .NodeLabels }}
// {{ $item }}: {{ $key -}}
{{ end }}
.host = "{{ .IP }}";
.port = "{{ $.TargetPort }}";
}
{{ end }}
{{- else -}}
// Without this dummy backend, varnish will not compile the code
// This is a dummy, and should not be used anywhere
backend dummy {
.host = "127.0.0.1";
.port = "0";
}
{{- end }}
sub init_backends {
// The line below is generated and creates a variable that is used to build custom logic
// when the user configured the backends incorrectly. E.g. return a custom error page that indicates the issue.
var.global_set("backendsFound", {{ if .Backends }}"true"{{ else }}"false"{{ end }}); //only strings are allowed to be set globally
new container_rr = directors.round_robin();
{{- range .Backends }}
container_rr.add_backend({{ .PodName }});
{{- end }}
}
# A static file as doesn't has the `.tmpl` extension
entrypoint.vcl: |
vcl 4.0;
import std;
import var;
include "backends.vcl";
sub vcl_init {
call init_backends;
return (ok);
}
sub vcl_recv {
if (req.restarts > 0) {
set req.hash_always_miss = true;
}
if (req.method == "GET" && req.url == "/heartbeat") {
return(synth(200, "OK"));
}
// If backends are not configured correctly
if (!(var.global_get("backendsFound") == "true")) {
return(synth(503, "No backends configured"));
}
set req.backend_hint = container_rr.backend();
if (req.method == "GET" && req.url == "/liveness") {
if (!std.healthy(req.backend_hint)) {
return(synth(503, "No healthy backends"));
}
return(synth(200, "OK"));
}
// Do not cache paths with health (healthcheck cache fix)
if (req.url ~ "health") {
return (pass);
}
if (req.method == "PURGE") {
# if (!client.ip ~ purge) {
# return(synth(405, "This IP is not allowed to send PURGE requests."));
# }
#Force cache miss
set req.hash_always_miss = true;
#ban("req.url ~ "+req.url); //The ban command is working
return (purge);
}
return (hash);
}
sub vcl_synth {
set resp.http.Content-Type = "text/html; charset=utf-8";
if (!(var.global_get("backendsFound") == "true")) { //error message if no backends configured
synthetic( {"<!DOCTYPE html>
<html>
<head>
<title>Incorrect backend configuration"</title>
</head>
<body>
<h1>Incorrect backend configuration</h1>
<p>Please check your deployment. It may not have pods running or Varnish is pointed to a non existing deployment.</p>
<p>XID: "} + req.xid + {"</p>
<hr>
</body>
</html>
"} );
} else { //default error message for the rest of the cases
synthetic( {"<!DOCTYPE html>
<html>
<head>
<title>"} + resp.status + " " + resp.reason + {"</title>
</head>
<body>
<h1>Error "} + resp.status + " " + resp.reason + {"</h1>
<p>"} + resp.reason + {"</p>
<h3>Guru meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"} );
}
return (deliver);
}
sub vcl_hash {
// Called after vcl_recv to create a hash value for the request. This is used as a key
// to look up the object in Varnish.
hash_data(req.url);
return (lookup);
}
sub vcl_hit {
// Do not serve stale objects
if (obj.ttl >= 0s) {
return (deliver);
}
return (restart);
}
sub vcl_backend_response {
// Do not cache 404s from backends
if (beresp.status == 404) {
set beresp.ttl = 0s;
}
return (deliver);
}
sub vcl_deliver {
set resp.http.grace = req.http.grace;
if (obj.hits > 0) {
set resp.http.X-Varnish-Cache = "HIT";
}
else {
set resp.http.X-Varnish-Cache = "MISS";
}
return (deliver);
}
sub vcl_fini {
return (ok);
}