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