diff --git a/plugins/inputs/prometheus/prometheus.go b/plugins/inputs/prometheus/prometheus.go index 5d4a4dc6b..28f92b608 100644 --- a/plugins/inputs/prometheus/prometheus.go +++ b/plugins/inputs/prometheus/prometheus.go @@ -134,7 +134,14 @@ func (p *Prometheus) Init() error { p.NodeIP = envVarNodeIP } + p.Log.Infof("Using pod scrape scope at node level to get pod list using cAdvisor.") + } + if p.MonitorKubernetesPodsMethod == MonitorMethodNone { + p.MonitorKubernetesPodsMethod = MonitorMethodAnnotations + } + + if p.isNodeScrapeScope || p.MonitorKubernetesPodsMethod != MonitorMethodAnnotations { // Parse label and field selectors - will be used to filter pods after cAdvisor call var err error p.podLabelSelector, err = labels.Parse(p.KubernetesLabelSelector) @@ -150,14 +157,9 @@ func (p *Prometheus) Init() error { return fmt.Errorf("the field selector %s is not supported for pods", invalidSelector) } - p.Log.Infof("Using pod scrape scope at node level to get pod list using cAdvisor.") p.Log.Infof("Using the label selector: %v and field selector: %v", p.podLabelSelector, p.podFieldSelector) } - if p.MonitorKubernetesPodsMethod == MonitorMethodNone { - p.MonitorKubernetesPodsMethod = MonitorMethodAnnotations - } - ctx := context.Background() client, err := p.HTTPClientConfig.CreateClient(ctx, p.Log) if err != nil { diff --git a/plugins/inputs/prometheus/prometheus_test.go b/plugins/inputs/prometheus/prometheus_test.go index a8258a37b..d89f94ec0 100644 --- a/plugins/inputs/prometheus/prometheus_test.go +++ b/plugins/inputs/prometheus/prometheus_test.go @@ -326,3 +326,22 @@ func TestInitConfigErrors(t *testing.T) { expectedMessage = "the field selector spec.containerNames is not supported for pods" require.Error(t, err, expectedMessage) } + +func TestInitConfigSelectors(t *testing.T) { + p := &Prometheus{ + MetricVersion: 2, + Log: testutil.Logger{}, + URLs: nil, + URLTag: "url", + MonitorPods: true, + MonitorKubernetesPodsMethod: MonitorMethodSettings, + PodScrapeInterval: 60, + KubernetesLabelSelector: "app=test", + KubernetesFieldSelector: "spec.nodeName=node-0", + } + err := p.Init() + require.NoError(t, err) + + require.NotNil(t, p.podLabelSelector) + require.NotNil(t, p.podFieldSelector) +}