diff --git a/config/core/configmaps/features.yaml b/config/core/configmaps/features.yaml index e0159504a96..0b351176338 100644 --- a/config/core/configmaps/features.yaml +++ b/config/core/configmaps/features.yaml @@ -39,3 +39,8 @@ data: # ALPHA feature: The subscriber-strict flag force subscriptions to define a subscriber # For more details: https://github.com/knative/eventing/issues/5756 strict-subscriber: "disabled" + + # ALPHA feature: The new-trigger-filters flag allows you to use the new `filters` field + # in Trigger objects with its rich filtering capabilities. + # For more details: https://github.com/knative/eventing/issues/5204 + new-trigger-filters: "disabled" diff --git a/config/core/resources/trigger.yaml b/config/core/resources/trigger.yaml index 14e27549cd2..8c81d76716b 100644 --- a/config/core/resources/trigger.yaml +++ b/config/core/resources/trigger.yaml @@ -53,6 +53,7 @@ spec: spec: description: Spec defines the desired state of the Trigger. type: object + x-kubernetes-preserve-unknown-fields: true properties: broker: description: Broker is the broker that this trigger receives events from. diff --git a/docs/eventing-api.md b/docs/eventing-api.md index 37cbe032b8e..d4e35db0acb 100644 --- a/docs/eventing-api.md +++ b/docs/eventing-api.md @@ -1764,6 +1764,29 @@ filter will be sent to the Subscriber. If not specified, will default to allowin +filters
+ + +[]SubscriptionsAPIFilter + + + + +(Optional) +

Filters is an experimental field that conforms to the CNCF CloudEvents Subscriptions +API. It’s an array of filter expressions that evaluate to true or false. +If any filter expression in the array evaluates to false, the event MUST +NOT be sent to the Subscriber. If all the filter expressions in the array +evaluate to true, the event MUST be attempted to be delivered. Absence of +a filter or empty array implies a value of true. In the event of users +specifying both Filter and Filters, then the latter will override the former. +This will allow users to try out the effect of the new Filters field +without compromising the existing attribute-based Filter and try it out on existing +Trigger objects.

+ + + + subscriber
@@ -1772,8 +1795,8 @@ knative.dev/pkg/apis/duck/v1.Destination -

Subscriber is the addressable that receives events from the Broker that pass the Filter. It -is required.

+

Subscriber is the addressable that receives events from the Broker that pass +the Filter. It is required.

@@ -1927,6 +1950,140 @@ resolved delivery options.

+

SubscriptionsAPIFilter +

+

+(Appears on:SubscriptionsAPIFilter, TriggerSpec) +

+

+

SubscriptionsAPIFilter allows defining a filter expression using CloudEvents +Subscriptions API. If multiple filters are specified, then the same semantics +of SubscriptionsAPIFilter.All is applied. If no filter dialect or empty +object is specified, then the filter always accept the events.

+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FieldDescription
+all
+ + +[]SubscriptionsAPIFilter + + +
+(Optional) +

All evaluates to true if all the nested expressions evaluate to true. +It must contain at least one filter expression.

+
+any
+ + +[]SubscriptionsAPIFilter + + +
+(Optional) +

Any evaluates to true if at least one of the nested expressions evaluates +to true. It must contain at least one filter expression.

+
+not
+ + +SubscriptionsAPIFilter + + +
+(Optional) +

Not evaluates to true if the nested expression evaluates to false.

+
+exact
+ +map[string]string + +
+(Optional) +

Exact evaluates to true if the value of the matching CloudEvents +attribute 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 +expression cannot be empty strings.

+
+prefix
+ +map[string]string + +
+(Optional) +

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 +expression cannot be empty strings.

+
+suffix
+ +map[string]string + +
+(Optional) +

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 +expression cannot be empty strings.

+
+Extensions
+ +map[string]*k8s.io/apimachinery/pkg/runtime.RawExtension + +
+

+(Members of Extensions are embedded into this type.) +

+(Optional) +

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.

+

TriggerFilter

@@ -1956,8 +2113,8 @@ TriggerFilterAttributes

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.

