From cb966ebf6b7a73dae412b7e72a76c16173fa812b Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Fri, 21 Jul 2023 09:06:51 +0000 Subject: [PATCH] fix(inputs.prometheus): Fix missing metrics when multiple plugin instances specified (#13627) --- plugins/inputs/prometheus/kubernetes.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/plugins/inputs/prometheus/kubernetes.go b/plugins/inputs/prometheus/kubernetes.go index fccb1a905..9bf7fa9b2 100644 --- a/plugins/inputs/prometheus/kubernetes.go +++ b/plugins/inputs/prometheus/kubernetes.go @@ -125,8 +125,8 @@ func shouldScrapePod(pod *corev1.Pod, p *Prometheus) bool { return isCandidate && shouldScrape } -// Share informer across all instances of this plugin -var informerfactory informers.SharedInformerFactory +// Share informer per namespace across all instances of this plugin +var informerfactory map[string]informers.SharedInformerFactory // An edge case exists if a pod goes offline at the same time a new pod is created // (without the scrape annotations). K8s may re-assign the old pod ip to the non-scrape @@ -142,16 +142,23 @@ func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clients } if informerfactory == nil { + informerfactory = make(map[string]informers.SharedInformerFactory) + } + + var f informers.SharedInformerFactory + var ok bool + if f, ok = informerfactory[p.PodNamespace]; !ok { var informerOptions []informers.SharedInformerOption if p.PodNamespace != "" { informerOptions = append(informerOptions, informers.WithNamespace(p.PodNamespace)) } - informerfactory = informers.NewSharedInformerFactoryWithOptions(clientset, resyncinterval, informerOptions...) + f = informers.NewSharedInformerFactoryWithOptions(clientset, resyncinterval, informerOptions...) + informerfactory[p.PodNamespace] = f } - p.nsStore = informerfactory.Core().V1().Namespaces().Informer().GetStore() + p.nsStore = f.Core().V1().Namespaces().Informer().GetStore() - podinformer := informerfactory.Core().V1().Pods() + podinformer := f.Core().V1().Pods() _, err := podinformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: func(newObj interface{}) { newPod, ok := newObj.(*corev1.Pod) @@ -195,8 +202,8 @@ func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clients }, }) - informerfactory.Start(ctx.Done()) - informerfactory.WaitForCacheSync(wait.NeverStop) + f.Start(ctx.Done()) + f.WaitForCacheSync(wait.NeverStop) return err }