@@ -14,6 +14,7 @@ import (
14
14
"github.com/go-kit/log"
15
15
"github.com/prometheus/common/model"
16
16
"github.com/prometheus/prometheus/discovery"
17
+ "github.com/prometheus/prometheus/discovery/kubernetes"
17
18
"github.com/prometheus/prometheus/discovery/targetgroup"
18
19
19
20
"github.com/grafana/loki/v3/clients/pkg/promtail/api"
@@ -594,3 +595,70 @@ func TestLabelSetUpdate(t *testing.T) {
594
595
require .Equal (t , 0 , len (syncer .fileEventWatchers ))
595
596
596
597
}
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