feat: notify ReconciliationSucceeded only after modification

This commit is contained in:
Bastien Riviere 2023-09-03 13:44:16 +02:00
parent 4234eee583
commit 6b035f4deb
Signed by: babariviere
GPG key ID: 4E5F0839249F162E

View file

@ -9,15 +9,21 @@ import (
"time" "time"
) )
type FluxNotification struct { type fluxInvolvedObject struct {
InvolvedObject struct {
Kind string `json:"kind"` Kind string `json:"kind"`
Namespace string `json:"namespace"` Namespace string `json:"namespace"`
Name string `json:"name"` Name string `json:"name"`
UID string `json:"uid"` UID string `json:"uid"`
APIVersion string `json:"apiVersion"` APIVersion string `json:"apiVersion"`
ResourceVersion string `json:"resourceVersion"` ResourceVersion string `json:"resourceVersion"`
} `json:"involvedObject"` }
func (f fluxInvolvedObject) String() string {
return strings.ToLower(f.Kind) + "/" + f.Namespace + "." + f.Name
}
type FluxNotification struct {
InvolvedObject fluxInvolvedObject `json:"involvedObject"`
Severity string `json:"severity"` Severity string `json:"severity"`
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
Message string `json:"message"` Message string `json:"message"`
@ -31,10 +37,15 @@ type FluxNotification struct {
ReportingInstance string `json:"reportingInstance"` ReportingInstance string `json:"reportingInstance"`
} }
type FluxHandler struct{} type FluxHandler struct {
// Register all modifications of reconciliations
reconciliations map[string]bool
}
func NewFluxHandler() FluxHandler { func NewFluxHandler() FluxHandler {
return FluxHandler{} return FluxHandler{
reconciliations: make(map[string]bool),
}
} }
func (f FluxHandler) FormatNotification(r io.Reader) (Notification, error) { func (f FluxHandler) FormatNotification(r io.Reader) (Notification, error) {
@ -47,13 +58,21 @@ func (f FluxHandler) FormatNotification(r io.Reader) (Notification, error) {
return Notification{}, err return Notification{}, err
} }
obj := not.InvolvedObject.String()
if not.Reason == "ReconciliationSucceeded" { if not.Reason == "ReconciliationSucceeded" {
if ok := f.reconciliations[obj]; !ok {
// Filter out spammy ReconciliationSucceeded notification // Filter out spammy ReconciliationSucceeded notification
return Notification{}, nil return Notification{}, nil
} }
title := fmt.Sprintf("[%s] %s %s/%s.%s", not.Severity, not.Reason, // we will print the object so skip it next time it spam
strings.ToLower(not.InvolvedObject.Kind), not.InvolvedObject.Namespace, not.InvolvedObject.Name) f.reconciliations[obj] = false
} else {
// object has been modified, we can print it next time
f.reconciliations[obj] = true
}
title := fmt.Sprintf("[%s] %s %s", not.Severity, not.Reason, obj)
body := not.Message + "\n\n**revision**\n" + not.Metadata.Revision body := not.Message + "\n\n**revision**\n" + not.Metadata.Revision
l.Debug("flux notification", slog.Group("notification", l.Debug("flux notification", slog.Group("notification",