-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
77 lines (63 loc) · 1.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"context"
"log"
"os"
"strings"
"github.com/bluele/slack"
"github.com/ericchiang/k8s"
corev1 "github.com/ericchiang/k8s/apis/core/v1"
)
func main() {
log.Print("Starting k8s-event-notifier...")
var hookURL = os.Getenv("SLACK_API_URL")
var slackText = os.Getenv("SLACK_TEXT")
var eventFilter = strings.Split(os.Getenv("EVENT_FILTER"), ",")
reasonList := []string{}
reasonList = append(reasonList, eventFilter...)
client, err := k8s.NewInClusterClient()
if err != nil {
log.Printf("Client ERROR: %s\n", err.Error())
}
k8s.Register("", "v1", "events", false, &corev1.Event{})
var events corev1.Event
for {
watcher, err := client.Watch(context.Background(), "", &events)
if err != nil {
log.Printf("Watch ERROR: %s\n", err.Error())
}
defer watcher.Close()
EventLoop:
for {
e := new(corev1.Event)
_, err := watcher.Next(e)
if err != nil {
log.Printf("Event ERROR: %s\n", err.Error())
watcher.Close()
break EventLoop
}
if ok := stringInSlice(*e.Reason, reasonList); ok {
log.Print(*e.InvolvedObject.Name, *e.Message)
text := *e.InvolvedObject.Name + " in " + *e.InvolvedObject.Namespace + " namespace"
hook := slack.NewWebHook(hookURL)
err := hook.PostMessage(&slack.WebHookPostPayload{
Text: slackText,
Attachments: []*slack.Attachment{
{Title: *e.Message, Text: text, Color: "#9741f4", Pretext: *e.Reason},
},
})
if err != nil {
log.Printf("Slack Post ERROR: %s\n", err.Error())
}
}
}
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}