forked from airwallex/k8s-pod-restart-info-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglechat.go
153 lines (128 loc) · 3.3 KB
/
googlechat.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"
"k8s.io/klog/v2"
)
type GoogleChat struct {
WebhookUrl string
ClusterName string
MuteSeconds int
History map[string]time.Time
}
type GoogleChatMessage struct {
Text string `json:"text"`
}
func NewGoogleChat() GoogleChat {
var webhookUrl, clusterName string
if webhookUrl = os.Getenv("GOOGLECHAT_WEBHOOK_URL"); webhookUrl == "" {
klog.Exit("Environment variable GOOGLECHAT_WEBHOOK_URL is not set")
}
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("Clustername: %s, muteseconds: %d\n", clusterName, muteSeconds)
return GoogleChat{
WebhookUrl: webhookUrl,
ClusterName: clusterName,
MuteSeconds: muteSeconds,
History: make(map[string]time.Time),
}
}
func (g GoogleChat) sendHTTPPost(data []byte) error {
// Send the HTTP POST request
resp, err := http.Post(g.WebhookUrl, "application/json", bytes.NewBuffer(data))
if err != nil {
klog.Infof("Error sending HTTP POST request: %v", err)
return err
}
defer resp.Body.Close()
// Check the response status code
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf("unexpected status code: %d", resp.StatusCode)
klog.Infof("Unexpected status code: %d", resp.StatusCode)
return err
}
return nil
}
func (g GoogleChat) sendToRoom(msg GoogleChatMessage) error {
// Marshal the message into JSON
data, err := json.Marshal(msg)
if err != nil {
return err
}
// Send the HTTP POST request
err = g.sendHTTPPost(data)
if err != nil {
return err
}
klog.Infof("Message sent successfully to Google Chat room")
return nil
}
func (g GoogleChat) sendToRoomPodStatus(msg GoogleChatMessage) error {
// Marshal the message into JSON
data, err := json.Marshal(msg)
if err != nil {
return err
}
// Send the HTTP POST request
err = g.sendHTTPPost(data)
if err != nil {
return err
}
klog.Infof("Message PodStatus sent successfully to Google Chat room")
return nil
}
func (g GoogleChat) sendToRoomPodEvent(msg GoogleChatMessage) error {
// Marshal the message into JSON
data, err := json.Marshal(msg)
if err != nil {
return err
}
// Send the HTTP POST request
err = g.sendHTTPPost(data)
if err != nil {
return err
}
klog.Infof("Message PodEvent sent successfully to Google Chat room")
return nil
}
func (g GoogleChat) sendToRoomNodeEvents(msg GoogleChatMessage) error {
// Marshal the message into JSON
data, err := json.Marshal(msg)
if err != nil {
return err
}
// Send the HTTP POST request
err = g.sendHTTPPost(data)
if err != nil {
return err
}
klog.Infof("Message NodeEvents sent successfully to Google Chat room")
return nil
}
func (g GoogleChat) sendToRoomContainerLogs(msg GoogleChatMessage) error {
// Marshal the message into JSON
data, err := json.Marshal(msg)
if err != nil {
return err
}
// Send the HTTP POST request
err = g.sendHTTPPost(data)
if err != nil {
return err
}
klog.Infof("Message ContainerLogs sent successfully to Google Chat room")
return nil
}