Skip to content

Commit

Permalink
Enhance Notification : Brief notification type
Browse files Browse the repository at this point in the history
This commit,
- enables sending brief event notifications to channels
- adds a optional filed "notiftype" under channel config to change between default and brief notif types
- adds Message() method to event object for creating brief messages (to use across handlers)
  • Loading branch information
codenio committed Jul 19, 2019
1 parent 4303984 commit cf65ed3
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 121 deletions.
25 changes: 17 additions & 8 deletions pkg/config/config.go
Expand Up @@ -24,6 +24,10 @@ const (
WarningEvent EventType = "warning"
// AllEvent to watch all events
AllEvent EventType = "all"
// DefaultNotify is the Default NotifType
DefaultNotify NotifType = "default"
// BriefNotify for short events notification
BriefNotify NotifType = " brief"
)

// EventType to watch
Expand All @@ -35,6 +39,9 @@ var ConfigFileName = "config.yaml"
// Notify flag to toggle event notification
var Notify = true

// NotifType to change notification type
type NotifType string

// Config structure of configuration yaml file
type Config struct {
Resources []Resource
Expand All @@ -59,9 +66,10 @@ type Communications struct {

// Slack configuration to authentication and send notifications
type Slack struct {
Enabled bool
Channel string
Token string `yaml:",omitempty"`
Enabled bool
Channel string
NotifType string `yaml:",omitempty"`
Token string `yaml:",omitempty"`
}

// ElasticSearch config auth settings
Expand All @@ -83,11 +91,12 @@ type Index struct {

// Mattermost configuration to authentication and send notifications
type Mattermost struct {
Enabled bool
URL string
Token string
Team string
Channel string
Enabled bool
URL string
Token string
Team string
Channel string
NotifType string `yaml:",omitempty"`
}

// Settings for multicluster support
Expand Down
42 changes: 42 additions & 0 deletions pkg/events/events.go
Expand Up @@ -183,3 +183,45 @@ func New(object interface{}, eventType config.EventType, kind string) Event {

return event
}

// Message returns event message in brief format.
// included as a part of event package to use across handlers.
func (event *Event) Message() (msg string) {
message := ""
if len(event.Messages) > 0 {
for _, m := range event.Messages {
message = message + m
}
}
switch event.Type {
case "create", "delete", "update":
msg = fmt.Sprintf(
"A %s `%s` in of cluster %s, namespace `%s` has been %s:\n```%s```",
event.Kind,
event.Name,
event.Cluster,
event.Namespace,
event.Type+"d",
message,
)
case "error":
msg = fmt.Sprintf(
"Error Occurred in %s: `%s` of cluster %s, namespace `%s`:\n```%s``` ",
event.Kind,
event.Name,
event.Cluster,
event.Namespace,
message,
)
case "warning":
msg = fmt.Sprintf(
"Warning %s: `%s` of cluster %s, namespace `%s`:\n```%s``` ",
event.Kind,
event.Name,
event.Cluster,
event.Namespace,
message,
)
}
return msg
}
123 changes: 70 additions & 53 deletions pkg/notify/mattermost.go
Expand Up @@ -16,6 +16,7 @@ type Mattermost struct {
Client *model.Client4
Channel string
ClusterName string
NotifType string
}

// NewMattermost returns new Mattermost object
Expand Down Expand Up @@ -44,77 +45,93 @@ func NewMattermost(c *config.Config) (Notifier, error) {
Client: client,
Channel: botChannel.Id,
ClusterName: c.Settings.ClusterName,
NotifType: c.Communications.Mattermost.NotifType,
}, nil
}

// SendEvent sends event notification to Mattermost
func (m *Mattermost) SendEvent(event events.Event) error {
log.Logger.Info(fmt.Sprintf(">> Sending to Mattermost: %+v", event))

fields := []*model.SlackAttachmentField{
{
Title: "Kind",
Value: event.Kind,
Short: true,
},
{
Title: "Name",
Value: event.Name,
Short: true,
},
}
var fields []*model.SlackAttachmentField

switch m.NotifType {
case "brief":
// set missing cluster name to event object
event.Cluster = m.ClusterName
fields = []*model.SlackAttachmentField{
{
Title: fmt.Sprintf("%s", event.Kind+" "+string(event.Type)),
Value: event.Message(),
},
}

if event.Namespace != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Namespace",
Value: event.Namespace,
Short: true,
})
}
default:
fields = []*model.SlackAttachmentField{
{
Title: "Kind",
Value: event.Kind,
Short: true,
},
{
Title: "Name",
Value: event.Name,
Short: true,
},
}

if event.Reason != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Reason",
Value: event.Reason,
Short: true,
})
}
if event.Namespace != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Namespace",
Value: event.Namespace,
Short: true,
})
}

if len(event.Messages) > 0 {
message := ""
for _, m := range event.Messages {
message = message + m
if event.Reason != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Reason",
Value: event.Reason,
Short: true,
})
}
fields = append(fields, &model.SlackAttachmentField{
Title: "Message",
Value: message,
})
}

if event.Action != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Action",
Value: event.Action,
})
}
if len(event.Messages) > 0 {
message := ""
for _, m := range event.Messages {
message = message + m
}
fields = append(fields, &model.SlackAttachmentField{
Title: "Message",
Value: message,
})
}

if event.Action != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Action",
Value: event.Action,
})
}

if len(event.Recommendations) > 0 {
rec := ""
for _, r := range event.Recommendations {
rec = rec + r
if len(event.Recommendations) > 0 {
rec := ""
for _, r := range event.Recommendations {
rec = rec + r
}
fields = append(fields, &model.SlackAttachmentField{
Title: "Recommendations",
Value: rec,
})
}

// Add clustername in the message
fields = append(fields, &model.SlackAttachmentField{
Title: "Recommendations",
Value: rec,
Title: "Cluster",
Value: m.ClusterName,
})
}

// Add clustername in the message
fields = append(fields, &model.SlackAttachmentField{
Title: "Cluster",
Value: m.ClusterName,
})

attachment := []*model.SlackAttachment{{
Fields: fields,
Footer: "BotKube",
Expand Down

0 comments on commit cf65ed3

Please sign in to comment.