+specified values. Nested context attributes are not supported as keys. Only +string values are supported.

@@ -1969,8 +2126,8 @@ specified values.

TriggerFilterAttributes is a map of context attribute names to values for -filtering by equality. Only exact matches will pass the filter. You can use the value “ -to indicate all strings match.

+filtering by equality. Only exact matches will pass the filter. You can use +the value “ to indicate all strings match.

TriggerSpec

@@ -2015,6 +2172,29 @@ filter will be sent to the Subscriber. If not specified, will default to allowin +filters
+ + +[]SubscriptionsAPIFilter + + + + +(Optional) +

Filters is an experimental field that conforms to the CNCF CloudEvents Subscriptions +API. It’s an array of filter expressions that evaluate to true or false. +If any filter expression in the array evaluates to false, the event MUST +NOT be sent to the Subscriber. If all the filter expressions in the array +evaluate to true, the event MUST be attempted to be delivered. Absence of +a filter or empty array implies a value of true. In the event of users +specifying both Filter and Filters, then the latter will override the former. +This will allow users to try out the effect of the new Filters field +without compromising the existing attribute-based Filter and try it out on existing +Trigger objects.

+ + + + subscriber
@@ -2023,8 +2203,8 @@ knative.dev/pkg/apis/duck/v1.Destination -

Subscriber is the addressable that receives events from the Broker that pass the Filter. It -is required.

+

Subscriber is the addressable that receives events from the Broker that pass +the Filter. It is required.

