Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require (
github.com/openshift/library-go v0.0.0-20240905123346-5bdbfe35a6f5
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.87.0
github.com/prometheus-operator/prometheus-operator/pkg/client v0.87.0
github.com/prometheus/client_golang v1.23.2
github.com/prometheus/client_model v0.6.2
github.com/prometheus/common v0.67.4
github.com/prometheus/prometheus v0.308.0
github.com/sirupsen/logrus v1.9.3
Expand Down Expand Up @@ -57,8 +59,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.23.2 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand Down
109 changes: 109 additions & 0 deletions internal/managementrouter/alerts_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package managementrouter

import (
"context"
"encoding/json"
"net/http"

"github.com/openshift/monitoring-plugin/pkg/k8s"
)

type GetAlertsResponse struct {
Data GetAlertsResponseData `json:"data"`
Warnings []string `json:"warnings,omitempty"`
}

type GetAlertsResponseData struct {
Alerts []k8s.PrometheusAlert `json:"alerts"`
}

func (hr *httpRouter) GetAlerts(w http.ResponseWriter, req *http.Request) {
state, labels, err := parseStateAndLabels(req.URL.Query())
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
ctx := req.Context()

alerts, err := hr.managementClient.GetAlerts(ctx, k8s.GetAlertsRequest{
Labels: labels,
State: state,
})
if err != nil {
handleError(w, err)
return
}

w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(GetAlertsResponse{
Data: GetAlertsResponseData{
Alerts: alerts,
},
Warnings: hr.alertWarnings(ctx),
}); err != nil {
log.WithError(err).Warn("failed to encode alerts response")
}
}

func (hr *httpRouter) alertWarnings(ctx context.Context) []string {
health, ok := hr.alertingHealth(ctx)
if !ok {
return nil
}

warnings := []string{}
if health.UserWorkloadEnabled && health.UserWorkload != nil {
warnings = append(warnings, buildRouteWarnings(health.UserWorkload.Prometheus, k8s.UserWorkloadRouteName, "user workload Prometheus")...)
warnings = append(warnings, buildRouteWarnings(health.UserWorkload.Alertmanager, k8s.UserWorkloadAlertmanagerRouteName, "user workload Alertmanager")...)
}

return warnings
}

//nolint:unused // used by the rules listing handler in a subsequent branch
func (hr *httpRouter) rulesWarnings(ctx context.Context) []string {
health, ok := hr.alertingHealth(ctx)
if !ok {
return nil
}

if health.UserWorkloadEnabled && health.UserWorkload != nil {
return buildRouteWarnings(health.UserWorkload.Prometheus, k8s.UserWorkloadRouteName, "user workload Prometheus")
}

return nil
}

func (hr *httpRouter) alertingHealth(ctx context.Context) (k8s.AlertingHealth, bool) {
if hr.managementClient == nil {
return k8s.AlertingHealth{}, false
}

health, err := hr.managementClient.GetAlertingHealth(ctx)
if err != nil {
log.WithError(err).Warn("alerting health unavailable")
return k8s.AlertingHealth{}, false
}

return health, true
}

func buildRouteWarnings(route k8s.AlertingRouteHealth, expectedName string, friendlyName string) []string {
if route.Name != "" && route.Name != expectedName {
return nil
}
if route.FallbackReachable {
return nil
}

switch route.Status {
case k8s.RouteNotFound:
return []string{friendlyName + " route is missing"}
case k8s.RouteUnreachable:
return []string{friendlyName + " route is unreachable"}
default:
return nil
}
}
Loading