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