diff --git a/pkg/apis/eventing/v1/trigger_types.go b/pkg/apis/eventing/v1/trigger_types.go index aa842be3f80..25dd0f0ac03 100644 --- a/pkg/apis/eventing/v1/trigger_types.go +++ b/pkg/apis/eventing/v1/trigger_types.go @@ -83,8 +83,22 @@ type TriggerSpec struct { // +optional Filter *TriggerFilter `json:"filter,omitempty"` - // Subscriber is the addressable that receives events from the Broker that pass the Filter. It - // is required. + // Filters is an experimental field that conforms to the CNCF CloudEvents Subscriptions + // API. It's an array of filter expressions that evaluate to true or false. + // If any filter expression in the array evaluates to false, the event MUST + // NOT be sent to the Subscriber. If all the filter expressions in the array + // evaluate to true, the event MUST be attempted to be delivered. Absence of + // a filter or empty array implies a value of true. In the event of users + // specifying both Filter and Filters, then the latter will override the former. + // This will allow users to try out the effect of the new Filters field + // without compromising the existing attribute-based Filter and try it out on existing + // Trigger objects. + // + // +optional + Filters []SubscriptionsAPIFilter `json:"filters,omitempty"` + + // Subscriber is the addressable that receives events from the Broker that pass + // the Filter. It is required. Subscriber duckv1.Destination `json:"subscriber"` // Delivery contains the delivery spec for this specific trigger. @@ -96,17 +110,77 @@ 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 - // specified values. - // - // Nested context attributes are not supported as keys. Only string values are supported. + // specified values. Nested context attributes are not supported as keys. Only + // string values are supported. // // +optional Attributes TriggerFilterAttributes `json:"attributes,omitempty"` } +// SubscriptionsAPIFilter allows defining a filter expression using CloudEvents +// Subscriptions API. If multiple filters are specified, then the same semantics +// of SubscriptionsAPIFilter.All is applied. If no filter dialect or empty +// object is specified, then the filter always accept the events. +type SubscriptionsAPIFilter struct { + // All evaluates to true if all the nested expressions evaluate to true. + // It must contain at least one filter expression. + // + // +optional + All []SubscriptionsAPIFilter `json:"all,omitempty"` + + // Any evaluates to true if at least one of the nested expressions evaluates + // to true. It must contain at least one filter expression. + // + // +optional + Any []SubscriptionsAPIFilter `json:"any,omitempty"` + + // Not evaluates to true if the nested expression evaluates to false. + // + // +optional + Not *SubscriptionsAPIFilter `json:"not,omitempty"` + + // Exact evaluates to true if the value of the matching CloudEvents + // attribute 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 + // expression cannot 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 + // expression cannot 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 + // expression cannot 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 -// filtering by equality. Only exact matches will pass the filter. You can use the value '' -// to indicate all strings match. +// filtering by equality. Only exact matches will pass the filter. You can use +// the value '' to indicate all strings match. type TriggerFilterAttributes map[string]string // TriggerStatus represents the current state of a Trigger. diff --git a/pkg/apis/eventing/v1/trigger_validation.go b/pkg/apis/eventing/v1/trigger_validation.go index 4b367340a6b..772a33f17a1 100644 --- a/pkg/apis/eventing/v1/trigger_validation.go +++ b/pkg/apis/eventing/v1/trigger_validation.go @@ -22,10 +22,11 @@ import ( "fmt" "regexp" + corev1 "k8s.io/api/core/v1" "knative.dev/pkg/apis" "knative.dev/pkg/kmp" - corev1 "k8s.io/api/core/v1" + "knative.dev/eventing/pkg/apis/feature" ) var ( @@ -35,7 +36,7 @@ var ( // Validate the Trigger. func (t *Trigger) Validate(ctx context.Context) *apis.FieldError { - errs := t.Spec.Validate(ctx).ViaField("spec") + errs := t.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec") errs = t.validateAnnotation(errs, DependencyAnnotation, t.validateDependencyAnnotation) errs = t.validateAnnotation(errs, InjectionAnnotation, t.validateInjectionAnnotation) if apis.IsInUpdate(ctx) { @@ -46,36 +47,20 @@ func (t *Trigger) Validate(ctx context.Context) *apis.FieldError { } // Validate the TriggerSpec. -func (ts *TriggerSpec) Validate(ctx context.Context) *apis.FieldError { - var errs *apis.FieldError +func (ts *TriggerSpec) Validate(ctx context.Context) (errs *apis.FieldError) { if ts.Broker == "" { - fe := apis.ErrMissingField("broker") - 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) - } - } - } - - if fe := ts.Subscriber.Validate(ctx); fe != nil { - errs = errs.Also(fe.ViaField("subscriber")) - } - - if ts.Delivery != nil { - if de := ts.Delivery.Validate(ctx); de != nil { - errs = errs.Also(de.ViaField("delivery")) - } + errs = errs.Also(apis.ErrMissingField("broker")) } - return errs + return errs.Also( + ValidateAttributeFilters(ts.Filter).ViaField("filter"), + ).Also( + ValidateSubscriptionAPIFiltersList(ctx, ts.Filters).ViaField("filters"), + ).Also( + ts.Subscriber.Validate(ctx).ViaField("subscriber"), + ).Also( + ts.Delivery.Validate(ctx).ViaField("delivery"), + ) } // CheckImmutableFields checks that any immutable fields were not changed. @@ -163,3 +148,122 @@ func (t *Trigger) validateInjectionAnnotation(injectionAnnotation string) *apis. } return nil } + +func ValidateAttributeFilters(filter *TriggerFilter) (errs *apis.FieldError) { + if filter == nil { + return nil + } + return errs.Also(ValidateAttributesNames(filter.Attributes).ViaField("attributes")) +} + +func ValidateAttributesNames(attrs map[string]string) (errs *apis.FieldError) { + for attr := range attrs { + if !validAttributeName.MatchString(attr) { + errs = errs.Also(apis.ErrInvalidKeyName(attr, apis.CurrentField, "Attribute name must start with a letter and can only contain lowercase alphanumeric").ViaKey(attr)) + } + } + return errs +} + +func ValidateSingleAttributeMap(expr map[string]string) (errs *apis.FieldError) { + if len(expr) == 0 { + return nil + } + + if len(expr) != 1 { + return apis.ErrGeneric("Multiple items found, can have only one key-value", apis.CurrentField) + } + for attr := range expr { + if !validAttributeName.MatchString(attr) { + errs = errs.Also(apis.ErrInvalidKeyName(attr, apis.CurrentField, "Attribute name must start with a letter and can only contain lowercase alphanumeric").ViaKey(attr)) + } + } + return errs +} + +func ValidateSubscriptionAPIFiltersList(ctx context.Context, filters []SubscriptionsAPIFilter) (errs *apis.FieldError) { + if filters == nil || !feature.FromContext(ctx).IsEnabled(feature.NewTriggerFilters) { + return nil + } + + for i, f := range filters { + f := f + errs = errs.Also(ValidateSubscriptionAPIFilter(ctx, &f)).ViaIndex(i) + } + return errs +} + +func ValidateSubscriptionAPIFilter(ctx context.Context, filter *SubscriptionsAPIFilter) (errs *apis.FieldError) { + if filter == nil { + return nil + } + errs = errs.Also( + ValidateOneOf(filter), + ).Also( + ValidateSingleAttributeMap(filter.Exact).ViaField("exact"), + ).Also( + ValidateSingleAttributeMap(filter.Prefix).ViaField("prefix"), + ).Also( + ValidateSingleAttributeMap(filter.Suffix).ViaField("suffix"), + ).Also( + ValidateSubscriptionAPIFiltersList(ctx, filter.All).ViaField("all"), + ).Also( + ValidateSubscriptionAPIFiltersList(ctx, filter.Any).ViaField("any"), + ).Also( + ValidateSubscriptionAPIFilter(ctx, filter.Not).ViaField("not"), + ) + return errs +} + +func ValidateOneOf(filter *SubscriptionsAPIFilter) (err *apis.FieldError) { + if filter != nil && hasMultipleDialects(filter) { + return apis.ErrGeneric("multiple dialects found, filters can have only one dialect set") + } + return nil +} + +func hasMultipleDialects(filter *SubscriptionsAPIFilter) bool { + dialectFound := false + if len(filter.Exact) > 0 { + dialectFound = true + } + if len(filter.Prefix) > 0 { + if dialectFound { + return true + } else { + dialectFound = true + } + } + if len(filter.Suffix) > 0 { + if dialectFound { + return true + } else { + dialectFound = true + } + } + if len(filter.All) > 0 { + if dialectFound { + return true + } else { + dialectFound = true + } + } + if len(filter.Any) > 0 { + if dialectFound { + return true + } else { + dialectFound = true + } + } + if filter.Not != nil { + if dialectFound { + return true + } else { + dialectFound = true + } + } + if len(filter.Extensions) > 0 && dialectFound { + return true + } + return false +} diff --git a/pkg/apis/eventing/v1/trigger_validation_test.go b/pkg/apis/eventing/v1/trigger_validation_test.go index 629164c479d..7fd70f124af 100644 --- a/pkg/apis/eventing/v1/trigger_validation_test.go +++ b/pkg/apis/eventing/v1/trigger_validation_test.go @@ -23,17 +23,24 @@ 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" + "knative.dev/eventing/pkg/apis/feature" ) var ( - validEmptyFilter = &TriggerFilter{} - validAttributesFilter = &TriggerFilter{ - Attributes: TriggerFilterAttributes{ + validEmptyTriggerFilter = newTriggerFilter(nil) + validTriggerFilter = newTriggerFilter( + map[string]string{ "type": "other_type", "source": "other_source", + }) + validSubscriptionAPIFilter = &SubscriptionsAPIFilter{ + Exact: map[string]string{ + "type": "other_type", }, } validSubscriber = duckv1.Destination{ @@ -76,7 +83,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "default", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: nil, @@ -100,7 +107,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -118,7 +125,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -136,7 +143,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -153,7 +160,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -170,7 +177,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -187,7 +194,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -225,7 +232,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "default", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -266,7 +273,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test-broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -282,7 +289,7 @@ func TestTriggerValidation(t *testing.T) { }, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, Delivery: &eventingduckv1.DeliverySpec{ BackoffDelay: &invalidString, @@ -315,7 +322,7 @@ func TestTriggerUpdateValidation(t *testing.T) { }, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, tNew: &Trigger{ @@ -324,7 +331,7 @@ func TestTriggerUpdateValidation(t *testing.T) { }, Spec: TriggerSpec{ Broker: "anotherBroker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -370,7 +377,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "missing broker", ts: &TriggerSpec{ Broker: "", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: validSubscriber, }, want: func() *apis.FieldError { @@ -380,10 +387,8 @@ func TestTriggerSpecValidation(t *testing.T) { }, { name: "missing attributes keys, match all", ts: &TriggerSpec{ - Broker: "test_broker", - Filter: &TriggerFilter{ - Attributes: TriggerFilterAttributes{}, - }, + Broker: "test_broker", + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }, want: &apis.FieldError{}, @@ -391,44 +396,40 @@ func TestTriggerSpecValidation(t *testing.T) { name: "invalid attribute name, start with number", ts: &TriggerSpec{ Broker: "test_broker", - Filter: &TriggerFilter{ - Attributes: TriggerFilterAttributes{ + Filter: newTriggerFilter( + map[string]string{ "0invalid": "my-value", - }, - }, + }), Subscriber: validSubscriber, }, - want: &apis.FieldError{ - Message: `Invalid attribute name: "0invalid"`, - Paths: []string{"filter.attributes"}, - }, + want: apis.ErrInvalidKeyName("0invalid", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("attributes", "0invalid").ViaField("filter"), }, { name: "invalid attribute name, capital letters", ts: &TriggerSpec{ Broker: "test_broker", - Filter: &TriggerFilter{ - Attributes: TriggerFilterAttributes{ + Filter: newTriggerFilter( + map[string]string{ "invALID": "my-value", - }, - }, + }), Subscriber: validSubscriber, }, - want: &apis.FieldError{ - Message: `Invalid attribute name: "invALID"`, - Paths: []string{"filter.attributes"}, - }, + want: apis.ErrInvalidKeyName("invALID", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("attributes", "invALID").ViaField("filter"), }, { name: "missing subscriber", ts: &TriggerSpec{ Broker: "test_broker", - Filter: validAttributesFilter, + Filter: validTriggerFilter, }, want: apis.ErrGeneric("expected at least one, got none", "subscriber.ref", "subscriber.uri"), }, { name: "missing subscriber.ref.name", ts: &TriggerSpec{ Broker: "test_broker", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: invalidSubscriber, }, want: apis.ErrMissingField("subscriber.ref.name"), @@ -436,7 +437,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "missing broker", ts: &TriggerSpec{ Broker: "", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: validSubscriber, }, want: apis.ErrMissingField("broker"), @@ -444,7 +445,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "valid empty filter", ts: &TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }, want: &apis.FieldError{}, @@ -452,7 +453,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "valid SourceAndType filter", ts: &TriggerSpec{ Broker: "test_broker", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: validSubscriber, }, want: &apis.FieldError{}, @@ -460,7 +461,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "valid Attributes filter", ts: &TriggerSpec{ Broker: "test_broker", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: validSubscriber, }, want: &apis.FieldError{}, @@ -468,7 +469,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "invalid delivery, invalid delay string", ts: &TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, Delivery: &eventingduckv1.DeliverySpec{ BackoffDelay: &invalidString, @@ -487,6 +488,257 @@ func TestTriggerSpecValidation(t *testing.T) { } } +func TestFilterSpecValidation(t *testing.T) { + newTriggerFiltersEnabledCtx := feature.ToContext(context.TODO(), feature.Flags{ + feature.NewTriggerFilters: feature.Enabled, + }) + tests := []struct { + name string + filter *TriggerFilter + filters []SubscriptionsAPIFilter + want *apis.FieldError + }{{ + name: "missing filters, match all", + filters: nil, + want: &apis.FieldError{}, + }, { + name: "invalid exact filter attribute name, start with number", + filters: []SubscriptionsAPIFilter{ + { + Exact: map[string]string{ + "0invalid": "some-value", + }, + }}, + want: apis.ErrInvalidKeyName("0invalid", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("exact", "0invalid").ViaFieldIndex("filters", 0), + }, { + name: "invalid exact filter attribute name, capital letters", + filters: []SubscriptionsAPIFilter{ + { + Exact: map[string]string{ + "invALID": "some-value", + }, + }}, + want: apis.ErrInvalidKeyName("invALID", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("exact", "invALID").ViaFieldIndex("filters", 0), + }, { + name: "valid empty filter", + filter: validEmptyTriggerFilter, + want: &apis.FieldError{}, + }, { + name: "valid SourceAndType filter", + filter: validTriggerFilter, + want: &apis.FieldError{}, + }, { + name: "invalid multiple dialects", + filters: []SubscriptionsAPIFilter{ + { + Exact: map[string]string{ + "myext": "abc", + }, + Suffix: map[string]string{ + "myext": "abc", + }, + }}, + want: apis.ErrGeneric("multiple dialects found, filters can have only one dialect set"), + }, { + name: "valid Attributes filter", + filter: validTriggerFilter, + want: &apis.FieldError{}, + }, { + name: "exact filter contains more than one attribute", + filters: []SubscriptionsAPIFilter{ + { + Exact: map[string]string{ + "myext": "abc", + "anotherext": "xyz", + }, + }}, + want: apis.ErrGeneric("Multiple items found, can have only one key-value", "exact").ViaFieldIndex("filters", 0), + }, { + name: "valid exact filter", + filters: []SubscriptionsAPIFilter{ + { + Exact: map[string]string{ + "valid": "abc", + }, + }}, + want: &apis.FieldError{}, + }, { + name: "suffix filter contains more than one attribute", + filters: []SubscriptionsAPIFilter{ + { + Suffix: map[string]string{ + "myext": "abc", + "anotherext": "xyz", + }, + }}, + want: apis.ErrGeneric("Multiple items found, can have only one key-value", "suffix").ViaFieldIndex("filters", 0), + }, { + name: "suffix filter contains invalid attribute name", + filters: []SubscriptionsAPIFilter{ + { + Suffix: map[string]string{ + "invALID": "abc", + }, + }}, + want: apis.ErrInvalidKeyName("invALID", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("suffix", "invALID").ViaFieldIndex("filters", 0), + }, { + name: "valid suffix filter", + filters: []SubscriptionsAPIFilter{ + { + Suffix: map[string]string{ + "valid": "abc", + }, + }}, + want: &apis.FieldError{}, + }, { + name: "prefix filter contains more than one attribute", + filters: []SubscriptionsAPIFilter{ + { + Prefix: map[string]string{ + "myext": "abc", + "anotherext": "xyz", + }, + }}, + want: apis.ErrGeneric("Multiple items found, can have only one key-value", "prefix").ViaFieldIndex("filters", 0), + }, { + name: "prefix filter contains invalid attribute name", + filters: []SubscriptionsAPIFilter{ + { + Prefix: map[string]string{ + "invALID": "abc", + }, + }}, + want: apis.ErrInvalidKeyName("invALID", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("prefix", "invALID").ViaFieldIndex("filters", 0), + }, { + name: "valid prefix filter", + filters: []SubscriptionsAPIFilter{ + { + Prefix: map[string]string{ + "valid": "abc", + }, + }}, + want: &apis.FieldError{}, + }, { + name: "not nested expression is valid", + filters: []SubscriptionsAPIFilter{ + { + Not: validSubscriptionAPIFilter, + }}, + want: &apis.FieldError{}, + }, { + name: "not nested expression is invalid", + filters: []SubscriptionsAPIFilter{ + { + Not: &SubscriptionsAPIFilter{ + Prefix: map[string]string{ + "invALID": "abc", + }, + }, + }}, + want: apis.ErrInvalidKeyName("invALID", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("prefix", "invALID").ViaField("not").ViaFieldIndex("filters", 0), + }, { + name: "all filter is nil", + filters: []SubscriptionsAPIFilter{ + { + All: nil, + }}, + want: &apis.FieldError{}, + }, { + name: "all filter is valid", + filters: []SubscriptionsAPIFilter{ + { + All: []SubscriptionsAPIFilter{ + *validSubscriptionAPIFilter, + { + Exact: map[string]string{"myattr": "myval"}, + }, + }, + }}, + want: &apis.FieldError{}, + }, { + name: "all filter sub expression is invalid", + filters: []SubscriptionsAPIFilter{ + { + All: []SubscriptionsAPIFilter{ + *validSubscriptionAPIFilter, + { + Exact: map[string]string{"myattr": "myval"}, + }, + { + Prefix: map[string]string{ + "invALID": "abc", + }, + }, + }, + }}, + want: apis.ErrInvalidKeyName("invALID", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("prefix", "invALID").ViaFieldIndex("all", 2).ViaFieldIndex("filters", 0), + }, { + name: "any filter is valid", + filters: []SubscriptionsAPIFilter{ + { + Any: []SubscriptionsAPIFilter{ + *validSubscriptionAPIFilter, + { + Exact: map[string]string{"myattr": "myval"}, + }, + }, + }}, + want: &apis.FieldError{}, + }, { + name: "any filter sub expression is invalid", + filters: []SubscriptionsAPIFilter{ + { + Any: []SubscriptionsAPIFilter{ + *validSubscriptionAPIFilter, + { + Exact: map[string]string{"myattr": "myval"}, + }, + { + Prefix: map[string]string{"invALID": "abc"}, + }, + }, + }}, + want: apis.ErrInvalidKeyName("invALID", apis.CurrentField, + "Attribute name must start with a letter and can only contain "+ + "lowercase alphanumeric").ViaFieldKey("prefix", "invALID").ViaFieldIndex("any", 2).ViaFieldIndex("filters", 0)}, { + name: "raw extension expression is valid", + filters: []SubscriptionsAPIFilter{ + { + 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, + Filters: test.filters, + Subscriber: validSubscriber, + } + got := ts.Validate(newTriggerFiltersEnabledCtx) + 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 @@ -529,7 +781,7 @@ func TestTriggerImmutableFields(t *testing.T) { original: &Trigger{ Spec: TriggerSpec{ Broker: "broker", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: validSubscriber, }, }, @@ -569,3 +821,9 @@ func TestTriggerImmutableFields(t *testing.T) { }) } } + +func newTriggerFilter(attrs map[string]string) *TriggerFilter { + return &TriggerFilter{ + Attributes: attrs, + } +} diff --git a/pkg/apis/eventing/v1/zz_generated.deepcopy.go b/pkg/apis/eventing/v1/zz_generated.deepcopy.go index 80d8532638a..df852553761 100644 --- a/pkg/apis/eventing/v1/zz_generated.deepcopy.go +++ b/pkg/apis/eventing/v1/zz_generated.deepcopy.go @@ -134,6 +134,77 @@ 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 *SubscriptionsAPIFilter) DeepCopyInto(out *SubscriptionsAPIFilter) { + *out = *in + if in.All != nil { + in, out := &in.All, &out.All + *out = make([]SubscriptionsAPIFilter, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Any != nil { + in, out := &in.Any, &out.Any + *out = make([]SubscriptionsAPIFilter, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Not != nil { + in, out := &in.Not, &out.Not + *out = new(SubscriptionsAPIFilter) + (*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 SubscriptionsAPIFilter. +func (in *SubscriptionsAPIFilter) DeepCopy() *SubscriptionsAPIFilter { + if in == nil { + return nil + } + out := new(SubscriptionsAPIFilter) + 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 @@ -248,6 +319,13 @@ func (in *TriggerSpec) DeepCopyInto(out *TriggerSpec) { *out = new(TriggerFilter) (*in).DeepCopyInto(*out) } + if in.Filters != nil { + in, out := &in.Filters, &out.Filters + *out = make([]SubscriptionsAPIFilter, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } in.Subscriber.DeepCopyInto(&out.Subscriber) if in.Delivery != nil { in, out := &in.Delivery, &out.Delivery diff --git a/pkg/apis/feature/flag_names.go b/pkg/apis/feature/flag_names.go index 1559d64f9d4..a404c8fdc92 100644 --- a/pkg/apis/feature/flag_names.go +++ b/pkg/apis/feature/flag_names.go @@ -21,4 +21,5 @@ const ( DeliveryTimeout = "delivery-timeout" KReferenceMapping = "kreference-mapping" StrictSubscriber = "strict-subscriber" + NewTriggerFilters = "new-trigger-filters" )