Skip to content

Commit

Permalink
Introduce omitLookup config flag (#115)
Browse files Browse the repository at this point in the history
This flag wraps the Label and Annotation lookup to prevent API
throttling

Signed-off-by: Boris Petersen <boris.petersen@dkb.de>
  • Loading branch information
transacid committed Aug 4, 2023
1 parent 9e7f84b commit bbcbeeb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 25 additions & 19 deletions pkg/kube/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bbcbeeb

Please sign in to comment.