diff --git a/pkg/apis/triggers/v1alpha1/event_listener_types.go b/pkg/apis/triggers/v1alpha1/event_listener_types.go index 51d326707..b338c2533 100644 --- a/pkg/apis/triggers/v1alpha1/event_listener_types.go +++ b/pkg/apis/triggers/v1alpha1/event_listener_types.go @@ -61,10 +61,10 @@ type EventListenerSpec struct { ServiceAccountName string `json:"serviceAccountName,omitempty"` Triggers []EventListenerTrigger `json:"triggers"` // Trigger groups allow for centralized processing of an interceptor chain - TriggerGroups []EventListenerTriggerGroup `json:"triggerGroups"` - NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"` - LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"` - Resources Resources `json:"resources,omitempty"` + TriggerGroups []EventListenerTriggerGroup `json:"triggerGroups"` + NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"` + LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"` + Resources Resources `json:"resources,omitempty"` } type Resources struct { diff --git a/pkg/apis/triggers/v1alpha1/event_listener_validation.go b/pkg/apis/triggers/v1alpha1/event_listener_validation.go index ccb029307..db2a7766e 100644 --- a/pkg/apis/triggers/v1alpha1/event_listener_validation.go +++ b/pkg/apis/triggers/v1alpha1/event_listener_validation.go @@ -72,20 +72,6 @@ func (s *EventListenerSpec) validate(ctx context.Context) (errs *apis.FieldError if s.Resources.CustomResource != nil { errs = errs.Also(validateCustomObject(s.Resources.CustomResource).ViaField("spec.resources.customResource")) } - - for i, group := range s.TriggerGroups { - errs = errs.Also(group.validate(ctx).ViaField(fmt.Sprintf("spec.triggerGroups[%d]", i))) - } - return errs -} - -func (g *EventListenerTriggerGroup) validate(ctx context.Context) (errs *apis.FieldError) { - if g.TriggerSelector.LabelSelector == nil && len(g.TriggerSelector.NamespaceSelector.MatchNames) == 0 { - errs = errs.Also(apis.ErrMissingOneOf("triggerSelector.labelSelector", "triggerSelector.namespaceSelector")) - } - if len(g.Interceptors) == 0 { - errs = errs.Also(apis.ErrMissingField("interceptors")) - } return errs } diff --git a/pkg/apis/triggers/v1alpha1/event_listener_validation_test.go b/pkg/apis/triggers/v1alpha1/event_listener_validation_test.go index 9c7d9d45c..89745f867 100644 --- a/pkg/apis/triggers/v1alpha1/event_listener_validation_test.go +++ b/pkg/apis/triggers/v1alpha1/event_listener_validation_test.go @@ -526,35 +526,6 @@ func Test_EventListenerValidate(t *testing.T) { }}, }, }, - }, { - name: "Valid event listener with TriggerGroup and namespaceSelector", - el: &v1alpha1.EventListener{ - ObjectMeta: metav1.ObjectMeta{ - Name: "name", - Namespace: "namespace", - }, - Spec: v1alpha1.EventListenerSpec{ - TriggerGroups: []v1alpha1.EventListenerTriggerGroup{{ - Name: "my-group", - Interceptors: []*v1alpha1.TriggerInterceptor{{ - Ref: v1alpha1.InterceptorRef{ - Name: "cel", - }, - Params: []v1alpha1.InterceptorParams{{ - Name: "filter", - Value: test.ToV1JSON(t, "has(body.repository)"), - }}, - }}, - TriggerSelector: v1alpha1.EventListenerTriggerSelector{ - NamespaceSelector: v1alpha1.NamespaceSelector{ - MatchNames: []string{ - "foobar", - }, - }, - }, - }}, - }, - }, }} for _, tc := range tests { @@ -1225,48 +1196,6 @@ func TestEventListenerValidate_error(t *testing.T) { }, }, }, - }, { - name: "missing label and namespace selector", - el: &v1alpha1.EventListener{ - ObjectMeta: metav1.ObjectMeta{ - Name: "name", - Namespace: "namespace", - }, - Spec: v1alpha1.EventListenerSpec{ - TriggerGroups: []v1alpha1.EventListenerTriggerGroup{{ - Name: "my-group", - Interceptors: []*v1alpha1.TriggerInterceptor{{ - Ref: v1alpha1.InterceptorRef{ - Name: "cel", - }, - Params: []v1alpha1.InterceptorParams{{ - Name: "filter", - Value: test.ToV1JSON(t, "has(body.repository)"), - }}, - }}, - }}, - }, - }, - }, { - name: "triggerGroup requires interceptor", - el: &v1alpha1.EventListener{ - ObjectMeta: metav1.ObjectMeta{ - Name: "name", - Namespace: "namespace", - }, - Spec: v1alpha1.EventListenerSpec{ - TriggerGroups: []v1alpha1.EventListenerTriggerGroup{{ - Name: "my-group", - TriggerSelector: v1alpha1.EventListenerTriggerSelector{ - LabelSelector: &metav1.LabelSelector{ - MatchLabels: map[string]string{ - "foo": "bar", - }, - }, - }, - }}, - }, - }, }} for _, tc := range tests { diff --git a/pkg/apis/triggers/v1beta1/event_listener_types.go b/pkg/apis/triggers/v1beta1/event_listener_types.go index 5ab4f8eb3..646e48a4d 100644 --- a/pkg/apis/triggers/v1beta1/event_listener_types.go +++ b/pkg/apis/triggers/v1beta1/event_listener_types.go @@ -60,10 +60,10 @@ type EventListenerSpec struct { ServiceAccountName string `json:"serviceAccountName,omitempty"` Triggers []EventListenerTrigger `json:"triggers"` // Trigger groups allow for centralized processing of an interceptor chain - TriggerGroups []EventListenerTriggerGroup `json:"triggerGroups"` - NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"` - LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"` - Resources Resources `json:"resources,omitempty"` + TriggerGroups []EventListenerTriggerGroup `json:"triggerGroups"` + NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"` + LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"` + Resources Resources `json:"resources,omitempty"` } type Resources struct { @@ -117,7 +117,7 @@ type EventListenerTrigger struct { // TODO: Docstring type EventListenerTriggerGroup struct { Name string `json:"name"` - Interceptors []*TriggerInterceptor `json:"interceptors"` + Interceptors []*TriggerInterceptor `json:"interceptors"` TriggerSelector EventListenerTriggerSelector `json:"triggerSelector"` } @@ -127,7 +127,6 @@ type EventListenerTriggerSelector struct { LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"` } - // EventInterceptor provides a hook to intercept and pre-process events type EventInterceptor = TriggerInterceptor diff --git a/pkg/apis/triggers/v1beta1/event_listener_validation.go b/pkg/apis/triggers/v1beta1/event_listener_validation.go index 5bdab0a9d..1c81e9798 100644 --- a/pkg/apis/triggers/v1beta1/event_listener_validation.go +++ b/pkg/apis/triggers/v1beta1/event_listener_validation.go @@ -72,6 +72,20 @@ func (s *EventListenerSpec) validate(ctx context.Context) (errs *apis.FieldError if s.Resources.CustomResource != nil { errs = errs.Also(validateCustomObject(s.Resources.CustomResource).ViaField("spec.resources.customResource")) } + + for i, group := range s.TriggerGroups { + errs = errs.Also(group.validate(ctx).ViaField(fmt.Sprintf("spec.triggerGroups[%d]", i))) + } + return errs +} + +func (g *EventListenerTriggerGroup) validate(ctx context.Context) (errs *apis.FieldError) { + if g.TriggerSelector.LabelSelector == nil && len(g.TriggerSelector.NamespaceSelector.MatchNames) == 0 { + errs = errs.Also(apis.ErrMissingOneOf("triggerSelector.labelSelector", "triggerSelector.namespaceSelector")) + } + if len(g.Interceptors) == 0 { + errs = errs.Also(apis.ErrMissingField("interceptors")) + } return errs } diff --git a/pkg/apis/triggers/v1beta1/event_listener_validation_test.go b/pkg/apis/triggers/v1beta1/event_listener_validation_test.go index 0dab7eb4c..62a12db14 100644 --- a/pkg/apis/triggers/v1beta1/event_listener_validation_test.go +++ b/pkg/apis/triggers/v1beta1/event_listener_validation_test.go @@ -482,7 +482,35 @@ func Test_EventListenerValidate(t *testing.T) { }}, }, }, - }} + }, { + name: "Valid event listener with TriggerGroup and namespaceSelector", + el: &triggersv1beta1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: triggersv1beta1.EventListenerSpec{ + TriggerGroups: []triggersv1beta1.EventListenerTriggerGroup{{ + Name: "my-group", + Interceptors: []*triggersv1beta1.TriggerInterceptor{{ + Ref: triggersv1beta1.InterceptorRef{ + Name: "cel", + }, + Params: []triggersv1beta1.InterceptorParams{{ + Name: "filter", + Value: test.ToV1JSON(t, "has(body.repository)"), + }}, + }}, + TriggerSelector: triggersv1beta1.EventListenerTriggerSelector{ + NamespaceSelector: triggersv1beta1.NamespaceSelector{ + MatchNames: []string{ + "foobar", + }, + }, + }, + }}, + }, + }}} for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { @@ -1160,6 +1188,50 @@ func TestEventListenerValidate_error(t *testing.T) { }, }, wantErr: apis.ErrMultipleOneOf("spec.triggers[0].template or bindings or interceptors", "spec.triggers[0].triggerRef"), + }, { + name: "missing label and namespace selector", + el: &triggersv1beta1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: triggersv1beta1.EventListenerSpec{ + TriggerGroups: []triggersv1beta1.EventListenerTriggerGroup{{ + Name: "my-group", + Interceptors: []*triggersv1beta1.TriggerInterceptor{{ + Ref: triggersv1beta1.InterceptorRef{ + Name: "cel", + }, + Params: []triggersv1beta1.InterceptorParams{{ + Name: "filter", + Value: test.ToV1JSON(t, "has(body.repository)"), + }}, + }}, + }}, + }, + }, + wantErr: apis.ErrMissingOneOf("spec.triggerGroups[0].triggerSelector.labelSelector", "spec.triggerGroups[0].triggerSelector.namespaceSelector"), + }, { + name: "triggerGroup requires interceptor", + el: &triggersv1beta1.EventListener{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "namespace", + }, + Spec: triggersv1beta1.EventListenerSpec{ + TriggerGroups: []triggersv1beta1.EventListenerTriggerGroup{{ + Name: "my-group", + TriggerSelector: triggersv1beta1.EventListenerTriggerSelector{ + LabelSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "foo": "bar", + }, + }, + }, + }}, + }, + }, + wantErr: apis.ErrMissingField("spec.triggerGroups[0].interceptors"), }} for _, tc := range tests { diff --git a/pkg/sink/sink.go b/pkg/sink/sink.go index 23bc23ba0..5624fcf20 100644 --- a/pkg/sink/sink.go +++ b/pkg/sink/sink.go @@ -44,7 +44,7 @@ import ( "k8s.io/client-go/kubernetes" ) -var( +var ( emptyExtensions = map[string]interface{}{} ) @@ -226,7 +226,6 @@ func (r Sink) processTriggerGroups(g triggersv1.EventListenerTriggerGroup, reque triggerReq.Header = header triggerReq.Body = ioutil.NopCloser(bytes.NewBuffer(payload)) - wg.Add(len(trItems)) for _, t := range trItems { go func(t triggersv1.Trigger) {