Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #30 from Ticketmaster/slack-notifications
Browse files Browse the repository at this point in the history
add slack notifications
  • Loading branch information
medyagh authored Sep 19, 2017
2 parents 96a2046 + eee417f commit b415b10
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
19 changes: 19 additions & 0 deletions actions/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

libs "github.com/k8guard/k8guardlibs"
"github.com/k8guard/k8guardlibs/k8s"
"github.com/nlopes/slack"
"github.com/tbruyelle/hipchat-go/hipchat"
"gopkg.in/gomail.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -21,6 +22,9 @@ import (
var lastHipchat = time.Time{}
var lastHipchatMutex = &sync.Mutex{}

var lastSlack = time.Time{}
var lastSlackMutex = &sync.Mutex{}

const messageTemplate = `
{{if .LastWarning}}
Expand Down Expand Up @@ -79,6 +83,7 @@ func NotifyOfViolation(actionMessage actionMessage) {
}

go notifyHipChat(tpl.String(), ns, actionMessage.LastWarning)
go notifySlack(tpl.String(), ns, actionMessage.LastWarning)
notifyEmail(tpl.String(), ns, actionMessage.LastWarning)

}
Expand Down Expand Up @@ -141,6 +146,20 @@ func notifyHipChat(message string, namespace *v1.Namespace, lastWarning bool) {

}

func notifySlack(message string, namespace *v1.Namespace, lastWarning bool) {
canChat := time.Now().Sub(lastSlack) < libs.Cfg.DurationBetweenChatNotifications
if canChat {
time.Sleep(libs.Cfg.DurationBetweenChatNotifications)
}
lastSlack = time.Now()
lastSlackMutex.Unlock()
api := slack.New(libs.Cfg.SlackToken)
_, _, err := api.PostMessage(libs.Cfg.SlackChannel, message, slack.PostMessageParameters{})
if err != nil {
libs.Log.Error(err)
}
}

func notifyEmail(actionMessage string, namespace *v1.Namespace, lastWarning bool) {

if len(libs.Cfg.SmtpServer) == 0 {
Expand Down
11 changes: 7 additions & 4 deletions glide.lock

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

3 changes: 2 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import:

- package: github.com/k8guard/k8guardlibs
repo: https://github.com/k8guard/k8guardlibs.git
version: 8cec16bc4ab2dd3555caa42239d7bf985a7641d5
version: 7f5c929fff140db1da48751db28bf1e8f87bf161
vcs: git

- package: github.com/nlopes/slack

- package: gopkg.in/gomail.v2

0 comments on commit b415b10

Please sign in to comment.