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

Implement regex exclusions for alerts #138

Merged
merged 1 commit into from
Feb 8, 2021
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
8 changes: 6 additions & 2 deletions api/v1beta1/alert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (

// AlertSpec defines an alerting rule for events involving a list of objects
type AlertSpec struct {
// Send events using this provider
// Send events using this provider.
// +required
ProviderRef meta.LocalObjectReference `json:"providerRef"`

Expand All @@ -38,10 +38,14 @@ type AlertSpec struct {
// +optional
EventSeverity string `json:"eventSeverity,omitempty"`

// Filter events based on the involved objects
// Filter events based on the involved objects.
// +required
EventSources []CrossNamespaceObjectReference `json:"eventSources"`

// A list of Golang regular expressions to be used for excluding messages.
// +optional
ExclusionList []string `json:"exclusionList,omitempty"`

// Short description of the impact and affected cluster.
// +optional
Summary string `json:"summary,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

10 changes: 8 additions & 2 deletions config/crd/bases/notification.toolkit.fluxcd.io_alerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ spec:
- error
type: string
eventSources:
description: Filter events based on the involved objects
description: Filter events based on the involved objects.
items:
description: CrossNamespaceObjectReference contains enough information
to let you locate the typed referenced object at cluster level
Expand Down Expand Up @@ -91,8 +91,14 @@ spec:
- name
type: object
type: array
exclusionList:
description: A list of Golang regular expressions to be used for excluding
messages.
items:
type: string
type: array
providerRef:
description: Send events using this provider
description: Send events using this provider.
properties:
name:
description: Name of the referent
Expand Down
32 changes: 28 additions & 4 deletions docs/api/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ github.com/fluxcd/pkg/apis/meta.LocalObjectReference
</em>
</td>
<td>
<p>Send events using this provider</p>
<p>Send events using this provider.</p>
</td>
</tr>
<tr>
Expand All @@ -106,7 +106,19 @@ If set to &lsquo;info&rsquo; no events will be filtered.</p>
</em>
</td>
<td>
<p>Filter events based on the involved objects</p>
<p>Filter events based on the involved objects.</p>
</td>
</tr>
<tr>
<td>
<code>exclusionList</code><br>
<em>
[]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>A list of Golang regular expressions to be used for excluding messages.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -465,7 +477,7 @@ github.com/fluxcd/pkg/apis/meta.LocalObjectReference
</em>
</td>
<td>
<p>Send events using this provider</p>
<p>Send events using this provider.</p>
</td>
</tr>
<tr>
Expand All @@ -491,7 +503,19 @@ If set to &lsquo;info&rsquo; no events will be filtered.</p>
</em>
</td>
<td>
<p>Filter events based on the involved objects</p>
<p>Filter events based on the involved objects.</p>
</td>
</tr>
<tr>
<td>
<code>exclusionList</code><br>
<em>
[]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>A list of Golang regular expressions to be used for excluding messages.</p>
</td>
</tr>
<tr>
Expand Down
34 changes: 32 additions & 2 deletions docs/spec/v1beta1/alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Spec:

```go
type AlertSpec struct {
// Send events using this provider
// Send events using this provider.
// +required
ProviderRef meta.LocalObjectReference `json:"providerRef"`

Expand All @@ -17,10 +17,14 @@ type AlertSpec struct {
// +optional
EventSeverity string `json:"eventSeverity,omitempty"`

// Filter events based on the involved objects
// Filter events based on the involved objects.
// +required
EventSources []CrossNamespaceObjectReference `json:"eventSources"`

// A list of Golang regular expressions to be used for excluding messages.
// +optional
ExclusionList []string `json:"exclusionList,omitempty"`

// Short description of the impact and affected cluster.
// +optional
Summary string `json:"summary,omitempty"`
Expand Down Expand Up @@ -115,3 +119,29 @@ spec:
- kind: HelmRelease
name: nginx-ingress
```

Skip alerting if the message matches a [Go regex](https://golang.org/pkg/regexp/syntax)
from the exclusion list:

```yaml
apiVersion: notification.toolkit.fluxcd.io/v1beta1
kind: Alert
metadata:
name: flux-system
namespace: flux-system
spec:
providerRef:
name: on-call-slack
eventSeverity: error
eventSources:
- kind: GitRepository
name: flux-system
exclusionList:
- "waiting.*socket"
```

The above definition will not send alerts for transient Git clone errors like:

```
unable to clone 'ssh://[email protected]/v3/...', error: SSH could not read data: Error waiting on socket
```
15 changes: 15 additions & 0 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package server
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -72,6 +74,19 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
continue
}

// skip alert if the message matches a regex from the exclusion list
if len(alert.Spec.ExclusionList) > 0 {
for _, exp := range alert.Spec.ExclusionList {
if r, err := regexp.Compile(exp); err == nil {
if r.Match([]byte(event.Message)) {
continue
}
} else {
s.logger.Error(err, fmt.Sprintf("failed to compile regex: %s", exp))
}
}
}

// filter alerts by object and severity
for _, source := range alert.Spec.EventSources {
if source.Namespace == "" {
Expand Down