Skip to content

Commit

Permalink
Move triggergroup validation to v1beta1 and remove v1alpha1
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyom committed Oct 5, 2021
1 parent c5fa882 commit aee6879
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 98 deletions.
8 changes: 4 additions & 4 deletions pkg/apis/triggers/v1alpha1/event_listener_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 0 additions & 14 deletions pkg/apis/triggers/v1alpha1/event_listener_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
71 changes: 0 additions & 71 deletions pkg/apis/triggers/v1alpha1/event_listener_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions pkg/apis/triggers/v1beta1/event_listener_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
}

Expand All @@ -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

Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/triggers/v1beta1/event_listener_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
74 changes: 73 additions & 1 deletion pkg/apis/triggers/v1beta1/event_listener_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
"k8s.io/client-go/kubernetes"
)

var(
var (
emptyExtensions = map[string]interface{}{}
)

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit aee6879

Please sign in to comment.