Skip to content

Commit

Permalink
fix(inputs.prometheus): List namespaces only when filtering by namesp…
Browse files Browse the repository at this point in the history
…ace (#14871)
  • Loading branch information
srebhan committed Mar 7, 2024
1 parent a6d550e commit 4664b6d
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions plugins/inputs/prometheus/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ func (p *Prometheus) watchPod(ctx context.Context, clientset *kubernetes.Clients
informerfactory[p.PodNamespace] = f
}

p.nsStore = f.Core().V1().Namespaces().Informer().GetStore()
if p.nsAnnotationPass != nil || p.nsAnnotationDrop != nil {
p.nsStore = f.Core().V1().Namespaces().Informer().GetStore()
}

podinformer := f.Core().V1().Pods()
_, err := podinformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
Expand Down Expand Up @@ -335,9 +337,6 @@ func podHasMatchingFieldSelector(pod *corev1.Pod, fieldSelector fields.Selector)

// Get corev1.Namespace object by name
func getNamespaceObject(name string, p *Prometheus) *corev1.Namespace {
if p.nsStore == nil { // can happen in tests
return nil
}
nsObj, exists, err := p.nsStore.GetByKey(name)
if err != nil {
p.Log.Errorf("Err fetching namespace '%s': %v", name, err)
Expand All @@ -354,9 +353,13 @@ func getNamespaceObject(name string, p *Prometheus) *corev1.Namespace {
}

func namespaceAnnotationMatch(nsName string, p *Prometheus) bool {
// In case of no filtering or any issues with acquiring namespace information
// just let it pass trough...
if (p.nsAnnotationPass == nil && p.nsAnnotationDrop == nil) || p.nsStore == nil {
return true
}
ns := getNamespaceObject(nsName, p)
if ns == nil {
// in case of errors or other problems let it through
return true
}

Expand Down

0 comments on commit 4664b6d

Please sign in to comment.