Skip to content

Commit

Permalink
Fix panic on missing root event type for k8s source (#1481)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok authored Nov 11, 2024
1 parent ad313c8 commit 2b275b4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
24 changes: 14 additions & 10 deletions internal/source/kubernetes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"context"
"strings"

"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -156,7 +157,7 @@ func mergeResourceEvents(cfgs map[string]SourceConfig) mergedEvents {
if _, ok := out[resource.Type]; !ok {
out[resource.Type] = make(map[config.EventType]struct{})
}
for _, e := range flattenEventTypes(cfg.Event.Types, resource.Event.Types) {
for _, e := range flattenEventTypes(cfg.Event, resource.Event) {
out[resource.Type][e] = struct{}{}
}
}
Expand All @@ -178,7 +179,7 @@ func (r *Router) mergeEventRoutes(resource string, cfgs map[string]SourceConfig)
cfg := srcCfg.cfg
for idx := range cfg.Resources {
r := cfg.Resources[idx] // make sure that we work on a copy
for _, e := range flattenEventTypes(cfg.Event.Types, r.Event.Types) {
for _, e := range flattenEventTypes(cfg.Event, r.Event) {
if resource != r.Type {
continue
}
Expand All @@ -188,7 +189,7 @@ func (r *Router) mergeEventRoutes(resource string, cfgs map[string]SourceConfig)
Annotations: resourceStringMap(cfg.Annotations, r.Annotations),
Labels: resourceStringMap(cfg.Labels, r.Labels),
ResourceName: r.Name,
Event: resourceEvent(*cfg.Event, r.Event),
Event: resourceEvent(cfg.Event, r.Event),
}
if e == config.UpdateEvent {
route.UpdateSetting = &config.UpdateSetting{
Expand Down Expand Up @@ -248,7 +249,7 @@ func (r *Router) setEventRouteForRecommendationsIfShould(routeMap *map[config.Ev
func eventRoutes(routeTable map[string][]entry, targetResource string, targetEvent config.EventType) []route {
var out []route
for _, routedEvent := range routeTable[targetResource] {
if routedEvent.Event == targetEvent {
if strings.EqualFold(string(routedEvent.Event), string(targetEvent)) {
out = append(out, routedEvent.Routes...)
}
}
Expand Down Expand Up @@ -287,10 +288,13 @@ func (r *Router) mappedInformer(event config.EventType) (registration, bool) {
return registration{}, false
}

func flattenEventTypes(globalEvents []config.EventType, resourceEvents config.KubernetesResourceEventTypes) []config.EventType {
checkEvents := globalEvents
if len(resourceEvents) > 0 {
checkEvents = resourceEvents
func flattenEventTypes(globalEvents *config.KubernetesEvent, resourceEvents config.KubernetesEvent) []config.EventType {
var checkEvents []config.EventType
if globalEvents != nil {
checkEvents = globalEvents.Types
}
if len(resourceEvents.Types) > 0 {
checkEvents = resourceEvents.Types
}

var out []config.EventType
Expand Down Expand Up @@ -321,10 +325,10 @@ func resourceStringMap(sourceMap *map[string]string, resourceMap map[string]stri
return sourceMap
}

func resourceEvent(sourceEvent, resourceEvent config.KubernetesEvent) *config.KubernetesEvent {
func resourceEvent(sourceEvent *config.KubernetesEvent, resourceEvent config.KubernetesEvent) *config.KubernetesEvent {
if resourceEvent.AreConstraintsDefined() {
return &resourceEvent
}

return &sourceEvent
return sourceEvent
}
34 changes: 34 additions & 0 deletions internal/source/kubernetes/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,40 @@ func TestRouter_BuildTable_CreatesRoutesWithProperEventsList(t *testing.T) {
}
}

func TestRouter_BuildTable_WithoutRootTypes(t *testing.T) {
const resourceType = "autoscaling/v2/horizontalpodautoscalers"

givenCfg := map[string]SourceConfig{
"k8s-events": {
name: "k8s-events",
cfg: config.Config{
Resources: []config.Resource{
{
Type: resourceType,
Event: config.KubernetesEvent{
Reason: config.RegexConstraints{
Include: []string{
"SuccessfulRescale",
},
},
Types: config.KubernetesResourceEventTypes{
"Normal",
},
},
},
},
Namespaces: &config.RegexConstraints{
Include: []string{
".*",
},
},
},
},
}
router := NewRouter(nil, nil, loggerx.NewNoop()).BuildTable(givenCfg)
assert.Len(t, router.getSourceRoutes(resourceType, config.NormalEvent), 1)
}

func TestRouterListMergingNestedFields(t *testing.T) {
// given
router := NewRouter(nil, nil, loggerx.NewNoop())
Expand Down

0 comments on commit 2b275b4

Please sign in to comment.