This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
/
pagerdutyeventer.go
376 lines (332 loc) · 12 KB
/
pagerdutyeventer.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
Copyright 2022 The KubeDiag Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pagerdutyeventer
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"time"
"github.com/go-logr/logr"
diagnosisv1 "github.com/kubediag/kubediag/api/v1"
"github.com/kubediag/kubediag/pkg/util"
"github.com/prometheus/client_golang/prometheus"
kafkago "github.com/segmentio/kafka-go"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
const (
offset64 uint64 = 14695981039346656037
prime64 uint64 = 1099511628211
separatorByte byte = 255
)
var (
pagerdutyEventReceivedCount = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "pagerduty_event_received_count",
Help: "Counter of pagerduty event received",
},
)
pagerdutyEventProcessSuccessCount = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "pagerduty_event_process_success_count",
Help: "Counter of successful pagerduty event process",
},
)
pagerdutyEventProcessErrorCount = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "pagerduty_event_process_error_count",
Help: "Counter of erroneous pagerduty event process",
},
)
)
// PagerDutyEventer can handle valid pagerduty events.
type PagerDutyEventer interface {
// Handler handles http requests.
Handler(http.ResponseWriter, *http.Request)
}
// pagerdutyEventer manages pagerduty event received by kubediag.
type pagerdutyEventer struct {
// Context carries values across API boundaries.
context.Context
// Logger represents the ability to log messages.
logr.Logger
// client knows how to perform CRUD operations on Kubernetes objects.
client client.Client
// cache knows how to load Kubernetes objects.
cache cache.Cache
// sinkEventToKafka enables the pagerduty handler to write message to kafka cluster.
sinkEventToKafka bool
// kafkaAddress is the addresses used to connect to the kafka cluster.
kafkaAddress string
// sinkEventToWebhookReceiver enables the pagerduty handler to write message to a webhook receiver.
sinkEventToWebhookReceiver bool
// webhookAddress is the addresses used to connect to the webhook receiver.
// It is valid only if sinkEventToWebhookReceiver is true.
webhookAddress string
// pagerdutyEventerEnabled indicates whether pagerdutyEventer is enabled.
pagerdutyEventerEnabled bool
}
// NewPagerDutyEventer creates a new PagerDutyEventer.
func NewPagerDutyEventer(
ctx context.Context,
logger logr.Logger,
cli client.Client,
cache cache.Cache,
sinkEventToKafka bool,
kafkaAddress string,
sinkEventToWebhookReceiver bool,
webhookAddress string,
pagerdutyEventerEnabled bool,
) PagerDutyEventer {
metrics.Registry.MustRegister(
pagerdutyEventReceivedCount,
)
return &pagerdutyEventer{
Context: ctx,
Logger: logger,
client: cli,
cache: cache,
sinkEventToKafka: sinkEventToKafka,
kafkaAddress: kafkaAddress,
sinkEventToWebhookReceiver: sinkEventToWebhookReceiver,
webhookAddress: webhookAddress,
pagerdutyEventerEnabled: pagerdutyEventerEnabled,
}
}
// Handler handles http requests for pagerduty events.
func (pe *pagerdutyEventer) Handler(w http.ResponseWriter, r *http.Request) {
if !pe.pagerdutyEventerEnabled {
http.Error(w, "pagerdutyEventer is not enabled", http.StatusUnprocessableEntity)
return
}
switch r.Method {
case "POST":
pagerdutyEventReceivedCount.Inc()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
pagerdutyEventProcessErrorCount.Inc()
pe.Error(err, "unable to read request body")
http.Error(w, fmt.Sprintf("unable to read request body: %v", err), http.StatusBadRequest)
return
}
defer r.Body.Close()
// Unmarshal request body into pagerduty message.
var pagerDutyMessage PagerDutyMessage
err = json.Unmarshal(body, &pagerDutyMessage)
if err != nil {
pagerdutyEventProcessErrorCount.Inc()
pe.Error(err, "failed to unmarshal request body")
http.Error(w, fmt.Sprintf("failed to unmarshal request body: %v", err), http.StatusInternalServerError)
return
}
// Return 400 if the payload is nil.
if pagerDutyMessage.Payload == nil {
pagerdutyEventProcessErrorCount.Inc()
pe.Error(fmt.Errorf("nil payload"), "invalid pagerduty message payload.", "body", r.Body)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
pagerdutyEvent := new(bytes.Buffer)
err = json.NewEncoder(pagerdutyEvent).Encode(pagerDutyMessage.Payload)
if err != nil {
pagerdutyEventProcessErrorCount.Inc()
pe.Error(err, "unable to encode pagerduty event.", "payload", pagerDutyMessage.Payload)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
// Store pagerduty event in the etcd of kubernetes.
name := fmt.Sprintf("%s.%s.%d", strings.ToLower(pagerDutyMessage.Payload.Group), strings.ToLower(pagerDutyMessage.Payload.Class), CommonEventToSignature(*pagerDutyMessage.Payload))
namespace := util.DefautlNamespace
now := metav1.Now()
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: name,
}
labels := make(map[string]string)
labels["source"] = pagerDutyMessage.Payload.Source
labels["severity"] = pagerDutyMessage.Payload.Severity
labels["class"] = pagerDutyMessage.Payload.Class
labels["component"] = pagerDutyMessage.Payload.Component
labels["group"] = pagerDutyMessage.Payload.Group
labels["resolved"] = "false"
labels["diagnosed"] = "false"
var commonEvent diagnosisv1.CommonEvent
if err := pe.client.Get(pe, namespacedName, &commonEvent); err != nil {
if apierrors.IsNotFound(err) {
commonEvent = diagnosisv1.CommonEvent{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: diagnosisv1.CommonEventSpec{
Summary: pagerDutyMessage.Payload.Summary,
Source: pagerDutyMessage.Payload.Source,
Severity: pagerDutyMessage.Payload.Severity,
Timestamp: pagerDutyMessage.Payload.Timestamp,
Class: pagerDutyMessage.Payload.Class,
Component: pagerDutyMessage.Payload.Component,
Group: pagerDutyMessage.Payload.Group,
CustomDetails: pagerDutyMessage.Payload.CustomDetails,
},
}
err = pe.client.Create(pe, &commonEvent)
if err != nil {
pagerdutyEventProcessErrorCount.Inc()
pe.Error(err, "unable to create Event")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
} else {
pagerdutyEventProcessErrorCount.Inc()
pe.Error(err, "unable to fetch Event")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
if commonEvent.Status.LastUpdateTime == nil || commonEvent.Status.LastUpdateTime.Before(&now) {
commonEvent.Status.Count += 1
commonEvent.Status.LastUpdateTime = &now
if err := pe.client.Status().Update(pe, &commonEvent); err != nil {
pe.Error(err, "unable to update Event")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
// Write the pagerduty message to webhook server.
if pe.sinkEventToWebhookReceiver {
err = pe.writePagerDutyMessageToWebhookReceiver(commonEvent)
if err != nil {
pagerdutyEventProcessErrorCount.Inc()
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
pe.Info("sending pagerduty message to webhook receiver successfully", "source", commonEvent.Spec.Source, "severity", commonEvent.Spec.Severity, "class", commonEvent.Spec.Class, "group", commonEvent.Spec.Group)
}
// Write the pagerduty message to kafka.
if pe.sinkEventToKafka {
topic := pagerDutyMessage.Payload.Group
partition := 0
err = pe.writePagerDutyMessageToKafka(topic, partition, pagerdutyEvent)
if err != nil {
pagerdutyEventProcessErrorCount.Inc()
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
pe.Info("sending pagerduty message to kafka successfully", "payload", pagerDutyMessage.Payload)
}
// Increment counter of successful pagerduty event process.
pagerdutyEventProcessSuccessCount.Inc()
w.Write([]byte("OK"))
default:
http.Error(w, fmt.Sprintf("method %s is not supported", r.Method), http.StatusMethodNotAllowed)
}
}
// CommonEventToSignature returns a signature for a given common event.
func CommonEventToSignature(payload PagerDutyPayload) uint64 {
sum := hashNew()
sum = hashAdd(sum, payload.Summary)
sum = hashAddByte(sum, separatorByte)
sum = hashAdd(sum, payload.Source)
sum = hashAddByte(sum, separatorByte)
sum = hashAdd(sum, payload.Severity)
sum = hashAddByte(sum, separatorByte)
sum = hashAdd(sum, payload.Timestamp)
sum = hashAddByte(sum, separatorByte)
sum = hashAdd(sum, payload.Class)
sum = hashAddByte(sum, separatorByte)
sum = hashAdd(sum, payload.Component)
sum = hashAddByte(sum, separatorByte)
sum = hashAdd(sum, payload.Group)
sum = hashAddByte(sum, separatorByte)
customDetailNames := make([]string, 0, len(payload.CustomDetails))
for customDetailName := range payload.CustomDetails {
customDetailNames = append(customDetailNames, customDetailName)
}
sort.Strings(customDetailNames)
for _, customDetailName := range customDetailNames {
sum = hashAdd(sum, customDetailName)
sum = hashAddByte(sum, separatorByte)
sum = hashAdd(sum, payload.CustomDetails[customDetailName])
sum = hashAddByte(sum, separatorByte)
}
return sum
}
// writePagerDutyMessageToWebhookReceiver writes a pagerduty message to kafka.
func (pe *pagerdutyEventer) writePagerDutyMessageToWebhookReceiver(commonEvent diagnosisv1.CommonEvent) error {
body, err := json.Marshal(commonEvent.Spec)
if err != nil {
pe.Error(err, "failed to marshal common event")
return err
}
resp, err := http.Post(pe.webhookAddress, "application/json", bytes.NewBuffer(body))
if err != nil {
pe.Error(err, "failed to post common event to webhook receiver")
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("failed to send common event to webhook receiver: %v", resp.Status)
pe.Error(err, "http response with erroneous status", "status", resp.Status)
return err
}
return nil
}
// writePagerDutyMessageToKafka writes a pagerduty message to kafka.
func (pe *pagerdutyEventer) writePagerDutyMessageToKafka(topic string, partition int, pagerdutyEvent *bytes.Buffer) error {
conn, err := kafkago.DialLeader(context.Background(), "tcp", pe.kafkaAddress, topic, partition)
if err != nil {
pe.Error(err, "failed to connect kafka.", "address", pe.kafkaAddress, "topic", topic)
return err
}
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
_, err = conn.WriteMessages(
kafkago.Message{Value: pagerdutyEvent.Bytes()},
)
if err != nil {
pe.Error(err, "failed to write kafka messages.", "message", pagerdutyEvent.Bytes())
return err
}
if err := conn.Close(); err != nil {
pe.Error(err, "failed to close kafka writer.")
return err
}
return nil
}
// hashNew initializes a new fnv64a hash value.
func hashNew() uint64 {
return offset64
}
// hashAdd adds a string to a fnv64a hash value, returning the updated hash.
func hashAdd(h uint64, s string) uint64 {
for i := 0; i < len(s); i++ {
h ^= uint64(s[i])
h *= prime64
}
return h
}
// hashAddByte adds a byte to a fnv64a hash value, returning the updated hash.
func hashAddByte(h uint64, b byte) uint64 {
h ^= uint64(b)
h *= prime64
return h
}