Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alert summary to notification metadata #97

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/v1beta1/alert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type AlertSpec struct {
// +required
EventSources []CrossNamespaceObjectReference `json:"eventSources"`

// Short description of the impact and affected cluster.
// +optional
Summary string `json:"summary,omitempty"`

// This flag tells the controller to suspend subsequent events dispatching.
// Defaults to false.
// +optional
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ spec:
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
type: object
summary:
description: Short description of the impact and affected cluster.
type: string
suspend:
description: This flag tells the controller to suspend subsequent
events dispatching. Defaults to false.
Expand Down
24 changes: 24 additions & 0 deletions docs/api/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ If set to &lsquo;info&rsquo; no events will be filtered.</p>
</tr>
<tr>
<td>
<code>summary</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Short description of the impact and affected cluster.</p>
</td>
</tr>
<tr>
<td>
<code>suspend</code><br>
<em>
bool
Expand Down Expand Up @@ -491,6 +503,18 @@ If set to &lsquo;info&rsquo; no events will be filtered.</p>
</tr>
<tr>
<td>
<code>summary</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Short description of the impact and affected cluster.</p>
</td>
</tr>
<tr>
<td>
<code>suspend</code><br>
<em>
bool
Expand Down
22 changes: 22 additions & 0 deletions docs/spec/v1beta1/alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type AlertSpec struct {
// +required
EventSources []CrossNamespaceObjectReference `json:"eventSources"`

// Short description of the impact and affected cluster.
// +optional
Summary string `json:"summary,omitempty"`

// This flag tells the controller to suspend subsequent events dispatching.
// Defaults to false.
// +optional
Expand Down Expand Up @@ -93,3 +97,21 @@ spec:
```

If you don't specify an event source namespace, the alert namespace will be used.

You can add a summary to describe the impact of an event:

```yaml
apiVersion: notification.toolkit.fluxcd.io/v1beta1
kind: Alert
metadata:
name: ingress
namespace: nginx
spec:
summary: "Ingress traffic affected in production (us-west-2)"
providerRef:
name: on-call-slack
eventSeverity: error
eventSources:
- kind: HelmRelease
name: nginx-ingress
```
48 changes: 24 additions & 24 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
}
}

// find providers
alertProviders := make([]notifier.Interface, 0)
if len(alerts) == 0 {
s.logger.Info("Discarding event, no alerts found for the involved object",
"object", event.InvolvedObject.Namespace+"/"+event.InvolvedObject.Name,
Expand All @@ -98,7 +96,12 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
return
}

// find providers
s.logger.Info("Dispatching event",
"object", event.InvolvedObject.Namespace+"/"+event.InvolvedObject.Name,
"kind", event.InvolvedObject.Kind,
"message", event.Message)

// dispatch notifications
for _, alert := range alerts {
var provider v1beta1.Provider
providerName := types.NamespacedName{Namespace: alert.Namespace, Name: alert.Spec.ProviderRef.Name}
Expand All @@ -107,8 +110,7 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
if err != nil {
s.logger.Error(err, "failed to read provider",
"provider", providerName)
w.WriteHeader(http.StatusBadRequest)
return
continue
}

webhook := provider.Spec.Address
Expand All @@ -122,8 +124,7 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
s.logger.Error(err, "failed to read secret",
"provider", providerName,
"secret", secretName.Name)
w.WriteHeader(http.StatusBadRequest)
return
continue
}

if address, ok := secret.Data["address"]; ok {
Expand All @@ -138,8 +139,7 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
if webhook == "" {
s.logger.Error(nil, "provider has no address",
"provider", providerName)
w.WriteHeader(http.StatusBadRequest)
return
continue
}

factory := notifier.NewFactory(webhook, provider.Spec.Proxy, provider.Spec.Username, provider.Spec.Channel, token)
Expand All @@ -148,27 +148,27 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
s.logger.Error(err, "failed to initialise provider",
"provider", providerName,
"type", provider.Spec.Type)
w.WriteHeader(http.StatusBadRequest)
return
continue
}

alertProviders = append(alertProviders, sender)
}

s.logger.Info("Dispatching event",
"object", event.InvolvedObject.Namespace+"/"+event.InvolvedObject.Name,
"kind", event.InvolvedObject.Kind,
"message", event.Message)
notification := *event
if alert.Spec.Summary != "" {
if notification.Metadata == nil {
notification.Metadata = map[string]string{
"summary": alert.Spec.Summary,
}
} else {
notification.Metadata["summary"] = alert.Spec.Summary
}
}

// send notifications in the background
for _, provider := range alertProviders {
go func(p notifier.Interface, e recorder.Event) {
if err := p.Post(e); err != nil {
go func(n notifier.Interface, e recorder.Event) {
if err := n.Post(e); err != nil {
s.logger.Error(err, "failed to send notification",
"object", e.InvolvedObject.Namespace+"/"+e.InvolvedObject.Name,
"kind", event.InvolvedObject.Kind)
"kind", e.InvolvedObject.Kind)
}
}(provider, *event)
}(sender, notification)
}

w.WriteHeader(http.StatusAccepted)
Expand Down