fix(inputs.prometheus): Fix missing metrics when multiple plugin instances specified (#13627)
This commit is contained in:
parent
42c7a2027f
commit
cb966ebf6b
|
|
@ -125,8 +125,8 @@ func shouldScrapePod(pod *corev1.Pod, p *Prometheus) bool {
|
||||||
return isCandidate && shouldScrape
|
return isCandidate && shouldScrape
|
||||||
}
|
}
|
||||||
|
|
||||||
// Share informer across all instances of this plugin
|
// Share informer per namespace across all instances of this plugin
|
||||||
var informerfactory informers.SharedInformerFactory
|
var informerfactory map[string]informers.SharedInformerFactory
|
||||||
|
|
||||||
// An edge case exists if a pod goes offline at the same time a new pod is created
|
// 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
|
// (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 {
|
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
|
var informerOptions []informers.SharedInformerOption
|
||||||
if p.PodNamespace != "" {
|
if p.PodNamespace != "" {
|
||||||
informerOptions = append(informerOptions, informers.WithNamespace(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{
|
_, err := podinformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||||
AddFunc: func(newObj interface{}) {
|
AddFunc: func(newObj interface{}) {
|
||||||
newPod, ok := newObj.(*corev1.Pod)
|
newPod, ok := newObj.(*corev1.Pod)
|
||||||
|
|
@ -195,8 +202,8 @@ func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clients
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
informerfactory.Start(ctx.Done())
|
f.Start(ctx.Done())
|
||||||
informerfactory.WaitForCacheSync(wait.NeverStop)
|
f.WaitForCacheSync(wait.NeverStop)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue