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
9e962196
Commit
9e962196
authored
Sep 01, 2021
by
Geoff Simmons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add via and authority fields to the VCL spec and templates.
parent
a6a3d145
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
200 additions
and
0 deletions
+200
-0
spec.go
pkg/varnish/vcl/spec.go
+8
-0
via_authority_backend.golden
pkg/varnish/vcl/testdata/via_authority_backend.golden
+79
-0
via_backend.golden
pkg/varnish/vcl/testdata/via_backend.golden
+76
-0
vcl_test.go
pkg/varnish/vcl/vcl_test.go
+13
-0
vcl_tmpl.go
pkg/varnish/vcl/vcl_tmpl.go
+24
-0
No files found.
pkg/varnish/vcl/spec.go
View file @
9e962196
...
@@ -154,6 +154,7 @@ type Service struct {
...
@@ -154,6 +154,7 @@ type Service struct {
Addresses
[]
Address
Addresses
[]
Address
Probe
*
Probe
Probe
*
Probe
Director
*
Director
Director
*
Director
Authority
*
string
ExternalName
string
ExternalName
string
ExternalPort
string
ExternalPort
string
HostHeader
string
HostHeader
string
...
@@ -169,6 +170,7 @@ type Service struct {
...
@@ -169,6 +170,7 @@ type Service struct {
MaxDNSQueries
uint16
MaxDNSQueries
uint16
ProxyHeader
uint8
ProxyHeader
uint8
FollowDNSRedirects
bool
FollowDNSRedirects
bool
Via
bool
}
}
func
(
svc
Service
)
hash
(
hash
hash
.
Hash
)
{
func
(
svc
Service
)
hash
(
hash
hash
.
Hash
)
{
...
@@ -182,6 +184,9 @@ func (svc Service) hash(hash hash.Hash) {
...
@@ -182,6 +184,9 @@ func (svc Service) hash(hash hash.Hash) {
if
svc
.
Director
!=
nil
{
if
svc
.
Director
!=
nil
{
svc
.
Director
.
hash
(
hash
)
svc
.
Director
.
hash
(
hash
)
}
}
if
svc
.
Authority
!=
nil
{
hash
.
Write
([]
byte
(
*
svc
.
Authority
))
}
hash
.
Write
([]
byte
(
svc
.
ExternalName
))
hash
.
Write
([]
byte
(
svc
.
ExternalName
))
hash
.
Write
([]
byte
(
svc
.
ExternalPort
))
hash
.
Write
([]
byte
(
svc
.
ExternalPort
))
hash
.
Write
([]
byte
(
svc
.
HostHeader
))
hash
.
Write
([]
byte
(
svc
.
HostHeader
))
...
@@ -201,6 +206,9 @@ func (svc Service) hash(hash hash.Hash) {
...
@@ -201,6 +206,9 @@ func (svc Service) hash(hash hash.Hash) {
if
svc
.
FollowDNSRedirects
{
if
svc
.
FollowDNSRedirects
{
hash
.
Write
([]
byte
{
1
})
hash
.
Write
([]
byte
{
1
})
}
}
if
svc
.
Via
{
hash
.
Write
([]
byte
{
1
})
}
}
}
// interface for sorting []Service
// interface for sorting []Service
...
...
pkg/varnish/vcl/testdata/via_authority_backend.golden
0 → 100644
View file @
9e962196
vcl 4.1;
import std;
import directors;
import re2;
import dynamic;
include "bogo_backend.vcl";
backend vk8s_via { .path = "/run/offload/onload.sock"; }
backend vk8s_default_coffee-6b9f5c47d7-bdt68_80 {
.host = "192.0.2.4";
.port = "80";
}
backend vk8s_default_coffee-6b9f5c47d7-l5zvl_80 {
.host = "192.0.2.5";
.port = "80";
}
backend vk8s_default_tea-5798f99dc5-5wj8n_80 {
.host = "192.0.2.1";
.port = "80";
.via = vk8s_via;
.authority = "www.tea.org";
}
backend vk8s_default_tea-5798f99dc5-hn27l_80 {
.host = "192.0.2.2";
.port = "80";
.via = vk8s_via;
.authority = "www.tea.org";
}
backend vk8s_default_tea-5798f99dc5-5wj8n_80 {
.host = "192.0.2.3";
.port = "80";
.via = vk8s_via;
.authority = "www.tea.org";
}
sub vcl_init {
new vk8s_hosts = re2.set(anchor=both);
vk8s_hosts.add("\Qcafe.example.com\E(:\d+)?");
vk8s_hosts.compile();
new vk8s_coffee-svc_director = directors.round_robin();
vk8s_coffee-svc_director.add_backend(vk8s_default_coffee-6b9f5c47d7-bdt68_80
);
vk8s_coffee-svc_director.add_backend(vk8s_default_coffee-6b9f5c47d7-l5zvl_80
);
new vk8s_tea-svc_director = directors.round_robin();
vk8s_tea-svc_director.add_backend(vk8s_default_tea-5798f99dc5-5wj8n_80
);
vk8s_tea-svc_director.add_backend(vk8s_default_tea-5798f99dc5-hn27l_80
);
vk8s_tea-svc_director.add_backend(vk8s_default_tea-5798f99dc5-5wj8n_80
);
new vk8s_cafe_example_com_url = re2.set(posix_syntax=true, anchor=start);
vk8s_cafe_example_com_url.add("/coffee",
backend=vk8s_coffee-svc_director.backend());
vk8s_cafe_example_com_url.add("/tea",
backend=vk8s_tea-svc_director.backend());
vk8s_cafe_example_com_url.compile();
}
sub vcl_backend_fetch {
set bereq.backend = vk8s_notfound;
if (vk8s_hosts.match(bereq.http.Host)) {
if (vk8s_hosts.which(select=FIRST) == 1) {
if (vk8s_cafe_example_com_url.match(bereq.url)) {
set bereq.backend = vk8s_cafe_example_com_url.backend(select=FIRST);
}
}
}
if (bereq.backend == vk8s_notfound) {
return (error(404));
}
}
pkg/varnish/vcl/testdata/via_backend.golden
0 → 100644
View file @
9e962196
vcl 4.1;
import std;
import directors;
import re2;
import dynamic;
include "bogo_backend.vcl";
backend vk8s_via { .path = "/run/offload/onload.sock"; }
backend vk8s_default_coffee-6b9f5c47d7-bdt68_80 {
.host = "192.0.2.4";
.port = "80";
}
backend vk8s_default_coffee-6b9f5c47d7-l5zvl_80 {
.host = "192.0.2.5";
.port = "80";
}
backend vk8s_default_tea-5798f99dc5-5wj8n_80 {
.host = "192.0.2.1";
.port = "80";
.via = vk8s_via;
}
backend vk8s_default_tea-5798f99dc5-hn27l_80 {
.host = "192.0.2.2";
.port = "80";
.via = vk8s_via;
}
backend vk8s_default_tea-5798f99dc5-5wj8n_80 {
.host = "192.0.2.3";
.port = "80";
.via = vk8s_via;
}
sub vcl_init {
new vk8s_hosts = re2.set(anchor=both);
vk8s_hosts.add("\Qcafe.example.com\E(:\d+)?");
vk8s_hosts.compile();
new vk8s_coffee-svc_director = directors.round_robin();
vk8s_coffee-svc_director.add_backend(vk8s_default_coffee-6b9f5c47d7-bdt68_80
);
vk8s_coffee-svc_director.add_backend(vk8s_default_coffee-6b9f5c47d7-l5zvl_80
);
new vk8s_tea-svc_director = directors.round_robin();
vk8s_tea-svc_director.add_backend(vk8s_default_tea-5798f99dc5-5wj8n_80
);
vk8s_tea-svc_director.add_backend(vk8s_default_tea-5798f99dc5-hn27l_80
);
vk8s_tea-svc_director.add_backend(vk8s_default_tea-5798f99dc5-5wj8n_80
);
new vk8s_cafe_example_com_url = re2.set(posix_syntax=true, anchor=start);
vk8s_cafe_example_com_url.add("/coffee",
backend=vk8s_coffee-svc_director.backend());
vk8s_cafe_example_com_url.add("/tea",
backend=vk8s_tea-svc_director.backend());
vk8s_cafe_example_com_url.compile();
}
sub vcl_backend_fetch {
set bereq.backend = vk8s_notfound;
if (vk8s_hosts.match(bereq.http.Host)) {
if (vk8s_hosts.which(select=FIRST) == 1) {
if (vk8s_cafe_example_com_url.match(bereq.url)) {
set bereq.backend = vk8s_cafe_example_com_url.backend(select=FIRST);
}
}
}
if (bereq.backend == vk8s_notfound) {
return (error(404));
}
}
pkg/varnish/vcl/vcl_test.go
View file @
9e962196
...
@@ -600,6 +600,19 @@ func TestBackendConfig(t *testing.T) {
...
@@ -600,6 +600,19 @@ func TestBackendConfig(t *testing.T) {
templateTest
(
t
,
ingressTmpl
,
cafeProbeDir
,
"backendcfg.golden"
)
templateTest
(
t
,
ingressTmpl
,
cafeProbeDir
,
"backendcfg.golden"
)
}
}
func
TestViaBackend
(
t
*
testing
.
T
)
{
teaSvc
.
Via
=
true
cafeSpec
.
IntSvcs
[
"tea-svc"
]
=
teaSvc
templateTest
(
t
,
ingressTmpl
,
cafeSpec
,
"via_backend.golden"
)
teaSvc
.
Authority
=
new
(
string
)
*
teaSvc
.
Authority
=
"www.tea.org"
cafeSpec
.
IntSvcs
[
"tea-svc"
]
=
teaSvc
templateTest
(
t
,
ingressTmpl
,
cafeSpec
,
"via_authority_backend.golden"
)
}
// Code boilerplate for writing the golden file.
// Code boilerplate for writing the golden file.
// import ioutils
// import ioutils
// func TestXXX(t *testing.T) {
// func TestXXX(t *testing.T) {
...
...
pkg/varnish/vcl/vcl_tmpl.go
View file @
9e962196
...
@@ -81,6 +81,9 @@ probe {{probeName $name}} {
...
@@ -81,6 +81,9 @@ probe {{probeName $name}} {
{{- end}}
{{- end}}
{{- template "ProbeDef" .IntSvcs -}}
{{- template "ProbeDef" .IntSvcs -}}
{{- template "ProbeDef" .ExtSvcs}}
{{- template "ProbeDef" .ExtSvcs}}
{{- if needsVia .IntSvcs .ExtSvcs }}
backend vk8s_via { .path = "/run/offload/onload.sock"; }
{{- end }}
{{range $name, $svc := .IntSvcs -}}
{{range $name, $svc := .IntSvcs -}}
{{range $addr := $svc.Addresses -}}
{{range $addr := $svc.Addresses -}}
...
@@ -109,6 +112,12 @@ backend {{backendName $svc $addr}} {
...
@@ -109,6 +112,12 @@ backend {{backendName $svc $addr}} {
{{- if .Probe}}
{{- if .Probe}}
.probe = {{probeName $name}};
.probe = {{probeName $name}};
{{- end}}
{{- end}}
{{- if .Via}}
.via = vk8s_via;
{{- end}}
{{- if .Authority}}
.authority = "{{.Authority}}";
{{- end}}
{{- end}}
{{- end}}
}
}
{{end -}}
{{end -}}
...
@@ -257,11 +266,26 @@ func hostRegex(host string) string {
...
@@ -257,11 +266,26 @@ func hostRegex(host string) string {
return
`\Q`
+
host
+
`\E`
return
`\Q`
+
host
+
`\E`
}
}
func
needsVia
(
intSvcs
,
extSvcs
map
[
string
]
Service
)
bool
{
for
_
,
svc
:=
range
intSvcs
{
if
svc
.
Via
{
return
true
}
}
for
_
,
svc
:=
range
extSvcs
{
if
svc
.
Via
{
return
true
}
}
return
false
}
var
vclFuncs
=
template
.
FuncMap
{
var
vclFuncs
=
template
.
FuncMap
{
"plusOne"
:
func
(
i
int
)
int
{
return
i
+
1
},
"plusOne"
:
func
(
i
int
)
int
{
return
i
+
1
},
"dirType"
:
func
(
svc
Service
)
string
{
return
dirType
(
svc
)
},
"dirType"
:
func
(
svc
Service
)
string
{
return
dirType
(
svc
)
},
"hostRegex"
:
func
(
host
string
)
string
{
return
hostRegex
(
host
)
},
"hostRegex"
:
func
(
host
string
)
string
{
return
hostRegex
(
host
)
},
"backendName"
:
backendName
,
"backendName"
:
backendName
,
"needsVia"
:
needsVia
,
"probeName"
:
func
(
name
string
)
string
{
"probeName"
:
func
(
name
string
)
string
{
return
mangle
(
name
+
"_probe"
)
return
mangle
(
name
+
"_probe"
)
},
},
...
...
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