|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" |
| 10 | + "github.com/prometheus/common/model" |
| 11 | + "gotest.tools/v3/assert" |
| 12 | + appsv1 "k8s.io/api/apps/v1" |
| 13 | + "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + |
| 18 | + uiv1 "github.com/rhobs/observability-operator/pkg/apis/uiplugin/v1alpha1" |
| 19 | + "github.com/rhobs/observability-operator/test/e2e/framework" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + healthAnalyzerDeploymentName = "health-analyzer" |
| 24 | + prometheusRuleNamespace = "openshift-monitoring" |
| 25 | +) |
| 26 | + |
| 27 | +func clusterHealthAnalyzer(t *testing.T) { |
| 28 | + f.SkipIfClusterVersionBelow(t, "4.19") |
| 29 | + |
| 30 | + testStart := time.Now() |
| 31 | + t.Logf("[TIMING] Test started at %s", testStart.Format(time.RFC3339)) |
| 32 | + |
| 33 | + err := monv1.AddToScheme(f.K8sClient.Scheme()) |
| 34 | + assert.NilError(t, err, "failed to add monv1 to scheme") |
| 35 | + |
| 36 | + stepStart := time.Now() |
| 37 | + plugin := newMonitoringUIPlugin(t) |
| 38 | + err = f.K8sClient.Create(t.Context(), plugin) |
| 39 | + assert.NilError(t, err, "failed to create monitoring UIPlugin") |
| 40 | + t.Logf("[TIMING] UIPlugin creation took %s", time.Since(stepStart)) |
| 41 | + |
| 42 | + t.Cleanup(func() { |
| 43 | + if t.Failed() { |
| 44 | + dumpClusterHealthAnalyzerDebug(t, plugin.Name) |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + stepStart = time.Now() |
| 49 | + t.Log("Waiting for health-analyzer deployment to become ready...") |
| 50 | + haDeployment := appsv1.Deployment{} |
| 51 | + f.GetResourceWithRetry(t, healthAnalyzerDeploymentName, uiPluginInstallNS, &haDeployment) |
| 52 | + f.AssertDeploymentReady(healthAnalyzerDeploymentName, uiPluginInstallNS, framework.WithTimeout(5*time.Minute))(t) |
| 53 | + t.Logf("[TIMING] health-analyzer deployment ready after %s (elapsed since test start: %s)", time.Since(stepStart), time.Since(testStart)) |
| 54 | + |
| 55 | + suffix := strconv.FormatInt(time.Now().UnixNano()%100000, 10) |
| 56 | + ruleName := "e2e-health-analyzer-" + suffix |
| 57 | + alertName := "E2EHealthAnalyzer" + suffix |
| 58 | + |
| 59 | + stepStart = time.Now() |
| 60 | + rule := newAlwaysFiringRule(t, ruleName, alertName) |
| 61 | + err = f.K8sClient.Create(t.Context(), rule) |
| 62 | + assert.NilError(t, err, "failed to create PrometheusRule") |
| 63 | + t.Logf("[TIMING] PrometheusRule creation took %s", time.Since(stepStart)) |
| 64 | + |
| 65 | + stepStart = time.Now() |
| 66 | + t.Log("Waiting for alert to fire in Prometheus...") |
| 67 | + alertQuery := fmt.Sprintf(`ALERTS{alertname="%s",alertstate="firing"}`, alertName) |
| 68 | + err = f.AssertPromQLResultWithOptions(t, alertQuery, |
| 69 | + func(v model.Value) error { |
| 70 | + vec, ok := v.(model.Vector) |
| 71 | + if !ok || len(vec) == 0 { |
| 72 | + return fmt.Errorf("expected firing alert, got: %v", v) |
| 73 | + } |
| 74 | + return nil |
| 75 | + }, |
| 76 | + framework.WithPollInterval(30*time.Second), |
| 77 | + framework.WithTimeout(10*time.Minute), |
| 78 | + ) |
| 79 | + assert.NilError(t, err, "alert %s never fired", alertName) |
| 80 | + t.Logf("[TIMING] Alert firing took %s (elapsed since test start: %s)", time.Since(stepStart), time.Since(testStart)) |
| 81 | + |
| 82 | + stepStart = time.Now() |
| 83 | + t.Log("Waiting for cluster-health-analyzer to expose incident metric...") |
| 84 | + incidentQuery := fmt.Sprintf(`cluster_health_components_map{src_alertname="%s",src_severity="warning"}`, alertName) |
| 85 | + err = f.AssertPromQLResultWithOptions(t, incidentQuery, |
| 86 | + func(v model.Value) error { |
| 87 | + vec, ok := v.(model.Vector) |
| 88 | + if !ok || len(vec) == 0 { |
| 89 | + return fmt.Errorf("expected incident metric, got: %v", v) |
| 90 | + } |
| 91 | + return nil |
| 92 | + }, |
| 93 | + framework.WithPollInterval(30*time.Second), |
| 94 | + framework.WithTimeout(15*time.Minute), |
| 95 | + ) |
| 96 | + assert.NilError(t, err, "incident metric for %s never appeared", alertName) |
| 97 | + t.Logf("[TIMING] Incident metric appeared after %s (elapsed since test start: %s)", time.Since(stepStart), time.Since(testStart)) |
| 98 | + |
| 99 | + t.Logf("[TIMING] Total test duration: %s", time.Since(testStart)) |
| 100 | +} |
| 101 | + |
| 102 | +func newMonitoringUIPlugin(t *testing.T) *uiv1.UIPlugin { |
| 103 | + plugin := &uiv1.UIPlugin{ |
| 104 | + ObjectMeta: metav1.ObjectMeta{ |
| 105 | + Name: "monitoring", |
| 106 | + }, |
| 107 | + Spec: uiv1.UIPluginSpec{ |
| 108 | + Type: uiv1.TypeMonitoring, |
| 109 | + Monitoring: &uiv1.MonitoringConfig{ |
| 110 | + ClusterHealthAnalyzer: &uiv1.ClusterHealthAnalyzerReference{ |
| 111 | + Enabled: true, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + existing := &uiv1.UIPlugin{} |
| 118 | + err := f.K8sClient.Get(t.Context(), client.ObjectKey{Name: plugin.Name}, existing) |
| 119 | + if err == nil { |
| 120 | + t.Log("UIPlugin 'monitoring' already exists, deleting before recreation...") |
| 121 | + if err := f.K8sClient.Delete(t.Context(), existing); err != nil { |
| 122 | + t.Fatalf("failed to delete existing UIPlugin: %v", err) |
| 123 | + } |
| 124 | + waitForUIPluginDeletion(existing) |
| 125 | + } else if !errors.IsNotFound(err) { |
| 126 | + t.Fatalf("failed to check for existing UIPlugin: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + f.CleanUp(t, func() { |
| 130 | + if err := f.K8sClient.Delete(t.Context(), plugin); err != nil && !errors.IsNotFound(err) { |
| 131 | + t.Logf("warning: failed to delete UIPlugin during cleanup: %v", err) |
| 132 | + } |
| 133 | + waitForUIPluginDeletion(plugin) |
| 134 | + }) |
| 135 | + return plugin |
| 136 | +} |
| 137 | + |
| 138 | +func newAlwaysFiringRule(t *testing.T, ruleName, alertName string) *monv1.PrometheusRule { |
| 139 | + rule := &monv1.PrometheusRule{ |
| 140 | + ObjectMeta: metav1.ObjectMeta{ |
| 141 | + Name: ruleName, |
| 142 | + Namespace: prometheusRuleNamespace, |
| 143 | + Labels: map[string]string{ |
| 144 | + "app.kubernetes.io/name": "kube-prometheus", |
| 145 | + "app.kubernetes.io/part-of": "openshift-monitoring", |
| 146 | + "prometheus": "k8s", |
| 147 | + "role": "alert-rules", |
| 148 | + }, |
| 149 | + }, |
| 150 | + Spec: monv1.PrometheusRuleSpec{ |
| 151 | + Groups: []monv1.RuleGroup{{ |
| 152 | + Name: "health-analyzer-test-" + ruleName, |
| 153 | + Rules: []monv1.Rule{{ |
| 154 | + Alert: alertName, |
| 155 | + Expr: intstr.FromString(`vector(1)`), |
| 156 | + Labels: map[string]string{"severity": "warning"}, |
| 157 | + Annotations: map[string]string{ |
| 158 | + "summary": "E2E static test alert for cluster health analyzer.", |
| 159 | + }, |
| 160 | + }}, |
| 161 | + }}, |
| 162 | + }, |
| 163 | + } |
| 164 | + f.CleanUp(t, func() { |
| 165 | + if err := f.K8sClient.Delete(t.Context(), rule); err != nil && !errors.IsNotFound(err) { |
| 166 | + t.Logf("warning: failed to delete PrometheusRule during cleanup: %v", err) |
| 167 | + } |
| 168 | + }) |
| 169 | + return rule |
| 170 | +} |
| 171 | + |
| 172 | +func dumpClusterHealthAnalyzerDebug(t *testing.T, pluginName string) { |
| 173 | + t.Helper() |
| 174 | + ctx := t.Context() |
| 175 | + |
| 176 | + // UIPlugin-specific diagnostics |
| 177 | + var plugin uiv1.UIPlugin |
| 178 | + if err := f.K8sClient.Get(ctx, client.ObjectKey{Name: pluginName}, &plugin); err != nil { |
| 179 | + t.Logf("Failed to get UIPlugin %q: %v", pluginName, err) |
| 180 | + } else { |
| 181 | + t.Logf("UIPlugin %q generation=%d, resourceVersion=%s", pluginName, plugin.Generation, plugin.ResourceVersion) |
| 182 | + t.Logf("UIPlugin spec.type=%s", plugin.Spec.Type) |
| 183 | + if plugin.Spec.Monitoring != nil { |
| 184 | + if plugin.Spec.Monitoring.ClusterHealthAnalyzer != nil { |
| 185 | + t.Logf("UIPlugin spec.monitoring.clusterHealthAnalyzer.enabled=%v", plugin.Spec.Monitoring.ClusterHealthAnalyzer.Enabled) |
| 186 | + } |
| 187 | + if plugin.Spec.Monitoring.Incidents != nil { |
| 188 | + t.Logf("UIPlugin spec.monitoring.incidents.enabled=%v", plugin.Spec.Monitoring.Incidents.Enabled) |
| 189 | + } |
| 190 | + } |
| 191 | + if len(plugin.Status.Conditions) == 0 { |
| 192 | + t.Log("UIPlugin has no status conditions") |
| 193 | + } |
| 194 | + for _, c := range plugin.Status.Conditions { |
| 195 | + t.Logf("UIPlugin condition: type=%s status=%s reason=%s message=%s", c.Type, c.Status, c.Reason, c.Message) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + var plugins uiv1.UIPluginList |
| 200 | + if err := f.K8sClient.List(ctx, &plugins); err != nil { |
| 201 | + t.Logf("Failed to list UIPlugins: %v", err) |
| 202 | + } else { |
| 203 | + t.Logf("Total UIPlugins in cluster: %d", len(plugins.Items)) |
| 204 | + for _, p := range plugins.Items { |
| 205 | + t.Logf(" UIPlugin: name=%s type=%s conditions=%d", p.Name, p.Spec.Type, len(p.Status.Conditions)) |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + // Generic namespace diagnostics (deployments, pods, events) |
| 210 | + f.DumpNamespaceDebug(t, uiPluginInstallNS) |
| 211 | +} |
| 212 | + |
0 commit comments