Skip to content

Commit

Permalink
Fix Alert .spec.eventMetadata behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
  • Loading branch information
matheuscscp committed May 18, 2023
1 parent 49122b9 commit 03b1c9a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 20 deletions.
9 changes: 5 additions & 4 deletions api/v1beta2/alert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ 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.
// EventMetadata is an optional field for adding metadata to events dispatched by the
// controller. This can be used for enhancing the context of the event. If a field
// would override one already present on the original event as generated by the emitter,
// then the override doesn't happen, i.e. the original value is preserved, and an error
// log is printed.
// +optional
EventMetadata map[string]string `json:"eventMetadata,omitempty"`

Expand Down
8 changes: 5 additions & 3 deletions config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ spec:
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.
to events dispatched by the controller. This can be used for enhancing
the context of the event. If a field would override one already
present on the original event as generated by the emitter, then
the override doesn't happen, i.e. the original value is preserved,
and an error log is printed.
type: object
eventSeverity:
default: info
Expand Down
18 changes: 10 additions & 8 deletions docs/api/v1beta2/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ map[string]string
</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>
<p>EventMetadata is an optional field for adding metadata to events dispatched by the
controller. This can be used for enhancing the context of the event. If a field
would override one already present on the original event as generated by the emitter,
then the override doesn&rsquo;t happen, i.e. the original value is preserved, and an error
log is printed.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -637,10 +638,11 @@ map[string]string
</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>
<p>EventMetadata is an optional field for adding metadata to events dispatched by the
controller. This can be used for enhancing the context of the event. If a field
would override one already present on the original event as generated by the emitter,
then the override doesn&rsquo;t happen, i.e. the original value is preserved, and an error
log is printed.</p>
</td>
</tr>
<tr>
Expand Down
9 changes: 5 additions & 4 deletions docs/spec/v1beta2/alerts.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ 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.
`.spec.eventMetadata` is an optional field for adding metadata to events dispatched by
the controller. This can be used for enhancing the context of the event. If a field
would override one already present on the original event as generated by the emitter,
then the override doesn't happen, i.e. the original value is preserved, and an error
log is printed.

#### Example

Expand Down
49 changes: 49 additions & 0 deletions internal/controllers/alert_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -186,8 +187,22 @@ func TestAlertReconciler_EventHandler(t *testing.T) {
stopCh := make(chan struct{})
go eventServer.ListenAndServe(stopCh, eventMdlw, store)

rcvG := g
var rcvMetadata map[string]string
rcvServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req = r

if rcvMetadata != nil {
b, err := io.ReadAll(r.Body)
rcvG.Expect(err).ToNot(HaveOccurred())

var event eventv1.Event
err = json.Unmarshal(b, &event)
rcvG.Expect(err).ToNot(HaveOccurred())

rcvG.Expect(event.Metadata).To(BeEquivalentTo(rcvMetadata))
}

w.WriteHeader(200)
}))
defer rcvServer.Close()
Expand Down Expand Up @@ -272,6 +287,9 @@ func TestAlertReconciler_EventHandler(t *testing.T) {
"doesnotoccur", // not intended to match
"excluded",
},
EventMetadata: map[string]string{
"foo": "bar",
},
},
}
inclusionAlert := alert.DeepCopy()
Expand Down Expand Up @@ -334,12 +352,38 @@ func TestAlertReconciler_EventHandler(t *testing.T) {
name string
modifyEventFunc func(e eventv1.Event) eventv1.Event
forwarded bool
rcvMetadata map[string]string
}{
{
name: "forwards when source is a match",
modifyEventFunc: func(e eventv1.Event) eventv1.Event { return e },
forwarded: true,
},
{
name: "forwards alert metadata",
modifyEventFunc: func(e eventv1.Event) eventv1.Event {
e.Message = "test"
return e
},
forwarded: true,
rcvMetadata: map[string]string{
"foo": "bar",
},
},
{
name: "forwards original event metadata (does not override with alert metadata)",
modifyEventFunc: func(e eventv1.Event) eventv1.Event {
e.Message = "test"
e.Metadata = map[string]string{
"foo": "baz",
}
return e
},
forwarded: true,
rcvMetadata: map[string]string{
"foo": "baz",
},
},
{
name: "drops event when source Kind does not match",
modifyEventFunc: func(e eventv1.Event) eventv1.Event {
Expand Down Expand Up @@ -423,6 +467,8 @@ func TestAlertReconciler_EventHandler(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
rcvG = g
rcvMetadata = tt.rcvMetadata
// Reset the common variables to their fixture value.
req = nil
event = *eventFixture.DeepCopy()
Expand Down Expand Up @@ -470,6 +516,7 @@ func TestAlertReconciler_EventHandler(t *testing.T) {
name string
modifyEventFunc func(e eventv1.Event) eventv1.Event
forwarded bool
rcvMetadata map[string]string
}{
{
name: "forwards when message matches inclusion list",
Expand Down Expand Up @@ -497,6 +544,8 @@ func TestAlertReconciler_EventHandler(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
rcvG = g
rcvMetadata = tt.rcvMetadata
// Reset the common variables to their fixture value.
req = nil
event = *eventFixture2.DeepCopy()
Expand Down
13 changes: 12 additions & 1 deletion internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,18 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
meta = make(map[string]string)
}
for key, value := range alert.Spec.EventMetadata {
meta[key] = value
if originalValue, alreadyPresent := meta[key]; !alreadyPresent {
meta[key] = value
} else {
const msg = "cannot override metadata field already present on event"
s.logger.Error(errors.New(msg), msg,
"reconciler kind", apiv1beta2.AlertKind,
"name", alert.Name,
"namespace", alert.Namespace,
"metadata field name", key,
"metadata field original value", originalValue,
"metadata field override value", value)
}
}
if alert.Spec.Summary != "" {
meta["summary"] = alert.Spec.Summary
Expand Down

0 comments on commit 03b1c9a

Please sign in to comment.