Skip to content

Commit 81be272

Browse files
authored
Merge pull request #519 from matheuscscp/spec-metadata
Add event metadata field to Alert spec
2 parents ed5b2fe + e9d1fb3 commit 81be272

File tree

6 files changed

+92
-7
lines changed

6 files changed

+92
-7
lines changed

api/v1beta2/alert_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ type AlertSpec struct {
5050
// +optional
5151
InclusionList []string `json:"inclusionList,omitempty"`
5252

53+
// EventMetadata is an optional field for adding metadata to events emitted by the
54+
// controller. Metadata fields added by the controller have priority over the fields
55+
// added here, and the fields added here have priority over fields originally present
56+
// in the event.
57+
// +optional
58+
EventMetadata map[string]string `json:"eventMetadata,omitempty"`
59+
5360
// ExclusionList specifies a list of Golang regular expressions
5461
// to be used for excluding messages.
5562
// +optional

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ spec:
240240
description: AlertSpec defines an alerting rule for events involving a
241241
list of objects.
242242
properties:
243+
eventMetadata:
244+
additionalProperties:
245+
type: string
246+
description: EventMetadata is an optional field for adding metadata
247+
to events emitted by the controller. Metadata fields added by the
248+
controller have priority over the fields added here, and the fields
249+
added here have priority over fields originally present in the event.
250+
type: object
243251
eventSeverity:
244252
default: info
245253
description: EventSeverity specifies how to filter events based on

docs/api/v1beta2/notification.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ to be used for including messages.</p>
127127
</tr>
128128
<tr>
129129
<td>
130+
<code>eventMetadata</code><br>
131+
<em>
132+
map[string]string
133+
</em>
134+
</td>
135+
<td>
136+
<em>(Optional)</em>
137+
<p>EventMetadata is an optional field for adding metadata to events emitted by the
138+
controller. Metadata fields added by the controller have priority over the fields
139+
added here, and the fields added here have priority over fields originally present
140+
in the event.</p>
141+
</td>
142+
</tr>
143+
<tr>
144+
<td>
130145
<code>exclusionList</code><br>
131146
<em>
132147
[]string
@@ -615,6 +630,21 @@ to be used for including messages.</p>
615630
</tr>
616631
<tr>
617632
<td>
633+
<code>eventMetadata</code><br>
634+
<em>
635+
map[string]string
636+
</em>
637+
</td>
638+
<td>
639+
<em>(Optional)</em>
640+
<p>EventMetadata is an optional field for adding metadata to events emitted by the
641+
controller. Metadata fields added by the controller have priority over the fields
642+
added here, and the fields added here have priority over fields originally present
643+
in the event.</p>
644+
</td>
645+
</tr>
646+
<tr>
647+
<td>
618648
<code>exclusionList</code><br>
619649
<em>
620650
[]string

docs/spec/v1beta2/alerts.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ starting the controller with the `--no-cross-namespace-refs=true` flag.
162162
When this flag is set, alerts can only refer to event sources in the same namespace as the alert object,
163163
preventing tenants from subscribing to another tenant's events.
164164

165+
### Event metadata
166+
167+
`.spec.eventMetadata` is an optional field for adding metadata to events emitted by the
168+
controller. Metadata fields added by the controller have priority over the fields
169+
added here, and the fields added here have priority over fields originally present
170+
in the event.
171+
172+
#### Example
173+
174+
Add metadata fields to successful `HelmRelease` events:
175+
176+
```yaml
177+
---
178+
apiVersion: notification.toolkit.fluxcd.io/v1beta2
179+
kind: Alert
180+
metadata:
181+
name: <name>
182+
spec:
183+
eventSources:
184+
- kind: HelmRelease
185+
name: '*'
186+
inclusionList:
187+
- ".*succeeded.*"
188+
eventMetadata:
189+
app.kubernetes.io/env: "production"
190+
app.kubernetes.io/cluster: "my-cluster"
191+
app.kubernetes.io/region: "us-east-1"
192+
```
193+
165194
### Event severity
166195

167196
`.spec.eventSeverity` is an optional field to filter events based on severity. When not specified, or

internal/server/event_handlers.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,18 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
273273
}
274274

275275
notification := *event.DeepCopy()
276+
meta := notification.Metadata
277+
if meta == nil {
278+
meta = make(map[string]string)
279+
}
280+
for key, value := range alert.Spec.EventMetadata {
281+
meta[key] = value
282+
}
276283
if alert.Spec.Summary != "" {
277-
if notification.Metadata == nil {
278-
notification.Metadata = map[string]string{
279-
"summary": alert.Spec.Summary,
280-
}
281-
} else {
282-
notification.Metadata["summary"] = alert.Spec.Summary
283-
}
284+
meta["summary"] = alert.Spec.Summary
285+
}
286+
if len(meta) > 0 {
287+
notification.Metadata = meta
284288
}
285289

286290
go func(n notifier.Interface, e eventv1.Event) {

0 commit comments

Comments
 (0)