Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 3c7f3ed

Browse files
author
odacremolbap
committed
use broker project's filters
1 parent e264cfc commit 3c7f3ed

File tree

6 files changed

+12
-202
lines changed

6 files changed

+12
-202
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.19
44

55
require (
66
github.com/stretchr/testify v1.8.0
7-
github.com/triggermesh/brokers v0.0.0-20221018093436-f87a18b58712
7+
github.com/triggermesh/brokers v0.0.0-20221018150809-c9d0300abb23
88
go.uber.org/zap v1.22.0
99
k8s.io/api v0.24.4
1010
k8s.io/apimachinery v0.24.4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ github.com/triggermesh/brokers v0.0.0-20220926134808-88d6a8629e30 h1:EjHSGHBT/7h
508508
github.com/triggermesh/brokers v0.0.0-20220926134808-88d6a8629e30/go.mod h1:64gU9A1hlgsU6/51B3h7eDsg7KI4BvuzAE9uJ8h6V9U=
509509
github.com/triggermesh/brokers v0.0.0-20221018093436-f87a18b58712 h1:ARMQw1DtiJwfXI8Dx9pxgzSZti+iOw2znopgmmqOunI=
510510
github.com/triggermesh/brokers v0.0.0-20221018093436-f87a18b58712/go.mod h1:qcsxTLxVIa1R0XLm1g/0eMf8RpMI7EYlHiEU3gP+llg=
511+
github.com/triggermesh/brokers v0.0.0-20221018150809-c9d0300abb23 h1:Vun0NLmhusgnV1ohaeIVQy7NEMzqIpWIWNDDkxnrJkw=
512+
github.com/triggermesh/brokers v0.0.0-20221018150809-c9d0300abb23/go.mod h1:qcsxTLxVIa1R0XLm1g/0eMf8RpMI7EYlHiEU3gP+llg=
511513
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
512514
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
513515
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=

pkg/apis/eventing/v1alpha1/deepcopy_generated.go

Lines changed: 2 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/eventing/v1alpha1/trigger_types.go

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"knative.dev/pkg/apis"
1212
duckv1 "knative.dev/pkg/apis/duck/v1"
1313
"knative.dev/pkg/kmeta"
14+
15+
"github.com/triggermesh/brokers/pkg/config/broker"
1416
)
1517

1618
// +genclient
@@ -42,55 +44,6 @@ var (
4244
_ duckv1.KRShaped = (*Trigger)(nil)
4345
)
4446

