Skip to content

Commit

Permalink
events: fix for G104 (CWE-703)
Browse files Browse the repository at this point in the history
Reported by gosec.

G104 (CWE-703): Errors unhandled. (Confidence: HIGH, Severity: LOW)

This commit handles the previously ignored errors.
  • Loading branch information
dnaeon committed Oct 28, 2024
1 parent eeb6694 commit c232606
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
5 changes: 4 additions & 1 deletion cmd/event-logger/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ func (o *Options) Run(stopCh <-chan struct{}) error {
return err
}

eventLogger := config.New()
eventLogger, err := config.New()
if err != nil {
return err
}

eventLogger.Run(stopCh)

Expand Down
36 changes: 21 additions & 15 deletions pkg/events/events_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// NewEventInformerFuncForNamespace returns function which creates new event informer for a given namespace.
func NewEventInformerFuncForNamespace(origin, namespace string) kubeinformersinterfaces.NewInformerFunc {
func NewEventInformerFuncForNamespace(namespace string) kubeinformersinterfaces.NewInformerFunc {
return func(clientset kubernetes.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
watchlist := cache.NewListWatchFromClient(
clientset.CoreV1().RESTClient(),
Expand All @@ -31,24 +31,30 @@ func NewEventInformerFuncForNamespace(origin, namespace string) kubeinformersint
resyncPeriod,
cache.Indexers{},
)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if isV1Event(obj) {
v1Event := obj.(*v1.Event)
printV1Event(v1Event, origin)
}
},
UpdateFunc: func(_ interface{}, newObject interface{}) {
if isV1Event(newObject) {
v1Event := newObject.(*v1.Event)
printV1Event(v1Event, origin)
}
},
})

return informer
}
}

func addEventHandler(informer cache.SharedIndexInformer, origin string) error {
_, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if isV1Event(obj) {
v1Event := obj.(*v1.Event)
printV1Event(v1Event, origin)
}
},
UpdateFunc: func(_ interface{}, newObject interface{}) {
if isV1Event(newObject) {
v1Event := newObject.(*v1.Event)
printV1Event(v1Event, origin)
}
},
})

return err
}

func isV1Event(obj interface{}) bool {
_, ok := obj.(*v1.Event)
return ok
Expand Down
28 changes: 16 additions & 12 deletions pkg/events/gardener_event_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,35 @@ type GardenerEventWatcher struct {
}

// New returns new GardenerEventWatcherConfig
func (e *GardenerEventWatcherConfig) New() *GardenerEventWatcher {
func (e *GardenerEventWatcherConfig) New() (*GardenerEventWatcher, error) {
for indx, namespace := range e.SeedEventWatcherConfig.Namespaces {
_ = e.SeedKubeInformerFactories[indx].InformerFor(&v1.Event{},
NewEventInformerFuncForNamespace(
"seed",
namespace,
),
informer := e.SeedKubeInformerFactories[indx].InformerFor(
&v1.Event{},
NewEventInformerFuncForNamespace(namespace),
)
if err := addEventHandler(informer, "seed"); err != nil {
return nil, err
}
}

if e.ShootEventWatcherConfig.Kubeconfig != "" {
for indx, namespace := range e.ShootEventWatcherConfig.Namespaces {
_ = e.ShootKubeInformerFactories[indx].InformerFor(&v1.Event{},
NewEventInformerFuncForNamespace(
"shoot",
namespace,
),
informer := e.ShootKubeInformerFactories[indx].InformerFor(
&v1.Event{},
NewEventInformerFuncForNamespace(namespace),
)
if err := addEventHandler(informer, "shoot"); err != nil {
return nil, err
}
}
}

return &GardenerEventWatcher{
watcher := &GardenerEventWatcher{
SeedKubeInformerFactories: e.SeedKubeInformerFactories,
ShootKubeInformerFactories: e.ShootKubeInformerFactories,
}

return watcher, nil
}

// Run start the GardenerEventWatcher lifecycle
Expand Down

0 comments on commit c232606

Please sign in to comment.