Commit 08f6e38d authored by Geoff Simmons's avatar Geoff Simmons

Fix generation of probes for backends from ExternalName Services.

Ref gitlab issue #20
parent bd43dbbd
......@@ -96,3 +96,89 @@ func TestExternalNameSvc(t *testing.T) {
}
}
}
var extTequilaSvc = Service{
Name: "tequila-svc",
ExternalName: "tequila.example.com",
ExternalPort: "88",
Probe: &Probe{
URL: "/shot/",
ExpResponse: 418,
Timeout: "5s",
Interval: "10s",
Initial: "1",
Window: "4",
Threshold: "3",
},
HostHeader: "tequila.example.org",
ConnectTimeout: "1s",
FirstByteTimeout: "2s",
BetweenBytesTimeout: "3s",
MaxConnections: 100,
ProxyHeader: 2,
}
var extMetaxaSvc = Service{
Name: "metaxa-svc",
ExternalName: "metaxa.example.com",
ExternalPort: "8080",
Probe: &Probe{
Request: []string{
"GET /the-worm/ HTTP/1.1",
"Host: mextaxa.example.org",
"Connection: close",
},
Timeout: "10s",
Interval: "20s",
Initial: "2",
Window: "5",
Threshold: "3",
},
HostHeader: "metaxa.example.org",
ConnectTimeout: "2s",
FirstByteTimeout: "3s",
BetweenBytesTimeout: "4s",
MaxConnections: 200,
ProxyHeader: 1,
}
var boozeSpec = Spec{
DefaultService: Service{},
Rules: []Rule{
{
Host: "tequila.example.edu",
PathMap: map[string]Service{
"/": extTequilaSvc,
},
},
{
Host: "metaxa.example.edu",
PathMap: map[string]Service{
"/": extMetaxaSvc,
},
},
},
ExtSvcs: map[string]Service{
"tequila-svc": extTequilaSvc,
"metaxa-svc": extMetaxaSvc,
},
}
func TestExternalNameBcfg(t *testing.T) {
var buf bytes.Buffer
gold := "extname_bcfg.golden"
if err := ingressTmpl.Execute(&buf, boozeSpec); err != nil {
t.Fatal("Execute():", err)
}
ok, err := cmpGold(buf.Bytes(), gold)
if err != nil {
t.Fatalf("Reading %s: %v", gold, err)
}
if !ok {
t.Errorf("Generated VCL for IngressSpec does not match gold "+
"file: %s", gold)
if testing.Verbose() {
t.Logf("Generated: %s", buf.String())
}
}
}
vcl 4.1;
import std;
import directors;
import re2;
import dynamic;
backend vk8s_notfound {
# 192.0.2.0/24 reserved for docs & examples (RFC5737).
.host = "192.0.2.255";
.port = "80";
}
probe vk8s_metaxa-svc_probe {
.request =
"GET /the-worm/ HTTP/1.1"
"Host: mextaxa.example.org"
"Connection: close"
;
.timeout = 10s;
.interval = 20s;
.initial = 2;
.window = 5;
.threshold = 3;
}
probe vk8s_tequila-svc_probe {
.url = "/shot/";
.expected_response = 418;
.timeout = 5s;
.interval = 10s;
.initial = 1;
.window = 4;
.threshold = 3;
}
sub vcl_init {
new vk8s_hosts = re2.set(anchor=both);
vk8s_hosts.add("\Qtequila.example.edu\E(:\d+)?");
vk8s_hosts.add("\Qmetaxa.example.edu\E(:\d+)?");
vk8s_hosts.compile();
new vk8s_resolver = dynamic.resolver();
vk8s_resolver.set_resolution_type(STUB);
new vk8s_metaxa-svc_director = dynamic.director(
ttl_from = dns,
ttl = 30s,
resolver = vk8s_resolver.use()
, port = "8080"
, host_header = "metaxa.example.org"
, connect_timeout = 2s
, first_byte_timeout = 3s
, between_bytes_timeout = 4s
, proxy_header = 1
, max_connections = 200
, probe = vk8s_metaxa-svc_probe
);
new vk8s_tequila-svc_director = dynamic.director(
ttl_from = dns,
ttl = 30s,
resolver = vk8s_resolver.use()
, port = "88"
, host_header = "tequila.example.org"
, connect_timeout = 1s
, first_byte_timeout = 2s
, between_bytes_timeout = 3s
, proxy_header = 2
, max_connections = 100
, probe = vk8s_tequila-svc_probe
);
new vk8s_tequila_example_edu_url = re2.set(posix_syntax=true, anchor=start);
vk8s_tequila_example_edu_url.add("/",
backend=vk8s_tequila-svc_director.backend("tequila.example.com"));
vk8s_tequila_example_edu_url.compile();
new vk8s_metaxa_example_edu_url = re2.set(posix_syntax=true, anchor=start);
vk8s_metaxa_example_edu_url.add("/",
backend=vk8s_metaxa-svc_director.backend("metaxa.example.com"));
vk8s_metaxa_example_edu_url.compile();
}
sub vk8s_set_backend {
set req.backend_hint = vk8s_notfound;
if (vk8s_hosts.match(req.http.Host)) {
if (vk8s_hosts.nmatches() != 1) {
# Fail fast when the match was not unique.
return (fail);
}
if (0 != 0) {
#
}
elsif (vk8s_hosts.which() == 1) {
if (vk8s_tequila_example_edu_url.match(req.url)) {
set req.backend_hint = vk8s_tequila_example_edu_url.backend(select=FIRST);
}
}
elsif (vk8s_hosts.which() == 2) {
if (vk8s_metaxa_example_edu_url.match(req.url)) {
set req.backend_hint = vk8s_metaxa_example_edu_url.backend(select=FIRST);
}
}
}
if (req.backend_hint == vk8s_notfound) {
return (synth(404));
}
}
sub vcl_miss {
call vk8s_set_backend;
}
sub vcl_pass {
call vk8s_set_backend;
}
sub vcl_pipe {
call vk8s_set_backend;
}
sub vcl_hit {
if (obj.ttl < 0s) {
# Set a backend for a background fetch.
call vk8s_set_backend;
}
}
......@@ -11,7 +11,8 @@ backend vk8s_notfound {
.port = "80";
}
{{- range $name, $svc := .IntSvcs}}
{{- define "ProbeDef"}}
{{- range $name, $svc := .}}
{{- if $svc.Probe}}
{{with $svc.Probe}}
probe {{probeName $name}} {
......@@ -46,6 +47,9 @@ probe {{probeName $name}} {
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- template "ProbeDef" .IntSvcs -}}
{{- template "ProbeDef" .ExtSvcs}}
{{range $name, $svc := .IntSvcs -}}
{{range $addr := $svc.Addresses -}}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment