Skip to content

Commit 65a94f6

Browse files
sradcoAI Assistant
andcommitted
management: add CRD support and create alert rule API
Add AlertingRule, AlertRelabelConfig, and RelabeledRules CRD interfaces with the management client, router, server wiring, and POST /api/v1/alerting/rules endpoint. 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 74a097e commit 65a94f6

33 files changed

Lines changed: 2832 additions & 7 deletions

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ RUN make install-backend
2525

2626
COPY cmd/ cmd/
2727
COPY pkg/ pkg/
28+
COPY internal/ internal/
2829

2930
ENV GOEXPERIMENT=strictfipsruntime
3031
ENV CGO_ENABLED=1

Dockerfile.dev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ RUN go mod download
2828

2929
COPY cmd/ cmd/
3030
COPY pkg/ pkg/
31+
COPY internal/ internal/
3132

3233
RUN go build -mod=mod -o plugin-backend cmd/plugin-backend.go
3334

Dockerfile.dev-mcp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ RUN go mod download
3131

3232
COPY cmd/ cmd/
3333
COPY pkg/ pkg/
34+
COPY internal/ internal/
3435

3536
RUN go build -mod=mod -o plugin-backend cmd/plugin-backend.go
3637

Dockerfile.devspace

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ RUN make install-backend
2020
COPY config/ config/
2121
COPY cmd/ cmd/
2222
COPY pkg/ pkg/
23+
COPY internal/ internal/
2324

2425
RUN make build-backend
2526

Dockerfile.konflux

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ RUN make install-backend
2828

2929
COPY cmd/ cmd/
3030
COPY pkg/ pkg/
31+
COPY internal/ internal/
3132

3233
ENV GOEXPERIMENT=strictfipsruntime
3334
ENV CGO_ENABLED=1

Dockerfile.mcp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ RUN make install-backend
2828

2929
COPY cmd/ cmd/
3030
COPY pkg/ pkg/
31+
COPY internal/ internal/
3132

3233
ENV GOOS=${TARGETOS:-linux}
3334
ENV GOARCH=${TARGETARCH}

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ start-backend:
5656

5757
.PHONY: test-backend
5858
test-backend:
59-
go test ./pkg/... -v
59+
go test ./pkg/... ./internal/... -v
60+
61+
.PHONY: test-e2e
62+
test-e2e:
63+
PLUGIN_URL=http://localhost:9001 go test -v -timeout=150m -count=1 ./test/e2e
6064

6165
.PHONY: test-frontend
6266
test-frontend:

