From bbcbeeb38571994da6691727d125f7d990707669 Mon Sep 17 00:00:00 2001 From: Boris Petersen Date: Fri, 4 Aug 2023 18:48:46 +0200 Subject: [PATCH] Introduce omitLookup config flag (#115) This flag wraps the Label and Annotation lookup to prevent API throttling Signed-off-by: Boris Petersen --- main.go | 2 +- pkg/exporter/config.go | 3 ++- pkg/kube/watcher.go | 44 ++++++++++++++++++++++++------------------ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index f9e2d00e..acdd2476 100644 --- a/main.go +++ b/main.go @@ -87,7 +87,7 @@ func main() { } } - w := kube.NewEventWatcher(kubecfg, cfg.Namespace, cfg.MaxEventAgeSeconds, metricsStore, onEvent) + w := kube.NewEventWatcher(kubecfg, cfg.Namespace, cfg.MaxEventAgeSeconds, metricsStore, onEvent, cfg.OmitLookup) ctx, cancel := context.WithCancel(context.Background()) leaderLost := make(chan bool) diff --git a/pkg/exporter/config.go b/pkg/exporter/config.go index baabcbb9..20be87ce 100644 --- a/pkg/exporter/config.go +++ b/pkg/exporter/config.go @@ -26,7 +26,8 @@ type Config struct { Receivers []sinks.ReceiverConfig `yaml:"receivers"` KubeQPS float32 `yaml:"kubeQPS,omitempty"` KubeBurst int `yaml:"kubeBurst,omitempty"` - MetricsNamePrefix string `yaml:"metricsNamePrefix,omitempty"` + MetricsNamePrefix string `yaml:"metricsNamePrefix,omitempty"` + OmitLookup bool `yaml:"omitLookup,omitempty"` } func (c *Config) Validate() error { diff --git a/pkg/kube/watcher.go b/pkg/kube/watcher.go index 9d0cb59f..02c7bf2b 100644 --- a/pkg/kube/watcher.go +++ b/pkg/kube/watcher.go @@ -20,13 +20,14 @@ type EventWatcher struct { informer cache.SharedInformer stopper chan struct{} labelCache *LabelCache + omitLookup bool annotationCache *AnnotationCache fn EventHandler maxEventAgeSeconds time.Duration metricsStore *metrics.Store } -func NewEventWatcher(config *rest.Config, namespace string, MaxEventAgeSeconds int64, metricsStore *metrics.Store, fn EventHandler) *EventWatcher { +func NewEventWatcher(config *rest.Config, namespace string, MaxEventAgeSeconds int64, metricsStore *metrics.Store, fn EventHandler, omitLookup bool) *EventWatcher { clientset := kubernetes.NewForConfigOrDie(config) factory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, informers.WithNamespace(namespace)) informer := factory.Core().V1().Events().Informer() @@ -35,6 +36,7 @@ func NewEventWatcher(config *rest.Config, namespace string, MaxEventAgeSeconds i informer: informer, stopper: make(chan struct{}), labelCache: NewLabelCache(config), + omitLookup: omitLookup, annotationCache: NewAnnotationCache(config), fn: fn, maxEventAgeSeconds: time.Second * time.Duration(MaxEventAgeSeconds), @@ -100,29 +102,33 @@ func (e *EventWatcher) onEvent(event *corev1.Event) { } ev.Event.ManagedFields = nil - labels, err := e.labelCache.GetLabelsWithCache(&event.InvolvedObject) - if err != nil { - if ev.InvolvedObject.Kind != "CustomResourceDefinition" { - log.Error().Err(err).Msg("Cannot list labels of the object") + if e.omitLookup { + ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy() + } else { + labels, err := e.labelCache.GetLabelsWithCache(&event.InvolvedObject) + if err != nil { + if ev.InvolvedObject.Kind != "CustomResourceDefinition" { + log.Error().Err(err).Msg("Cannot list labels of the object") + } else { + log.Debug().Err(err).Msg("Cannot list labels of the object (CRD)") + } + // Ignoring error, but log it anyways } else { - log.Debug().Err(err).Msg("Cannot list labels of the object (CRD)") + ev.InvolvedObject.Labels = labels + ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy() } - // Ignoring error, but log it anyways - } else { - ev.InvolvedObject.Labels = labels - ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy() - } - annotations, err := e.annotationCache.GetAnnotationsWithCache(&event.InvolvedObject) - if err != nil { - if ev.InvolvedObject.Kind != "CustomResourceDefinition" { - log.Error().Err(err).Msg("Cannot list annotations of the object") + annotations, err := e.annotationCache.GetAnnotationsWithCache(&event.InvolvedObject) + if err != nil { + if ev.InvolvedObject.Kind != "CustomResourceDefinition" { + log.Error().Err(err).Msg("Cannot list annotations of the object") + } else { + log.Debug().Err(err).Msg("Cannot list annotations of the object (CRD)") + } } else { - log.Debug().Err(err).Msg("Cannot list annotations of the object (CRD)") + ev.InvolvedObject.Annotations = annotations + ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy() } - } else { - ev.InvolvedObject.Annotations = annotations - ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy() } e.fn(ev)