Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
huseyinbabal committed Feb 19, 2023
1 parent 8243679 commit 7ef0031
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 45 deletions.
6 changes: 0 additions & 6 deletions cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ func run(ctx context.Context) error {
return reportFatalError("while creating executor factory", err)
}

router := source.NewRouter(mapper, dynamicCli, logger.WithField(componentLogFieldKey, "Router"))

var (
notifiers []notifier.Notifier
bots = map[string]bot.Bot{}
Expand All @@ -221,8 +219,6 @@ func run(ctx context.Context) error {
for commGroupName, commGroupCfg := range conf.Communications {
commGroupLogger := logger.WithField(commGroupFieldKey, commGroupName)

router.AddCommunicationsBindings(commGroupCfg)

scheduleBot := func(in bot.Bot) {
notifiers = append(notifiers, in)
bots[fmt.Sprintf("%s-%s", commGroupName, in.IntegrationName())] = in
Expand Down Expand Up @@ -349,8 +345,6 @@ func run(ctx context.Context) error {
}

actionProvider := action.NewProvider(logger.WithField(componentLogFieldKey, "Action Provider"), conf.Actions, executorFactory)
router.AddEnabledActionBindings(conf.Actions)
router.AddEnabledActionBindings(conf.Actions)

sourcePluginDispatcher := source.NewDispatcher(logger, notifiers, pluginManager, actionProvider, reporter)
scheduler := source.NewScheduler(logger, conf, sourcePluginDispatcher)
Expand Down
16 changes: 10 additions & 6 deletions internal/source/kubernetes/recommendation/resource_events.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package recommendation

import (
config2 "github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
"github.com/kubeshop/botkube/pkg/ptr"
)
Expand All @@ -12,22 +12,26 @@ const (
)

// ResourceEventsForConfig returns the resource event map for a given source recommendations config.
func ResourceEventsForConfig(recCfg config2.Recommendations) map[string]config2.EventType {
resTypes := make(map[string]config2.EventType)
func ResourceEventsForConfig(recCfg *config.Recommendations) map[string]config.EventType {
resTypes := make(map[string]config.EventType)
if recCfg == nil {
return resTypes
}

if ptr.IsTrue(recCfg.Ingress.TLSSecretValid) || ptr.IsTrue(recCfg.Ingress.BackendServiceValid) {
resTypes[ingressResourceType] = config2.CreateEvent
resTypes[ingressResourceType] = config.CreateEvent
}

if ptr.IsTrue(recCfg.Pod.NoLatestImageTag) || ptr.IsTrue(recCfg.Pod.LabelsSet) {
resTypes[podsResourceType] = config2.CreateEvent

resTypes[podsResourceType] = config.CreateEvent
}

return resTypes
}

// ShouldIgnoreEvent returns true if user doesn't listen to events for a given resource, apart from enabled recommendations.
func ShouldIgnoreEvent(recCfg config2.Recommendations, event event.Event) bool {
func ShouldIgnoreEvent(recCfg *config.Recommendations, event event.Event) bool {
if event.HasRecommendationsOrWarnings() {
// shouldn't be skipped
return false
Expand Down
28 changes: 3 additions & 25 deletions internal/source/kubernetes/recommendation/resource_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestResourceEventsForConfig(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
// when
actual := recommendation.ResourceEventsForConfig(testCase.RecCfg)
actual := recommendation.ResourceEventsForConfig(&testCase.RecCfg)

// then
assert.Equal(t, testCase.Expected, actual)
Expand Down Expand Up @@ -140,29 +140,7 @@ func TestShouldIgnoreEvent(t *testing.T) {
Type: config.CreateEvent,
},
InputSourceBindings: []string{"deployments", "pods"},
Expected: false,
},
{
Name: "User configured such event with source-wide namespace",
InputConfig: fixFullRecommendationConfig(),
InputEvent: event.Event{
Resource: recommendation.PodResourceType(),
Namespace: "default",
Type: config.CreateEvent,
},
InputSourceBindings: []string{"deployments", "pods-source-wide-ns"},
Expected: false,
},
{
Name: "User configured such event with source-wide namespace and resource ns override",
InputConfig: fixFullRecommendationConfig(),
InputEvent: event.Event{
Resource: recommendation.PodResourceType(),
Namespace: "kube-system",
Type: config.CreateEvent,
},
InputSourceBindings: []string{"deployments", "pods-ns-override"},
Expected: false,
Expected: true,
},
{
Name: "User didn't configure such resource",
Expand Down Expand Up @@ -202,7 +180,7 @@ func TestShouldIgnoreEvent(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
// when
actual := recommendation.ShouldIgnoreEvent(testCase.InputConfig, testCase.InputEvent)
actual := recommendation.ShouldIgnoreEvent(&testCase.InputConfig, testCase.InputEvent)

// then
assert.Equal(t, testCase.Expected, actual)
Expand Down
4 changes: 2 additions & 2 deletions internal/source/kubernetes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func mergeResourceEvents(cfg *config.Config) mergedEvents {
}
}

resForRecomms := recommendation.ResourceEventsForConfig(*cfg.Recommendations)
resForRecomms := recommendation.ResourceEventsForConfig(cfg.Recommendations)
for resourceType, eventType := range resForRecomms {
if _, ok := out[resourceType]; !ok {
out[resourceType] = make(map[config.EventType]struct{})
Expand Down Expand Up @@ -192,7 +192,7 @@ func (r *Router) mergeEventRoutes(resource string, cfg *config.Config) map[confi
}

// add routes related to recommendations
resForRecomms := recommendation.ResourceEventsForConfig(*cfg.Recommendations)
resForRecomms := recommendation.ResourceEventsForConfig(cfg.Recommendations)
r.setEventRouteForRecommendationsIfShould(&out, resForRecomms, resource)

return out
Expand Down
2 changes: 1 addition & 1 deletion internal/source/kubernetes/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (s *Source) handleEvent(ctx context.Context, event event.Event, updateDiffs
s.logger.Errorf("while running recommendations: %w", err)
}

if recommendation.ShouldIgnoreEvent(recCfg, event) {
if recommendation.ShouldIgnoreEvent(&recCfg, event) {
s.logger.Debugf("Skipping event as it is related to recommendation informers and doesn't have any recommendations: %#v", event)
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package controller
package kubernetes

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime/schema"
"testing"
)

// TODO: Refactor these tests as a part of https://github.com/kubeshop/botkube/issues/589
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestController_strToGVR(t *testing.T) {

for _, testCase := range tests {
t.Run(testCase.Name, func(t *testing.T) {
c := Controller{}
c := Source{}

res, err := c.strToGVR(testCase.Input)

Expand Down
2 changes: 1 addition & 1 deletion pkg/bot/interactive/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestInteractiveMessageToMarkdownMultiSelect(t *testing.T) {
},
},
{
Name: "Event",
Name: "Data",
Options: []api.OptionItem{
{
Name: "configmap",
Expand Down

0 comments on commit 7ef0031

Please sign in to comment.