From 05d484c08cbb67e0561925249c4cec1a1f83a092 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 6 Apr 2021 15:46:05 +0200 Subject: [PATCH 1/6] Trigger.Filter now supports the CNCF Subscription API Signed-off-by: Francesco Guardiani --- pkg/apis/eventing/v1/trigger_defaults.go | 2 +- pkg/apis/eventing/v1/trigger_defaults_test.go | 2 +- pkg/apis/eventing/v1/trigger_types.go | 57 +++- pkg/apis/eventing/v1/trigger_validation.go | 120 +++++++- .../eventing/v1/trigger_validation_test.go | 262 +++++++++++++++++- pkg/apis/eventing/v1/zz_generated.deepcopy.go | 103 +++++-- .../eventing/v1beta1/trigger_conversion.go | 0 pkg/broker/filter/filter_handler.go | 4 +- pkg/broker/filter/filter_handler_test.go | 12 +- pkg/reconciler/broker/trigger/trigger_test.go | 2 +- test/e2e/helpers/broker_test_helper.go | 2 +- test/lib/resources/eventing.go | 2 +- test/rekt/features/broker/control_plane.go | 2 +- 13 files changed, 514 insertions(+), 56 deletions(-) create mode 100644 pkg/apis/eventing/v1beta1/trigger_conversion.go diff --git a/pkg/apis/eventing/v1/trigger_defaults.go b/pkg/apis/eventing/v1/trigger_defaults.go index 4c90811f355..46649d9f97e 100644 --- a/pkg/apis/eventing/v1/trigger_defaults.go +++ b/pkg/apis/eventing/v1/trigger_defaults.go @@ -35,7 +35,7 @@ func (t *Trigger) SetDefaults(ctx context.Context) { func (ts *TriggerSpec) SetDefaults(ctx context.Context) { // Make a default filter that allows anything. if ts.Filter == nil { - ts.Filter = &TriggerFilter{} + ts.Filter = &FilterSpec{} } // Default the Subscriber namespace ts.Subscriber.SetDefaults(ctx) diff --git a/pkg/apis/eventing/v1/trigger_defaults_test.go b/pkg/apis/eventing/v1/trigger_defaults_test.go index 0d01bc10ea1..02d1a532826 100644 --- a/pkg/apis/eventing/v1/trigger_defaults_test.go +++ b/pkg/apis/eventing/v1/trigger_defaults_test.go @@ -31,7 +31,7 @@ var ( defaultBroker = "default" otherBroker = "other_broker" namespace = "testnamespace" - emptyTriggerFilter = &TriggerFilter{} + emptyTriggerFilter = &FilterSpec{} defaultTrigger = Trigger{Spec: TriggerSpec{Filter: emptyTriggerFilter}} ) diff --git a/pkg/apis/eventing/v1/trigger_types.go b/pkg/apis/eventing/v1/trigger_types.go index f40e8029299..b4909cce7f6 100644 --- a/pkg/apis/eventing/v1/trigger_types.go +++ b/pkg/apis/eventing/v1/trigger_types.go @@ -79,9 +79,10 @@ type TriggerSpec struct { // Filter is the filter to apply against all events from the Broker. Only events that pass this // filter will be sent to the Subscriber. If not specified, will default to allowing all events. + // If multiple filters are specified, then the same semantics of FilterSpec.All is applied. // // +optional - Filter *TriggerFilter `json:"filter,omitempty"` + Filter *FilterSpec `json:"filter,omitempty"` // Subscriber is the addressable that receives events from the Broker that pass the Filter. It // is required. @@ -92,7 +93,10 @@ type TriggerSpec struct { Delivery *eventingduckv1.DeliverySpec `json:"delivery,omitempty"` } -type TriggerFilter struct { +// FilterSpec allows to define a filter +// If multiple filters are specified, then the same semantics of FilterSpec.All is applied. +// If no filter dialect or empty object is specified, then the filter always accept the events. +type FilterSpec struct { // Attributes filters events by exact match on event context attributes. // Each key in the map is compared with the equivalent key in the event // context. An event passes the filter if all values are equal to the @@ -102,6 +106,55 @@ type TriggerFilter struct { // // +optional Attributes TriggerFilterAttributes `json:"attributes,omitempty"` + + // All evaluates to true if all the nested expressions evaluate to true. + // + // All must contain at least one filter expression. + // + // +optional + All []FilterSpec `json:"all,omitempty"` + + // Any evaluates to true if at least one of the nested expressions evaluate to true. + // + // Any must contain at least one filter expression. + // + // +optional + Any []FilterSpec `json:"any,omitempty"` + + // Not evaluates to true if the nested expression evaluates to false. + // + // +optional + Not *FilterSpec `json:"not,omitempty"` + + // Exact evaluates to true if the value of the matching CloudEvents attribute is matches exactly the String value specified (case sensitive). + // Exact must contain exactly one property, where the key is the name of the CloudEvents attribute to be matched, and its value is the String value to use in the comparison. + // + // The attribute name and value specified in the filter express cannot be be empty strings. + // + // +optional + Exact map[string]string `json:"exact,omitempty"` + + // Prefix evaluates to true if the value of the matching CloudEvents attribute starts with the String value specified (case sensitive). + // Prefix must contain exactly one property, where the key is the name of the CloudEvents attribute to be matched, and its value is the String value to use in the comparison. + // + // The attribute name and value specified in the filter express cannot be be empty strings. + // + // +optional + Prefix map[string]string `json:"prefix,omitempty"` + + // Suffix evaluates to true if the value of the matching CloudEvents attribute ends with the String value specified (case sensitive). + // Suffix must contain exactly one property, where the key is the name of the CloudEvents attribute to be matched, and its value is the String value to use in the comparison. + // + // The attribute name and value specified in the filter express cannot be be empty strings. + // + // +optional + Suffix map[string]string `json:"suffix,omitempty"` + + // Extensions includes the list of additional filter dialects supported by specific broker implementations. + // Check out the documentation of the broker implementation you're using to know about what additional filters are supported. + // + // +optional + Extensions map[string]*runtime.RawExtension `json:",inline"` } // TriggerFilterAttributes is a map of context attribute names to values for diff --git a/pkg/apis/eventing/v1/trigger_validation.go b/pkg/apis/eventing/v1/trigger_validation.go index 4b367340a6b..7b27d14147c 100644 --- a/pkg/apis/eventing/v1/trigger_validation.go +++ b/pkg/apis/eventing/v1/trigger_validation.go @@ -21,6 +21,7 @@ import ( "encoding/json" "fmt" "regexp" + "strings" "knative.dev/pkg/apis" "knative.dev/pkg/kmp" @@ -53,16 +54,8 @@ func (ts *TriggerSpec) Validate(ctx context.Context) *apis.FieldError { errs = errs.Also(fe) } - if ts.Filter != nil { - for attr := range map[string]string(ts.Filter.Attributes) { - if !validAttributeName.MatchString(attr) { - fe := &apis.FieldError{ - Message: fmt.Sprintf("Invalid attribute name: %q", attr), - Paths: []string{"filter.attributes"}, - } - errs = errs.Also(fe) - } - } + for _, err := range validateFilterSpec(ts.Filter, []string{"filter"}) { + errs = errs.Also(err) } if fe := ts.Subscriber.Validate(ctx); fe != nil { @@ -78,6 +71,113 @@ func (ts *TriggerSpec) Validate(ctx context.Context) *apis.FieldError { return errs } +func validateFilterSpec(filter *FilterSpec, path []string) (errs []*apis.FieldError) { + if filter == nil { + return nil + } + + // Validate Attributes + for attr := range map[string]string(filter.Attributes) { + if !validAttributeName.MatchString(attr) { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("Invalid attribute name: %q", attr), + Paths: []string{strings.Join(append(path, "attributes"), ".")}, + }) + } + } + + // Validate Exact + if filter.Exact != nil { + if len(filter.Exact) != 1 { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("Exact can have only one key-value"), + Paths: []string{strings.Join(append(path, "exact"), ".")}, + }) + } + for attr := range filter.Exact { + if !validAttributeName.MatchString(attr) { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("Invalid attribute name: %q", attr), + Paths: []string{strings.Join(append(path, "exact"), ".")}, + }) + } + } + } + + // Validate Prefix + if filter.Prefix != nil { + if len(filter.Prefix) != 1 { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("Prefix can have only one key-value"), + Paths: []string{strings.Join(append(path, "prefix"), ".")}, + }) + } + for attr := range filter.Prefix { + if !validAttributeName.MatchString(attr) { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("Invalid attribute name: %q", attr), + Paths: []string{strings.Join(append(path, "prefix"), ".")}, + }) + } + } + } + + // Validate Suffix + if filter.Suffix != nil { + if len(filter.Suffix) != 1 { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("Suffix can have only one key-value"), + Paths: []string{strings.Join(append(path, "suffix"), ".")}, + }) + } + for attr := range filter.Suffix { + if !validAttributeName.MatchString(attr) { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("Invalid attribute name: %q", attr), + Paths: []string{strings.Join(append(path, "suffix"), ".")}, + }) + } + } + } + + // Validate All + if filter.All != nil { + if len(filter.All) < 1 { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("All must contain at least one nested filter"), + Paths: []string{strings.Join(append(path, "all"), ".")}, + }) + } + + for i, f := range filter.All { + f := f + errs = append(errs, validateFilterSpec(&f, append(path, "all", fmt.Sprintf("[%d]", i)))...) + } + } + + // Validate Any + if filter.Any != nil { + if len(filter.Any) < 1 { + errs = append(errs, &apis.FieldError{ + Message: fmt.Sprintf("Any must contain at least one nested filter"), + Paths: []string{strings.Join(append(path, "any"), ".")}, + }) + } + + for i, f := range filter.Any { + f := f + errs = append(errs, validateFilterSpec(&f, append(path, "any", fmt.Sprintf("[%d]", i)))...) + } + } + + // Validate Not + if filter.Not != nil { + errs = append(errs, validateFilterSpec(filter.Not, append(path, "not"))...) + } + + return +} + // CheckImmutableFields checks that any immutable fields were not changed. func (t *Trigger) CheckImmutableFields(ctx context.Context, original *Trigger) *apis.FieldError { if original == nil { diff --git a/pkg/apis/eventing/v1/trigger_validation_test.go b/pkg/apis/eventing/v1/trigger_validation_test.go index 4dad96e5e89..2afa0636a1d 100644 --- a/pkg/apis/eventing/v1/trigger_validation_test.go +++ b/pkg/apis/eventing/v1/trigger_validation_test.go @@ -23,14 +23,16 @@ import ( "github.com/google/go-cmp/cmp" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1" + "k8s.io/apimachinery/pkg/runtime" "knative.dev/pkg/apis" duckv1 "knative.dev/pkg/apis/duck/v1" + + eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1" ) var ( - validEmptyFilter = &TriggerFilter{} - validAttributesFilter = &TriggerFilter{ + validEmptyFilter = &FilterSpec{} + validAttributesFilter = &FilterSpec{ Attributes: TriggerFilterAttributes{ "type": "other_type", "source": "other_source", @@ -367,7 +369,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "missing attributes keys, match all", ts: &TriggerSpec{ Broker: "test_broker", - Filter: &TriggerFilter{ + Filter: &FilterSpec{ Attributes: TriggerFilterAttributes{}, }, Subscriber: validSubscriber, @@ -377,7 +379,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "invalid attribute name, start with number", ts: &TriggerSpec{ Broker: "test_broker", - Filter: &TriggerFilter{ + Filter: &FilterSpec{ Attributes: TriggerFilterAttributes{ "0invalid": "my-value", }, @@ -392,7 +394,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "invalid attribute name, capital letters", ts: &TriggerSpec{ Broker: "test_broker", - Filter: &TriggerFilter{ + Filter: &FilterSpec{ Attributes: TriggerFilterAttributes{ "invALID": "my-value", }, @@ -473,6 +475,254 @@ func TestTriggerSpecValidation(t *testing.T) { } } +func TestFilterSpecValidation(t *testing.T) { + tests := []struct { + name string + filter *FilterSpec + want *apis.FieldError + }{{ + name: "missing attributes keys, match all", + filter: &FilterSpec{ + Attributes: TriggerFilterAttributes{}, + }, + want: &apis.FieldError{}, + }, { + name: "invalid attribute name, start with number", + filter: &FilterSpec{ + Attributes: TriggerFilterAttributes{ + "0invalid": "my-value", + }, + }, + want: &apis.FieldError{ + Message: `Invalid attribute name: "0invalid"`, + Paths: []string{"filter.attributes"}, + }, + }, { + name: "invalid attribute name, capital letters", + filter: &FilterSpec{ + Attributes: TriggerFilterAttributes{ + "invALID": "my-value", + }, + }, + want: &apis.FieldError{ + Message: `Invalid attribute name: "invALID"`, + Paths: []string{"filter.attributes"}, + }, + }, { + name: "valid empty filter", + filter: validEmptyFilter, + want: &apis.FieldError{}, + }, { + name: "valid SourceAndType filter", + filter: validAttributesFilter, + want: &apis.FieldError{}, + }, { + name: "valid Attributes filter", + filter: validAttributesFilter, + want: &apis.FieldError{}, + }, { + name: "exact filter contains more than one attribute", + filter: &FilterSpec{ + Exact: map[string]string{ + "myext": "abc", + "anotherext": "xyz", + }, + }, + want: &apis.FieldError{ + Message: `Exact can have only one key-value`, + Paths: []string{"filter.exact"}, + }, + }, { + name: "exact filter contains invalid attribute name", + filter: &FilterSpec{ + Exact: map[string]string{ + "invALID": "abc", + }, + }, + want: &apis.FieldError{ + Message: `Invalid attribute name: "invALID"`, + Paths: []string{"filter.exact"}, + }, + }, { + name: "valid exact filter", + filter: &FilterSpec{ + Exact: map[string]string{ + "valid": "abc", + }, + }, + want: &apis.FieldError{}, + }, { + name: "suffix filter contains more than one attribute", + filter: &FilterSpec{ + Suffix: map[string]string{ + "myext": "abc", + "anotherext": "xyz", + }, + }, + want: &apis.FieldError{ + Message: `Suffix can have only one key-value`, + Paths: []string{"filter.suffix"}, + }, + }, { + name: "suffix filter contains invalid attribute name", + filter: &FilterSpec{ + Suffix: map[string]string{ + "invALID": "abc", + }, + }, + want: &apis.FieldError{ + Message: `Invalid attribute name: "invALID"`, + Paths: []string{"filter.suffix"}, + }, + }, { + name: "valid suffix filter", + filter: &FilterSpec{ + Suffix: map[string]string{ + "valid": "abc", + }, + }, + want: &apis.FieldError{}, + }, { + name: "prefix filter contains more than one attribute", + filter: &FilterSpec{ + Prefix: map[string]string{ + "myext": "abc", + "anotherext": "xyz", + }, + }, + want: &apis.FieldError{ + Message: `Prefix can have only one key-value`, + Paths: []string{"filter.prefix"}, + }, + }, { + name: "prefix filter contains invalid attribute name", + filter: &FilterSpec{ + Prefix: map[string]string{ + "invALID": "abc", + }, + }, + want: &apis.FieldError{ + Message: `Invalid attribute name: "invALID"`, + Paths: []string{"filter.prefix"}, + }, + }, { + name: "valid prefix filter", + filter: &FilterSpec{ + Prefix: map[string]string{ + "valid": "abc", + }, + }, + want: &apis.FieldError{}, + }, { + name: "not nested expression is valid", + filter: &FilterSpec{ + Not: validAttributesFilter, + }, + want: &apis.FieldError{}, + }, { + name: "not nested expression is invalid", + filter: &FilterSpec{ + Not: &FilterSpec{ + Prefix: map[string]string{ + "invALID": "abc", + }, + }, + }, + want: &apis.FieldError{ + Message: `Invalid attribute name: "invALID"`, + Paths: []string{"filter.not.prefix"}, + }, + }, { + name: "all filter is empty", + filter: &FilterSpec{ + All: []FilterSpec{}, + }, + want: &apis.FieldError{ + Message: `All must contain at least one nested filter`, + Paths: []string{"filter.all"}, + }, + }, { + name: "all filter is valid", + filter: &FilterSpec{ + All: []FilterSpec{ + *validAttributesFilter, + {Exact: map[string]string{"myattr": "myval"}}, + }, + }, + want: &apis.FieldError{}, + }, { + name: "all filter sub expression is invalid", + filter: &FilterSpec{ + All: []FilterSpec{ + *validAttributesFilter, + {Exact: map[string]string{"myattr": "myval"}}, + {Prefix: map[string]string{ + "invALID": "abc", + }}, + }, + }, + want: &apis.FieldError{ + Message: `Invalid attribute name: "invALID"`, + Paths: []string{"filter.all.[2].prefix"}, + }, + }, { + name: "any filter is empty", + filter: &FilterSpec{ + Any: []FilterSpec{}, + }, + want: &apis.FieldError{ + Message: `Any must contain at least one nested filter`, + Paths: []string{"filter.any"}, + }, + }, { + name: "any filter is valid", + filter: &FilterSpec{ + Any: []FilterSpec{ + *validAttributesFilter, + {Exact: map[string]string{"myattr": "myval"}}, + }, + }, + want: &apis.FieldError{}, + }, { + name: "any filter sub expression is invalid", + filter: &FilterSpec{ + Any: []FilterSpec{ + *validAttributesFilter, + {Exact: map[string]string{"myattr": "myval"}}, + {Prefix: map[string]string{ + "invALID": "abc", + }}, + }, + }, + want: &apis.FieldError{ + Message: `Invalid attribute name: "invALID"`, + Paths: []string{"filter.any.[2].prefix"}, + }, + }, { + name: "raw extension expression is valid", + filter: &FilterSpec{ + Extensions: map[string]*runtime.RawExtension{ + "juel": {Raw: []byte("\"myExpressionUsingJUEL\"")}, + }, + }, + want: &apis.FieldError{}, + }} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ts := &TriggerSpec{ + Broker: "test_broker", + Filter: test.filter, + Subscriber: validSubscriber, + } + got := ts.Validate(context.TODO()) + if diff := cmp.Diff(test.want.Error(), got.Error()); diff != "" { + t.Errorf("Validate TriggerSpec (-want, +got) =\n%s", diff) + } + }) + } +} + func TestTriggerImmutableFields(t *testing.T) { tests := []struct { name string diff --git a/pkg/apis/eventing/v1/zz_generated.deepcopy.go b/pkg/apis/eventing/v1/zz_generated.deepcopy.go index c1f4be24871..9ec021cc105 100644 --- a/pkg/apis/eventing/v1/zz_generated.deepcopy.go +++ b/pkg/apis/eventing/v1/zz_generated.deepcopy.go @@ -132,6 +132,84 @@ func (in *BrokerStatus) DeepCopy() *BrokerStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *FilterSpec) DeepCopyInto(out *FilterSpec) { + *out = *in + if in.Attributes != nil { + in, out := &in.Attributes, &out.Attributes + *out = make(TriggerFilterAttributes, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.All != nil { + in, out := &in.All, &out.All + *out = make([]FilterSpec, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Any != nil { + in, out := &in.Any, &out.Any + *out = make([]FilterSpec, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Not != nil { + in, out := &in.Not, &out.Not + *out = new(FilterSpec) + (*in).DeepCopyInto(*out) + } + if in.Exact != nil { + in, out := &in.Exact, &out.Exact + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Prefix != nil { + in, out := &in.Prefix, &out.Prefix + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Suffix != nil { + in, out := &in.Suffix, &out.Suffix + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Extensions != nil { + in, out := &in.Extensions, &out.Extensions + *out = make(map[string]*runtime.RawExtension, len(*in)) + for key, val := range *in { + var outVal *runtime.RawExtension + if val == nil { + (*out)[key] = nil + } else { + in, out := &val, &outVal + *out = new(runtime.RawExtension) + (*in).DeepCopyInto(*out) + } + (*out)[key] = outVal + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FilterSpec. +func (in *FilterSpec) DeepCopy() *FilterSpec { + if in == nil { + return nil + } + out := new(FilterSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Trigger) DeepCopyInto(out *Trigger) { *out = *in @@ -160,29 +238,6 @@ func (in *Trigger) DeepCopyObject() runtime.Object { return nil } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *TriggerFilter) DeepCopyInto(out *TriggerFilter) { - *out = *in - if in.Attributes != nil { - in, out := &in.Attributes, &out.Attributes - *out = make(TriggerFilterAttributes, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TriggerFilter. -func (in *TriggerFilter) DeepCopy() *TriggerFilter { - if in == nil { - return nil - } - out := new(TriggerFilter) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in TriggerFilterAttributes) DeepCopyInto(out *TriggerFilterAttributes) { { @@ -243,7 +298,7 @@ func (in *TriggerSpec) DeepCopyInto(out *TriggerSpec) { *out = *in if in.Filter != nil { in, out := &in.Filter, &out.Filter - *out = new(TriggerFilter) + *out = new(FilterSpec) (*in).DeepCopyInto(*out) } in.Subscriber.DeepCopyInto(&out.Subscriber) diff --git a/pkg/apis/eventing/v1beta1/trigger_conversion.go b/pkg/apis/eventing/v1beta1/trigger_conversion.go new file mode 100644 index 00000000000..e69de29bb2d diff --git a/pkg/broker/filter/filter_handler.go b/pkg/broker/filter/filter_handler.go index 6314fe6aac0..4075d6bc854 100644 --- a/pkg/broker/filter/filter_handler.go +++ b/pkg/broker/filter/filter_handler.go @@ -324,7 +324,7 @@ func (h *Handler) getTrigger(ref path.NamespacedNameUID) (*eventingv1.Trigger, e return t, nil } -func filterEvent(ctx context.Context, filter *eventingv1.TriggerFilter, event cloudevents.Event) eventfilter.FilterResult { +func filterEvent(ctx context.Context, filter *eventingv1.FilterSpec, event cloudevents.Event) eventfilter.FilterResult { if filter == nil { return eventfilter.NoFilter } @@ -337,7 +337,7 @@ func filterEvent(ctx context.Context, filter *eventingv1.TriggerFilter, event cl // triggerFilterAttribute returns the filter attribute value for a given `attributeName`. If it doesn't not exist, // returns the any value filter. -func triggerFilterAttribute(filter *eventingv1.TriggerFilter, attributeName string) string { +func triggerFilterAttribute(filter *eventingv1.FilterSpec, attributeName string) string { attributeValue := eventingv1.TriggerAnyFilter if filter != nil { attrs := map[string]string(filter.Attributes) diff --git a/pkg/broker/filter/filter_handler_test.go b/pkg/broker/filter/filter_handler_test.go index cccfa5b4003..d0d6147da10 100644 --- a/pkg/broker/filter/filter_handler_test.go +++ b/pkg/broker/filter/filter_handler_test.go @@ -555,8 +555,8 @@ func (h *fakeHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { } } -func makeTriggerFilterWithAttributes(t, s string) *eventingv1.TriggerFilter { - return &eventingv1.TriggerFilter{ +func makeTriggerFilterWithAttributes(t, s string) *eventingv1.FilterSpec { + return &eventingv1.FilterSpec{ Attributes: eventingv1.TriggerFilterAttributes{ "type": t, "source": s, @@ -564,8 +564,8 @@ func makeTriggerFilterWithAttributes(t, s string) *eventingv1.TriggerFilter { } } -func makeTriggerFilterWithAttributesAndExtension(t, s, e string) *eventingv1.TriggerFilter { - return &eventingv1.TriggerFilter{ +func makeTriggerFilterWithAttributesAndExtension(t, s, e string) *eventingv1.FilterSpec { + return &eventingv1.FilterSpec{ Attributes: eventingv1.TriggerFilterAttributes{ "type": t, "source": s, @@ -574,13 +574,13 @@ func makeTriggerFilterWithAttributesAndExtension(t, s, e string) *eventingv1.Tri } } -func makeTriggerWithDifferentUID(filter *eventingv1.TriggerFilter) *eventingv1.Trigger { +func makeTriggerWithDifferentUID(filter *eventingv1.FilterSpec) *eventingv1.Trigger { t := makeTrigger(filter) t.ObjectMeta.UID = "wrongone" return t } -func makeTrigger(filter *eventingv1.TriggerFilter) *eventingv1.Trigger { +func makeTrigger(filter *eventingv1.FilterSpec) *eventingv1.Trigger { return &eventingv1.Trigger{ TypeMeta: metav1.TypeMeta{ APIVersion: "eventing.knative.dev/v1beta1", diff --git a/pkg/reconciler/broker/trigger/trigger_test.go b/pkg/reconciler/broker/trigger/trigger_test.go index 06cd7ac0cad..1ae3092c30c 100644 --- a/pkg/reconciler/broker/trigger/trigger_test.go +++ b/pkg/reconciler/broker/trigger/trigger_test.go @@ -928,7 +928,7 @@ func makeTrigger(subscriberNamespace string) *eventingv1.Trigger { }, Spec: eventingv1.TriggerSpec{ Broker: brokerName, - Filter: &eventingv1.TriggerFilter{ + Filter: &eventingv1.FilterSpec{ Attributes: map[string]string{"Source": "Any", "Type": "Any"}, }, Subscriber: duckv1.Destination{ diff --git a/test/e2e/helpers/broker_test_helper.go b/test/e2e/helpers/broker_test_helper.go index de6a8ef4212..430102fe89b 100644 --- a/test/e2e/helpers/broker_test_helper.go +++ b/test/e2e/helpers/broker_test_helper.go @@ -148,7 +148,7 @@ func TestBrokerWithManyTriggers(ctx context.Context, t *testing.T, brokerCreator // to set in the subscriber and services pod // The attributes in these test cases will be used as assertions on the receivers eventFilters []eventTestCase - // TriggerFilter with DeprecatedSourceAndType or not + // FilterSpec with DeprecatedSourceAndType or not deprecatedTriggerFilter bool }{ { diff --git a/test/lib/resources/eventing.go b/test/lib/resources/eventing.go index 6c71ca99bc3..4573fac185d 100644 --- a/test/lib/resources/eventing.go +++ b/test/lib/resources/eventing.go @@ -222,7 +222,7 @@ func WithAttributesTriggerFilter(eventSource, eventType string, extensions map[s attrs[k] = fmt.Sprintf("%v", v) } return func(t *eventingv1.Trigger) { - t.Spec.Filter = &eventingv1.TriggerFilter{ + t.Spec.Filter = &eventingv1.FilterSpec{ Attributes: eventingv1.TriggerFilterAttributes(attrs), } } diff --git a/test/rekt/features/broker/control_plane.go b/test/rekt/features/broker/control_plane.go index 4262fe6f005..22a403dc542 100644 --- a/test/rekt/features/broker/control_plane.go +++ b/test/rekt/features/broker/control_plane.go @@ -255,7 +255,7 @@ func ControlPlaneTrigger_WithInvalidFilters(brokerName string) *feature.Feature trigger := getTrigger(ctx, t) if trigger.Spec.Filter == nil { - trigger.Spec.Filter = &eventingv1.TriggerFilter{ + trigger.Spec.Filter = &eventingv1.FilterSpec{ Attributes: map[string]string{}, } } else if trigger.Spec.Filter.Attributes == nil { From 1a3e2ae33ee73c782eb3ab1490765bef2a160318 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 6 Apr 2021 16:36:34 +0200 Subject: [PATCH 2/6] Typo Added definition to the OpenAPI Signed-off-by: Francesco Guardiani --- config/core/resources/trigger.yaml | 36 +++++++++++++++++++++++++++ pkg/apis/eventing/v1/trigger_types.go | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/config/core/resources/trigger.yaml b/config/core/resources/trigger.yaml index 02ff5987093..59187a51810 100644 --- a/config/core/resources/trigger.yaml +++ b/config/core/resources/trigger.yaml @@ -100,6 +100,42 @@ spec: description: 'Attributes filters events by exact match on event context attributes. Each key in the map is compared with the equivalent key in the event context. An event passes the filter if all values are equal to the specified values. Nested context attributes are not supported as keys. Only string values are supported. ' type: object x-kubernetes-preserve-unknown-fields: true + exact: + description: 'Exact evaluates to true if the value of the matching CloudEvents attribute is matches exactly the String value specified (case sensitive). Exact must contain exactly one property, where the key is the name of the CloudEvents attribute to be matched, and its value is the String value to use in the comparison. The attribute name and value specified in the filter express cannot be be empty strings.' + type: object + x-kubernetes-preserve-unknown-fields: true + prefix: + description: 'Prefix evaluates to true if the value of the matching CloudEvents attribute starts with the String value specified (case sensitive). Prefix must contain exactly one property, where the key is the name of the CloudEvents attribute to be matched, and its value is the String value to use in the comparison. The attribute name and value specified in the filter express cannot be be empty strings.' + type: object + x-kubernetes-preserve-unknown-fields: true + suffix: + description: 'Suffix evaluates to true if the value of the matching CloudEvents attribute ends with the String value specified (case sensitive). | Suffix must contain exactly one property, where the key is the name of the CloudEvents attribute to be matched, and its value is the String value to use in the comparison. he attribute name and value specified in the filter express cannot be be empty strings.' + type: object + x-kubernetes-preserve-unknown-fields: true + not: + description: 'Not evaluates to true if the nested expression evaluates to false.' + type: object + # Because kube doesn't allow to use $ref, we can't recursively define this schema. + x-kubernetes-preserve-unknown-fields: true + all: + description: 'All evaluates to true if all the nested expressions evaluate to true. All must contain at least one filter expression.' + type: array + items: + # Because kube doesn't allow to use $ref, we can't recursively reference to the filter schema. + type: object + description: "Sub schema" + x-kubernetes-preserve-unknown-fields: true + any: + description: 'Any evaluates to true if at least one of the nested expressions evaluate to true. Any must contain at least one filter expression.' + type: array + items: + # Because kube doesn't allow to use $ref, we can't recursively reference to the filter schema. + type: object + description: "Sub schema" + x-kubernetes-preserve-unknown-fields: true + # This allows extension filter dialects + additionalProperties: true + x-kubernetes-preserve-unknown-fields: true subscriber: description: Subscriber is the addressable that receives events from the Broker that pass the Filter. It is required. type: object diff --git a/pkg/apis/eventing/v1/trigger_types.go b/pkg/apis/eventing/v1/trigger_types.go index b4909cce7f6..add742ae616 100644 --- a/pkg/apis/eventing/v1/trigger_types.go +++ b/pkg/apis/eventing/v1/trigger_types.go @@ -93,7 +93,7 @@ type TriggerSpec struct { Delivery *eventingduckv1.DeliverySpec `json:"delivery,omitempty"` } -// FilterSpec allows to define a filter +// FilterSpec allows to define a filter expression. // If multiple filters are specified, then the same semantics of FilterSpec.All is applied. // If no filter dialect or empty object is specified, then the filter always accept the events. type FilterSpec struct { From a12fd62db9d10dc243519be67c2f4adaa230f07e Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Wed, 7 Apr 2021 09:42:40 +0200 Subject: [PATCH 3/6] Removed blank file Signed-off-by: Francesco Guardiani --- pkg/apis/eventing/v1beta1/trigger_conversion.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 pkg/apis/eventing/v1beta1/trigger_conversion.go diff --git a/pkg/apis/eventing/v1beta1/trigger_conversion.go b/pkg/apis/eventing/v1beta1/trigger_conversion.go deleted file mode 100644 index e69de29bb2d..00000000000 From 9bbe9f34abb0ee72397e3a2e1cd5410b08c9480e Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Wed, 7 Apr 2021 09:50:47 +0200 Subject: [PATCH 4/6] Removed useless Sprintf Signed-off-by: Francesco Guardiani --- pkg/apis/eventing/v1/trigger_validation.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/apis/eventing/v1/trigger_validation.go b/pkg/apis/eventing/v1/trigger_validation.go index 7b27d14147c..08286ee18b5 100644 --- a/pkg/apis/eventing/v1/trigger_validation.go +++ b/pkg/apis/eventing/v1/trigger_validation.go @@ -108,7 +108,7 @@ func validateFilterSpec(filter *FilterSpec, path []string) (errs []*apis.FieldEr if filter.Prefix != nil { if len(filter.Prefix) != 1 { errs = append(errs, &apis.FieldError{ - Message: fmt.Sprintf("Prefix can have only one key-value"), + Message: "Prefix can have only one key-value", Paths: []string{strings.Join(append(path, "prefix"), ".")}, }) } @@ -126,7 +126,7 @@ func validateFilterSpec(filter *FilterSpec, path []string) (errs []*apis.FieldEr if filter.Suffix != nil { if len(filter.Suffix) != 1 { errs = append(errs, &apis.FieldError{ - Message: fmt.Sprintf("Suffix can have only one key-value"), + Message: "Suffix can have only one key-value", Paths: []string{strings.Join(append(path, "suffix"), ".")}, }) } @@ -144,7 +144,7 @@ func validateFilterSpec(filter *FilterSpec, path []string) (errs []*apis.FieldEr if filter.All != nil { if len(filter.All) < 1 { errs = append(errs, &apis.FieldError{ - Message: fmt.Sprintf("All must contain at least one nested filter"), + Message: "All must contain at least one nested filter", Paths: []string{strings.Join(append(path, "all"), ".")}, }) } @@ -159,7 +159,7 @@ func validateFilterSpec(filter *FilterSpec, path []string) (errs []*apis.FieldEr if filter.Any != nil { if len(filter.Any) < 1 { errs = append(errs, &apis.FieldError{ - Message: fmt.Sprintf("Any must contain at least one nested filter"), + Message: "Any must contain at least one nested filter", Paths: []string{strings.Join(append(path, "any"), ".")}, }) } From 20a98902a94ca3b5054ab16786e8a168dd124058 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Wed, 14 Apr 2021 11:12:01 +0200 Subject: [PATCH 5/6] Reverted renaming Signed-off-by: Francesco Guardiani --- pkg/apis/eventing/v1/trigger_defaults.go | 2 +- pkg/apis/eventing/v1/trigger_defaults_test.go | 2 +- pkg/apis/eventing/v1/trigger_types.go | 16 ++--- pkg/apis/eventing/v1/trigger_validation.go | 4 +- .../eventing/v1/trigger_validation_test.go | 68 +++++++++---------- pkg/apis/eventing/v1/zz_generated.deepcopy.go | 16 ++--- pkg/broker/filter/filter_handler.go | 4 +- pkg/broker/filter/filter_handler_test.go | 12 ++-- pkg/reconciler/broker/trigger/trigger_test.go | 2 +- test/e2e/helpers/broker_test_helper.go | 2 +- test/lib/resources/eventing.go | 2 +- test/rekt/features/broker/control_plane.go | 2 +- 12 files changed, 66 insertions(+), 66 deletions(-) diff --git a/pkg/apis/eventing/v1/trigger_defaults.go b/pkg/apis/eventing/v1/trigger_defaults.go index 46649d9f97e..4c90811f355 100644 --- a/pkg/apis/eventing/v1/trigger_defaults.go +++ b/pkg/apis/eventing/v1/trigger_defaults.go @@ -35,7 +35,7 @@ func (t *Trigger) SetDefaults(ctx context.Context) { func (ts *TriggerSpec) SetDefaults(ctx context.Context) { // Make a default filter that allows anything. if ts.Filter == nil { - ts.Filter = &FilterSpec{} + ts.Filter = &TriggerFilter{} } // Default the Subscriber namespace ts.Subscriber.SetDefaults(ctx) diff --git a/pkg/apis/eventing/v1/trigger_defaults_test.go b/pkg/apis/eventing/v1/trigger_defaults_test.go index 02d1a532826..0d01bc10ea1 100644 --- a/pkg/apis/eventing/v1/trigger_defaults_test.go +++ b/pkg/apis/eventing/v1/trigger_defaults_test.go @@ -31,7 +31,7 @@ var ( defaultBroker = "default" otherBroker = "other_broker" namespace = "testnamespace" - emptyTriggerFilter = &FilterSpec{} + emptyTriggerFilter = &TriggerFilter{} defaultTrigger = Trigger{Spec: TriggerSpec{Filter: emptyTriggerFilter}} ) diff --git a/pkg/apis/eventing/v1/trigger_types.go b/pkg/apis/eventing/v1/trigger_types.go index add742ae616..51b3bb016bd 100644 --- a/pkg/apis/eventing/v1/trigger_types.go +++ b/pkg/apis/eventing/v1/trigger_types.go @@ -79,10 +79,10 @@ type TriggerSpec struct { // Filter is the filter to apply against all events from the Broker. Only events that pass this // filter will be sent to the Subscriber. If not specified, will default to allowing all events. - // If multiple filters are specified, then the same semantics of FilterSpec.All is applied. + // If multiple filters are specified, then the same semantics of TriggerFilter.All is applied. // // +optional - Filter *FilterSpec `json:"filter,omitempty"` + Filter *TriggerFilter `json:"filter,omitempty"` // Subscriber is the addressable that receives events from the Broker that pass the Filter. It // is required. @@ -93,10 +93,10 @@ type TriggerSpec struct { Delivery *eventingduckv1.DeliverySpec `json:"delivery,omitempty"` } -// FilterSpec allows to define a filter expression. -// If multiple filters are specified, then the same semantics of FilterSpec.All is applied. +// TriggerFilter allows to define a filter expression. +// If multiple filters are specified, then the same semantics of TriggerFilter.All is applied. // If no filter dialect or empty object is specified, then the filter always accept the events. -type FilterSpec struct { +type TriggerFilter struct { // Attributes filters events by exact match on event context attributes. // Each key in the map is compared with the equivalent key in the event // context. An event passes the filter if all values are equal to the @@ -112,19 +112,19 @@ type FilterSpec struct { // All must contain at least one filter expression. // // +optional - All []FilterSpec `json:"all,omitempty"` + All []TriggerFilter `json:"all,omitempty"` // Any evaluates to true if at least one of the nested expressions evaluate to true. // // Any must contain at least one filter expression. // // +optional - Any []FilterSpec `json:"any,omitempty"` + Any []TriggerFilter `json:"any,omitempty"` // Not evaluates to true if the nested expression evaluates to false. // // +optional - Not *FilterSpec `json:"not,omitempty"` + Not *TriggerFilter `json:"not,omitempty"` // Exact evaluates to true if the value of the matching CloudEvents attribute is matches exactly the String value specified (case sensitive). // Exact must contain exactly one property, where the key is the name of the CloudEvents attribute to be matched, and its value is the String value to use in the comparison. diff --git a/pkg/apis/eventing/v1/trigger_validation.go b/pkg/apis/eventing/v1/trigger_validation.go index 08286ee18b5..5c92692a33f 100644 --- a/pkg/apis/eventing/v1/trigger_validation.go +++ b/pkg/apis/eventing/v1/trigger_validation.go @@ -71,7 +71,7 @@ func (ts *TriggerSpec) Validate(ctx context.Context) *apis.FieldError { return errs } -func validateFilterSpec(filter *FilterSpec, path []string) (errs []*apis.FieldError) { +func validateFilterSpec(filter *TriggerFilter, path []string) (errs []*apis.FieldError) { if filter == nil { return nil } @@ -90,7 +90,7 @@ func validateFilterSpec(filter *FilterSpec, path []string) (errs []*apis.FieldEr if filter.Exact != nil { if len(filter.Exact) != 1 { errs = append(errs, &apis.FieldError{ - Message: fmt.Sprintf("Exact can have only one key-value"), + Message: "Exact can have only one key-value", Paths: []string{strings.Join(append(path, "exact"), ".")}, }) } diff --git a/pkg/apis/eventing/v1/trigger_validation_test.go b/pkg/apis/eventing/v1/trigger_validation_test.go index 2afa0636a1d..10dc180ebd5 100644 --- a/pkg/apis/eventing/v1/trigger_validation_test.go +++ b/pkg/apis/eventing/v1/trigger_validation_test.go @@ -31,8 +31,8 @@ import ( ) var ( - validEmptyFilter = &FilterSpec{} - validAttributesFilter = &FilterSpec{ + validEmptyFilter = &TriggerFilter{} + validAttributesFilter = &TriggerFilter{ Attributes: TriggerFilterAttributes{ "type": "other_type", "source": "other_source", @@ -369,7 +369,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "missing attributes keys, match all", ts: &TriggerSpec{ Broker: "test_broker", - Filter: &FilterSpec{ + Filter: &TriggerFilter{ Attributes: TriggerFilterAttributes{}, }, Subscriber: validSubscriber, @@ -379,7 +379,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "invalid attribute name, start with number", ts: &TriggerSpec{ Broker: "test_broker", - Filter: &FilterSpec{ + Filter: &TriggerFilter{ Attributes: TriggerFilterAttributes{ "0invalid": "my-value", }, @@ -394,7 +394,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "invalid attribute name, capital letters", ts: &TriggerSpec{ Broker: "test_broker", - Filter: &FilterSpec{ + Filter: &TriggerFilter{ Attributes: TriggerFilterAttributes{ "invALID": "my-value", }, @@ -478,17 +478,17 @@ func TestTriggerSpecValidation(t *testing.T) { func TestFilterSpecValidation(t *testing.T) { tests := []struct { name string - filter *FilterSpec + filter *TriggerFilter want *apis.FieldError }{{ name: "missing attributes keys, match all", - filter: &FilterSpec{ + filter: &TriggerFilter{ Attributes: TriggerFilterAttributes{}, }, want: &apis.FieldError{}, }, { name: "invalid attribute name, start with number", - filter: &FilterSpec{ + filter: &TriggerFilter{ Attributes: TriggerFilterAttributes{ "0invalid": "my-value", }, @@ -499,7 +499,7 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "invalid attribute name, capital letters", - filter: &FilterSpec{ + filter: &TriggerFilter{ Attributes: TriggerFilterAttributes{ "invALID": "my-value", }, @@ -522,7 +522,7 @@ func TestFilterSpecValidation(t *testing.T) { want: &apis.FieldError{}, }, { name: "exact filter contains more than one attribute", - filter: &FilterSpec{ + filter: &TriggerFilter{ Exact: map[string]string{ "myext": "abc", "anotherext": "xyz", @@ -534,7 +534,7 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "exact filter contains invalid attribute name", - filter: &FilterSpec{ + filter: &TriggerFilter{ Exact: map[string]string{ "invALID": "abc", }, @@ -545,7 +545,7 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "valid exact filter", - filter: &FilterSpec{ + filter: &TriggerFilter{ Exact: map[string]string{ "valid": "abc", }, @@ -553,7 +553,7 @@ func TestFilterSpecValidation(t *testing.T) { want: &apis.FieldError{}, }, { name: "suffix filter contains more than one attribute", - filter: &FilterSpec{ + filter: &TriggerFilter{ Suffix: map[string]string{ "myext": "abc", "anotherext": "xyz", @@ -565,7 +565,7 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "suffix filter contains invalid attribute name", - filter: &FilterSpec{ + filter: &TriggerFilter{ Suffix: map[string]string{ "invALID": "abc", }, @@ -576,7 +576,7 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "valid suffix filter", - filter: &FilterSpec{ + filter: &TriggerFilter{ Suffix: map[string]string{ "valid": "abc", }, @@ -584,7 +584,7 @@ func TestFilterSpecValidation(t *testing.T) { want: &apis.FieldError{}, }, { name: "prefix filter contains more than one attribute", - filter: &FilterSpec{ + filter: &TriggerFilter{ Prefix: map[string]string{ "myext": "abc", "anotherext": "xyz", @@ -596,7 +596,7 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "prefix filter contains invalid attribute name", - filter: &FilterSpec{ + filter: &TriggerFilter{ Prefix: map[string]string{ "invALID": "abc", }, @@ -607,7 +607,7 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "valid prefix filter", - filter: &FilterSpec{ + filter: &TriggerFilter{ Prefix: map[string]string{ "valid": "abc", }, @@ -615,14 +615,14 @@ func TestFilterSpecValidation(t *testing.T) { want: &apis.FieldError{}, }, { name: "not nested expression is valid", - filter: &FilterSpec{ + filter: &TriggerFilter{ Not: validAttributesFilter, }, want: &apis.FieldError{}, }, { name: "not nested expression is invalid", - filter: &FilterSpec{ - Not: &FilterSpec{ + filter: &TriggerFilter{ + Not: &TriggerFilter{ Prefix: map[string]string{ "invALID": "abc", }, @@ -634,8 +634,8 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "all filter is empty", - filter: &FilterSpec{ - All: []FilterSpec{}, + filter: &TriggerFilter{ + All: []TriggerFilter{}, }, want: &apis.FieldError{ Message: `All must contain at least one nested filter`, @@ -643,8 +643,8 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "all filter is valid", - filter: &FilterSpec{ - All: []FilterSpec{ + filter: &TriggerFilter{ + All: []TriggerFilter{ *validAttributesFilter, {Exact: map[string]string{"myattr": "myval"}}, }, @@ -652,8 +652,8 @@ func TestFilterSpecValidation(t *testing.T) { want: &apis.FieldError{}, }, { name: "all filter sub expression is invalid", - filter: &FilterSpec{ - All: []FilterSpec{ + filter: &TriggerFilter{ + All: []TriggerFilter{ *validAttributesFilter, {Exact: map[string]string{"myattr": "myval"}}, {Prefix: map[string]string{ @@ -667,8 +667,8 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "any filter is empty", - filter: &FilterSpec{ - Any: []FilterSpec{}, + filter: &TriggerFilter{ + Any: []TriggerFilter{}, }, want: &apis.FieldError{ Message: `Any must contain at least one nested filter`, @@ -676,8 +676,8 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "any filter is valid", - filter: &FilterSpec{ - Any: []FilterSpec{ + filter: &TriggerFilter{ + Any: []TriggerFilter{ *validAttributesFilter, {Exact: map[string]string{"myattr": "myval"}}, }, @@ -685,8 +685,8 @@ func TestFilterSpecValidation(t *testing.T) { want: &apis.FieldError{}, }, { name: "any filter sub expression is invalid", - filter: &FilterSpec{ - Any: []FilterSpec{ + filter: &TriggerFilter{ + Any: []TriggerFilter{ *validAttributesFilter, {Exact: map[string]string{"myattr": "myval"}}, {Prefix: map[string]string{ @@ -700,7 +700,7 @@ func TestFilterSpecValidation(t *testing.T) { }, }, { name: "raw extension expression is valid", - filter: &FilterSpec{ + filter: &TriggerFilter{ Extensions: map[string]*runtime.RawExtension{ "juel": {Raw: []byte("\"myExpressionUsingJUEL\"")}, }, diff --git a/pkg/apis/eventing/v1/zz_generated.deepcopy.go b/pkg/apis/eventing/v1/zz_generated.deepcopy.go index 9ec021cc105..e0c6d1d5ddc 100644 --- a/pkg/apis/eventing/v1/zz_generated.deepcopy.go +++ b/pkg/apis/eventing/v1/zz_generated.deepcopy.go @@ -133,7 +133,7 @@ func (in *BrokerStatus) DeepCopy() *BrokerStatus { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *FilterSpec) DeepCopyInto(out *FilterSpec) { +func (in *TriggerFilter) DeepCopyInto(out *TriggerFilter) { *out = *in if in.Attributes != nil { in, out := &in.Attributes, &out.Attributes @@ -144,21 +144,21 @@ func (in *FilterSpec) DeepCopyInto(out *FilterSpec) { } if in.All != nil { in, out := &in.All, &out.All - *out = make([]FilterSpec, len(*in)) + *out = make([]TriggerFilter, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.Any != nil { in, out := &in.Any, &out.Any - *out = make([]FilterSpec, len(*in)) + *out = make([]TriggerFilter, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.Not != nil { in, out := &in.Not, &out.Not - *out = new(FilterSpec) + *out = new(TriggerFilter) (*in).DeepCopyInto(*out) } if in.Exact != nil { @@ -200,12 +200,12 @@ func (in *FilterSpec) DeepCopyInto(out *FilterSpec) { return } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FilterSpec. -func (in *FilterSpec) DeepCopy() *FilterSpec { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TriggerFilter. +func (in *TriggerFilter) DeepCopy() *TriggerFilter { if in == nil { return nil } - out := new(FilterSpec) + out := new(TriggerFilter) in.DeepCopyInto(out) return out } @@ -298,7 +298,7 @@ func (in *TriggerSpec) DeepCopyInto(out *TriggerSpec) { *out = *in if in.Filter != nil { in, out := &in.Filter, &out.Filter - *out = new(FilterSpec) + *out = new(TriggerFilter) (*in).DeepCopyInto(*out) } in.Subscriber.DeepCopyInto(&out.Subscriber) diff --git a/pkg/broker/filter/filter_handler.go b/pkg/broker/filter/filter_handler.go index 4075d6bc854..6314fe6aac0 100644 --- a/pkg/broker/filter/filter_handler.go +++ b/pkg/broker/filter/filter_handler.go @@ -324,7 +324,7 @@ func (h *Handler) getTrigger(ref path.NamespacedNameUID) (*eventingv1.Trigger, e return t, nil } -func filterEvent(ctx context.Context, filter *eventingv1.FilterSpec, event cloudevents.Event) eventfilter.FilterResult { +func filterEvent(ctx context.Context, filter *eventingv1.TriggerFilter, event cloudevents.Event) eventfilter.FilterResult { if filter == nil { return eventfilter.NoFilter } @@ -337,7 +337,7 @@ func filterEvent(ctx context.Context, filter *eventingv1.FilterSpec, event cloud // triggerFilterAttribute returns the filter attribute value for a given `attributeName`. If it doesn't not exist, // returns the any value filter. -func triggerFilterAttribute(filter *eventingv1.FilterSpec, attributeName string) string { +func triggerFilterAttribute(filter *eventingv1.TriggerFilter, attributeName string) string { attributeValue := eventingv1.TriggerAnyFilter if filter != nil { attrs := map[string]string(filter.Attributes) diff --git a/pkg/broker/filter/filter_handler_test.go b/pkg/broker/filter/filter_handler_test.go index d0d6147da10..cccfa5b4003 100644 --- a/pkg/broker/filter/filter_handler_test.go +++ b/pkg/broker/filter/filter_handler_test.go @@ -555,8 +555,8 @@ func (h *fakeHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { } } -func makeTriggerFilterWithAttributes(t, s string) *eventingv1.FilterSpec { - return &eventingv1.FilterSpec{ +func makeTriggerFilterWithAttributes(t, s string) *eventingv1.TriggerFilter { + return &eventingv1.TriggerFilter{ Attributes: eventingv1.TriggerFilterAttributes{ "type": t, "source": s, @@ -564,8 +564,8 @@ func makeTriggerFilterWithAttributes(t, s string) *eventingv1.FilterSpec { } } -func makeTriggerFilterWithAttributesAndExtension(t, s, e string) *eventingv1.FilterSpec { - return &eventingv1.FilterSpec{ +func makeTriggerFilterWithAttributesAndExtension(t, s, e string) *eventingv1.TriggerFilter { + return &eventingv1.TriggerFilter{ Attributes: eventingv1.TriggerFilterAttributes{ "type": t, "source": s, @@ -574,13 +574,13 @@ func makeTriggerFilterWithAttributesAndExtension(t, s, e string) *eventingv1.Fil } } -func makeTriggerWithDifferentUID(filter *eventingv1.FilterSpec) *eventingv1.Trigger { +func makeTriggerWithDifferentUID(filter *eventingv1.TriggerFilter) *eventingv1.Trigger { t := makeTrigger(filter) t.ObjectMeta.UID = "wrongone" return t } -func makeTrigger(filter *eventingv1.FilterSpec) *eventingv1.Trigger { +func makeTrigger(filter *eventingv1.TriggerFilter) *eventingv1.Trigger { return &eventingv1.Trigger{ TypeMeta: metav1.TypeMeta{ APIVersion: "eventing.knative.dev/v1beta1", diff --git a/pkg/reconciler/broker/trigger/trigger_test.go b/pkg/reconciler/broker/trigger/trigger_test.go index 1ae3092c30c..06cd7ac0cad 100644 --- a/pkg/reconciler/broker/trigger/trigger_test.go +++ b/pkg/reconciler/broker/trigger/trigger_test.go @@ -928,7 +928,7 @@ func makeTrigger(subscriberNamespace string) *eventingv1.Trigger { }, Spec: eventingv1.TriggerSpec{ Broker: brokerName, - Filter: &eventingv1.FilterSpec{ + Filter: &eventingv1.TriggerFilter{ Attributes: map[string]string{"Source": "Any", "Type": "Any"}, }, Subscriber: duckv1.Destination{ diff --git a/test/e2e/helpers/broker_test_helper.go b/test/e2e/helpers/broker_test_helper.go index 430102fe89b..de6a8ef4212 100644 --- a/test/e2e/helpers/broker_test_helper.go +++ b/test/e2e/helpers/broker_test_helper.go @@ -148,7 +148,7 @@ func TestBrokerWithManyTriggers(ctx context.Context, t *testing.T, brokerCreator // to set in the subscriber and services pod // The attributes in these test cases will be used as assertions on the receivers eventFilters []eventTestCase - // FilterSpec with DeprecatedSourceAndType or not + // TriggerFilter with DeprecatedSourceAndType or not deprecatedTriggerFilter bool }{ { diff --git a/test/lib/resources/eventing.go b/test/lib/resources/eventing.go index 4573fac185d..6c71ca99bc3 100644 --- a/test/lib/resources/eventing.go +++ b/test/lib/resources/eventing.go @@ -222,7 +222,7 @@ func WithAttributesTriggerFilter(eventSource, eventType string, extensions map[s attrs[k] = fmt.Sprintf("%v", v) } return func(t *eventingv1.Trigger) { - t.Spec.Filter = &eventingv1.FilterSpec{ + t.Spec.Filter = &eventingv1.TriggerFilter{ Attributes: eventingv1.TriggerFilterAttributes(attrs), } } diff --git a/test/rekt/features/broker/control_plane.go b/test/rekt/features/broker/control_plane.go index 22a403dc542..4262fe6f005 100644 --- a/test/rekt/features/broker/control_plane.go +++ b/test/rekt/features/broker/control_plane.go @@ -255,7 +255,7 @@ func ControlPlaneTrigger_WithInvalidFilters(brokerName string) *feature.Feature trigger := getTrigger(ctx, t) if trigger.Spec.Filter == nil { - trigger.Spec.Filter = &eventingv1.FilterSpec{ + trigger.Spec.Filter = &eventingv1.TriggerFilter{ Attributes: map[string]string{}, } } else if trigger.Spec.Filter.Attributes == nil { From 89db05fe22e3978f223c6414484d296837bd3891 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Wed, 14 Apr 2021 11:24:35 +0200 Subject: [PATCH 6/6] Update codegen Signed-off-by: Francesco Guardiani --- pkg/apis/eventing/v1/zz_generated.deepcopy.go | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/pkg/apis/eventing/v1/zz_generated.deepcopy.go b/pkg/apis/eventing/v1/zz_generated.deepcopy.go index e0c6d1d5ddc..cbdd7cb8780 100644 --- a/pkg/apis/eventing/v1/zz_generated.deepcopy.go +++ b/pkg/apis/eventing/v1/zz_generated.deepcopy.go @@ -132,6 +132,34 @@ func (in *BrokerStatus) DeepCopy() *BrokerStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Trigger) DeepCopyInto(out *Trigger) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Trigger. +func (in *Trigger) DeepCopy() *Trigger { + if in == nil { + return nil + } + out := new(Trigger) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *Trigger) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TriggerFilter) DeepCopyInto(out *TriggerFilter) { *out = *in @@ -210,34 +238,6 @@ func (in *TriggerFilter) DeepCopy() *TriggerFilter { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Trigger) DeepCopyInto(out *Trigger) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) - in.Status.DeepCopyInto(&out.Status) - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Trigger. -func (in *Trigger) DeepCopy() *Trigger { - if in == nil { - return nil - } - out := new(Trigger) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *Trigger) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in TriggerFilterAttributes) DeepCopyInto(out *TriggerFilterAttributes) { {