-
Notifications
You must be signed in to change notification settings - Fork 49
/
slack.go
97 lines (82 loc) · 2.76 KB
/
slack.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"encoding/json"
"os"
"strconv"
"strings"
"time"
"github.com/slack-go/slack"
"k8s.io/klog/v2"
)
type Slack struct {
WebhookUrl string
DefaultChannel string // Slack channel name
Username string // Slack username (will show in slack message)
ClusterName string // Kubernete cluster name (will show in slack message)
MuteSeconds int // The time to mute duplicate alerts
// History stores sent alerts, key: Namespace/podName, value: sentTime
History map[string]time.Time
}
type SlackMessage struct {
Title string
Text string
Footer string
}
func NewSlack() Slack {
var slackWebhookUrl, slackChannel, slackUsername, clusterName string
if slackWebhookUrl = os.Getenv("SLACK_WEBHOOK_URL"); slackWebhookUrl == "" {
klog.Exit("Environment variable SLACK_WEBHOOK_URL is not set")
}
if slackChannel = os.Getenv("SLACK_CHANNEL"); slackChannel == "" {
slackChannel = "restart-info-nonprod"
klog.Warningf("Environment variable SLACK_CHANNEL is not set, default: %s\n", slackChannel)
}
if slackUsername = os.Getenv("SLACK_USERNAME"); slackUsername == "" {
slackUsername = "k8s-pod-restart-info-collector"
klog.Warningf("Environment variable SLACK_USERNAME is not set, default: %s\n", slackUsername)
}
if clusterName = os.Getenv("CLUSTER_NAME"); clusterName == "" {
clusterName = "cluster-name"
klog.Warningf("Environment variable CLUSTER_NAME is not set, default: %s\n", clusterName)
}
muteSeconds, err := strconv.Atoi(os.Getenv("MUTE_SECONDS"))
if err != nil {
muteSeconds = 600
klog.Warningf("Environment variable MUTE_SECONDS is not set, default: %d\n", muteSeconds)
}
klog.Infof("Slack Info: channel: %s, username: %s, clustername: %s, muteseconds: %d\n", slackChannel, slackUsername, clusterName, muteSeconds)
return Slack{
WebhookUrl: slackWebhookUrl,
DefaultChannel: slackChannel,
Username: slackUsername,
ClusterName: clusterName,
MuteSeconds: muteSeconds,
History: make(map[string]time.Time),
}
}
func (s Slack) sendToChannel(msg SlackMessage, slackChannel string) error {
channel := s.DefaultChannel
if slackChannel != "" {
channel = slackChannel
}
attachment := slack.Attachment{
Text: msg.Text,
Pretext: msg.Title,
Footer: msg.Footer,
MarkdownIn: []string{"text", "pretext"},
Color: "#4599DF",
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
}
err := slack.PostWebhook(s.WebhookUrl, &slack.WebhookMessage{
Username: s.Username,
Channel: channel,
IconEmoji: ":kubernetes:",
Attachments: []slack.Attachment{attachment},
})
if err != nil {
klog.Errorf("Sending to Slack channel failed with %v", err)
return err
}
klog.Infof("Sent: [%s] to Slack.\n\n", strings.Replace(msg.Title, "\n", " ", -1))
return nil
}