Commit bfff5b5d authored by Geoff Simmons's avatar Geoff Simmons

Use only one CompareType in the custom resource types.

Reduces repetition in the code. The CRD limits the comparison
operations permitted for certain configs (ACL, rewrite,
req-disposition).
parent 3e97eb44
...@@ -138,18 +138,44 @@ const ( ...@@ -138,18 +138,44 @@ const (
Blacklist = "blacklist" Blacklist = "blacklist"
) )
// CompareType classifies a string comparison. // CompareType classifies comparison operations performed for
// conditional configurations. Some of these are not permitted for
// certain config types; the custom resource defines which values are
// available.
type CompareType string type CompareType string
const ( const (
// Equal means compare strings with ==. // Equal specifies equality -- string equality, numeric
// equality, or membership in a set of fixed strings.
Equal CompareType = "equal" Equal CompareType = "equal"
// NotEqual means compare with !=. // NotEqual specifies non-equality.
NotEqual = "not-equal" NotEqual = "not-equal"
// Match means compare with ~ (the Value is a regex). // Match specifies a regular expression match.
Match = "match" Match = "match"
// NotMatch means compare with !~. // NotMatch specifies a regular expression non-match.
NotMatch = "not-match" NotMatch = "not-match"
// Prefix specifies that a string has a prefix in a set of
// fixed strings.
Prefix = "prefix"
// NotPrefix specifies that a string does not have a prefix
// in a set of fixed strings.
NotPrefix = "not-prefix"
// Exists specifies that a request header exists.
Exists = "exists"
// NotExists specifies that a request header does not exist.
NotExists = "not-exists"
// Greater specifies the > relation between a VCL variable
// with a numeric value and a constant.
Greater = "greater"
// GreaterEqual specifies the >= relation for a numeric VCL
// variable and a constant.
GreaterEqual = "greater-equal"
// Less specifies the < relation for a numeric VCL variable
// and a constant.
Less = "less"
// LessEqual specifies the <= relation for a numeric VCL
// variable and a constant.
LessEqual = "less-equal"
) )
// ResultHdrType describes the configuration for writing a header as // ResultHdrType describes the configuration for writing a header as
...@@ -238,19 +264,6 @@ const ( ...@@ -238,19 +264,6 @@ const (
Delete = "delete" Delete = "delete"
) )
// RewriteCompare classifies the comparison operation used to evaluate
// the conditions for a rewrite.
type RewriteCompare string
const (
// RewriteMatch means that a regex match is executed.
RewriteMatch RewriteCompare = "match"
// RewriteEqual means that fixed strings are tested for equality.
RewriteEqual = "equal"
// Prefix indicates a fixed-string prefix match.
Prefix = "prefix"
)
// VCLSubType classifies the VCL subroutine in which a rewrite is // VCLSubType classifies the VCL subroutine in which a rewrite is
// executed. // executed.
type VCLSubType string type VCLSubType string
...@@ -329,49 +342,11 @@ type RewriteSpec struct { ...@@ -329,49 +342,11 @@ type RewriteSpec struct {
Target string `json:"target,omitempty"` Target string `json:"target,omitempty"`
Source string `json:"source,omitempty"` Source string `json:"source,omitempty"`
Method MethodType `json:"method,omitempty"` Method MethodType `json:"method,omitempty"`
Compare RewriteCompare `json:"compare,omitempty"` Compare CompareType `json:"compare,omitempty"`
VCLSub VCLSubType `json:"vcl-sub,omitempty"` VCLSub VCLSubType `json:"vcl-sub,omitempty"`
Select SelectType `json:"select,omitempty"` Select SelectType `json:"select,omitempty"`
} }
// ReqCompareType classifies comparison operations performed for the
// Conditions in a request disposition specification.
type ReqCompareType string
const (
// ReqEqual specifies equality -- string equality, numeric
// equality, or membership in a set of fixed strings.
ReqEqual ReqCompareType = "equal"
// ReqNotEqual specifies non-equality.
ReqNotEqual = "not-equal"
// ReqMatch specifies a regular expression match.
ReqMatch = "match"
// ReqNotMatch specifies a regular expression non-match.
ReqNotMatch = "not-match"
// ReqPrefix specifies that a string has a prefix in a set of
// fixed strings.
ReqPrefix = "prefix"
// ReqNotPrefix specifies that a string does not have a prefix
// in a set of fixed strings.
ReqNotPrefix = "not-prefix"
// Exists specifies that a request header exists.
Exists = "exists"
// NotExists specifies that a request header does not exist.
NotExists = "not-exists"
// Greater specifies the > relation between a VCL variable
// with a numeric value and a constant.
Greater = "greater"
// GreaterEqual specifies the >= relation for a numeric VCL
// variable and a constant.
GreaterEqual = "greater-equal"
// Less specifies the < relation for a numeric VCL variable
// and a constant.
Less = "less"
// LessEqual specifies the <= relation for a numeric VCL
// variable and a constant.
LessEqual = "less-equal"
)
// ReqCondition specifies (one of) the conditions that must be true if // ReqCondition specifies (one of) the conditions that must be true if
// a client request disposition is to be executed. // a client request disposition is to be executed.
type ReqCondition struct { type ReqCondition struct {
...@@ -379,7 +354,7 @@ type ReqCondition struct { ...@@ -379,7 +354,7 @@ type ReqCondition struct {
MatchFlags *MatchFlagsType `json:"match-flags,omitempty"` MatchFlags *MatchFlagsType `json:"match-flags,omitempty"`
Count *int64 `json:"count,omitempty"` Count *int64 `json:"count,omitempty"`
Comparand string `json:"comparand"` Comparand string `json:"comparand"`
Compare ReqCompareType `json:"compare,omitempty"` Compare CompareType `json:"compare,omitempty"`
} }
// RecvReturn is a name for the disposition of a client request. // RecvReturn is a name for the disposition of a client request.
......
...@@ -689,9 +689,9 @@ func (worker *NamespaceWorker) configRewrites(spec *vcl.Spec, ...@@ -689,9 +689,9 @@ func (worker *NamespaceWorker) configRewrites(spec *vcl.Spec,
} }
switch rw.Compare { switch rw.Compare {
case vcr_v1alpha1.RewriteMatch: case vcr_v1alpha1.Match:
vclRw.Compare = vcl.RewriteMatch vclRw.Compare = vcl.RewriteMatch
case vcr_v1alpha1.RewriteEqual: case vcr_v1alpha1.Equal:
vclRw.Compare = vcl.RewriteEqual vclRw.Compare = vcl.RewriteEqual
case vcr_v1alpha1.Prefix: case vcr_v1alpha1.Prefix:
vclRw.Compare = vcl.Prefix vclRw.Compare = vcl.Prefix
...@@ -784,22 +784,22 @@ func (worker *NamespaceWorker) configReqDisps(spec *vcl.Spec, ...@@ -784,22 +784,22 @@ func (worker *NamespaceWorker) configReqDisps(spec *vcl.Spec,
vclCond.Count = &count vclCond.Count = &count
} }
switch cond.Compare { switch cond.Compare {
case vcr_v1alpha1.ReqEqual: case vcr_v1alpha1.Equal:
vclCond.Compare = vcl.ReqEqual vclCond.Compare = vcl.ReqEqual
vclCond.Negate = false vclCond.Negate = false
case vcr_v1alpha1.ReqNotEqual: case vcr_v1alpha1.NotEqual:
vclCond.Compare = vcl.ReqEqual vclCond.Compare = vcl.ReqEqual
vclCond.Negate = true vclCond.Negate = true
case vcr_v1alpha1.ReqMatch: case vcr_v1alpha1.Match:
vclCond.Compare = vcl.ReqMatch vclCond.Compare = vcl.ReqMatch
vclCond.Negate = false vclCond.Negate = false
case vcr_v1alpha1.ReqNotMatch: case vcr_v1alpha1.NotMatch:
vclCond.Compare = vcl.ReqMatch vclCond.Compare = vcl.ReqMatch
vclCond.Negate = true vclCond.Negate = true
case vcr_v1alpha1.ReqPrefix: case vcr_v1alpha1.Prefix:
vclCond.Compare = vcl.ReqPrefix vclCond.Compare = vcl.ReqPrefix
vclCond.Negate = false vclCond.Negate = false
case vcr_v1alpha1.ReqNotPrefix: case vcr_v1alpha1.NotPrefix:
vclCond.Compare = vcl.ReqPrefix vclCond.Compare = vcl.ReqPrefix
vclCond.Negate = true vclCond.Negate = true
case vcr_v1alpha1.Exists: case vcr_v1alpha1.Exists:
......
...@@ -217,7 +217,7 @@ var reqDispHarness = []struct { ...@@ -217,7 +217,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.method", Comparand: "req.method",
Compare: vcr_v1alpha1.ReqEqual, Compare: vcr_v1alpha1.Equal,
Values: []string{"PRI"}, Values: []string{"PRI"},
}, },
}, },
...@@ -238,7 +238,7 @@ var reqDispHarness = []struct { ...@@ -238,7 +238,7 @@ var reqDispHarness = []struct {
}, },
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.proto", Comparand: "req.proto",
Compare: vcr_v1alpha1.ReqPrefix, Compare: vcr_v1alpha1.Prefix,
Values: []string{"HTTP/1.1"}, Values: []string{"HTTP/1.1"},
MatchFlags: &vcr_v1alpha1.MatchFlagsType{ MatchFlags: &vcr_v1alpha1.MatchFlagsType{
CaseSensitive: &nope, CaseSensitive: &nope,
...@@ -254,7 +254,7 @@ var reqDispHarness = []struct { ...@@ -254,7 +254,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.method", Comparand: "req.method",
Compare: vcr_v1alpha1.ReqNotEqual, Compare: vcr_v1alpha1.NotEqual,
Values: []string{ Values: []string{
"GET", "GET",
"HEAD", "HEAD",
...@@ -275,7 +275,7 @@ var reqDispHarness = []struct { ...@@ -275,7 +275,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.method", Comparand: "req.method",
Compare: vcr_v1alpha1.ReqNotEqual, Compare: vcr_v1alpha1.NotEqual,
Values: []string{ Values: []string{
"GET", "GET",
"HEAD", "HEAD",
...@@ -435,7 +435,7 @@ var reqDispHarness = []struct { ...@@ -435,7 +435,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.method", Comparand: "req.method",
Compare: vcr_v1alpha1.ReqEqual, Compare: vcr_v1alpha1.Equal,
Values: []string{"CONNECT"}, Values: []string{"CONNECT"},
}, },
}, },
...@@ -447,7 +447,7 @@ var reqDispHarness = []struct { ...@@ -447,7 +447,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.method", Comparand: "req.method",
Compare: vcr_v1alpha1.ReqNotEqual, Compare: vcr_v1alpha1.NotEqual,
Values: []string{ Values: []string{
"GET", "GET",
"HEAD", "HEAD",
...@@ -515,7 +515,7 @@ var reqDispHarness = []struct { ...@@ -515,7 +515,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.url", Comparand: "req.url",
Compare: vcr_v1alpha1.ReqMatch, Compare: vcr_v1alpha1.Match,
Values: []string{ Values: []string{
`\.png$`, `\.png$`,
`\.jpe?g$`, `\.jpe?g$`,
...@@ -532,7 +532,7 @@ var reqDispHarness = []struct { ...@@ -532,7 +532,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.url", Comparand: "req.url",
Compare: vcr_v1alpha1.ReqPrefix, Compare: vcr_v1alpha1.Prefix,
Values: []string{ Values: []string{
"/interactive/", "/interactive/",
"/basket/", "/basket/",
...@@ -594,7 +594,7 @@ var reqDispHarness = []struct { ...@@ -594,7 +594,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.method", Comparand: "req.method",
Compare: vcr_v1alpha1.ReqEqual, Compare: vcr_v1alpha1.Equal,
Values: []string{"PURGE"}, Values: []string{"PURGE"},
}, },
}, },
...@@ -626,7 +626,7 @@ var reqDispHarness = []struct { ...@@ -626,7 +626,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{ Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{ vcr_v1alpha1.ReqCondition{
Comparand: "req.url", Comparand: "req.url",
Compare: vcr_v1alpha1.ReqNotPrefix, Compare: vcr_v1alpha1.NotPrefix,
Values: []string{ Values: []string{
"/foo/", "/foo/",
"/bar/", "/bar/",
......
...@@ -122,7 +122,7 @@ func validateRewrites(rewrites []vcr_v1alpha1.RewriteSpec) error { ...@@ -122,7 +122,7 @@ func validateRewrites(rewrites []vcr_v1alpha1.RewriteSpec) error {
return fmt.Errorf("select value %s not permitted with "+ return fmt.Errorf("select value %s not permitted with "+
"compare value %s", rw.Select, rw.Compare) "compare value %s", rw.Select, rw.Compare)
} }
if rw.Compare != vcr_v1alpha1.RewriteMatch && if rw.Compare != vcr_v1alpha1.Match &&
rw.MatchFlags != nil && rw.MatchFlags != nil &&
((rw.MatchFlags.MaxMem != nil && ((rw.MatchFlags.MaxMem != nil &&
*rw.MatchFlags.MaxMem != 0) || *rw.MatchFlags.MaxMem != 0) ||
...@@ -202,10 +202,10 @@ func validateReqDisps(reqDisps []vcr_v1alpha1.RequestDispSpec) error { ...@@ -202,10 +202,10 @@ func validateReqDisps(reqDisps []vcr_v1alpha1.RequestDispSpec) error {
} }
if cond.Count != nil { if cond.Count != nil {
switch cond.Compare { switch cond.Compare {
case vcr_v1alpha1.ReqMatch, case vcr_v1alpha1.Match,
vcr_v1alpha1.ReqNotMatch, vcr_v1alpha1.NotMatch,
vcr_v1alpha1.ReqPrefix, vcr_v1alpha1.Prefix,
vcr_v1alpha1.ReqNotPrefix, vcr_v1alpha1.NotPrefix,
vcr_v1alpha1.Exists, vcr_v1alpha1.Exists,
vcr_v1alpha1.NotExists: vcr_v1alpha1.NotExists:
return fmt.Errorf("illegal compare "+ return fmt.Errorf("illegal compare "+
......
...@@ -48,7 +48,7 @@ func TestValidateReqDisps(t *testing.T) { ...@@ -48,7 +48,7 @@ func TestValidateReqDisps(t *testing.T) {
}, },
Conditions: []vcr_v1alpha1.ReqCondition{{ Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.url", Comparand: "req.url",
Compare: vcr_v1alpha1.ReqEqual, Compare: vcr_v1alpha1.Equal,
}}, }},
}}, }},
{{ {{
...@@ -57,7 +57,7 @@ func TestValidateReqDisps(t *testing.T) { ...@@ -57,7 +57,7 @@ func TestValidateReqDisps(t *testing.T) {
}, },
Conditions: []vcr_v1alpha1.ReqCondition{{ Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.url", Comparand: "req.url",
Compare: vcr_v1alpha1.ReqEqual, Compare: vcr_v1alpha1.Equal,
Count: &zero, Count: &zero,
Values: []string{"/foo"}, Values: []string{"/foo"},
}}, }},
...@@ -88,7 +88,7 @@ func TestValidateReqDisps(t *testing.T) { ...@@ -88,7 +88,7 @@ func TestValidateReqDisps(t *testing.T) {
}, },
Conditions: []vcr_v1alpha1.ReqCondition{{ Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.url", Comparand: "req.url",
Compare: vcr_v1alpha1.ReqEqual, Compare: vcr_v1alpha1.Equal,
Count: &zero, Count: &zero,
}}, }},
}}, }},
...@@ -98,7 +98,7 @@ func TestValidateReqDisps(t *testing.T) { ...@@ -98,7 +98,7 @@ func TestValidateReqDisps(t *testing.T) {
}, },
Conditions: []vcr_v1alpha1.ReqCondition{{ Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.http.Host", Comparand: "req.http.Host",
Compare: vcr_v1alpha1.ReqEqual, Compare: vcr_v1alpha1.Equal,
Count: &zero, Count: &zero,
}}, }},
}}, }},
...@@ -108,7 +108,7 @@ func TestValidateReqDisps(t *testing.T) { ...@@ -108,7 +108,7 @@ func TestValidateReqDisps(t *testing.T) {
}, },
Conditions: []vcr_v1alpha1.ReqCondition{{ Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.restarts", Comparand: "req.restarts",
Compare: vcr_v1alpha1.ReqEqual, Compare: vcr_v1alpha1.Equal,
Values: []string{"/foo"}, Values: []string{"/foo"},
}}, }},
}}, }},
......
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