Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
k8s-ingress
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
3
Merge Requests
3
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
uplex-varnish
k8s-ingress
Commits
8fff6733
Commit
8fff6733
authored
Dec 10, 2018
by
Geoff Simmons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First VCL template for a self-sharding Varnish cluster.
parent
c1f1433e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
156 additions
and
0 deletions
+156
-0
self-shard.tmpl
cmd/varnish/vcl/self-shard.tmpl
+100
-0
vcl_test.go
cmd/varnish/vcl/vcl_test.go
+56
-0
No files found.
cmd/varnish/vcl/self-shard.tmpl
0 → 100644
View file @
8fff6733
vcl 4.0;
import std;
import directors;
probe vk8s_probe_varnish {
.request = "HEAD /vk8s_cluster_health HTTP/1.1"
"Host: vk8s_cluster";
.interval = 1s;
}
{{range $node := .ClusterNodes -}}
backend {{$node.Name}} {
.host = "{{(index $node.Addresses 0).IP}}";
.port = "{{(index $node.Addresses 0).Port}}";
.probe = probe_varnish;
}
{{end -}}
acl vk8s_cluster_acl {
{{- range $node := .ClusterNodes}}
"{{(index $node.Addresses 0).IP}}";
{{- end}}
}
sub vcl_init {
new vk8s_cluster = directors.shard();
{{range $node := .ClusterNodes -}}
varnish_director.add_backend({{$node.Name}});
{{end -}}
varnish_director.reconfigure();
}
sub vcl_recv {
if (remote.ip ~ vk8s_cluster_acl) {
if (req.http.Host == "vk8s_cluster") {
if (req.url == "/vk8s_cluster_health") {
return (vcl(vk8s_readiness));
}
return (synth(404));
}
# prevent deadlock for accidental cyclic requests
set req.hash_ignore_busy = true;
# if we're async, don't deliver stale
if (req.http.VK8S-Is-Bgfetch == "true") {
set req.grace = 0s;
}
return (hash);
}
}
sub vcl_backend_fetch {
if (bereq.retries == 0
&& !bereq.uncacheable
&& remote.ip !~ vk8s_cluster_acl
&& "" + varnish_director.backend(resolve=NOW) != server.identity) {
set bereq.backend = varnish_director.backend(resolve=LAZY);
set bereq.http.VK8S-Is-Bgfetch = bereq.is_bgfetch;
return (fetch);
}
}
sub vcl_backend_response {
if (bereq.backend == varnish_director.backend(resolve=LAZY)) {
if (beresp.http.VK8S-Cluster-TTL) {
set beresp.ttl = std.duration(
beresp.http.VK8S-Cluster-TTL + "s", 1s);
{{- /* XXX make this TTL configurable */}}
if (beresp.ttl > 5m) {
set beresp.ttl = 5m;
}
unset beresp.http.VK8S-Cluster-TTL;
}
else {
set beresp.uncacheable = true;
}
return (deliver);
}
}
sub vcl_backend_error {
if (bereq.backend == varnish_director.backend(resolve=LAZY)) {
return (deliver);
}
}
sub vcl_deliver {
unset resp.http.VK8S-Cluster-TTL;
if (remote.ip ~ vk8s_cluster_acl) {
if (! obj.uncacheable) {
set resp.http.X-Cluster-TTL = obj.ttl;
}
return (deliver);
}
}
cmd/varnish/vcl/vcl_test.go
View file @
8fff6733
...
...
@@ -34,6 +34,7 @@ import (
"path/filepath"
"reflect"
"testing"
"text/template"
)
var
teaSvc
=
Service
{
...
...
@@ -215,3 +216,58 @@ func TestCanoncial(t *testing.T) {
}
}
}
var
varnishCluster
=
struct
{
ClusterNodes
[]
Service
}{
ClusterNodes
:
[]
Service
{
Service
{
Name
:
"varnish-8445d4f7f-z2b9p"
,
Addresses
:
[]
Address
{
{
"172.17.0.12"
,
80
},
},
},
Service
{
Name
:
"varnish-8445d4f7f-k22dn"
,
Addresses
:
[]
Address
{
{
"172.17.0.13"
,
80
},
},
},
Service
{
Name
:
"varnish-8445d4f7f-ldljf"
,
Addresses
:
[]
Address
{
{
"172.17.0.14"
,
80
},
},
},
},
}
func
TestShardTemplate
(
t
*
testing
.
T
)
{
var
buf
bytes
.
Buffer
tmpl
,
err
:=
template
.
New
(
"self-shard.tmpl"
)
.
Funcs
(
fMap
)
.
ParseFiles
(
"self-shard.tmpl"
)
if
err
!=
nil
{
t
.
Error
(
"Cannot parse shard template:"
,
err
)
return
}
if
err
:=
tmpl
.
Execute
(
&
buf
,
varnishCluster
);
err
!=
nil
{
t
.
Error
(
"Execute():"
,
err
)
return
}
t
.
Log
(
buf
.
String
())
// goldpath := filepath.Join("testdata", "ingressrule.golden")
// gold, err := ioutil.ReadFile(goldpath)
// if err != nil {
// t.Fatalf("Error reading %s: %v", goldpath, err)
// }
// if !bytes.Equal(buf.Bytes(), gold) {
// t.Errorf("Generated VCL for IngressSpec does not match gold "+
// "file: %s", goldpath)
// if testing.Verbose() {
// t.Log("Generated VCL:", string(buf.Bytes()))
// t.Log(goldpath, ":", string(gold))
// }
// }
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment