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

Commit

Permalink
fix(distributor): Include event filter for project, stage, service (#…
Browse files Browse the repository at this point in the history
…6968) (#6972)

* fix(distributor): Include event filter for project, stage, service

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>

* fix typo

Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl authored Feb 23, 2022
1 parent 2181a5e commit 6ab050d
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
29 changes: 26 additions & 3 deletions distributor/pkg/lib/events/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func (p *Poller) doPollEvents() {
}

func (p *Poller) pollEventsForSubscription(subscription keptnmodels.EventSubscription) {
events, err := p.shipyardControlAPI.GetOpenTriggeredEvents(api.EventFilter{
EventType: subscription.Event,
})

eventFilter := getEventFilterForSubscription(subscription)
events, err := p.shipyardControlAPI.GetOpenTriggeredEvents(eventFilter)
if err != nil {
logger.Errorf("Could not retrieve events of type %s: %s", subscription.Event, err)
return
Expand Down Expand Up @@ -112,6 +112,29 @@ func (p *Poller) pollEventsForSubscription(subscription keptnmodels.EventSubscri
p.ceCache.Keep(subscription.ID, ToIDs(events))
}

// getEventFilterForSubscription returns the event filter for the subscription
// Per default, it only sets the event type of the subscription.
// If exactly one project, stage or service is specified respectively, they are included in the filter.
// However, this is only a (very) short term solution for the RBAC use case.
// In the long term, we should just pass the subscription ID in the request, since the backend knows the required filters associated with the subscription.
func getEventFilterForSubscription(subscription keptnmodels.EventSubscription) api.EventFilter {
eventFilter := api.EventFilter{
EventType: subscription.Event,
}

if len(subscription.Filter.Projects) == 1 {
eventFilter.Project = subscription.Filter.Projects[0]
}
if len(subscription.Filter.Stages) == 1 {
eventFilter.Stage = subscription.Filter.Stages[0]
}
if len(subscription.Filter.Services) == 1 {
eventFilter.Service = subscription.Filter.Services[0]
}

return eventFilter
}

func (p *Poller) sendEvent(e keptnmodels.KeptnContextExtendedCE, subscription keptnmodels.EventSubscription) error {
event := v0_2_0.ToCloudEvent(e)
matcher := NewEventMatcherFromSubscription(subscription)
Expand Down
96 changes: 96 additions & 0 deletions distributor/pkg/lib/events/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -178,3 +179,98 @@ func Test_PollAndForwardEvents2(t *testing.T) {
cancel()
executionContext.Wg.Wait()
}

func Test_getEventFilterForSubscription(t *testing.T) {
type args struct {
subscription keptnmodels.EventSubscription
}
tests := []struct {
name string
args args
want keptnapi.EventFilter
}{
{
name: "get default filter",
args: args{
subscription: keptnmodels.EventSubscription{
Event: "my-event",
},
},
want: keptnapi.EventFilter{
EventType: "my-event",
},
},
{
name: "multiple projects - get default filter",
args: args{
subscription: keptnmodels.EventSubscription{
Event: "my-event",
Filter: keptnmodels.EventSubscriptionFilter{
Projects: []string{"a", "b"},
},
},
},
want: keptnapi.EventFilter{
EventType: "my-event",
},
},
{
name: "one project",
args: args{
subscription: keptnmodels.EventSubscription{
Event: "my-event",
Filter: keptnmodels.EventSubscriptionFilter{
Projects: []string{"a"},
},
},
},
want: keptnapi.EventFilter{
EventType: "my-event",
Project: "a",
},
},
{
name: "one project, one stage",
args: args{
subscription: keptnmodels.EventSubscription{
Event: "my-event",
Filter: keptnmodels.EventSubscriptionFilter{
Projects: []string{"a"},
Stages: []string{"stage-a"},
},
},
},
want: keptnapi.EventFilter{
EventType: "my-event",
Project: "a",
Stage: "stage-a",
},
},
{
name: "one project, one stage, one service",
args: args{
subscription: keptnmodels.EventSubscription{
Event: "my-event",
Filter: keptnmodels.EventSubscriptionFilter{
Projects: []string{"a"},
Stages: []string{"stage-a"},
Services: []string{"service-a"},
},
},
},
want: keptnapi.EventFilter{
EventType: "my-event",
Project: "a",
Stage: "stage-a",
Service: "service-a",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getEventFilterForSubscription(tt.args.subscription); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getEventFilterForSubscription() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 6ab050d

Please sign in to comment.