@@ -46,7 +46,6 @@ const (
46
46
userWorkloadTestNs = "user-workload-test"
47
47
)
48
48
49
-
50
49
type scenario struct {
51
50
name string
52
51
assertion func (* testing.T )
@@ -106,6 +105,7 @@ func TestUserWorkloadMonitoringWithStorage(t *testing.T) {
106
105
{"assert prometheus and alertmanager is not deployed in user namespace" , assertPrometheusAlertmanagerInUserNamespace },
107
106
{"assert grpc tls rotation" , assertGRPCTLSRotation },
108
107
{"assert enforced target limit is configured" , assertEnforcedTargetLimit (10 )},
108
+ {"assert namespace opt out removes appropriate targets" , assertNamespaceOptOut },
109
109
{"enable user workload monitoring, assert prometheus rollout" , createUserWorkloadAssets (cm )},
110
110
{"set VolumeClaimTemplate for prometheus CR, assert that it is created" , assertVolumeClaimsConfigAndRollout (rolloutParams {
111
111
namespace : f .UserWorkloadMonitoringNs ,
@@ -1181,6 +1181,61 @@ func assertEnforcedTargetLimit(limit uint64) func(*testing.T) {
1181
1181
}
1182
1182
}
1183
1183
1184
+ func assertNamespaceOptOut (t * testing.T ) {
1185
+ ctx := context .Background ()
1186
+
1187
+ serviceMonitorJobName := "serviceMonitor/user-workload-test/prometheus-example-monitor/0"
1188
+
1189
+ // Ensure the target for the example ServiceMonitor exists.
1190
+ f .ThanosQuerierClient .WaitForTargetsReturn (t , 5 * time .Minute , func (body []byte ) error {
1191
+ return getActiveTarget (body , serviceMonitorJobName )
1192
+ })
1193
+
1194
+ // Add opt-out label to namespace.
1195
+ ns , err := f .KubeClient .CoreV1 ().Namespaces ().Get (ctx , userWorkloadTestNs , metav1.GetOptions {})
1196
+ if err != nil {
1197
+ t .Fatalf ("Failed to fetch user-workload namespace: %v" , err )
1198
+ }
1199
+
1200
+ labels := ns .GetLabels ()
1201
+ labels ["openshift.io/user-monitoring" ] = "false"
1202
+ ns .SetLabels (labels )
1203
+
1204
+ _ , err = f .KubeClient .CoreV1 ().Namespaces ().Update (ctx , ns , metav1.UpdateOptions {})
1205
+ if err != nil {
1206
+ t .Fatalf ("Failed to apply user-monitoring opt-out label: %v" , err )
1207
+ }
1208
+
1209
+ // Ensure the target for the example ServiceMonitor is removed.
1210
+ f .ThanosQuerierClient .WaitForTargetsReturn (t , 5 * time .Minute , func (body []byte ) error {
1211
+ if err := getActiveTarget (body , serviceMonitorJobName ); err == nil {
1212
+ return fmt .Errorf ("target '%s' exists, but should not" , serviceMonitorJobName )
1213
+ }
1214
+
1215
+ return nil
1216
+ })
1217
+
1218
+ // Remove opt-out label from namespace.
1219
+ ns , err = f .KubeClient .CoreV1 ().Namespaces ().Get (ctx , userWorkloadTestNs , metav1.GetOptions {})
1220
+ if err != nil {
1221
+ t .Fatalf ("Failed to fetch user-workload namespace: %v" , err )
1222
+ }
1223
+
1224
+ labels = ns .GetLabels ()
1225
+ delete (labels , "openshift.io/user-monitoring" )
1226
+ ns .SetLabels (labels )
1227
+
1228
+ _ , err = f .KubeClient .CoreV1 ().Namespaces ().Update (ctx , ns , metav1.UpdateOptions {})
1229
+ if err != nil {
1230
+ t .Fatalf ("Failed to remove user-monitoring opt-out label: %v" , err )
1231
+ }
1232
+
1233
+ // Ensure the target for the example ServiceMonitor is recreated.
1234
+ f .ThanosQuerierClient .WaitForTargetsReturn (t , 5 * time .Minute , func (body []byte ) error {
1235
+ return getActiveTarget (body , serviceMonitorJobName )
1236
+ })
1237
+ }
1238
+
1184
1239
func updateConfigmap (cm * v1.ConfigMap ) func (t * testing.T ) {
1185
1240
ctx := context .Background ()
1186
1241
return func (t * testing.T ) {
0 commit comments