@@ -199,6 +199,7 @@ func TestUserWorkloadMonitoringOptOut(t *testing.T) {
199199 f func (* testing.T )
200200 }{
201201 {"assert namespace opt out removes appropriate targets" , assertNamespaceOptOut },
202+ {"assert service monitor opt out removes appropriate targets" , assertServiceMonitorOptOut },
202203 } {
203204 t .Run (scenario .name , scenario .f )
204205 }
@@ -1408,7 +1409,7 @@ func assertGRPCTLSRotation(t *testing.T) {
14081409func assertNamespaceOptOut (t * testing.T ) {
14091410 ctx := context .Background ()
14101411
1411- serviceMonitorJobName := "serviceMonitor/user-workload-test/prometheus-example-monitor /0"
1412+ serviceMonitorJobName := fmt . Sprintf ( "serviceMonitor/%s/%s /0" , userWorkloadTestNs , serviceMonitorTestName )
14121413
14131414 // Ensure the target for the example ServiceMonitor exists.
14141415 f .ThanosQuerierClient .WaitForTargetsReturn (t , 5 * time .Minute , func (body []byte ) error {
@@ -1459,3 +1460,58 @@ func assertNamespaceOptOut(t *testing.T) {
14591460 return getActiveTarget (body , serviceMonitorJobName )
14601461 })
14611462}
1463+
1464+ func assertServiceMonitorOptOut (t * testing.T ) {
1465+ ctx := context .Background ()
1466+
1467+ serviceMonitorJobName := fmt .Sprintf ("serviceMonitor/%s/%s/0" , userWorkloadTestNs , serviceMonitorTestName )
1468+
1469+ // Ensure the target for the example ServiceMonitor exists.
1470+ f .ThanosQuerierClient .WaitForTargetsReturn (t , 5 * time .Minute , func (body []byte ) error {
1471+ return getActiveTarget (body , serviceMonitorJobName )
1472+ })
1473+
1474+ // Add opt-out label to service monitor.
1475+ sm , err := f .MonitoringClient .ServiceMonitors (userWorkloadTestNs ).Get (ctx , serviceMonitorTestName , metav1.GetOptions {})
1476+ if err != nil {
1477+ t .Fatalf ("Failed to fetch user-workload service monitor: %v" , err )
1478+ }
1479+
1480+ labels := sm .GetLabels ()
1481+ labels ["openshift.io/user-monitoring" ] = "false"
1482+ sm .SetLabels (labels )
1483+
1484+ _ , err = f .MonitoringClient .ServiceMonitors (userWorkloadTestNs ).Update (ctx , sm , metav1.UpdateOptions {})
1485+ if err != nil {
1486+ t .Fatalf ("Failed to apply user-monitoring opt-out label: %v" , err )
1487+ }
1488+
1489+ // Ensure the target for the example ServiceMonitor is removed.
1490+ f .ThanosQuerierClient .WaitForTargetsReturn (t , 5 * time .Minute , func (body []byte ) error {
1491+ if err := getActiveTarget (body , serviceMonitorJobName ); err == nil {
1492+ return fmt .Errorf ("target '%s' exists, but should not" , serviceMonitorJobName )
1493+ }
1494+
1495+ return nil
1496+ })
1497+
1498+ // Remove opt-out label from namespace.
1499+ sm , err = f .MonitoringClient .ServiceMonitors (userWorkloadTestNs ).Get (ctx , serviceMonitorTestName , metav1.GetOptions {})
1500+ if err != nil {
1501+ t .Fatalf ("Failed to fetch user-workload service monitor: %v" , err )
1502+ }
1503+
1504+ labels = sm .GetLabels ()
1505+ delete (labels , "openshift.io/user-monitoring" )
1506+ sm .SetLabels (labels )
1507+
1508+ _ , err = f .MonitoringClient .ServiceMonitors (userWorkloadTestNs ).Update (ctx , sm , metav1.UpdateOptions {})
1509+ if err != nil {
1510+ t .Fatalf ("Failed to remove user-monitoring opt-out label: %v" , err )
1511+ }
1512+
1513+ // Ensure the target for the example ServiceMonitor is recreated.
1514+ f .ThanosQuerierClient .WaitForTargetsReturn (t , 5 * time .Minute , func (body []byte ) error {
1515+ return getActiveTarget (body , serviceMonitorJobName )
1516+ })
1517+ }
0 commit comments