Skip to content

Commit d2b6fd8

Browse files
LashaJinichaudum
andauthored
fix(promtail): Fix API level node filtering of Kubernetes SD to only fetch pod metadata for pods of the current node (#14267)
Co-authored-by: Christian Haudum <christian.haudum@gmail.com>
1 parent 3f4c4ed commit d2b6fd8

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

‎clients/pkg/promtail/targets/file/filetargetmanager.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ func (tm *FileTargetManager) fulfillKubePodSelector(selectors []kubernetes.Selec
264264
return []kubernetes.SelectorConfig{{Role: kubernetes.RolePod, Field: nodeSelector}}
265265
}
266266

267-
for _, selector := range selectors {
268-
if selector.Field == "" {
269-
selector.Field = nodeSelector
270-
} else if !strings.Contains(selector.Field, nodeSelector) {
271-
selector.Field += "," + nodeSelector
267+
for i := range selectors {
268+
if selectors[i].Field == "" {
269+
selectors[i].Field = nodeSelector
270+
} else if !strings.Contains(selectors[i].Field, nodeSelector) {
271+
selectors[i].Field += "," + nodeSelector
272272
}
273-
selector.Role = kubernetes.RolePod
273+
selectors[i].Role = kubernetes.RolePod
274274
}
275275

276276
return selectors

‎clients/pkg/promtail/targets/file/filetargetmanager_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/go-kit/log"
1515
"github.com/prometheus/common/model"
1616
"github.com/prometheus/prometheus/discovery"
17+
"github.com/prometheus/prometheus/discovery/kubernetes"
1718
"github.com/prometheus/prometheus/discovery/targetgroup"
1819

1920
"github.com/grafana/loki/v3/clients/pkg/promtail/api"
@@ -594,3 +595,70 @@ func TestLabelSetUpdate(t *testing.T) {
594595
require.Equal(t, 0, len(syncer.fileEventWatchers))
595596

596597
}
598+
599+
func TestFulfillKubePodSelector(t *testing.T) {
600+
w := log.NewSyncWriter(os.Stderr)
601+
logger := log.NewLogfmtLogger(w)
602+
logDirName := newTestLogDirectories(t)
603+
604+
positionsFileName := filepath.Join(logDirName, "positions.yml")
605+
ps, err := newTestPositions(logger, positionsFileName)
606+
if err != nil {
607+
t.Fatal(err)
608+
}
609+
610+
client := fake.New(func() {})
611+
defer client.Stop()
612+
613+
ftm, err := newTestFileTargetManager(logger, client, ps, logDirName+"/*")
614+
assert.NoError(t, err)
615+
616+
host := "test-host"
617+
618+
// empty selectors
619+
selectors := []kubernetes.SelectorConfig{}
620+
expected := []kubernetes.SelectorConfig{
621+
{Role: kubernetes.RolePod, Field: fmt.Sprintf("%s=%s", kubernetesPodNodeField, host)},
622+
}
623+
624+
result := ftm.fulfillKubePodSelector(selectors, host)
625+
require.Equal(t, expected, result)
626+
627+
// non-empty selectors with empty field
628+
selectors = []kubernetes.SelectorConfig{
629+
{Role: kubernetes.RolePod, Field: ""},
630+
}
631+
expected = []kubernetes.SelectorConfig{
632+
{Role: kubernetes.RolePod, Field: fmt.Sprintf("%s=%s", kubernetesPodNodeField, host)},
633+
}
634+
635+
result = ftm.fulfillKubePodSelector(selectors, host)
636+
require.Equal(t, expected, result)
637+
638+
// non-empty selectors with existing field without nodeSelector
639+
selectors = []kubernetes.SelectorConfig{
640+
{Role: kubernetes.RolePod, Field: "app=frontend"},
641+
}
642+
expectedField := "app=frontend," + fmt.Sprintf("%s=%s", kubernetesPodNodeField, host)
643+
expected = []kubernetes.SelectorConfig{
644+
{Role: kubernetes.RolePod, Field: expectedField},
645+
}
646+
647+
result = ftm.fulfillKubePodSelector(selectors, host)
648+
require.Equal(t, expected, result)
649+
650+
// non-empty selectors with existing Field containing nodeSelector
651+
nodeSelector := fmt.Sprintf("%s=%s", kubernetesPodNodeField, host)
652+
selectors = []kubernetes.SelectorConfig{
653+
{Role: kubernetes.RolePod, Field: nodeSelector},
654+
}
655+
expected = []kubernetes.SelectorConfig{
656+
{Role: kubernetes.RolePod, Field: nodeSelector},
657+
}
658+
659+
result = ftm.fulfillKubePodSelector(selectors, host)
660+
require.Equal(t, expected, result)
661+
662+
ftm.Stop()
663+
ps.Stop()
664+
}

0 commit comments

Comments
 (0)