@@ -24,6 +24,7 @@ import (
24
24
"github.com/go-kit/log"
25
25
"github.com/go-kit/log/level"
26
26
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
27
+ promv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
27
28
"github.com/prometheus-operator/prometheus-operator/pkg/assets"
28
29
fakemonitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/fake"
29
30
"github.com/prometheus-operator/prometheus-operator/pkg/informers"
@@ -35,6 +36,7 @@ import (
35
36
promconfig "github.com/prometheus/prometheus/config"
36
37
"github.com/prometheus/prometheus/discovery"
37
38
kubeDiscovery "github.com/prometheus/prometheus/discovery/kubernetes"
39
+ "github.com/prometheus/prometheus/discovery/targetgroup"
38
40
"github.com/stretchr/testify/assert"
39
41
"github.com/stretchr/testify/require"
40
42
v1 "k8s.io/api/core/v1"
@@ -59,6 +61,8 @@ func TestLoadConfig(t *testing.T) {
59
61
name string
60
62
serviceMonitors []* monitoringv1.ServiceMonitor
61
63
podMonitors []* monitoringv1.PodMonitor
64
+ scrapeConfigs []* promv1alpha1.ScrapeConfig
65
+ probes []* monitoringv1.Probe
62
66
want * promconfig.Config
63
67
wantErr bool
64
68
cfg allocatorconfig.Config
@@ -662,6 +666,136 @@ func TestLoadConfig(t *testing.T) {
662
666
},
663
667
},
664
668
},
669
+ {
670
+ name : "scrape configs selector test" ,
671
+ scrapeConfigs : []* promv1alpha1.ScrapeConfig {
672
+ {
673
+ ObjectMeta : metav1.ObjectMeta {
674
+ Name : "scrapeconfig-test-1" ,
675
+ Namespace : "test" ,
676
+ Labels : map [string ]string {
677
+ "testpod" : "testpod" ,
678
+ },
679
+ },
680
+ Spec : promv1alpha1.ScrapeConfigSpec {
681
+ JobName : func () * string {
682
+ j := "scrapeConfig/test/scrapeconfig-test-1"
683
+ return & j
684
+ }(),
685
+ StaticConfigs : []promv1alpha1.StaticConfig {
686
+ {
687
+ Targets : []promv1alpha1.Target {"127.0.0.1:8888" },
688
+ Labels : nil ,
689
+ },
690
+ },
691
+ },
692
+ },
693
+ },
694
+ cfg : allocatorconfig.Config {
695
+ PrometheusCR : allocatorconfig.PrometheusCRConfig {
696
+ ScrapeConfigSelector : & metav1.LabelSelector {
697
+ MatchLabels : map [string ]string {
698
+ "testpod" : "testpod" ,
699
+ },
700
+ },
701
+ },
702
+ },
703
+ want : & promconfig.Config {
704
+ ScrapeConfigs : []* promconfig.ScrapeConfig {
705
+ {
706
+ JobName : "scrapeConfig/test/scrapeconfig-test-1" ,
707
+ ScrapeInterval : model .Duration (30 * time .Second ),
708
+ ScrapeProtocols : defaultScrapeProtocols ,
709
+ ScrapeTimeout : model .Duration (10 * time .Second ),
710
+ HonorTimestamps : true ,
711
+ HonorLabels : false ,
712
+ Scheme : "http" ,
713
+ MetricsPath : "/metrics" ,
714
+ ServiceDiscoveryConfigs : []discovery.Config {
715
+ discovery.StaticConfig {
716
+ & targetgroup.Group {
717
+ Targets : []model.LabelSet {
718
+ map [model.LabelName ]model.LabelValue {
719
+ "__address__" : "127.0.0.1:8888" ,
720
+ },
721
+ },
722
+ Labels : map [model.LabelName ]model.LabelValue {},
723
+ Source : "0" ,
724
+ },
725
+ },
726
+ },
727
+ HTTPClientConfig : config .DefaultHTTPClientConfig ,
728
+ EnableCompression : true ,
729
+ },
730
+ },
731
+ },
732
+ },
733
+ {
734
+ name : "probe selector test" ,
735
+ probes : []* monitoringv1.Probe {
736
+ {
737
+ ObjectMeta : metav1.ObjectMeta {
738
+ Name : "probe-test-1" ,
739
+ Namespace : "test" ,
740
+ Labels : map [string ]string {
741
+ "testpod" : "testpod" ,
742
+ },
743
+ },
744
+ Spec : monitoringv1.ProbeSpec {
745
+ JobName : "probe/test/probe-1/0" ,
746
+ ProberSpec : monitoringv1.ProberSpec {
747
+ URL : "localhost:50671" ,
748
+ Path : "/metrics" ,
749
+ },
750
+ Targets : monitoringv1.ProbeTargets {
751
+ StaticConfig : & monitoringv1.ProbeTargetStaticConfig {
752
+ Targets : []string {"prometheus.io" },
753
+ },
754
+ },
755
+ },
756
+ },
757
+ },
758
+ cfg : allocatorconfig.Config {
759
+ PrometheusCR : allocatorconfig.PrometheusCRConfig {
760
+ ProbeSelector : & metav1.LabelSelector {
761
+ MatchLabels : map [string ]string {
762
+ "testpod" : "testpod" ,
763
+ },
764
+ },
765
+ },
766
+ },
767
+ want : & promconfig.Config {
768
+ ScrapeConfigs : []* promconfig.ScrapeConfig {
769
+ {
770
+ JobName : "probe/test/probe-test-1" ,
771
+ ScrapeInterval : model .Duration (30 * time .Second ),
772
+ ScrapeProtocols : defaultScrapeProtocols ,
773
+ ScrapeTimeout : model .Duration (10 * time .Second ),
774
+ HonorTimestamps : true ,
775
+ HonorLabels : false ,
776
+ Scheme : "http" ,
777
+ MetricsPath : "/metrics" ,
778
+ ServiceDiscoveryConfigs : []discovery.Config {
779
+ discovery.StaticConfig {
780
+ & targetgroup.Group {
781
+ Targets : []model.LabelSet {
782
+ map [model.LabelName ]model.LabelValue {
783
+ "__address__" : "prometheus.io" ,
784
+ },
785
+ },
786
+ Labels : map [model.LabelName ]model.LabelValue {
787
+ "namespace" : "test" ,
788
+ },
789
+ Source : "0" ,
790
+ },
791
+ },
792
+ },
793
+ HTTPClientConfig : config .DefaultHTTPClientConfig ,
794
+ EnableCompression : true ,
795
+ },
796
+ },
797
+ },
798
+ },
665
799
{
666
800
name : "service monitor namespace selector test" ,
667
801
serviceMonitors : []* monitoringv1.ServiceMonitor {
@@ -805,7 +939,7 @@ func TestLoadConfig(t *testing.T) {
805
939
}
806
940
for _ , tt := range tests {
807
941
t .Run (tt .name , func (t * testing.T ) {
808
- w , _ := getTestPrometheusCRWatcher (t , tt .serviceMonitors , tt .podMonitors , tt .cfg )
942
+ w , _ := getTestPrometheusCRWatcher (t , tt .serviceMonitors , tt .podMonitors , tt .probes , tt . scrapeConfigs , tt . cfg )
809
943
810
944
// Start namespace informers in order to populate cache.
811
945
go w .nsInformer .Run (w .stopChannel )
@@ -910,7 +1044,7 @@ func TestNamespaceLabelUpdate(t *testing.T) {
910
1044
ScrapeConfigs : []* promconfig.ScrapeConfig {},
911
1045
}
912
1046
913
- w , source := getTestPrometheusCRWatcher (t , nil , podMonitors , cfg )
1047
+ w , source := getTestPrometheusCRWatcher (t , nil , podMonitors , nil , nil , cfg )
914
1048
events := make (chan Event , 1 )
915
1049
eventInterval := 5 * time .Millisecond
916
1050
@@ -976,7 +1110,7 @@ func TestRateLimit(t *testing.T) {
976
1110
eventInterval := 500 * time .Millisecond
977
1111
cfg := allocatorconfig.Config {}
978
1112
979
- w , _ := getTestPrometheusCRWatcher (t , nil , nil , cfg )
1113
+ w , _ := getTestPrometheusCRWatcher (t , nil , nil , nil , nil , cfg )
980
1114
defer w .Close ()
981
1115
w .eventInterval = eventInterval
982
1116
@@ -1037,7 +1171,7 @@ func TestRateLimit(t *testing.T) {
1037
1171
1038
1172
// getTestPrometheusCRWatcher creates a test instance of PrometheusCRWatcher with fake clients
1039
1173
// and test secrets.
1040
- func getTestPrometheusCRWatcher (t * testing.T , svcMonitors []* monitoringv1.ServiceMonitor , podMonitors []* monitoringv1.PodMonitor , cfg allocatorconfig.Config ) (* PrometheusCRWatcher , * fcache.FakeControllerSource ) {
1174
+ func getTestPrometheusCRWatcher (t * testing.T , svcMonitors []* monitoringv1.ServiceMonitor , podMonitors []* monitoringv1.PodMonitor , probes [] * monitoringv1. Probe , scrapeConfigs [] * promv1alpha1. ScrapeConfig , cfg allocatorconfig.Config ) (* PrometheusCRWatcher , * fcache.FakeControllerSource ) {
1041
1175
mClient := fakemonitoringclient .NewSimpleClientset ()
1042
1176
for _ , sm := range svcMonitors {
1043
1177
if sm != nil {
@@ -1055,6 +1189,23 @@ func getTestPrometheusCRWatcher(t *testing.T, svcMonitors []*monitoringv1.Servic
1055
1189
}
1056
1190
}
1057
1191
}
1192
+ for _ , prb := range probes {
1193
+ if prb != nil {
1194
+ _ , err := mClient .MonitoringV1 ().Probes (prb .Namespace ).Create (context .Background (), prb , metav1.CreateOptions {})
1195
+ if err != nil {
1196
+ t .Fatal (t , err )
1197
+ }
1198
+ }
1199
+ }
1200
+
1201
+ for _ , scc := range scrapeConfigs {
1202
+ if scc != nil {
1203
+ _ , err := mClient .MonitoringV1alpha1 ().ScrapeConfigs (scc .Namespace ).Create (context .Background (), scc , metav1.CreateOptions {})
1204
+ if err != nil {
1205
+ t .Fatal (t , err )
1206
+ }
1207
+ }
1208
+ }
1058
1209
1059
1210
k8sClient := fake .NewSimpleClientset ()
1060
1211
_ , err := k8sClient .CoreV1 ().Secrets ("test" ).Create (context .Background (), & v1.Secret {
@@ -1094,6 +1245,10 @@ func getTestPrometheusCRWatcher(t *testing.T, svcMonitors []*monitoringv1.Servic
1094
1245
PodMonitorSelector : cfg .PrometheusCR .PodMonitorSelector ,
1095
1246
ServiceMonitorNamespaceSelector : cfg .PrometheusCR .ServiceMonitorNamespaceSelector ,
1096
1247
PodMonitorNamespaceSelector : cfg .PrometheusCR .PodMonitorNamespaceSelector ,
1248
+ ProbeSelector : cfg .PrometheusCR .ProbeSelector ,
1249
+ ProbeNamespaceSelector : cfg .PrometheusCR .ProbeNamespaceSelector ,
1250
+ ScrapeConfigSelector : cfg .PrometheusCR .ScrapeConfigSelector ,
1251
+ ScrapeConfigNamespaceSelector : cfg .PrometheusCR .ScrapeConfigNamespaceSelector ,
1097
1252
ServiceDiscoveryRole : & serviceDiscoveryRole ,
1098
1253
},
1099
1254
},
@@ -1136,6 +1291,8 @@ func getTestPrometheusCRWatcher(t *testing.T, svcMonitors []*monitoringv1.Servic
1136
1291
configGenerator : generator ,
1137
1292
podMonitorNamespaceSelector : cfg .PrometheusCR .PodMonitorNamespaceSelector ,
1138
1293
serviceMonitorNamespaceSelector : cfg .PrometheusCR .ServiceMonitorNamespaceSelector ,
1294
+ probeNamespaceSelector : cfg .PrometheusCR .ProbeNamespaceSelector ,
1295
+ scrapeConfigNamespaceSelector : cfg .PrometheusCR .ScrapeConfigNamespaceSelector ,
1139
1296
resourceSelector : resourceSelector ,
1140
1297
store : store ,
1141
1298
}, source
0 commit comments