Skip to content

Commit bbcbeeb

Browse files
authored
Introduce omitLookup config flag (#115)
This flag wraps the Label and Annotation lookup to prevent API throttling Signed-off-by: Boris Petersen <[email protected]>
1 parent 9e7f84b commit bbcbeeb

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func main() {
8787
}
8888
}
8989

90-
w := kube.NewEventWatcher(kubecfg, cfg.Namespace, cfg.MaxEventAgeSeconds, metricsStore, onEvent)
90+
w := kube.NewEventWatcher(kubecfg, cfg.Namespace, cfg.MaxEventAgeSeconds, metricsStore, onEvent, cfg.OmitLookup)
9191

9292
ctx, cancel := context.WithCancel(context.Background())
9393
leaderLost := make(chan bool)

pkg/exporter/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ type Config struct {
2626
Receivers []sinks.ReceiverConfig `yaml:"receivers"`
2727
KubeQPS float32 `yaml:"kubeQPS,omitempty"`
2828
KubeBurst int `yaml:"kubeBurst,omitempty"`
29-
MetricsNamePrefix string `yaml:"metricsNamePrefix,omitempty"`
29+
MetricsNamePrefix string `yaml:"metricsNamePrefix,omitempty"`
30+
OmitLookup bool `yaml:"omitLookup,omitempty"`
3031
}
3132

3233
func (c *Config) Validate() error {

pkg/kube/watcher.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ type EventWatcher struct {
2020
informer cache.SharedInformer
2121
stopper chan struct{}
2222
labelCache *LabelCache
23+
omitLookup bool
2324
annotationCache *AnnotationCache
2425
fn EventHandler
2526
maxEventAgeSeconds time.Duration
2627
metricsStore *metrics.Store
2728
}
2829

29-
func NewEventWatcher(config *rest.Config, namespace string, MaxEventAgeSeconds int64, metricsStore *metrics.Store, fn EventHandler) *EventWatcher {
30+
func NewEventWatcher(config *rest.Config, namespace string, MaxEventAgeSeconds int64, metricsStore *metrics.Store, fn EventHandler, omitLookup bool) *EventWatcher {
3031
clientset := kubernetes.NewForConfigOrDie(config)
3132
factory := informers.NewSharedInformerFactoryWithOptions(clientset, 0, informers.WithNamespace(namespace))
3233
informer := factory.Core().V1().Events().Informer()
@@ -35,6 +36,7 @@ func NewEventWatcher(config *rest.Config, namespace string, MaxEventAgeSeconds i
3536
informer: informer,
3637
stopper: make(chan struct{}),
3738
labelCache: NewLabelCache(config),
39+
omitLookup: omitLookup,
3840
annotationCache: NewAnnotationCache(config),
3941
fn: fn,
4042
maxEventAgeSeconds: time.Second * time.Duration(MaxEventAgeSeconds),
@@ -100,29 +102,33 @@ func (e *EventWatcher) onEvent(event *corev1.Event) {
100102
}
101103
ev.Event.ManagedFields = nil
102104

103-
labels, err := e.labelCache.GetLabelsWithCache(&event.InvolvedObject)
104-
if err != nil {
105-
if ev.InvolvedObject.Kind != "CustomResourceDefinition" {
106-
log.Error().Err(err).Msg("Cannot list labels of the object")
105+
if e.omitLookup {
106+
ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy()
107+
} else {
108+
labels, err := e.labelCache.GetLabelsWithCache(&event.InvolvedObject)
109+
if err != nil {
110+
if ev.InvolvedObject.Kind != "CustomResourceDefinition" {
111+
log.Error().Err(err).Msg("Cannot list labels of the object")
112+
} else {
113+
log.Debug().Err(err).Msg("Cannot list labels of the object (CRD)")
114+
}
115+
// Ignoring error, but log it anyways
107116
} else {
108-
log.Debug().Err(err).Msg("Cannot list labels of the object (CRD)")
117+
ev.InvolvedObject.Labels = labels
118+
ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy()
109119
}
110-
// Ignoring error, but log it anyways
111-
} else {
112-
ev.InvolvedObject.Labels = labels
113-
ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy()
114-
}
115120

116-
annotations, err := e.annotationCache.GetAnnotationsWithCache(&event.InvolvedObject)
117-
if err != nil {
118-
if ev.InvolvedObject.Kind != "CustomResourceDefinition" {
119-
log.Error().Err(err).Msg("Cannot list annotations of the object")
121+
annotations, err := e.annotationCache.GetAnnotationsWithCache(&event.InvolvedObject)
122+
if err != nil {
123+
if ev.InvolvedObject.Kind != "CustomResourceDefinition" {
124+
log.Error().Err(err).Msg("Cannot list annotations of the object")
125+
} else {
126+
log.Debug().Err(err).Msg("Cannot list annotations of the object (CRD)")
127+
}
120128
} else {
121-
log.Debug().Err(err).Msg("Cannot list annotations of the object (CRD)")
129+
ev.InvolvedObject.Annotations = annotations
130+
ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy()
122131
}
123-
} else {
124-
ev.InvolvedObject.Annotations = annotations
125-
ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy()
126132
}
127133

128134
e.fn(ev)

0 commit comments

Comments
 (0)