Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic on missing root event type for k8s source #1481

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading