Skip to content

Commit ffe8541

Browse files
sradcoAI Assistant
andcommitted
k8s: add alert listing/query and filter primitives
Add PrometheusAlerts implementation with: - Alert fetching from platform and user-workload Prometheus/Thanos - Rule group fetching with namespace-scoped Thanos tenancy queries - State-based filtering (firing, pending, silenced) - Label-based flat filtering with key=value matching - TLS transport with service CA for in-cluster communication Signed-off-by: Shirly Radco <sradco@redhat.com> Signed-off-by: João Vilaça <jvilaca@redhat.com> Signed-off-by: Aviv Litman <alitman@redhat.com> Co-authored-by: AI Assistant <noreply@cursor.com>
1 parent ba3697b commit ffe8541

6 files changed

Lines changed: 1211 additions & 1 deletion

File tree

pkg/k8s/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"k8s.io/client-go/rest"
99

1010
osmv1client "github.com/openshift/client-go/monitoring/clientset/versioned"
11+
routeclient "github.com/openshift/client-go/route/clientset/versioned"
1112
monitoringv1client "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
1213
"github.com/sirupsen/logrus"
1314
)
@@ -22,6 +23,8 @@ type client struct {
2223
osmv1clientset *osmv1client.Clientset
2324
config *rest.Config
2425

26+
prometheusAlerts *prometheusAlerts
27+
2528
prometheusRuleManager *prometheusRuleManager
2629
namespaceManager *namespaceManager
2730
}
@@ -42,6 +45,11 @@ func NewClient(ctx context.Context, config *rest.Config) (Client, error) {
4245
return nil, fmt.Errorf("failed to create osmv1 clientset: %w", err)
4346
}
4447

48+
routeClientset, err := routeclient.NewForConfig(config)
49+
if err != nil {
50+
return nil, fmt.Errorf("failed to create route clientset: %w", err)
51+
}
52+
4553
c := &client{
4654
clientset: clientset,
4755
monitoringv1clientset: monitoringv1clientset,
@@ -54,6 +62,8 @@ func NewClient(ctx context.Context, config *rest.Config) (Client, error) {
5462
return nil, fmt.Errorf("failed to create PrometheusRule manager: %w", err)
5563
}
5664

65+
c.prometheusAlerts = newPrometheusAlerts(routeClientset, clientset.CoreV1(), config, c.prometheusRuleManager)
66+
5767
c.namespaceManager, err = newNamespaceManager(ctx, clientset)
5868
if err != nil {
5969
return nil, fmt.Errorf("failed to create namespace manager: %w", err)
@@ -70,6 +80,10 @@ func (c *client) TestConnection(_ context.Context) error {
7080
return nil
7181
}
7282

83+
func (c *client) PrometheusAlerts() PrometheusAlertsInterface {
84+
return c.prometheusAlerts
85+
}
86+
7387
func (c *client) PrometheusRules() PrometheusRuleInterface {
7488
return c.prometheusRuleManager
7589
}

pkg/k8s/const.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
package k8s
22

33
const (
4-
ClusterMonitoringNamespace = "openshift-monitoring"
4+
ClusterMonitoringNamespace = "openshift-monitoring"
5+
UserWorkloadMonitoringNamespace = "openshift-user-workload-monitoring"
6+
7+
PlatformRouteName = "prometheus-k8s"
8+
PlatformAlertmanagerRouteName = "alertmanager-main"
9+
UserWorkloadRouteName = "prometheus-user-workload"
10+
UserWorkloadAlertmanagerRouteName = "alertmanager-user-workload"
11+
PrometheusAlertsPath = "/v1/alerts"
12+
PrometheusRulesPath = "/v1/rules"
13+
AlertmanagerAlertsPath = "/api/v2/alerts"
14+
UserWorkloadAlertmanagerPort = 9095
15+
UserWorkloadPrometheusServiceName = "prometheus-user-workload-web"
16+
UserWorkloadPrometheusPort = 9090
17+
18+
ThanosQuerierServiceName = "thanos-querier"
19+
ThanosQuerierTenancyRulesPortName = "tenancy-rules"
20+
DefaultThanosQuerierTenancyRulesPort = 9093
21+
ThanosQuerierTenancyAlertsPath = "/api/v1/alerts"
22+
ThanosQuerierTenancyRulesPath = "/api/v1/rules"
23+
ServiceCAPath = "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt"
24+
25+
AlertSourceLabel = "openshift_io_alert_source"
26+
AlertSourcePlatform = "platform"
27+
AlertSourceUser = "user"
28+
AlertBackendLabel = "openshift_io_alert_backend"
29+
AlertBackendAM = "alertmanager"
30+
AlertBackendProm = "prometheus"
31+
AlertBackendThanos = "thanos"
532
)

0 commit comments

Comments
 (0)