Skip to content

Commit

Permalink
Include sync errors in notification of syncs
Browse files Browse the repository at this point in the history
Newer flux daemons (since fluxcd/flux#970) report any sync
failures for individual resources. Take advantage of this by including
the errors in sync notifications.
  • Loading branch information
squaremo committed Mar 12, 2018
1 parent d258fb4 commit ebb4872
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions flux-api/notifications/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type slackMsg struct {
}

type slackAttachment struct {
Title string `json:"title,omitempty"`
Fallback string `json:"fallback,omitempty"`
Text string `json:"text"`
Author string `json:"author_name,omitempty"`
Expand Down Expand Up @@ -179,6 +180,9 @@ func slackNotifySync(config config.Notifier, sync *event.Event) error {
if len(details.Commits) > 0 && details.Commits[0].Message != "" {
attachments = append(attachments, slackCommitsAttachment(details))
}
if len(details.Errors) > 0 {
attachments = append(attachments, slackSyncErrorAttachment(details))
}
return notify(syncEventType, config, slackMsg{
Username: config.Username,
Text: sync.String(),
Expand Down Expand Up @@ -258,6 +262,22 @@ func slackCommitsAttachment(ev *event.SyncEventMetadata) slackAttachment {
}
}

func slackSyncErrorAttachment(ev *event.SyncEventMetadata) slackAttachment {
buf := &bytes.Buffer{}
fmt.Fprintln(buf, "```")

for _, err := range ev.Errors {
fmt.Fprintf(buf, "%s (%s)\n %s\n", err.ID, err.Path, err.Error)
}
fmt.Fprintln(buf, "```")
return slackAttachment{
Title: "Resource sync errors",
Text: buf.String(),
Markdown: []string{"text"},
Color: "warning",
}
}

func notify(eventType string, config config.Notifier, msg slackMsg) error {
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(msg); err != nil {
Expand Down

0 comments on commit ebb4872

Please sign in to comment.