Skip to content

Commit

Permalink
fix: plugins(inputs.kubernetes) check for errors when adding pod info…
Browse files Browse the repository at this point in the history
…rmer event handler
  • Loading branch information
LukeWinikates committed May 1, 2023
1 parent 413e6ad commit 2e75e3e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions plugins/inputs/prometheus/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func (p *Prometheus) startK8s(ctx context.Context) error {
p.Log.Errorf("Unable to monitor pods with node scrape scope: %s", err.Error())
}
} else {
p.watchPod(ctx, client)
err = p.watchPod(ctx, client)
if err != nil {
p.Log.Errorf("Unable to monitor pod: %s", err.Error())
}
}
}
}
Expand Down Expand Up @@ -125,7 +128,7 @@ var informerfactory informers.SharedInformerFactory
// (without the scrape annotations). K8s may re-assign the old pod ip to the non-scrape
// pod, causing errors in the logs. This is only true if the pod going offline is not
// directed to do so by K8s.
func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clientset) {
func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clientset) error {
var resyncinterval time.Duration

if p.CacheRefreshInterval != 0 {
Expand All @@ -145,7 +148,7 @@ func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clients
p.nsStore = informerfactory.Core().V1().Namespaces().Informer().GetStore()

podinformer := informerfactory.Core().V1().Pods()
podinformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
_, err := podinformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(newObj interface{}) {
newPod, ok := newObj.(*corev1.Pod)
if !ok {
Expand Down Expand Up @@ -188,10 +191,16 @@ func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clients
},
})

if err != nil {
<-ctx.Done()
return err
}

informerfactory.Start(ctx.Done())
informerfactory.WaitForCacheSync(wait.NeverStop)

<-ctx.Done()
return err
}

func (p *Prometheus) cAdvisor(ctx context.Context, bearerToken string) error {
Expand Down

0 comments on commit 2e75e3e

Please sign in to comment.