go.mod

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ require (
66
github.com/evanphx/json-patch v4.12.0+incompatible
77
github.com/gorilla/handlers v1.5.2
88
github.com/gorilla/mux v1.8.1
9+
github.com/onsi/ginkgo/v2 v2.22.0
10+
github.com/onsi/gomega v1.36.1
11+
github.com/openshift/api v0.0.0-20251122153900-88cca31a44c9
912
github.com/openshift/client-go v0.0.0-20251123231646-4685125c2287
1013
github.com/openshift/library-go v0.0.0-20240905123346-5bdbfe35a6f5
1114
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.87.0
1215
github.com/prometheus-operator/prometheus-operator/pkg/client v0.87.0
16+
github.com/prometheus/common v0.67.4
17+
github.com/prometheus/prometheus v0.308.0
1318
github.com/sirupsen/logrus v1.9.3
1419
github.com/stretchr/testify v1.11.1
1520
gopkg.in/yaml.v2 v2.4.0
@@ -20,6 +25,7 @@ require (
2025
)
2126

2227
require (
28+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2329
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2430
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
2531
github.com/felixge/httpsnoop v1.0.4 // indirect
@@ -40,18 +46,20 @@ require (
4046
github.com/go-openapi/swag/stringutils v0.25.1 // indirect
4147
github.com/go-openapi/swag/typeutils v0.25.1 // indirect
4248
github.com/go-openapi/swag/yamlutils v0.25.1 // indirect
49+
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
4350
github.com/gogo/protobuf v1.3.2 // indirect
4451
github.com/google/gnostic-models v0.7.0 // indirect
4552
github.com/google/go-cmp v0.7.0 // indirect
4653
github.com/google/pprof v0.0.0-20250923004556-9e5a51aed1e8 // indirect
4754
github.com/google/uuid v1.6.0 // indirect
55+
github.com/grafana/regexp v0.0.0-20250905093917-f7b3be9d1853 // indirect
4856
github.com/json-iterator/go v1.1.12 // indirect
4957
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
5058
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
5159
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
52-
github.com/openshift/api v0.0.0-20251122153900-88cca31a44c9 // indirect
5360
github.com/pkg/errors v0.9.1 // indirect
5461
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
62+
github.com/prometheus/client_model v0.6.2 // indirect
5563
github.com/x448/float16 v0.8.4 // indirect
5664
go.yaml.in/yaml/v2 v2.4.3 // indirect
5765
go.yaml.in/yaml/v3 v3.0.4 // indirect
@@ -61,6 +69,7 @@ require (
6169
golang.org/x/term v0.36.0 // indirect
6270
golang.org/x/text v0.30.0 // indirect
6371
golang.org/x/time v0.13.0 // indirect
72+
golang.org/x/tools v0.37.0 // indirect
6473
google.golang.org/protobuf v1.36.10 // indirect
6574
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
6675
gopkg.in/inf.v0 v0.9.1 // indirect

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
2+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
13
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -61,6 +63,8 @@ github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyE
6163
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
6264
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
6365
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
66+
github.com/grafana/regexp v0.0.0-20250905093917-f7b3be9d1853 h1:cLN4IBkmkYZNnk7EAJ0BHIethd+J6LqxFNw5mSiI2bM=
67+
github.com/grafana/regexp v0.0.0-20250905093917-f7b3be9d1853/go.mod h1:+JKpmjMGhpgPL+rXZ5nsZieVzvarn86asRlBg4uNGnk=
6468
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
6569
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
6670
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
@@ -96,6 +100,12 @@ github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.87.0 h
96100
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.87.0/go.mod h1:WHiLZmOWVop/MoYvRD58LfnPeyE+dcITby/jQjg83Hw=
97101
github.com/prometheus-operator/prometheus-operator/pkg/client v0.87.0 h1:rrZriucuC8ZUOPr8Asvavb9pbzqXSsAeY79aH8xnXlc=
98102
github.com/prometheus-operator/prometheus-operator/pkg/client v0.87.0/go.mod h1:OMvC2XJGxPeEAKf5qB1u7DudV46HA8ePxYslRjxQcbk=
103+
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
104+
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
105+
github.com/prometheus/common v0.67.4 h1:yR3NqWO1/UyO1w2PhUvXlGQs/PtFmoveVO0KZ4+Lvsc=
106+
github.com/prometheus/common v0.67.4/go.mod h1:gP0fq6YjjNCLssJCQp0yk4M8W6ikLURwkdd/YKtTbyI=
107+
github.com/prometheus/prometheus v0.308.0 h1:kVh/5m1n6m4cSK9HYTDEbMxzuzCWyEdPdKSxFRxXj04=
108+
github.com/prometheus/prometheus v0.308.0/go.mod h1:xXYKzScyqyFHihpS0UsXpC2F3RA/CygOs7wb4mpdusE=
99109
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
100110
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
101111
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
@@ -113,6 +123,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
113123
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
114124
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
115125
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
126+
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
127+
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
116128
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
117129
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
118130
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package managementrouter
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"net/http"
7+
8+
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
9+
10+
"github.com/openshift/monitoring-plugin/pkg/management"
11+
)
12+
13+
type CreateAlertRuleRequest struct {
14+
AlertingRule *monitoringv1.Rule `json:"alertingRule,omitempty"`
15+
PrometheusRule *management.PrometheusRuleOptions `json:"prometheusRule,omitempty"`
16+
}
17+
18+
type CreateAlertRuleResponse struct {
19+
Id string `json:"id"`
20+
}
21+
22+
const maxRequestBodyBytes = 1 << 20 // 1 MB
23+
24+
func (hr *httpRouter) CreateAlertRule(w http.ResponseWriter, req *http.Request) {
25+
req.Body = http.MaxBytesReader(w, req.Body, maxRequestBodyBytes)
26+
27+
var payload CreateAlertRuleRequest
28+
if err := json.NewDecoder(req.Body).Decode(&payload); err != nil {
29+
writeError(w, http.StatusBadRequest, "invalid request body")
30+
return
31+
}
32+
33+
if payload.AlertingRule == nil {
34+
writeError(w, http.StatusBadRequest, "alertingRule is required")
35+
return
36+
}
37+
38+
alertRule := *payload.AlertingRule
39+
40+
var (
41+
id string
42+
err error
43+
)
44+
45+
if payload.PrometheusRule != nil {
46+
id, err = hr.managementClient.CreateUserDefinedAlertRule(req.Context(), alertRule, *payload.PrometheusRule)
47+
} else {
48+
id, err = hr.managementClient.CreatePlatformAlertRule(req.Context(), alertRule)
49+
}
50+
51+
if err != nil {
52+
handleError(w, err)
53+
return
54+
}
55+
56+
w.Header().Set("Content-Type", "application/json")
57+
w.WriteHeader(http.StatusCreated)
58+
if err := json.NewEncoder(w).Encode(CreateAlertRuleResponse{Id: id}); err != nil {
59+
log.Printf("failed to encode create alert rule response: %v", err)
60+
}
61+
}

0 commit comments

Comments
 (0)