fix(inputs.prometheus): Fix missing metrics when multiple plugin instances specified (#13627)

This commit is contained in:
Maxim Ivanov 2023-07-21 09:06:51 +00:00 committed by GitHub
parent 42c7a2027f
commit cb966ebf6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 7 deletions

View File

@ -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
}