From e9d1fb3d8662c3b14b10538511a21191ed9f82ea Mon Sep 17 00:00:00 2001
From: Matheus Pimenta
Date: Wed, 10 May 2023 14:27:39 +0100
Subject: [PATCH] Add event metadata field to Alert spec
Signed-off-by: Matheus Pimenta
---
api/v1beta2/alert_types.go | 7 +++++
api/v1beta2/zz_generated.deepcopy.go | 7 +++++
...notification.toolkit.fluxcd.io_alerts.yaml | 8 +++++
docs/api/v1beta2/notification.md | 30 +++++++++++++++++++
docs/spec/v1beta2/alerts.md | 29 ++++++++++++++++++
internal/server/event_handlers.go | 18 ++++++-----
6 files changed, 92 insertions(+), 7 deletions(-)
diff --git a/api/v1beta2/alert_types.go b/api/v1beta2/alert_types.go
index b6531cf..3a9e2f7 100644
--- a/api/v1beta2/alert_types.go
+++ b/api/v1beta2/alert_types.go
@@ -50,6 +50,13 @@ type AlertSpec struct {
// +optional
InclusionList []string `json:"inclusionList,omitempty"`
+ // EventMetadata is an optional field for adding metadata to events emitted by the
+ // controller. Metadata fields added by the controller have priority over the fields
+ // added here, and the fields added here have priority over fields originally present
+ // in the event.
+ // +optional
+ EventMetadata map[string]string `json:"eventMetadata,omitempty"`
+
// ExclusionList specifies a list of Golang regular expressions
// to be used for excluding messages.
// +optional
diff --git a/api/v1beta2/zz_generated.deepcopy.go b/api/v1beta2/zz_generated.deepcopy.go
index cdbd33f..78dd000 100644
--- a/api/v1beta2/zz_generated.deepcopy.go
+++ b/api/v1beta2/zz_generated.deepcopy.go
@@ -103,6 +103,13 @@ func (in *AlertSpec) DeepCopyInto(out *AlertSpec) {
*out = make([]string, len(*in))
copy(*out, *in)
}
+ if in.EventMetadata != nil {
+ in, out := &in.EventMetadata, &out.EventMetadata
+ *out = make(map[string]string, len(*in))
+ for key, val := range *in {
+ (*out)[key] = val
+ }
+ }
if in.ExclusionList != nil {
in, out := &in.ExclusionList, &out.ExclusionList
*out = make([]string, len(*in))
diff --git a/config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml b/config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml
index 23b9142..121e6dd 100644
--- a/config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml
+++ b/config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml
@@ -240,6 +240,14 @@ spec:
description: AlertSpec defines an alerting rule for events involving a
list of objects.
properties:
+ eventMetadata:
+ additionalProperties:
+ type: string
+ description: EventMetadata is an optional field for adding metadata
+ to events emitted by the controller. Metadata fields added by the
+ controller have priority over the fields added here, and the fields
+ added here have priority over fields originally present in the event.
+ type: object
eventSeverity:
default: info
description: EventSeverity specifies how to filter events based on
diff --git a/docs/api/v1beta2/notification.md b/docs/api/v1beta2/notification.md
index 0672eaf..d097025 100644
--- a/docs/api/v1beta2/notification.md
+++ b/docs/api/v1beta2/notification.md
@@ -127,6 +127,21 @@ to be used for including messages.
+eventMetadata
+
+map[string]string
+
+ |
+
+(Optional)
+ EventMetadata is an optional field for adding metadata to events emitted by the
+controller. Metadata fields added by the controller have priority over the fields
+added here, and the fields added here have priority over fields originally present
+in the event.
+ |
+
+
+
exclusionList
[]string
@@ -615,6 +630,21 @@ to be used for including messages.
|
+eventMetadata
+
+map[string]string
+
+ |
+
+(Optional)
+ EventMetadata is an optional field for adding metadata to events emitted by the
+controller. Metadata fields added by the controller have priority over the fields
+added here, and the fields added here have priority over fields originally present
+in the event.
+ |
+
+
+
exclusionList
[]string
diff --git a/docs/spec/v1beta2/alerts.md b/docs/spec/v1beta2/alerts.md
index cb4c329..a8aba15 100644
--- a/docs/spec/v1beta2/alerts.md
+++ b/docs/spec/v1beta2/alerts.md
@@ -162,6 +162,35 @@ starting the controller with the `--no-cross-namespace-refs=true` flag.
When this flag is set, alerts can only refer to event sources in the same namespace as the alert object,
preventing tenants from subscribing to another tenant's events.
+### Event metadata
+
+`.spec.eventMetadata` is an optional field for adding metadata to events emitted by the
+controller. Metadata fields added by the controller have priority over the fields
+added here, and the fields added here have priority over fields originally present
+in the event.
+
+#### Example
+
+Add metadata fields to successful `HelmRelease` events:
+
+```yaml
+---
+apiVersion: notification.toolkit.fluxcd.io/v1beta2
+kind: Alert
+metadata:
+ name:
+spec:
+ eventSources:
+ - kind: HelmRelease
+ name: '*'
+ inclusionList:
+ - ".*succeeded.*"
+ eventMetadata:
+ app.kubernetes.io/env: "production"
+ app.kubernetes.io/cluster: "my-cluster"
+ app.kubernetes.io/region: "us-east-1"
+```
+
### Event severity
`.spec.eventSeverity` is an optional field to filter events based on severity. When not specified, or
diff --git a/internal/server/event_handlers.go b/internal/server/event_handlers.go
index 5e7a4ce..b239a8e 100644
--- a/internal/server/event_handlers.go
+++ b/internal/server/event_handlers.go
@@ -273,14 +273,18 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
}
notification := *event.DeepCopy()
+ meta := notification.Metadata
+ if meta == nil {
+ meta = make(map[string]string)
+ }
+ for key, value := range alert.Spec.EventMetadata {
+ meta[key] = value
+ }
if alert.Spec.Summary != "" {
- if notification.Metadata == nil {
- notification.Metadata = map[string]string{
- "summary": alert.Spec.Summary,
- }
- } else {
- notification.Metadata["summary"] = alert.Spec.Summary
- }
+ meta["summary"] = alert.Spec.Summary
+ }
+ if len(meta) > 0 {
+ notification.Metadata = meta
}
go func(n notifier.Interface, e eventv1.Event) {
|