diff --git a/config/core/resources/trigger.yaml b/config/core/resources/trigger.yaml index 281ad42ed72..87b4351b366 100644 --- a/config/core/resources/trigger.yaml +++ b/config/core/resources/trigger.yaml @@ -101,6 +101,46 @@ 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 + filters: + description: 'Filters 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. ' + type: object + properties: + 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/docs/eventing-api.md b/docs/eventing-api.md index 1e2cb9cfe3f..ebce8864be5 100644 --- a/docs/eventing-api.md +++ b/docs/eventing-api.md @@ -1577,6 +1577,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 CNCF CloudEvents Subscriptions +API. It’s An array of filter expressions that evaluates 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 +evaluates 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 later will override the first. +This will allow our users to try out the effect of the new filters field +without compromising existing Filter objects and try it out on existing +Trigger objects.

+ + + + subscriber
@@ -1585,8 +1608,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.

@@ -1723,6 +1746,140 @@ delivered into the Broker mesh.

+

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

@@ -1752,8 +1909,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.

@@ -1765,8 +1922,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

@@ -1811,6 +1968,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 CNCF CloudEvents Subscriptions +API. It’s An array of filter expressions that evaluates 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 +evaluates 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 later will override the first. +This will allow our users to try out the effect of the new filters field +without compromising existing Filter objects and try it out on existing +Trigger objects.

+ + + + subscriber
@@ -1819,8 +1999,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 d7b876c868e..2c16c302c16 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 CNCF CloudEvents Subscriptions + // API. It's An array of filter expressions that evaluates 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 + // evaluates 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 later will override the first. + // This will allow our users to try out the effect of the new filters field + // without compromising existing Filter objects 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..28edad679b1 100644 --- a/pkg/apis/eventing/v1/trigger_validation.go +++ b/pkg/apis/eventing/v1/trigger_validation.go @@ -22,10 +22,9 @@ import ( "fmt" "regexp" + corev1 "k8s.io/api/core/v1" "knative.dev/pkg/apis" "knative.dev/pkg/kmp" - - corev1 "k8s.io/api/core/v1" ) var ( @@ -35,7 +34,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 +45,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")) - } - } - - return errs + errs = errs.Also(apis.ErrMissingField("broker")) + } + + return errs.Also( + ValidateAttributeFilters(ts.Filter).ViaField("filter"), + ).Also( + ValidateSubscriptionAPIFiltersList(ts.Filters, "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 +146,72 @@ 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) { + if attrs == nil { + return nil + } + + 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(filters []SubscriptionsAPIFilter, field string) (errs *apis.FieldError) { + if filters == nil { + return nil + } + if len(filters) == 0 { + return apis.ErrGeneric(fmt.Sprintf("%s must contain at least one nested filter", field), apis.CurrentField) + } + + for i, f := range filters { + f := f + errs = errs.Also(ValidateSubscriptionAPIFilter(&f)).ViaIndex(i) + } + return errs +} + +func ValidateSubscriptionAPIFilter(filter *SubscriptionsAPIFilter) (errs *apis.FieldError) { + if filter == nil { + return nil + } + errs = errs.Also( + ValidateSingleAttributeMap(filter.Exact).ViaField("exact"), + ).Also( + ValidateSingleAttributeMap(filter.Prefix).ViaField("prefix"), + ).Also( + ValidateSingleAttributeMap(filter.Suffix).ViaField("suffix"), + ).Also( + ValidateSubscriptionAPIFiltersList(filter.All, "all").ViaField("all"), + ).Also( + ValidateSubscriptionAPIFiltersList(filter.Any, "any").ViaField("any"), + ).Also(ValidateSubscriptionAPIFilter(filter.Not).ViaField("not")) + return errs +} diff --git a/pkg/apis/eventing/v1/trigger_validation_test.go b/pkg/apis/eventing/v1/trigger_validation_test.go index 629164c479d..d0cf0cab419 100644 --- a/pkg/apis/eventing/v1/trigger_validation_test.go +++ b/pkg/apis/eventing/v1/trigger_validation_test.go @@ -23,17 +23,23 @@ 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{ - 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 +82,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "default", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: nil, @@ -100,7 +106,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -118,7 +124,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -136,7 +142,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -153,7 +159,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -170,7 +176,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -187,7 +193,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -225,7 +231,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "default", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -266,7 +272,7 @@ func TestTriggerValidation(t *testing.T) { }}, Spec: TriggerSpec{ Broker: "test-broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -282,7 +288,7 @@ func TestTriggerValidation(t *testing.T) { }, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, Delivery: &eventingduckv1.DeliverySpec{ BackoffDelay: &invalidString, @@ -315,7 +321,7 @@ func TestTriggerUpdateValidation(t *testing.T) { }, Spec: TriggerSpec{ Broker: "test_broker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, tNew: &Trigger{ @@ -324,7 +330,7 @@ func TestTriggerUpdateValidation(t *testing.T) { }, Spec: TriggerSpec{ Broker: "anotherBroker", - Filter: validEmptyFilter, + Filter: validEmptyTriggerFilter, Subscriber: validSubscriber, }}, want: &apis.FieldError{ @@ -370,7 +376,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "missing broker", ts: &TriggerSpec{ Broker: "", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: validSubscriber, }, want: func() *apis.FieldError { @@ -380,10 +386,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 +395,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 +436,7 @@ func TestTriggerSpecValidation(t *testing.T) { name: "missing broker", ts: &TriggerSpec{ Broker: "", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: validSubscriber, }, want: apis.ErrMissingField("broker"), @@ -444,7 +444,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 +452,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 +460,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 +468,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 +487,256 @@ func TestTriggerSpecValidation(t *testing.T) { } } +func TestFilterSpecValidation(t *testing.T) { + 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: "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 empty", + filters: []SubscriptionsAPIFilter{ + { + All: []SubscriptionsAPIFilter{}, + }}, + want: apis.ErrGeneric("all must contain at least one nested filter", "all").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 empty", + filters: []SubscriptionsAPIFilter{ + { + Any: []SubscriptionsAPIFilter{}, + }}, + want: apis.ErrGeneric("any must contain at least one nested filter", "any").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(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 @@ -529,7 +779,7 @@ func TestTriggerImmutableFields(t *testing.T) { original: &Trigger{ Spec: TriggerSpec{ Broker: "broker", - Filter: validAttributesFilter, + Filter: validTriggerFilter, Subscriber: validSubscriber, }, }, @@ -569,3 +819,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 908b4e354a8..960655d0a36 100644 --- a/pkg/apis/eventing/v1/zz_generated.deepcopy.go +++ b/pkg/apis/eventing/v1/zz_generated.deepcopy.go @@ -132,6 +132,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 @@ -246,6 +317,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