Add event metadata field to Alert spec

Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
This commit is contained in:
Matheus Pimenta 2023-05-10 14:27:39 +01:00
parent ed5b2fe266
commit e9d1fb3d86
6 changed files with 92 additions and 7 deletions

View File

@ -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

View File

@ -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))

View File

@ -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

View File

@ -127,6 +127,21 @@ to be used for including messages.</p>
</tr>
<tr>
<td>
<code>eventMetadata</code><br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>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.</p>
</td>
</tr>
<tr>
<td>
<code>exclusionList</code><br>
<em>
[]string
@ -615,6 +630,21 @@ to be used for including messages.</p>
</tr>
<tr>
<td>
<code>eventMetadata</code><br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>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.</p>
</td>
</tr>
<tr>
<td>
<code>exclusionList</code><br>
<em>
[]string

View File

@ -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: <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

View File

@ -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) {