Skip to content

Commit

Permalink
Enhance Notification : short/long notification type
Browse files Browse the repository at this point in the history
This commit,
- enables sending short/long event notifications to channels
- adds a optional filed "notiftype" under channel config to change between short (default) and long notif types
- adds Message() method to event object for creating brief messages (to use across handlers)
- BugFix: enable/disable recommendations through config file
  • Loading branch information
codenio committed Jul 26, 2019
1 parent 4303984 commit 7508ba9
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 127 deletions.
6 changes: 4 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,17 @@ communications:
enabled: false
channel: 'SLACK_CHANNEL'
token: 'SLACK_API_TOKEN'

notiftype: short # Change notification type short/long you want to receive. notiftype is optional and Default notification type is short (if not specified)

# Settings for Mattermost
mattermost:
enabled: false
url: 'MATTERMOST_SERVER_URL' # URL where Mattermost is running. e.g https://example.com:9243
token: 'MATTERMOST_TOKEN' # Personal Access token generated by BotKube user
team: 'MATTERMOST_TEAM' # Mattermost Team to configure with BotKube
channel: 'MATTERMOST_CHANNEL' # Mattermost Channel for receiving BotKube alerts

notiftype: short # Change notification type short/long you want to receive. notiftype is optional and Default notification type is short (if not specified)

# Settings for ELS
elasticsearch:
enabled: false
Expand Down
6 changes: 4 additions & 2 deletions deploy-all-in-one-tls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,17 @@ data:
enabled: false
channel: 'SLACK_CHANNEL'
token: 'SLACK_API_TOKEN'
notiftype: short # Change notification type short/long you want to receive. notiftype is optional and Default notification type is short (if not specified)
# Settings for Mattermost
mattermost:
enabled: false
url: 'MATTERMOST_SERVER_URL' # URL where Mattermost is running. e.g https://example.com:9243
token: 'MATTERMOST_TOKEN' # Personal Access token generated by BotKube user
team: 'MATTERMOST_TEAM' # Mattermost Team to configure with BotKube
channel: 'MATTERMOST_CHANNEL' # Mattermost Channel for receiving BotKube alerts
notiftype: short # Change notification type short/long you want to receive. notiftype is optional and Default notification type is short (if not specified)
# Settings for ELS
elasticsearch:
enable: false
Expand Down
6 changes: 4 additions & 2 deletions deploy-all-in-one.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,17 @@ data:
enabled: false
channel: 'SLACK_CHANNEL'
token: 'SLACK_API_TOKEN'
notiftype: short # Change notification type short/long you want to receive. notiftype is optional and Default notification type is short (if not specified)
# Settings for Mattermost
mattermost:
enabled: false
url: 'MATTERMOST_SERVER_URL' # URL where Mattermost is running. e.g https://example.com:9243
token: 'MATTERMOST_TOKEN' # Personal Access token generated by BotKube user
team: 'MATTERMOST_TEAM' # Mattermost Team to configure with BotKube
channel: 'MATTERMOST_CHANNEL' # Mattermost Channel for receiving BotKube alerts
notiftype: short # Change notification type short/long you want to receive. notiftype is optional and Default notification type is short (if not specified)
# Settings for ELS
elasticsearch:
enable: false
Expand Down
4 changes: 3 additions & 1 deletion helm/botkube/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ config:
enabled: false
channel: 'SLACK_CHANNEL' # Slack channel name without '#' prefix where you have added BotKube and want to receive notifications in
token: 'SLACK_API_TOKEN'
notiftype: short # Change notification type short/long you want to receive. notiftype is optional and Default notification type is short (if not specified)

# Settings for Mattermost
mattermost:
Expand All @@ -161,7 +162,8 @@ config:
token: 'MATTERMOST_TOKEN' # Personal Access token generated by BotKube user
team: 'MATTERMOST_TEAM' # Mattermost Team to configure with BotKube
channel: 'MATTERMOST_CHANNEL' # Mattermost Channel for receiving BotKube alerts

notiftype: short # Change notification type short/long you want to receive. notiftype is optional and Default notification type is short (if not specified)

# Settings for ELS
elasticsearch:
enabled: false
Expand Down
25 changes: 17 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const (
WarningEvent EventType = "warning"
// AllEvent to watch all events
AllEvent EventType = "all"
// ShortNotify is the Default NotifType
ShortNotify NotifType = "short"
// LongNotify for short events notification
LongNotify NotifType = "long"
)

// 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 NotifType `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 NotifType `yaml:",omitempty"`
}

// Settings for multicluster support
Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ func sendEvent(obj interface{}, c *config.Config, kind string, eventType config.
return
}

// check if Recommendations are disabled
if !c.Recommendations {
event.Recommendations = nil
log.Logger.Debug("Skipping Recommendations in Event Notifications")
}

var notifier notify.Notifier
// Send notification to communication channel
if c.Communications.Slack.Enabled {
Expand Down
50 changes: 50 additions & 0 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,53 @@ 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
}
}
if len(event.Recommendations) > 0 {
recommend := ""
for _, m := range event.Recommendations {
recommend = recommend + m
}
message = message + fmt.Sprintf("\nRecommendations: %s", recommend)
}

switch event.Type {
case config.CreateEvent, config.DeleteEvent, config.UpdateEvent:
msg = fmt.Sprintf(
"%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 config.ErrorEvent:
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 config.WarningEvent:
msg = fmt.Sprintf(
"Warning %s: `%s` of cluster `%s`, namespace `%s`:\n```%s``` ",
event.Kind,
event.Name,
event.Cluster,
event.Namespace,
message,
)
}
return msg
}
126 changes: 73 additions & 53 deletions pkg/notify/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Mattermost struct {
Client *model.Client4
Channel string
ClusterName string
NotifType config.NotifType
}

// NewMattermost returns new Mattermost object
Expand Down Expand Up @@ -44,76 +45,95 @@ 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 config.LongNotify:
fields = []*model.SlackAttachmentField{
{
Title: "Kind",
Value: event.Kind,
Short: true,
},
{
Title: "Name",
Value: event.Name,
Short: true,
},
}

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

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

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

if event.Action != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Action",
Value: event.Action,
})
}
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,
})
case config.ShortNotify:
fallthrough

default:
// 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(),
},
}
}

attachment := []*model.SlackAttachment{{
Fields: fields,
Expand Down
Loading

0 comments on commit 7508ba9

Please sign in to comment.