Commit 8c70952e authored by Geoff Simmons's avatar Geoff Simmons

Consistently use the vk8s prefix in generated VCL.

Also shorten some names that predictably include '.' -- Service
backends and Host strings.
parent 5b026b5b
......@@ -3,11 +3,11 @@ import re2;
sub vcl_init {
{{- range $auth := .Auths}}
new vk8s_{{vclMangle .Realm}}_auth = re2.set(anchor=both);
new {{vclMangle .Realm}}_auth = re2.set(anchor=both);
{{- range $cred := .Credentials}}
vk8s_{{vclMangle $auth.Realm}}_auth.add("\s*Basic\s+\Q{{$cred}}\E\s*");
{{vclMangle $auth.Realm}}_auth.add("\s*Basic\s+\Q{{$cred}}\E\s*");
{{- end}}
vk8s_{{vclMangle .Realm}}_auth.compile();
{{vclMangle .Realm}}_auth.compile();
{{end -}}
}
......@@ -21,9 +21,9 @@ sub vcl_recv {
req.url ~ "{{.Condition.URLRegex}}" &&
{{- end}}
{{- if eq .Status 401}}
!vk8s_{{vclMangle .Realm}}_auth.match(req.http.Authorization)
!{{vclMangle .Realm}}_auth.match(req.http.Authorization)
{{- else}}
!vk8s_{{vclMangle .Realm}}_auth.match(req.http.Proxy-Authorization)
!{{vclMangle .Realm}}_auth.match(req.http.Proxy-Authorization)
{{- end}}
) {
{{- if .UTF8 }}
......
......@@ -54,7 +54,7 @@ var fMap = template.FuncMap{
return urlMatcher(rule)
},
"aclName": func(name string) string {
return "vk8s_" + mangle(name) + "_acl"
return mangle(name) + "_acl"
},
}
......@@ -70,9 +70,7 @@ var (
shardTmpl *template.Template
authTmpl *template.Template
aclTmpl *template.Template
symPattern = regexp.MustCompile("^[[:alpha:]][[:word:]-]*$")
first = regexp.MustCompile("[[:alpha:]]")
restIllegal = regexp.MustCompile("[^[:word:]-]+")
vclIllegal = regexp.MustCompile("[^[:word:]-]+")
)
// InitTemplates initializes templates for VCL generation.
......@@ -140,22 +138,17 @@ func (spec Spec) GetSrc() (string, error) {
}
func mangle(s string) string {
var mangled string
bytes := []byte(s)
if s == "" || symPattern.Match(bytes) {
if s == "" {
return s
}
mangled = string(bytes[0])
if !first.Match(bytes[0:1]) {
mangled = "V" + mangled
}
rest := restIllegal.ReplaceAllFunc(bytes[1:], replIllegal)
mangled = mangled + string(rest)
return mangled
prefixed := "vk8s_" + s
bytes := []byte(prefixed)
mangled := vclIllegal.ReplaceAllFunc(bytes, replIllegal)
return string(mangled)
}
func backendName(svc Service, addr string) string {
return mangle(svc.Name + "_" + addr)
return mangle(svc.Name + "_" + strings.Replace(addr, ".", "_", -1))
}
func directorName(svc Service) string {
......@@ -163,7 +156,7 @@ func directorName(svc Service) string {
}
func urlMatcher(rule Rule) string {
return mangle(rule.Host + "_url")
return mangle(strings.Replace(rule.Host, ".", "_", -1) + "_url")
}
func aclMask(bits uint8) string {
......
......@@ -4,84 +4,84 @@ import std;
import directors;
import re2;
backend notfound {
backend vk8s_notfound {
# 192.0.2.0/24 reserved for docs & examples (RFC5737).
.host = "192.0.2.255";
.port = "80";
}
backend coffee-svc_192_2e_0_2e_2_2e_4 {
backend vk8s_coffee-svc_192_0_2_4 {
.host = "192.0.2.4";
.port = "80";
}
backend coffee-svc_192_2e_0_2e_2_2e_5 {
backend vk8s_coffee-svc_192_0_2_5 {
.host = "192.0.2.5";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_1 {
backend vk8s_tea-svc_192_0_2_1 {
.host = "192.0.2.1";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_2 {
backend vk8s_tea-svc_192_0_2_2 {
.host = "192.0.2.2";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_3 {
backend vk8s_tea-svc_192_0_2_3 {
.host = "192.0.2.3";
.port = "80";
}
sub vcl_init {
new hosts = re2.set(posix_syntax=true, literal=true, anchor=both);
hosts.add("cafe.example.com");
hosts.compile();
new coffee-svc_director = directors.round_robin();
coffee-svc_director.add_backend(coffee-svc_192_2e_0_2e_2_2e_4);
coffee-svc_director.add_backend(coffee-svc_192_2e_0_2e_2_2e_5);
new tea-svc_director = directors.round_robin();
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_1);
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_2);
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_3);
new cafe_2e_example_2e_com_url = re2.set(posix_syntax=true, anchor=start);
cafe_2e_example_2e_com_url.add("/coffee",
backend=coffee-svc_director.backend());
cafe_2e_example_2e_com_url.add("/tea",
backend=tea-svc_director.backend());
cafe_2e_example_2e_com_url.compile();
}
sub set_backend {
set req.backend_hint = notfound;
if (hosts.match(req.http.Host)) {
if (hosts.nmatches() != 1) {
new vk8s_hosts = re2.set(posix_syntax=true, literal=true, anchor=both);
vk8s_hosts.add("cafe.example.com");
vk8s_hosts.compile();
new vk8s_coffee-svc_director = directors.round_robin();
vk8s_coffee-svc_director.add_backend(vk8s_coffee-svc_192_0_2_4);
vk8s_coffee-svc_director.add_backend(vk8s_coffee-svc_192_0_2_5);
new vk8s_tea-svc_director = directors.round_robin();
vk8s_tea-svc_director.add_backend(vk8s_tea-svc_192_0_2_1);
vk8s_tea-svc_director.add_backend(vk8s_tea-svc_192_0_2_2);
vk8s_tea-svc_director.add_backend(vk8s_tea-svc_192_0_2_3);
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 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 (hosts.which() == 1) {
if (cafe_2e_example_2e_com_url.match(req.url)) {
set req.backend_hint = cafe_2e_example_2e_com_url.backend(select=FIRST);
elsif (vk8s_hosts.which() == 1) {
if (vk8s_cafe_example_com_url.match(req.url)) {
set req.backend_hint = vk8s_cafe_example_com_url.backend(select=FIRST);
}
}
}
if (req.backend_hint == notfound) {
if (req.backend_hint == vk8s_notfound) {
return (synth(404));
}
}
sub vcl_miss {
call set_backend;
call vk8s_set_backend;
}
sub vcl_pass {
call set_backend;
call vk8s_set_backend;
}
import std;
......
......@@ -4,82 +4,82 @@ import std;
import directors;
import re2;
backend notfound {
backend vk8s_notfound {
# 192.0.2.0/24 reserved for docs & examples (RFC5737).
.host = "192.0.2.255";
.port = "80";
}
backend coffee-svc_192_2e_0_2e_2_2e_4 {
backend vk8s_coffee-svc_192_0_2_4 {
.host = "192.0.2.4";
.port = "80";
}
backend coffee-svc_192_2e_0_2e_2_2e_5 {
backend vk8s_coffee-svc_192_0_2_5 {
.host = "192.0.2.5";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_1 {
backend vk8s_tea-svc_192_0_2_1 {
.host = "192.0.2.1";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_2 {
backend vk8s_tea-svc_192_0_2_2 {
.host = "192.0.2.2";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_3 {
backend vk8s_tea-svc_192_0_2_3 {
.host = "192.0.2.3";
.port = "80";
}
sub vcl_init {
new hosts = re2.set(posix_syntax=true, literal=true, anchor=both);
hosts.add("cafe.example.com");
hosts.compile();
new vk8s_hosts = re2.set(posix_syntax=true, literal=true, anchor=both);
vk8s_hosts.add("cafe.example.com");
vk8s_hosts.compile();
new coffee-svc_director = directors.round_robin();
coffee-svc_director.add_backend(coffee-svc_192_2e_0_2e_2_2e_4);
coffee-svc_director.add_backend(coffee-svc_192_2e_0_2e_2_2e_5);
new vk8s_coffee-svc_director = directors.round_robin();
vk8s_coffee-svc_director.add_backend(vk8s_coffee-svc_192_0_2_4);
vk8s_coffee-svc_director.add_backend(vk8s_coffee-svc_192_0_2_5);
new tea-svc_director = directors.round_robin();
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_1);
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_2);
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_3);
new vk8s_tea-svc_director = directors.round_robin();
vk8s_tea-svc_director.add_backend(vk8s_tea-svc_192_0_2_1);
vk8s_tea-svc_director.add_backend(vk8s_tea-svc_192_0_2_2);
vk8s_tea-svc_director.add_backend(vk8s_tea-svc_192_0_2_3);
new cafe_2e_example_2e_com_url = re2.set(posix_syntax=true, anchor=start);
cafe_2e_example_2e_com_url.add("/coffee",
backend=coffee-svc_director.backend());
cafe_2e_example_2e_com_url.add("/tea",
backend=tea-svc_director.backend());
cafe_2e_example_2e_com_url.compile();
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 set_backend {
set req.backend_hint = notfound;
if (hosts.match(req.http.Host)) {
if (hosts.nmatches() != 1) {
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 (hosts.which() == 1) {
if (cafe_2e_example_2e_com_url.match(req.url)) {
set req.backend_hint = cafe_2e_example_2e_com_url.backend(select=FIRST);
elsif (vk8s_hosts.which() == 1) {
if (vk8s_cafe_example_com_url.match(req.url)) {
set req.backend_hint = vk8s_cafe_example_com_url.backend(select=FIRST);
}
}
}
if (req.backend_hint == notfound) {
if (req.backend_hint == vk8s_notfound) {
return (synth(404));
}
}
sub vcl_miss {
call set_backend;
call vk8s_set_backend;
}
sub vcl_pass {
call set_backend;
call vk8s_set_backend;
}
......@@ -4,7 +4,7 @@ import std;
import directors;
import re2;
backend notfound {
backend vk8s_notfound {
# 192.0.2.0/24 reserved for docs & examples (RFC5737).
.host = "192.0.2.255";
.port = "80";
......@@ -21,11 +21,11 @@ backend {{backendName $svc $addr.IP}} {
sub vcl_init {
{{- if .Rules}}
new hosts = re2.set(posix_syntax=true, literal=true, anchor=both);
new vk8s_hosts = re2.set(posix_syntax=true, literal=true, anchor=both);
{{- range $rule := .Rules}}
hosts.add("{{$rule.Host}}");
vk8s_hosts.add("{{$rule.Host}}");
{{- end}}
hosts.compile();
vk8s_hosts.compile();
{{end}}
{{- range $name, $svc := .AllServices}}
......@@ -44,11 +44,11 @@ sub vcl_init {
{{end -}}
}
sub set_backend {
set req.backend_hint = notfound;
sub vk8s_set_backend {
set req.backend_hint = vk8s_notfound;
{{- if .Rules}}
if (hosts.match(req.http.Host)) {
if (hosts.nmatches() != 1) {
if (vk8s_hosts.match(req.http.Host)) {
if (vk8s_hosts.nmatches() != 1) {
# Fail fast when the match was not unique.
return (fail);
}
......@@ -56,7 +56,7 @@ sub set_backend {
#
}
{{- range $i, $rule := .Rules}}
elsif (hosts.which() == {{plusOne $i}}) {
elsif (vk8s_hosts.which() == {{plusOne $i}}) {
if ({{urlMatcher $rule}}.match(req.url)) {
set req.backend_hint = {{urlMatcher $rule}}.backend(select=FIRST);
}
......@@ -65,7 +65,7 @@ sub set_backend {
}
{{- end}}
if (req.backend_hint == notfound) {
if (req.backend_hint == vk8s_notfound) {
{{- if ne .DefaultService.Name ""}}
set req.backend_hint = {{dirName .DefaultService}}.backend();
{{- else}}
......@@ -75,9 +75,9 @@ sub set_backend {
}
sub vcl_miss {
call set_backend;
call vk8s_set_backend;
}
sub vcl_pass {
call set_backend;
call vk8s_set_backend;
}
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