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 (
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
const (
// Equal means compare strings with ==.
// Equal specifies equality -- string equality, numeric
// equality, or membership in a set of fixed strings.
Equal CompareType = "equal"
// NotEqual means compare with !=.
// NotEqual specifies non-equality.
NotEqual = "not-equal"
// Match means compare with ~ (the Value is a regex).
// Match specifies a regular expression match.
Match = "match"
// NotMatch means compare with !~.
// NotMatch specifies a regular expression non-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
......@@ -238,19 +264,6 @@ const (
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
// executed.
type VCLSubType string
......@@ -329,49 +342,11 @@ type RewriteSpec struct {
Target string `json:"target,omitempty"`
Source string `json:"source,omitempty"`
Method MethodType `json:"method,omitempty"`
Compare RewriteCompare `json:"compare,omitempty"`
Compare CompareType `json:"compare,omitempty"`
VCLSub VCLSubType `json:"vcl-sub,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
// a client request disposition is to be executed.
type ReqCondition struct {
......@@ -379,7 +354,7 @@ type ReqCondition struct {
MatchFlags *MatchFlagsType `json:"match-flags,omitempty"`
Count *int64 `json:"count,omitempty"`
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.
......
......@@ -689,9 +689,9 @@ func (worker *NamespaceWorker) configRewrites(spec *vcl.Spec,
}
switch rw.Compare {
case vcr_v1alpha1.RewriteMatch:
case vcr_v1alpha1.Match:
vclRw.Compare = vcl.RewriteMatch
case vcr_v1alpha1.RewriteEqual:
case vcr_v1alpha1.Equal:
vclRw.Compare = vcl.RewriteEqual
case vcr_v1alpha1.Prefix:
vclRw.Compare = vcl.Prefix
......@@ -784,22 +784,22 @@ func (worker *NamespaceWorker) configReqDisps(spec *vcl.Spec,
vclCond.Count = &count
}
switch cond.Compare {
case vcr_v1alpha1.ReqEqual:
case vcr_v1alpha1.Equal:
vclCond.Compare = vcl.ReqEqual
vclCond.Negate = false
case vcr_v1alpha1.ReqNotEqual:
case vcr_v1alpha1.NotEqual:
vclCond.Compare = vcl.ReqEqual
vclCond.Negate = true
case vcr_v1alpha1.ReqMatch:
case vcr_v1alpha1.Match:
vclCond.Compare = vcl.ReqMatch
vclCond.Negate = false
case vcr_v1alpha1.ReqNotMatch:
case vcr_v1alpha1.NotMatch:
vclCond.Compare = vcl.ReqMatch
vclCond.Negate = true
case vcr_v1alpha1.ReqPrefix:
case vcr_v1alpha1.Prefix:
vclCond.Compare = vcl.ReqPrefix
vclCond.Negate = false
case vcr_v1alpha1.ReqNotPrefix:
case vcr_v1alpha1.NotPrefix:
vclCond.Compare = vcl.ReqPrefix
vclCond.Negate = true
case vcr_v1alpha1.Exists:
......
......@@ -217,7 +217,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.method",
Compare: vcr_v1alpha1.ReqEqual,
Compare: vcr_v1alpha1.Equal,
Values: []string{"PRI"},
},
},
......@@ -238,7 +238,7 @@ var reqDispHarness = []struct {
},
vcr_v1alpha1.ReqCondition{
Comparand: "req.proto",
Compare: vcr_v1alpha1.ReqPrefix,
Compare: vcr_v1alpha1.Prefix,
Values: []string{"HTTP/1.1"},
MatchFlags: &vcr_v1alpha1.MatchFlagsType{
CaseSensitive: &nope,
......@@ -254,7 +254,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.method",
Compare: vcr_v1alpha1.ReqNotEqual,
Compare: vcr_v1alpha1.NotEqual,
Values: []string{
"GET",
"HEAD",
......@@ -275,7 +275,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.method",
Compare: vcr_v1alpha1.ReqNotEqual,
Compare: vcr_v1alpha1.NotEqual,
Values: []string{
"GET",
"HEAD",
......@@ -435,7 +435,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.method",
Compare: vcr_v1alpha1.ReqEqual,
Compare: vcr_v1alpha1.Equal,
Values: []string{"CONNECT"},
},
},
......@@ -447,7 +447,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.method",
Compare: vcr_v1alpha1.ReqNotEqual,
Compare: vcr_v1alpha1.NotEqual,
Values: []string{
"GET",
"HEAD",
......@@ -515,7 +515,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.url",
Compare: vcr_v1alpha1.ReqMatch,
Compare: vcr_v1alpha1.Match,
Values: []string{
`\.png$`,
`\.jpe?g$`,
......@@ -532,7 +532,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.url",
Compare: vcr_v1alpha1.ReqPrefix,
Compare: vcr_v1alpha1.Prefix,
Values: []string{
"/interactive/",
"/basket/",
......@@ -594,7 +594,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.method",
Compare: vcr_v1alpha1.ReqEqual,
Compare: vcr_v1alpha1.Equal,
Values: []string{"PURGE"},
},
},
......@@ -626,7 +626,7 @@ var reqDispHarness = []struct {
Conditions: []vcr_v1alpha1.ReqCondition{
vcr_v1alpha1.ReqCondition{
Comparand: "req.url",
Compare: vcr_v1alpha1.ReqNotPrefix,
Compare: vcr_v1alpha1.NotPrefix,
Values: []string{
"/foo/",
"/bar/",
......
......@@ -122,7 +122,7 @@ func validateRewrites(rewrites []vcr_v1alpha1.RewriteSpec) error {
return fmt.Errorf("select value %s not permitted with "+
"compare value %s", rw.Select, rw.Compare)
}
if rw.Compare != vcr_v1alpha1.RewriteMatch &&
if rw.Compare != vcr_v1alpha1.Match &&
rw.MatchFlags != nil &&
((rw.MatchFlags.MaxMem != nil &&
*rw.MatchFlags.MaxMem != 0) ||
......@@ -202,10 +202,10 @@ func validateReqDisps(reqDisps []vcr_v1alpha1.RequestDispSpec) error {
}
if cond.Count != nil {
switch cond.Compare {
case vcr_v1alpha1.ReqMatch,
vcr_v1alpha1.ReqNotMatch,
vcr_v1alpha1.ReqPrefix,
vcr_v1alpha1.ReqNotPrefix,
case vcr_v1alpha1.Match,
vcr_v1alpha1.NotMatch,
vcr_v1alpha1.Prefix,
vcr_v1alpha1.NotPrefix,
vcr_v1alpha1.Exists,
vcr_v1alpha1.NotExists:
return fmt.Errorf("illegal compare "+
......
......@@ -48,7 +48,7 @@ func TestValidateReqDisps(t *testing.T) {
},
Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.url",
Compare: vcr_v1alpha1.ReqEqual,
Compare: vcr_v1alpha1.Equal,
}},
}},
{{
......@@ -57,7 +57,7 @@ func TestValidateReqDisps(t *testing.T) {
},
Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.url",
Compare: vcr_v1alpha1.ReqEqual,
Compare: vcr_v1alpha1.Equal,
Count: &zero,
Values: []string{"/foo"},
}},
......@@ -88,7 +88,7 @@ func TestValidateReqDisps(t *testing.T) {
},
Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.url",
Compare: vcr_v1alpha1.ReqEqual,
Compare: vcr_v1alpha1.Equal,
Count: &zero,
}},
}},
......@@ -98,7 +98,7 @@ func TestValidateReqDisps(t *testing.T) {
},
Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.http.Host",
Compare: vcr_v1alpha1.ReqEqual,
Compare: vcr_v1alpha1.Equal,
Count: &zero,
}},
}},
......@@ -108,7 +108,7 @@ func TestValidateReqDisps(t *testing.T) {
},
Conditions: []vcr_v1alpha1.ReqCondition{{
Comparand: "req.restarts",
Compare: vcr_v1alpha1.ReqEqual,
Compare: vcr_v1alpha1.Equal,
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