45-
type Filter struct {
46-
// All evaluates to true if all the nested expressions evaluate to true.
47-
// It must contain at least one filter expression.
48-
//
49-
// +optional
50-
All []Filter `json:"all,omitempty"`
51-
52-
// Any evaluates to true if at least one of the nested expressions evaluates
53-
// to true. It must contain at least one filter expression.
54-
//
55-
// +optional
56-
Any []Filter `json:"any,omitempty"`
57-
58-
// Not evaluates to true if the nested expression evaluates to false.
59-
//
60-
// +optional
61-
Not *Filter `json:"not,omitempty"`
62-
63-
// Exact evaluates to true if the value of the matching CloudEvents
64-
// attribute matches exactly the String value specified (case-sensitive).
65-
// Exact must contain exactly one property, where the key is the name of the
66-
// CloudEvents attribute to be matched, and its value is the String value to
67-
// use in the comparison. The attribute name and value specified in the filter
68-
// expression cannot be empty strings.
69-
//
70-
// +optional
71-
Exact map[string]string `json:"exact,omitempty"`
72-
73-
// Prefix evaluates to true if the value of the matching CloudEvents
74-
// attribute starts with the String value specified (case-sensitive). Prefix
75-
// must contain exactly one property, where the key is the name of the
76-
// CloudEvents attribute to be matched, and its value is the String value to
77-
// use in the comparison. The attribute name and value specified in the filter
78-
// expression cannot be empty strings.
79-
//
80-
// +optional
81-
Prefix map[string]string `json:"prefix,omitempty"`
82-
83-
// Suffix evaluates to true if the value of the matching CloudEvents
84-
// attribute ends with the String value specified (case-sensitive). Suffix
85-
// must contain exactly one property, where the key is the name of the
86-
// CloudEvents attribute to be matched, and its value is the String value to
87-
// use in the comparison. The attribute name and value specified in the filter
88-
// expression cannot be empty strings.
89-
//
90-
// +optional
91-
Suffix map[string]string `json:"suffix,omitempty"`
92-
}
93-
9447
// TriggerSpec defines the desired state of Trigger
9548
type TriggerSpec struct {
9649
// Broker is the broker that this trigger receives events from.
@@ -108,7 +61,7 @@ type TriggerSpec struct {
10861
// Trigger objects.
10962
//
11063
// +optional
111-
Filters []Filter `json:"filters,omitempty"`
64+
Filters []broker.Filter `json:"filters,omitempty"`
11265

11366
// Target is the addressable that receives events from the Broker that pass
11467
// the Filter. It is required.

pkg/apis/eventing/v1alpha1/trigger_validation.go

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ package v1alpha1
55

66
import (
77
"context"
8-
"regexp"
98

9+
"github.com/triggermesh/brokers/pkg/config/broker"
1010
"knative.dev/pkg/apis"
1111
)
1212

13-
var (
14-
// Only allow lowercase alphanumeric, starting with letters.
15-
validAttributeName = regexp.MustCompile(`^[a-z][a-z0-9]*$`)
16-
)
17-
1813
// Validate the Trigger.
1914
func (t *Trigger) Validate(ctx context.Context) *apis.FieldError {
2015
errs := t.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")
@@ -23,99 +18,13 @@ func (t *Trigger) Validate(ctx context.Context) *apis.FieldError {
2318

2419
// Validate the TriggerSpec.
2520
func (ts *TriggerSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
26-
2721
errs = ts.Broker.Validate(ctx).ViaField("broker")
2822

2923
return errs.Also(
30-
ValidateSubscriptionAPIFiltersList(ctx, ts.Filters).ViaField("filters"),
24+
broker.ValidateSubscriptionAPIFiltersList(ctx, ts.Filters).ViaField("filters"),
3125
).Also(
3226
ts.Target.Validate(ctx).ViaField("target"),
3327
).Also(
3428
ts.Delivery.Validate(ctx).ViaField("delivery"),
3529
)
3630
}
37-
38-
func ValidateSubscriptionAPIFiltersList(ctx context.Context, filters []Filter) (errs *apis.FieldError) {
39-
for i, f := range filters {
40-
f := f
41-
errs = errs.Also(ValidateSubscriptionAPIFilter(ctx, &f)).ViaIndex(i)
42-
}
43-
return errs
44-
}
45-
46-
func ValidateAttributesNames(attrs map[string]string) (errs *apis.FieldError) {
47-
for attr := range attrs {
48-
if !validAttributeName.MatchString(attr) {
49-
errs = errs.Also(apis.ErrInvalidKeyName(attr, apis.CurrentField, "Attribute name must start with a letter and can only contain lowercase alphanumeric").ViaKey(attr))
50-
}
51-
}
52-
return errs
53-
}
54-
55-
func ValidateOneOf(filter *Filter) (err *apis.FieldError) {
56-
if filter != nil && hasMultipleDialects(filter) {
57-
return apis.ErrGeneric("multiple dialects found, filters can have only one dialect set")
58-
}
59-
return nil
60-
}
61-
62-
func hasMultipleDialects(filter *Filter) bool {
63-
dialectFound := false
64-
if len(filter.Exact) > 0 {
65-
dialectFound = true
66-
}
67-
if len(filter.Prefix) > 0 {
68-
if dialectFound {
69-
return true
70-
} else {
71-
dialectFound = true
72-
}
73-
}
74-
if len(filter.Suffix) > 0 {
75-
if dialectFound {
76-
return true
77-
} else {
78-
dialectFound = true
79-
}
80-
}
81-
if len(filter.All) > 0 {
82-
if dialectFound {
83-
return true
84-
} else {
85-
dialectFound = true
86-
}
87-
}
88-
if len(filter.Any) > 0 {
89-
if dialectFound {
90-
return true
91-
} else {
92-
dialectFound = true
93-
}
94-
}
95-
if filter.Not != nil && dialectFound {
96-
return true
97-
}
98-
return false
99-
}
100-
101-
func ValidateSubscriptionAPIFilter(ctx context.Context, filter *Filter) (errs *apis.FieldError) {
102-
if filter == nil {
103-
return nil
104-
}
105-
errs = errs.Also(
106-
ValidateOneOf(filter),
107-
).Also(
108-
ValidateAttributesNames(filter.Exact).ViaField("exact"),
109-
).Also(
110-
ValidateAttributesNames(filter.Prefix).ViaField("prefix"),
111-
).Also(
112-
ValidateAttributesNames(filter.Suffix).ViaField("suffix"),
113-
).Also(
114-
ValidateSubscriptionAPIFiltersList(ctx, filter.All).ViaField("all"),
115-
).Also(
116-
ValidateSubscriptionAPIFiltersList(ctx, filter.Any).ViaField("any"),
117-
).Also(
118-
ValidateSubscriptionAPIFilter(ctx, filter.Not).ViaField("not"),
119-
)
120-
return errs
121-
}

pkg/reconciler/redisbroker/reconcile_secret.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"go.uber.org/zap"
77
"sigs.k8s.io/yaml"
88

9-
"github.com/triggermesh/brokers/pkg/config/broker"
109
corev1 "k8s.io/api/core/v1"
1110
apierrs "k8s.io/apimachinery/pkg/api/errors"
1211
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -19,6 +18,8 @@ import (
1918
"knative.dev/pkg/logging"
2019
pkgreconciler "knative.dev/pkg/reconciler"
2120

21+
"github.com/triggermesh/brokers/pkg/config/broker"
22+
2223
eventingv1alpha1 "github.com/triggermesh/triggermesh-core/pkg/apis/eventing/v1alpha1"
2324
eventingv1alpha1listers "github.com/triggermesh/triggermesh-core/pkg/client/generated/listers/eventing/v1alpha1"
2425
"github.com/triggermesh/triggermesh-core/pkg/reconciler"

0 commit comments

Comments
 (0)