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
86b2d1aa
Commit
86b2d1aa
authored
Apr 02, 2019
by
Geoff Simmons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DRY -- use only one CompareType in VCL spec types.
parent
bfff5b5d
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
176 additions
and
250 deletions
+176
-250
ingress.go
pkg/controller/ingress.go
+38
-57
ingress_test.go
pkg/controller/ingress_test.go
+11
-11
acl.tmpl
pkg/varnish/vcl/acl.tmpl
+1
-1
auth.tmpl
pkg/varnish/vcl/auth.tmpl
+2
-2
recv_disposition.tmpl
pkg/varnish/vcl/recv_disposition.tmpl
+7
-7
recv_disposition_test.go
pkg/varnish/vcl/recv_disposition_test.go
+11
-11
rewrite.tmpl
pkg/varnish/vcl/rewrite.tmpl
+3
-3
rewrite_test.go
pkg/varnish/vcl/rewrite_test.go
+30
-25
spec.go
pkg/varnish/vcl/spec.go
+37
-53
template.go
pkg/varnish/vcl/template.go
+32
-78
vcl_test.go
pkg/varnish/vcl/vcl_test.go
+4
-2
No files found.
pkg/controller/ingress.go
View file @
86b2d1aa
...
...
@@ -454,6 +454,37 @@ func (worker *NamespaceWorker) configSharding(spec *vcl.Spec,
return
nil
}
func
configComparison
(
cmp
vcr_v1alpha1
.
CompareType
)
(
vcl
.
CompareType
,
bool
)
{
switch
cmp
{
case
vcr_v1alpha1
.
Equal
:
return
vcl
.
Equal
,
false
case
vcr_v1alpha1
.
NotEqual
:
return
vcl
.
Equal
,
true
case
vcr_v1alpha1
.
Match
:
return
vcl
.
Match
,
false
case
vcr_v1alpha1
.
NotMatch
:
return
vcl
.
Match
,
true
case
vcr_v1alpha1
.
Prefix
:
return
vcl
.
Prefix
,
false
case
vcr_v1alpha1
.
NotPrefix
:
return
vcl
.
Prefix
,
true
case
vcr_v1alpha1
.
Exists
:
return
vcl
.
Exists
,
false
case
vcr_v1alpha1
.
NotExists
:
return
vcl
.
Exists
,
true
case
vcr_v1alpha1
.
Greater
:
return
vcl
.
Greater
,
false
case
vcr_v1alpha1
.
GreaterEqual
:
return
vcl
.
GreaterEqual
,
false
case
vcr_v1alpha1
.
Less
:
return
vcl
.
Less
,
false
case
vcr_v1alpha1
.
LessEqual
:
return
vcl
.
LessEqual
,
false
default
:
return
vcl
.
Equal
,
false
}
}
func
configConditions
(
vclConds
[]
vcl
.
MatchTerm
,
vcfgConds
[]
vcr_v1alpha1
.
Condition
)
{
...
...
@@ -465,18 +496,8 @@ func configConditions(vclConds []vcl.MatchTerm,
Comparand
:
cond
.
Comparand
,
Value
:
cond
.
Value
,
}
switch
cond
.
Compare
{
case
vcr_v1alpha1
.
Equal
:
vclMatch
.
Compare
=
vcl
.
Equal
case
vcr_v1alpha1
.
NotEqual
:
vclMatch
.
Compare
=
vcl
.
NotEqual
case
vcr_v1alpha1
.
Match
:
vclMatch
.
Compare
=
vcl
.
Match
case
vcr_v1alpha1
.
NotMatch
:
vclMatch
.
Compare
=
vcl
.
NotMatch
default
:
vclMatch
.
Compare
=
vcl
.
Equal
}
vclMatch
.
Compare
,
vclMatch
.
Negate
=
configComparison
(
cond
.
Compare
)
vclConds
[
i
]
=
vclMatch
}
}
...
...
@@ -688,16 +709,10 @@ func (worker *NamespaceWorker) configRewrites(spec *vcl.Spec,
return
fmt
.
Errorf
(
"Illegal method %s"
,
rw
.
Method
)
}
switch
rw
.
Compare
{
case
vcr_v1alpha1
.
Match
:
vclRw
.
Compare
=
vcl
.
RewriteMatch
case
vcr_v1alpha1
.
Equal
:
vclRw
.
Compare
=
vcl
.
RewriteEqual
case
vcr_v1alpha1
.
Prefix
:
vclRw
.
Compare
=
vcl
.
Prefix
default
:
vclRw
.
Compare
=
vcl
.
RewriteMatch
if
rw
.
Compare
==
""
{
rw
.
Compare
=
vcr_v1alpha1
.
Match
}
vclRw
.
Compare
,
vclRw
.
Negate
=
configComparison
(
rw
.
Compare
)
switch
rw
.
VCLSub
{
case
vcr_v1alpha1
.
Recv
:
...
...
@@ -783,42 +798,8 @@ func (worker *NamespaceWorker) configReqDisps(spec *vcl.Spec,
count
:=
uint
(
*
cond
.
Count
)
vclCond
.
Count
=
&
count
}
switch
cond
.
Compare
{
case
vcr_v1alpha1
.
Equal
:
vclCond
.
Compare
=
vcl
.
ReqEqual
vclCond
.
Negate
=
false
case
vcr_v1alpha1
.
NotEqual
:
vclCond
.
Compare
=
vcl
.
ReqEqual
vclCond
.
Negate
=
true
case
vcr_v1alpha1
.
Match
:
vclCond
.
Compare
=
vcl
.
ReqMatch
vclCond
.
Negate
=
false
case
vcr_v1alpha1
.
NotMatch
:
vclCond
.
Compare
=
vcl
.
ReqMatch
vclCond
.
Negate
=
true
case
vcr_v1alpha1
.
Prefix
:
vclCond
.
Compare
=
vcl
.
ReqPrefix
vclCond
.
Negate
=
false
case
vcr_v1alpha1
.
NotPrefix
:
vclCond
.
Compare
=
vcl
.
ReqPrefix
vclCond
.
Negate
=
true
case
vcr_v1alpha1
.
Exists
:
vclCond
.
Compare
=
vcl
.
Exists
vclCond
.
Negate
=
false
case
vcr_v1alpha1
.
NotExists
:
vclCond
.
Compare
=
vcl
.
Exists
vclCond
.
Negate
=
true
case
vcr_v1alpha1
.
Greater
:
vclCond
.
Compare
=
vcl
.
Greater
case
vcr_v1alpha1
.
GreaterEqual
:
vclCond
.
Compare
=
vcl
.
GreaterEqual
case
vcr_v1alpha1
.
Less
:
vclCond
.
Compare
=
vcl
.
Less
case
vcr_v1alpha1
.
LessEqual
:
vclCond
.
Compare
=
vcl
.
LessEqual
default
:
vclCond
.
Compare
=
vcl
.
ReqEqual
}
vclCond
.
Compare
,
vclCond
.
Negate
=
configComparison
(
cond
.
Compare
)
if
cond
.
MatchFlags
!=
nil
{
vclCond
.
MatchFlags
=
configMatchFlags
(
*
cond
.
MatchFlags
)
...
...
pkg/controller/ingress_test.go
View file @
86b2d1aa
...
...
@@ -315,7 +315,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.method"
,
Compare
:
vcl
.
Req
Equal
,
Compare
:
vcl
.
Equal
,
Values
:
[]
string
{
"PRI"
},
MatchFlags
:
vcl
.
MatchFlagsType
{
CaseSensitive
:
true
,
...
...
@@ -339,7 +339,7 @@ var reqDispHarness = []struct {
},
vcl
.
Condition
{
Comparand
:
"req.esi_level"
,
Compare
:
vcl
.
Req
Equal
,
Compare
:
vcl
.
Equal
,
Count
:
&
uintZero
,
MatchFlags
:
vcl
.
MatchFlagsType
{
CaseSensitive
:
true
,
...
...
@@ -347,7 +347,7 @@ var reqDispHarness = []struct {
},
vcl
.
Condition
{
Comparand
:
"req.proto"
,
Compare
:
vcl
.
Req
Prefix
,
Compare
:
vcl
.
Prefix
,
Values
:
[]
string
{
"HTTP/1.1"
},
},
},
...
...
@@ -360,7 +360,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.method"
,
Compare
:
vcl
.
Req
Equal
,
Compare
:
vcl
.
Equal
,
Negate
:
true
,
Values
:
[]
string
{
"GET"
,
...
...
@@ -385,7 +385,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.method"
,
Compare
:
vcl
.
Req
Equal
,
Compare
:
vcl
.
Equal
,
Negate
:
true
,
Values
:
[]
string
{
"GET"
,
...
...
@@ -471,7 +471,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.method"
,
Compare
:
vcl
.
Req
Equal
,
Compare
:
vcl
.
Equal
,
Values
:
[]
string
{
"CONNECT"
},
MatchFlags
:
vcl
.
MatchFlagsType
{
CaseSensitive
:
true
,
...
...
@@ -486,7 +486,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.method"
,
Compare
:
vcl
.
Req
Equal
,
Compare
:
vcl
.
Equal
,
Negate
:
true
,
Values
:
[]
string
{
"GET"
,
...
...
@@ -551,7 +551,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.url"
,
Compare
:
vcl
.
Req
Match
,
Compare
:
vcl
.
Match
,
Values
:
[]
string
{
`\.png$`
,
`\.jpe?g$`
,
...
...
@@ -571,7 +571,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.url"
,
Compare
:
vcl
.
Req
Prefix
,
Compare
:
vcl
.
Prefix
,
Values
:
[]
string
{
"/interactive/"
,
"/basket/"
,
...
...
@@ -608,7 +608,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.method"
,
Compare
:
vcl
.
Req
Equal
,
Compare
:
vcl
.
Equal
,
Values
:
[]
string
{
"PURGE"
},
MatchFlags
:
vcl
.
MatchFlagsType
{
CaseSensitive
:
true
,
...
...
@@ -646,7 +646,7 @@ var reqDispHarness = []struct {
Conditions
:
[]
vcl
.
Condition
{
vcl
.
Condition
{
Comparand
:
"req.url"
,
Compare
:
vcl
.
Req
Prefix
,
Compare
:
vcl
.
Prefix
,
Negate
:
true
,
Values
:
[]
string
{
"/foo/"
,
...
...
pkg/varnish/vcl/acl.tmpl
View file @
86b2d1aa
...
...
@@ -16,7 +16,7 @@ sub vcl_recv {
{{- range .ACLs}}
if (
{{- range $cond := .Conditions}}
{{$cond.Comparand}} {{cmpRelation .Compare}} "{{.Value}}" &&
{{$cond.Comparand}} {{cmpRelation .Compare
.Negate
}} "{{.Value}}" &&
{{- end}}
{{aclCmp .Comparand}} {{if .Whitelist}}!{{end}}~ {{aclName .Name}}
) {
...
...
pkg/varnish/vcl/auth.tmpl
View file @
86b2d1aa
...
...
@@ -14,8 +14,8 @@ sub vcl_init {
sub vcl_recv {
{{- range .Auths}}
if (
{{- range
$cond :=
.Conditions}}
{{
$cond.Comparand}} {{cmpRelation .Compar
e}} "{{.Value}}" &&
{{- range .Conditions}}
{{
.Comparand}} {{cmpRelation .Compare .Negat
e}} "{{.Value}}" &&
{{- end}}
{{- if eq .Status 401}}
!{{credsMatcher .Realm}}.match(req.http.Authorization)
...
...
pkg/varnish/vcl/recv_disposition.tmpl
View file @
86b2d1aa
...
...
@@ -6,11 +6,11 @@ import selector;
{{range $cidx, $c := .Conditions -}}
{{if reqNeedsMatcher $c -}}
sub vcl_init {
new {{reqObj $didx $cidx}} = {{
reqVMOD $c
}}.set({{reqFlags $c}});
new {{reqObj $didx $cidx}} = {{
vmod $c.Compare
}}.set({{reqFlags $c}});
{{- range $val := $c.Values}}
{{reqObj $didx $cidx}}.add("{{$val}}");
{{- end}}
{{- if
reqNeedsCompile $c
}}
{{- if
needsCompile $c.Compare
}}
{{reqObj $didx $cidx}}.compile();
{{- end}}
}
...
...
@@ -24,13 +24,13 @@ sub vcl_recv {
{{- range $cidx, $cond := .Conditions}}
{{- if ne $cidx 0}} &&
{{end}}
{{- if
$cond
.Negate}}! {{end}}
{{- if .Negate}}! {{end}}
{{- if reqNeedsMatcher $cond}}
{{- reqObj $didx $cidx}}.{{
reqMatch $cond}}({{$cond
.Comparand}})
{{- else if exists
$cond
.Compare}}
{{-
$cond
.Comparand}}
{{- reqObj $didx $cidx}}.{{
match .Compare}}({{
.Comparand}})
{{- else if exists .Compare}}
{{- .Comparand}}
{{- else}}
{{-
$cond.Comparand}} {{reqCmpRelation $cond
}} {{value $cond}}
{{-
.Comparand}} {{cmpRelation .Compare .Negate
}} {{value $cond}}
{{- end}}
{{- end -}}
) {
...
...
pkg/varnish/vcl/recv_disposition_test.go
View file @
86b2d1aa
...
...
@@ -37,7 +37,7 @@ var builtinRecvSpec = Spec{
{
Conditions
:
[]
Condition
{{
Comparand
:
"req.method"
,
Compare
:
Req
Equal
,
Compare
:
Equal
,
Values
:
[]
string
{
"PRI"
},
}},
Disposition
:
DispositionType
{
...
...
@@ -54,12 +54,12 @@ var builtinRecvSpec = Spec{
},
{
Comparand
:
"req.esi_level"
,
Compare
:
Req
Equal
,
Compare
:
Equal
,
Count
:
&
zero
,
},
{
Comparand
:
"req.proto"
,
Compare
:
Req
Prefix
,
Compare
:
Prefix
,
Values
:
[]
string
{
"HTTP/1.1"
},
MatchFlags
:
MatchFlagsType
{
CaseSensitive
:
false
,
...
...
@@ -74,7 +74,7 @@ var builtinRecvSpec = Spec{
{
Conditions
:
[]
Condition
{{
Comparand
:
"req.method"
,
Compare
:
Req
Equal
,
Compare
:
Equal
,
Negate
:
true
,
Values
:
[]
string
{
"GET"
,
...
...
@@ -96,7 +96,7 @@ var builtinRecvSpec = Spec{
{
Conditions
:
[]
Condition
{{
Comparand
:
"req.method"
,
Compare
:
Req
Equal
,
Compare
:
Equal
,
Negate
:
true
,
Values
:
[]
string
{
"GET"
,
...
...
@@ -140,7 +140,7 @@ var pipeOnConnectSpec = Spec{
Dispositions
:
[]
DispositionSpec
{{
Conditions
:
[]
Condition
{{
Comparand
:
"req.method"
,
Compare
:
Req
Equal
,
Compare
:
Equal
,
Values
:
[]
string
{
"CONNECT"
},
}},
Disposition
:
DispositionType
{
...
...
@@ -158,7 +158,7 @@ var methodNotAllowedSpec = Spec{
Dispositions
:
[]
DispositionSpec
{{
Conditions
:
[]
Condition
{{
Comparand
:
"req.method"
,
Compare
:
Req
Equal
,
Compare
:
Equal
,
Negate
:
true
,
Values
:
[]
string
{
"GET"
,
...
...
@@ -189,7 +189,7 @@ var urlWhitelistSpec = Spec{
Dispositions
:
[]
DispositionSpec
{{
Conditions
:
[]
Condition
{{
Comparand
:
"req.url"
,
Compare
:
Req
Prefix
,
Compare
:
Prefix
,
Negate
:
true
,
Values
:
[]
string
{
"/foo"
,
...
...
@@ -217,7 +217,7 @@ var purgeMethodSpec = Spec{
Dispositions
:
[]
DispositionSpec
{{
Conditions
:
[]
Condition
{{
Comparand
:
"req.method"
,
Compare
:
Req
Equal
,
Compare
:
Equal
,
Values
:
[]
string
{
"PURGE"
},
}},
Disposition
:
DispositionType
{
...
...
@@ -235,7 +235,7 @@ var cacheableSpec = Spec{
Dispositions
:
[]
DispositionSpec
{{
Conditions
:
[]
Condition
{{
Comparand
:
"req.url"
,
Compare
:
Req
Match
,
Compare
:
Match
,
Values
:
[]
string
{
`\.png$`
,
`\.jpe?g$`
,
...
...
@@ -261,7 +261,7 @@ var nonCacheableSpec = Spec{
Dispositions
:
[]
DispositionSpec
{{
Conditions
:
[]
Condition
{{
Comparand
:
"req.url"
,
Compare
:
Req
Prefix
,
Compare
:
Prefix
,
Values
:
[]
string
{
"/interactive/"
,
"/basket/"
,
...
...
pkg/varnish/vcl/rewrite.tmpl
View file @
86b2d1aa
...
...
@@ -5,7 +5,7 @@ import selector;
{{range $i, $r := .Rewrites -}}
{{if needsMatcher $r -}}
sub vcl_init {
new {{rewrName $i}} = {{
rewrVMOD $r
}}.set({{rewrFlags $r}});
new {{rewrName $i}} = {{
vmod $r.Compare
}}.set({{rewrFlags $r}});
{{- range $rule := $r.Rules}}
{{rewrName $i}}.add("{{$rule.Value}}", string="{{$rule.Rewrite}}"
{{- if needsSave $r}}, save=true{{end -}}
...
...
@@ -13,7 +13,7 @@ sub vcl_init {
{{- if needsNeverCapture $r}}, never_capture=true{{end -}}
);
{{- end}}
{{- if needsCompile $r}}
{{- if needsCompile $r
.Compare
}}
{{rewrName $i}}.compile();
{{- end}}
}
...
...
@@ -21,7 +21,7 @@ sub vcl_init {
sub vcl_{{rewrSub $r}} {
{{- if needsMatcher $r}}
if ({{rewrName $i}}.{{
rewrMatch $r
}}({{$r.Source}})) {
if ({{rewrName $i}}.{{
match $r.Compare
}}({{$r.Source}})) {
{{- if needsUniqueCheck $r}}
if ({{rewrName $i}}.nmatches() != 1) {
std.log({{$r.Source}} + " had " +
...
...
pkg/varnish/vcl/rewrite_test.go
View file @
86b2d1aa
...
...
@@ -92,7 +92,7 @@ var replaceFromRewriteTest = Spec{
Rewrites
:
[]
Rewrite
{{
Target
:
"req.http.Host"
,
Source
:
"req.http.Host"
,
Compare
:
Rewrite
Equal
,
Compare
:
Equal
,
Rules
:
[]
RewriteRule
{
{
Value
:
"cafe.example.com"
,
...
...
@@ -119,7 +119,7 @@ var rewriteSubTest = Spec{
Rewrites
:
[]
Rewrite
{{
Target
:
"req.url"
,
Source
:
"req.url"
,
Compare
:
Rewrite
Match
,
Compare
:
Match
,
Rules
:
[]
RewriteRule
{
{
Value
:
`/foo(/|$)`
,
...
...
@@ -202,7 +202,7 @@ var conditionalDeleteTest = Spec{
Target
:
"resp.http.Via"
,
Source
:
"req.http.Delete-Via"
,
Method
:
Delete
,
Compare
:
Rewrite
Equal
,
Compare
:
Equal
,
Rules
:
[]
RewriteRule
{
{
Value
:
"true"
},
{
Value
:
"yes"
},
...
...
@@ -225,6 +225,7 @@ var rewriteExtractTest = Spec{
Target
:
"req.url"
,
Source
:
"req.url"
,
Method
:
RewriteMethod
,
Compare
:
Match
,
Rules
:
[]
RewriteRule
{{
Value
:
`/([^/]+)/([^/]+)(.*)`
,
Rewrite
:
`/\2/\1\3`
,
...
...
@@ -272,7 +273,7 @@ var rewriteFixedEqualTest = Spec{
Rewrites
:
[]
Rewrite
{{
Target
:
"bereq.url"
,
Source
:
"bereq.url"
,
Compare
:
Rewrite
Equal
,
Compare
:
Equal
,
Method
:
Sub
,
Rules
:
[]
RewriteRule
{
{
...
...
@@ -326,7 +327,7 @@ var rewritePrefixRegex = Spec{
Rewrites
:
[]
Rewrite
{{
Target
:
"bereq.url"
,
Source
:
"bereq.url"
,
Compare
:
Rewrite
Match
,
Compare
:
Match
,
Method
:
Sub
,
Rules
:
[]
RewriteRule
{
{
...
...
@@ -355,7 +356,7 @@ var rewritePrependIfExists = Spec{
Rewrites
:
[]
Rewrite
{{
Target
:
"resp.http.X-Bazz"
,
Source
:
"req.http.X-Bazz"
,
Compare
:
Rewrite
Match
,
Compare
:
Match
,
Method
:
Prepend
,
Rules
:
[]
RewriteRule
{
{
...
...
@@ -378,7 +379,7 @@ var rewriteExtractCookie = Spec{
Rewrites
:
[]
Rewrite
{{
Target
:
"req.http.Session-Token"
,
Source
:
"req.http.Cookie"
,
Compare
:
Rewrite
Match
,
Compare
:
Match
,
Method
:
RewriteMethod
,
Rules
:
[]
RewriteRule
{{
Value
:
`\bmysession\s*=\s*([^,;[:space:]]+)`
,
...
...
@@ -464,6 +465,7 @@ var rewriteAppendRule = Spec{
Target
:
"resp.http.Append-Rule-Target"
,
Source
:
"req.http.Append-Rule-Src"
,
Method
:
Append
,
Compare
:
Match
,
Rules
:
[]
RewriteRule
{{
Value
:
`.`
,
Rewrite
:
`AppendString`
,
...
...
@@ -651,6 +653,7 @@ var rewriteSelectOperations = Spec{
{
Target
:
"resp.http.Hdr"
,
Source
:
"req.url"
,
Compare
:
Match
,
Select
:
First
,
Method
:
Sub
,
Rules
:
[]
RewriteRule
{{
...
...
@@ -664,6 +667,7 @@ var rewriteSelectOperations = Spec{
{
Target
:
"resp.http.Hdr"
,
Source
:
"req.url"
,
Compare
:
Match
,
Select
:
Last
,
Method
:
Suball
,
Rules
:
[]
RewriteRule
{{
...
...
@@ -677,6 +681,7 @@ var rewriteSelectOperations = Spec{
{
Target
:
"resp.http.Hdr"
,
Source
:
"req.url"
,
Compare
:
Match
,
Select
:
First
,
Method
:
RewriteMethod
,
Rules
:
[]
RewriteRule
{{
...
...
pkg/varnish/vcl/spec.go
View file @
86b2d1aa
...
...
@@ -315,19 +315,34 @@ func (a byACLAddr) Len() int { return len(a) }
func
(
a
byACLAddr
)
Swap
(
i
,
j
int
)
{
a
[
i
],
a
[
j
]
=
a
[
j
],
a
[
i
]
}
func
(
a
byACLAddr
)
Less
(
i
,
j
int
)
bool
{
return
a
[
i
]
.
Addr
<
a
[
j
]
.
Addr
}
// CompareType classifies comparisons for MatchTerm.
// CompareType classifies comparison operations performed for
// conditional configurations. The comparison may be negated if the
// boolean Negate field is true.
type
CompareType
uint8
const
(
// Equal means compare strings for equality (==).
// Equal specifies equality -- string equality, numeric
// equality, or membership in a set of fixed strings.
Equal
CompareType
=
iota
// NotEqual means compare with !=.
NotEqual
// Match means compare for regex match (~) -- the MatchTerm
// Value is a regular expression.
// Match specifies a regular expression match.
Match
// NotMatch means compare with !~.
NotMatch
// Prefix specifies that a string has a prefix in a set of
// fixed strings.
Prefix
// Exists specifies that a request header exists.
Exists
// Greater specifies the > relation between a VCL variable
// with a numeric value and a constant.
Greater
// GreaterEqual specifies the >= relation for a numeric VCL
// variable and a constant.
GreaterEqual
// Less specifies the < relation for a numeric VCL variable
// and a constant.
Less
// LessEqual specifies the <= relation for a numeric VCL
// variable and a constant.
LessEqual
)
// MatchTerm is a term describing the comparison of a VCL object with
...
...
@@ -336,12 +351,18 @@ type MatchTerm struct {
Comparand
string
Value
string
Compare
CompareType
Negate
bool
}
func
(
match
MatchTerm
)
hash
(
hash
hash
.
Hash
)
{
hash
.
Write
([]
byte
(
match
.
Comparand
))
hash
.
Write
([]
byte
(
match
.
Value
))
hash
.
Write
([]
byte
{
byte
(
match
.
Compare
)})
if
match
.
Negate
{
hash
.
Write
([]
byte
{
byte
(
1
)})
}
else
{
hash
.
Write
([]
byte
{
byte
(
0
)})
}
}
// interface for sorting []MatchTerm
...
...
@@ -446,19 +467,6 @@ const (
Delete
)
// RewriteCompare classifies the comparison operation used to evaluate
// the conditions for a rewrite.
type
RewriteCompare
uint8
const
(
// RewriteMatch means that a regex match is executed.
RewriteMatch
RewriteCompare
=
iota
// RewriteEqual means that fixed strings are tested for equality.
RewriteEqual
// Prefix indicates a fixed-string prefix match.
Prefix
)
// SubType classifies the VCL subroutine in which a rewrite is
// executed.
type
SubType
uint8
...
...
@@ -625,7 +633,8 @@ type Rewrite struct {
Target
string
Source
string
Method
MethodType
Compare
RewriteCompare
Compare
CompareType
Negate
bool
VCLSub
SubType
Select
SelectType
}
...
...
@@ -640,6 +649,11 @@ func (rw Rewrite) hash(hash hash.Hash) {
hash
.
Write
([]
byte
(
rw
.
Source
))
hash
.
Write
([]
byte
{
byte
(
rw
.
Method
)})
hash
.
Write
([]
byte
{
byte
(
rw
.
Compare
)})
if
rw
.
Negate
{
hash
.
Write
([]
byte
{
byte
(
1
)})
}
else
{
hash
.
Write
([]
byte
{
byte
(
0
)})
}
hash
.
Write
([]
byte
{
byte
(
rw
.
VCLSub
)})
hash
.
Write
([]
byte
{
byte
(
rw
.
Select
)})
}
...
...
@@ -651,36 +665,6 @@ func (a byVCLSub) Len() int { return len(a) }
func
(
a
byVCLSub
)
Swap
(
i
,
j
int
)
{
a
[
i
],
a
[
j
]
=
a
[
j
],
a
[
i
]
}
func
(
a
byVCLSub
)
Less
(
i
,
j
int
)
bool
{
return
a
[
i
]
.
VCLSub
<
a
[
j
]
.
VCLSub
}
// ReqCompareType classifies comparison operations performed for the
// Conditions in a request disposition specification. The relation may
// be negated, if the Negate field in Condition is true.
type
ReqCompareType
uint8
const
(
// ReqEqual specifies equality -- string equality, numeric
// equality, or membership in a set of fixed strings.
ReqEqual
ReqCompareType
=
iota
// ReqMatch specifies a regular expression match.
ReqMatch
// ReqPrefix specifies that a string has a prefix in a set of
// fixed strings.
ReqPrefix
// Exists specifies that a request header exists.
Exists
// Greater specifies the > relation between a VCL variable
// with a numeric value and a constant.
Greater
// GreaterEqual specifies the >= relation for a numeric VCL
// variable and a constant.
GreaterEqual
// Less specifies the < relation for a numeric VCL variable
// and a constant.
Less
// LessEqual specifies the <= relation for a numeric VCL
// variable and a constant.
LessEqual
)
// Condition specifies (one of) the conditions that must be true if a
// client request disposition is to be executed.
type
Condition
struct
{
...
...
@@ -688,7 +672,7 @@ type Condition struct {
MatchFlags
MatchFlagsType
Count
*
uint
Comparand
string
Compare
Req
CompareType
Compare
CompareType
Negate
bool
}
...
...
pkg/varnish/vcl/template.go
View file @
86b2d1aa
...
...
@@ -47,24 +47,22 @@ var fMap = template.FuncMap{
"dirType"
:
func
(
svc
Service
)
string
{
return
dirType
(
svc
)
},
"rewrName"
:
func
(
i
int
)
string
{
return
rewrName
(
i
)
},
"needsMatcher"
:
func
(
rewr
Rewrite
)
bool
{
return
needsMatcher
(
rewr
)
},
"rewrVMOD"
:
func
(
rewr
Rewrite
)
string
{
return
rewrVMOD
(
rewr
)
},
"rewrFlags"
:
func
(
rewr
Rewrite
)
string
{
return
rewrFlags
(
rewr
)
},
"needsSave"
:
func
(
rewr
Rewrite
)
bool
{
return
needsSave
(
rewr
)
},
"needsCompile"
:
func
(
rewr
Rewrite
)
bool
{
return
needsCompile
(
rewr
)
},
"rewrSub"
:
func
(
rewr
Rewrite
)
string
{
return
rewrSub
(
rewr
)
},
"rewrMatch"
:
func
(
rewr
Rewrite
)
string
{
return
rewrMatch
(
rewr
)
},
"rewrOp"
:
func
(
rewr
Rewrite
)
string
{
return
rewrOp
(
rewr
)
},
"rewrSelect"
:
func
(
rewr
Rewrite
)
string
{
return
rewrSelect
(
rewr
)
},
"reqVMOD"
:
func
(
cond
Condition
)
string
{
return
reqVMOD
(
cond
)
},
"reqMatch"
:
func
(
cond
Condition
)
string
{
return
reqMatch
(
cond
)
},
"value"
:
func
(
cond
Condition
)
string
{
return
reqValue
(
cond
)
},
"reqFlags"
:
func
(
cond
Condition
)
string
{
return
reqFlags
(
cond
)
},
"exists"
:
func
(
cmp
ReqCompareType
)
bool
{
return
cmp
==
Exists
},
"needsCompile"
:
func
(
cmp
CompareType
)
bool
{
return
cmp
==
Match
},
"exists"
:
func
(
cmp
CompareType
)
bool
{
return
cmp
==
Exists
},
"match"
:
func
(
cmp
CompareType
)
string
{
return
match
(
cmp
)
},
"vmod"
:
func
(
cmp
CompareType
)
string
{
return
vmod
(
cmp
)
},
"aclCmp"
:
func
(
comparand
string
)
string
{
return
aclCmp
(
comparand
)
},
"cmpRelation"
:
func
(
cmp
CompareType
)
string
{
return
cmpRelation
(
cmp
)
"cmpRelation"
:
func
(
cmp
CompareType
,
negate
bool
)
string
{
return
cmpRelation
(
cmp
,
negate
)
},
"backendName"
:
func
(
svc
Service
,
addr
string
)
string
{
return
backendName
(
svc
,
addr
)
...
...
@@ -97,7 +95,7 @@ var fMap = template.FuncMap{
return
rewr
.
Method
==
Replace
},
"needsRegex"
:
func
(
rewr
Rewrite
)
bool
{
return
rewr
.
Compare
!=
Rewrite
Match
&&
return
rewr
.
Compare
!=
Match
&&
(
rewr
.
Method
==
Sub
||
rewr
.
Method
==
Suball
)
},
"saveRegex"
:
func
(
rewr
Rewrite
,
rule
RewriteRule
)
string
{
...
...
@@ -108,11 +106,10 @@ var fMap = template.FuncMap{
return
regex
+
"$"
},
"needsAll"
:
func
(
rewr
Rewrite
)
bool
{
return
rewr
.
Compare
!=
Rewrite
Match
&&
rewr
.
Method
==
Suball
return
rewr
.
Compare
!=
Match
&&
rewr
.
Method
==
Suball
},
"needsNeverCapture"
:
func
(
rewr
Rewrite
)
bool
{
return
rewr
.
Compare
==
RewriteMatch
&&
rewr
.
MatchFlags
.
NeverCapture
return
rewr
.
Compare
==
Match
&&
rewr
.
MatchFlags
.
NeverCapture
},
"rewrOperand1"
:
func
(
rewr
Rewrite
)
string
{
return
rewrOperand1
(
rewr
)
...
...
@@ -121,7 +118,7 @@ var fMap = template.FuncMap{
return
rewrOperand2
(
rewr
,
i
)
},
"needsUniqueCheck"
:
func
(
rewr
Rewrite
)
bool
{
if
rewr
.
Compare
==
Rewrite
Equal
||
rewr
.
Select
!=
Unique
{
if
rewr
.
Compare
==
Equal
||
rewr
.
Select
!=
Unique
{
return
false
}
switch
rewr
.
Method
{
...
...
@@ -142,12 +139,6 @@ var fMap = template.FuncMap{
"reqNeedsMatcher"
:
func
(
cond
Condition
)
bool
{
return
reqNeedsMatcher
(
cond
)
},
"reqNeedsCompile"
:
func
(
cond
Condition
)
bool
{
return
reqNeedsCompile
(
cond
)
},
"reqCmpRelation"
:
func
(
cond
Condition
)
string
{
return
reqCmpRelation
(
cond
)
},
}
const
(
...
...
@@ -333,28 +324,18 @@ func hasXFF(acls []ACL) bool {
return
false
}
func
cmpRelation
(
cmp
CompareType
)
string
{
func
cmpRelation
(
cmp
CompareType
,
negate
bool
)
string
{
switch
cmp
{
case
Equal
:
return
"=="
case
NotEqual
:
if
negate
{
return
"!="
}
return
"=="
case
Match
:
return
"~"
case
NotMatch
:
if
negate
{
return
"!~"
default
:
return
"__INVALID_COMPARISON_TYPE__"
}
}
func
reqCmpRelation
(
cond
Condition
)
string
{
switch
cond
.
Compare
{
case
ReqEqual
:
if
cond
.
Negate
{
return
"!="
}
return
"=="
return
"~"
case
Greater
:
return
">"
case
GreaterEqual
:
...
...
@@ -390,7 +371,7 @@ func needsMatcher(rewr Rewrite) bool {
func
reqNeedsMatcher
(
cond
Condition
)
bool
{
switch
cond
.
Compare
{
case
ReqMatch
,
Req
Prefix
:
case
Match
,
Prefix
:
return
true
case
Exists
,
Greater
,
GreaterEqual
,
Less
,
LessEqual
:
return
false
...
...
@@ -405,11 +386,15 @@ func rewrName(i int) string {
return
fmt
.
Sprintf
(
"vk8s_rewrite_%d"
,
i
)
}
func
rewrVMOD
(
rewr
Rewrite
)
string
{
if
rewr
.
Compare
==
RewriteMatch
{
func
vmod
(
cmp
CompareType
)
string
{
switch
cmp
{
case
Equal
,
Prefix
:
return
"selector"
case
Match
:
return
"re2"
default
:
return
"__INVALID_COMPARISON_FOR_VMOD__"
}
return
"selector"
}
func
matcherFlags
(
isSelector
bool
,
flagSpec
MatchFlagsType
)
string
{
...
...
@@ -458,15 +443,15 @@ func matcherFlags(isSelector bool, flagSpec MatchFlagsType) string {
}
func
rewrFlags
(
rewr
Rewrite
)
string
{
return
matcherFlags
(
rewr
.
Compare
!=
Rewrite
Match
,
rewr
.
MatchFlags
)
return
matcherFlags
(
rewr
.
Compare
!=
Match
,
rewr
.
MatchFlags
)
}
func
reqFlags
(
cond
Condition
)
string
{
return
matcherFlags
(
cond
.
Compare
!=
Req
Match
,
cond
.
MatchFlags
)
return
matcherFlags
(
cond
.
Compare
!=
Match
,
cond
.
MatchFlags
)
}
func
needsSave
(
rewr
Rewrite
)
bool
{
if
rewr
.
Compare
!=
Rewrite
Match
{
if
rewr
.
Compare
!=
Match
{
return
false
}
switch
rewr
.
Method
{
...
...
@@ -477,14 +462,6 @@ func needsSave(rewr Rewrite) bool {
}
}
func
needsCompile
(
rewr
Rewrite
)
bool
{
return
rewr
.
Compare
==
RewriteMatch
}
func
reqNeedsCompile
(
cond
Condition
)
bool
{
return
cond
.
Compare
==
ReqMatch
}
func
rewrSub
(
rewr
Rewrite
)
string
{
if
rewr
.
VCLSub
==
Unspecified
{
if
strings
.
HasPrefix
(
rewr
.
Target
,
"resp"
)
||
...
...
@@ -553,10 +530,9 @@ func rewrOperand2(rewr Rewrite, i int) string {
return
rewr
.
Source
}
// XXX DRY
func
rewrMatch
(
rewr
Rewrite
)
string
{
switch
rewr
.
Compare
{
case
RewriteMatch
,
RewriteEqual
:
func
match
(
cmp
CompareType
)
string
{
switch
cmp
{
case
Match
,
Equal
:
return
"match"
case
Prefix
:
return
"hasprefix"
...
...
@@ -565,23 +541,12 @@ func rewrMatch(rewr Rewrite) string {
}
}
func
reqMatch
(
cond
Condition
)
string
{
switch
cond
.
Compare
{
case
ReqMatch
,
ReqEqual
:
return
"match"
case
ReqPrefix
:
return
"hasprefix"
default
:
return
"__INVALID_MATCH_OPERATION__"
}
}
func
rewrOp
(
rewr
Rewrite
)
string
{
switch
rewr
.
Method
{
case
Sub
:
return
"sub"
case
Suball
:
if
rewr
.
Compare
==
Rewrite
Match
{
if
rewr
.
Compare
==
Match
{
return
"suball"
}
return
"sub"
...
...
@@ -592,17 +557,6 @@ func rewrOp(rewr Rewrite) string {
}
}
func
reqVMOD
(
cond
Condition
)
string
{
switch
cond
.
Compare
{
case
ReqEqual
,
ReqPrefix
:
return
"selector"
case
ReqMatch
:
return
"re2"
default
:
return
"__INVALID_COMPARISON_FOR_VMOD__"
}
}
func
reqValue
(
cond
Condition
)
string
{
if
cond
.
Count
!=
nil
{
return
fmt
.
Sprintf
(
"%d"
,
*
cond
.
Count
)
...
...
pkg/varnish/vcl/vcl_test.go
View file @
86b2d1aa
...
...
@@ -377,12 +377,14 @@ var acls = Spec{
Conditions
:
[]
MatchTerm
{
MatchTerm
{
Comparand
:
"req.http.Host"
,
Compare
:
NotEqual
,
Compare
:
Equal
,
Negate
:
true
,
Value
:
"cafe.example.com"
,
},
MatchTerm
{
Comparand
:
"req.url"
,
Compare
:
NotMatch
,
Compare
:
Match
,
Negate
:
true
,
Value
:
`^/tea(/|$)`
,
},
},
...
...
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