@@ -45,11 +45,26 @@ import (
4545)
4646
4747type updateTargetStatusTestCase struct {
48- desc string
49- targets []* prometheusv1.TargetsResult
50- podMonitorings []monitoringv1.PodMonitoring
51- clusterPodMonitorings []monitoringv1.ClusterPodMonitoring
52- expErr func (err error ) bool
48+ desc string
49+ targets []* prometheusv1.TargetsResult
50+ podMonitorings []monitoringv1.PodMonitoring
51+ initializeStatus []monitoringv1.PodMonitoringStatus
52+ clusterPodMonitorings []monitoringv1.ClusterPodMonitoring
53+ initializeClusterStatus []monitoringv1.PodMonitoringStatus
54+ expErr func (err error ) bool
55+ }
56+
57+ func (tc * updateTargetStatusTestCase ) getPodMonitoringCRDs () []monitoringv1.PodMonitoringCRD {
58+ var combinedList []monitoringv1.PodMonitoringCRD
59+
60+ for _ , pm := range tc .podMonitorings {
61+ combinedList = append (combinedList , & pm )
62+ }
63+
64+ for _ , pm := range tc .clusterPodMonitorings {
65+ combinedList = append (combinedList , & pm )
66+ }
67+ return combinedList
5368}
5469
5570// Given a list of test cases on PodMonitoring, creates a new list containing
@@ -84,6 +99,7 @@ func expand(testCases []updateTargetStatusTestCase) []updateTargetStatusTestCase
8499 }
85100 clusterTargets = append (clusterTargets , targetClusterPodMonitoring )
86101 }
102+
87103 for _ , pm := range tc .podMonitorings {
88104 pmCopy := pm .DeepCopy ()
89105 cpm := monitoringv1.ClusterPodMonitoring {
@@ -103,26 +119,41 @@ func expand(testCases []updateTargetStatusTestCase) []updateTargetStatusTestCase
103119 }
104120 clusterPodMonitorings = append (clusterPodMonitorings , cpm )
105121 }
122+
123+ initializeClusterStatus := make ([]monitoringv1.PodMonitoringStatus , 0 , len (tc .initializeStatus ))
124+ for _ , status := range tc .initializeStatus {
125+ statusCopy := status .DeepCopy ()
126+
127+ for idx , status := range statusCopy .EndpointStatuses {
128+ statusCopy .EndpointStatuses [idx ].Name = podMonitoringScrapePoolToClusterPodMonitoringScrapePool (status .Name )
129+ }
130+ initializeClusterStatus = append (initializeClusterStatus , * statusCopy )
131+ }
132+
106133 dataPodMonitorings := updateTargetStatusTestCase {
107- desc : tc .desc + "-pod-monitoring" ,
108- targets : tc .targets ,
109- podMonitorings : tc .podMonitorings ,
110- expErr : tc .expErr ,
134+ desc : tc .desc + "-pod-monitoring" ,
135+ targets : tc .targets ,
136+ podMonitorings : tc .podMonitorings ,
137+ initializeStatus : tc .initializeStatus ,
138+ expErr : tc .expErr ,
111139 }
112140 dataFinal = append (dataFinal , dataPodMonitorings )
113141 dataClusterPodMonitorings := updateTargetStatusTestCase {
114- desc : tc .desc + "-cluster-pod-monitoring" ,
115- targets : clusterTargets ,
116- clusterPodMonitorings : clusterPodMonitorings ,
117- expErr : tc .expErr ,
142+ desc : tc .desc + "-cluster-pod-monitoring" ,
143+ targets : clusterTargets ,
144+ clusterPodMonitorings : clusterPodMonitorings ,
145+ initializeClusterStatus : initializeClusterStatus ,
146+ expErr : tc .expErr ,
118147 }
119148 prometheusTargetsBoth := append (tc .targets , clusterTargets ... )
120149 dataBoth := updateTargetStatusTestCase {
121- desc : tc .desc + "-both" ,
122- targets : prometheusTargetsBoth ,
123- podMonitorings : tc .podMonitorings ,
124- clusterPodMonitorings : clusterPodMonitorings ,
125- expErr : tc .expErr ,
150+ desc : tc .desc + "-both" ,
151+ targets : prometheusTargetsBoth ,
152+ podMonitorings : tc .podMonitorings ,
153+ initializeStatus : tc .initializeStatus ,
154+ clusterPodMonitorings : clusterPodMonitorings ,
155+ initializeClusterStatus : initializeClusterStatus ,
156+ expErr : tc .expErr ,
126157 }
127158 dataFinal = append (dataFinal , dataClusterPodMonitorings )
128159 dataFinal = append (dataFinal , dataBoth )
@@ -1225,27 +1256,100 @@ func TestUpdateTargetStatus(t *testing.T) {
12251256 return err .Error () == "unknown scrape kind \" unknown\" "
12261257 },
12271258 },
1259+ // No targets, with PodMonitoring config.
1260+ {
1261+ desc : "no-targets-no-match" ,
1262+ podMonitorings : []monitoringv1.PodMonitoring {
1263+ {
1264+ ObjectMeta : metav1.ObjectMeta {Name : "prom-example-1" , Namespace : "gmp-test" },
1265+ Spec : monitoringv1.PodMonitoringSpec {},
1266+
1267+ Status : monitoringv1.PodMonitoringStatus {
1268+ MonitoringStatus : monitoringv1.MonitoringStatus {
1269+ ObservedGeneration : 2 ,
1270+ Conditions : []monitoringv1.MonitoringCondition {{
1271+ Type : monitoringv1 .ConfigurationCreateSuccess ,
1272+ Status : corev1 .ConditionTrue ,
1273+ LastUpdateTime : metav1.Time {},
1274+ LastTransitionTime : metav1.Time {},
1275+ Reason : "" ,
1276+ Message : "" ,
1277+ }},
1278+ },
1279+ },
1280+ },
1281+ },
1282+ initializeStatus : []monitoringv1.PodMonitoringStatus {
1283+ {
1284+ MonitoringStatus : monitoringv1.MonitoringStatus {
1285+ ObservedGeneration : 2 ,
1286+ Conditions : []monitoringv1.MonitoringCondition {{
1287+ Type : monitoringv1 .ConfigurationCreateSuccess ,
1288+ Status : corev1 .ConditionTrue ,
1289+ LastUpdateTime : metav1.Time {},
1290+ LastTransitionTime : metav1.Time {},
1291+ Reason : "" ,
1292+ Message : "" ,
1293+ }},
1294+ },
1295+ EndpointStatuses : []monitoringv1.ScrapeEndpointStatus {
1296+ {
1297+ Name : "PodMonitoring/gmp-test/prom-example-1/metrics" ,
1298+ ActiveTargets : 1 ,
1299+ UnhealthyTargets : 0 ,
1300+ LastUpdateTime : date ,
1301+ SampleGroups : []monitoringv1.SampleGroup {
1302+ {
1303+ SampleTargets : []monitoringv1.SampleTarget {
1304+ {
1305+ Health : "up" ,
1306+ Labels : map [model.LabelName ]model.LabelValue {
1307+ "instance" : "a" ,
1308+ },
1309+ LastScrapeDurationSeconds : "1.2" ,
1310+ },
1311+ },
1312+ Count : ptr .To (int32 (1 )),
1313+ },
1314+ },
1315+ CollectorsFraction : "1" ,
1316+ },
1317+ },
1318+ },
1319+ },
1320+ },
12281321 })
12291322
12301323 for _ , testCase := range testCases {
12311324 t .Run (fmt .Sprintf ("target-status-conversion-%s" , testCase .desc ), func (t * testing.T ) {
12321325 clientBuilder := newFakeClientBuilder ()
1233- for _ , podMonitoring := range testCase .podMonitorings {
1326+ for i , podMonitoring := range testCase .podMonitorings {
12341327 pmCopy := podMonitoring .DeepCopy ()
1235- pmCopy .GetPodMonitoringStatus ().EndpointStatuses = nil
1328+ if len (testCase .initializeStatus ) > 0 {
1329+ pmCopy .Status = testCase .initializeStatus [i ]
1330+ } else {
1331+ pmCopy .GetPodMonitoringStatus ().EndpointStatuses = nil
1332+ }
12361333 clientBuilder .WithObjects (pmCopy )
12371334 }
1238- for _ , clusterPodMonitoring := range testCase .clusterPodMonitorings {
1335+ for i , clusterPodMonitoring := range testCase .clusterPodMonitorings {
12391336 pmCopy := clusterPodMonitoring .DeepCopy ()
1240- pmCopy .GetPodMonitoringStatus ().EndpointStatuses = nil
1337+ if len (testCase .initializeClusterStatus ) > 0 {
1338+ pmCopy .Status = testCase .initializeClusterStatus [i ]
1339+ } else {
1340+ pmCopy .GetPodMonitoringStatus ().EndpointStatuses = nil
1341+ }
12411342 clientBuilder .WithObjects (pmCopy )
12421343 }
12431344
12441345 kubeClient := clientBuilder .Build ()
12451346
1246- err := updateTargetStatus (context .Background (), testr .New (t ), kubeClient , testCase .targets )
1347+ // fetchTargets(ctx, logger, opts, nil, targetFetchFromMap(prometheusTargetMap), kubeClient)
1348+ err := updateTargetStatus (context .Background (), testr .New (t ), kubeClient , testCase .targets , testCase .getPodMonitoringCRDs ())
12471349 if err != nil && (testCase .expErr == nil || ! testCase .expErr (err )) {
12481350 t .Fatalf ("unexpected error updating target status: %s" , err )
1351+ } else if err == nil && (testCase .expErr != nil ) {
1352+ t .Fatalf ("expected error missing when updating target status" )
12491353 }
12501354
12511355 for _ , podMonitoring := range testCase .podMonitorings {
@@ -1602,24 +1706,6 @@ func TestShouldPoll(t *testing.T) {
16021706 should : false ,
16031707 expErr : true ,
16041708 },
1605- {
1606- desc : "should not poll targets - no podmonitorings" ,
1607- objs : []client.Object {
1608- & monitoringv1.OperatorConfig {
1609- ObjectMeta : metav1.ObjectMeta {
1610- Name : "config" ,
1611- Namespace : "gmp-public" ,
1612- },
1613- Features : monitoringv1.OperatorFeatures {
1614- TargetStatus : monitoringv1.TargetStatusSpec {
1615- Enabled : true ,
1616- },
1617- },
1618- },
1619- },
1620- should : false ,
1621- expErr : false ,
1622- },
16231709 {
16241710 desc : "should not poll targets - disabled" ,
16251711 objs : []client.Object {
0 commit comments