Skip to content

Commit

Permalink
Add event metadata field to Alert spec
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Pimenta <[email protected]>
  • Loading branch information
matheuscscp committed May 11, 2023
1 parent ed5b2fe commit e9d1fb3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
7 changes: 7 additions & 0 deletions api/v1beta2/alert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions docs/api/v1beta2/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions docs/spec/v1beta2/alerts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e9d1fb3

Please sign in to comment.