diff --git a/api/v1beta2/alert_types.go b/api/v1beta2/alert_types.go index b6531cf11..3a9e2f7c3 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 cdbd33f57..78dd00068 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 23b9142b4..121e6ddd1 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 0672eaf27..d097025a5 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 cb4c32927..c06af0729 100644 --- a/docs/spec/v1beta2/alerts.md +++ b/docs/spec/v1beta2/alerts.md @@ -162,6 +162,13 @@ 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. + ### 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 5e7a4ce51..b239a8e57 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) {