forked from wichert/k8s-sentry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.go
275 lines (238 loc) · 7.81 KB
/
application.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
/*
Copyright 2019 Wichert Akkerman
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 main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/getsentry/sentry-go"
lru "github.com/hashicorp/golang-lru"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
type terminationKey struct {
podUID types.UID
containerName string
}
type application struct {
clientset *kubernetes.Clientset
defaultEnvironment string
terminationsSeen *lru.Cache
namespaces []string
}
func (app *application) Run() (chan struct{}, error) {
terminationsSeen, err := lru.New(500)
if err != nil {
return nil, err
}
app.terminationsSeen = terminationsSeen
stop := make(chan struct{})
for _, namespace := range app.namespaces {
go app.monitorEvents(namespace, stop)
go app.monitorPods(namespace, stop)
}
return stop, nil
}
func (app application) monitorPods(namespace string, stop chan struct{}) {
watchList := cache.NewListWatchFromClient(
app.clientset.CoreV1().RESTClient(),
"pods",
namespace,
fields.Everything(),
)
_, controller := cache.NewInformer(
watchList,
&v1.Pod{},
time.Second*30,
cache.ResourceEventHandlerFuncs{
UpdateFunc: app.handlePodUpdate,
},
)
controller.Run(stop)
}
func (app application) monitorEvents(namespace string, stop chan struct{}) {
watchList := cache.NewListWatchFromClient(
app.clientset.CoreV1().RESTClient(),
"events",
namespace,
fields.Everything(),
)
_, controller := cache.NewInformer(
watchList,
&v1.Event{},
time.Second*30,
cache.ResourceEventHandlerFuncs{
AddFunc: app.handleEventAdd,
},
)
controller.Run(stop)
}
func (app *application) handlePodUpdate(oldObj, newObj interface{}) {
pod, ok := newObj.(*v1.Pod)
if !ok {
sentry.CaptureMessage("Unexpected pod type")
return
}
var sentryEvent *sentry.Event
if pod.Status.Phase == v1.PodFailed {
// All containers in the pod have terminated, and at least one container has
// terminated in a failure (exited with a non-zero exit code or was stopped by the system).
sentryEvent = sentry.NewEvent()
sentryEvent.Message = pod.Status.Message
sentryEvent.ServerName = pod.Spec.NodeName
sentryEvent.Tags["reason"] = pod.Status.Reason
} else {
// The Pod is still running. Check if one of its containers terminated with a non-zero exit code.
// If so report that as an error.
// Note that this will fail if multiple containers in the pod are terminating at the same time.
// Since that should be rare, and hopefully someone will investigate on any error anyway we
// ignore that situation (for now).
allContainers := append(pod.Status.InitContainerStatuses, pod.Status.ContainerStatuses...)
for _, status := range allContainers {
if status.LastTerminationState.Terminated != nil && status.LastTerminationState.Terminated.ExitCode != 0 && app.isNewTermination(pod, &status) {
// Note that we only care about terminations in the last half seconds. This prevents
// us from treating updates for other reasons after a container terminated as another
// occurance of the termination.
sentryEvent = sentry.NewEvent()
sentryEvent.Message = status.LastTerminationState.Terminated.Message
sentryEvent.ServerName = pod.Spec.NodeName
if sentryEvent.Message == "" {
// OOMKilled does not leave a message :(
sentryEvent.Message = status.LastTerminationState.Terminated.Reason
}
sentryEvent.Release = status.Image
sentryEvent.Tags["reason"] = status.LastTerminationState.Terminated.Reason
sentryEvent.Extra["exit-code"] = strconv.FormatInt(int64(status.LastTerminationState.Terminated.ExitCode), 10)
sentryEvent.Extra["restartCount"] = status.RestartCount
break
}
}
}
// There are many reasons a Pod can be updated. We are only interested in containers
// that terminated uncleanly
if sentryEvent != nil {
sentryEvent.Logger = "kubernetes"
sentryEvent.Level = sentry.LevelError
if app.defaultEnvironment != "" {
sentryEvent.Environment = app.defaultEnvironment
} else {
sentryEvent.Environment = pod.Namespace
}
sentryEvent.Fingerprint = append(
[]string{
sentryEvent.Tags["reason"],
sentryEvent.Message,
},
fingerprintFromMeta(&pod.ObjectMeta)...)
sentryEvent.Tags["namespace"] = pod.Namespace
if pod.ClusterName != "" {
sentryEvent.Tags["cluster"] = pod.ClusterName
}
sentryEvent.Tags["kind"] = pod.Kind
for k, v := range pod.ObjectMeta.Labels {
sentryEvent.Tags[k] = v
}
sentryEvent.Message = fmt.Sprintf("Pod/%s: %s", pod.Name, sentryEvent.Message)
sentry.CaptureEvent(sentryEvent)
}
}
func (app *application) isNewTermination(pod *v1.Pod, status *v1.ContainerStatus) bool {
finishedAt := status.LastTerminationState.Terminated.FinishedAt
age := metav1.Now().Sub(finishedAt.Time)
key := terminationKey{
podUID: pod.UID,
containerName: status.Name,
}
cachedTime, seen := app.terminationsSeen.Get(key)
app.terminationsSeen.Add(key, finishedAt)
// We skip old records. These happen if a container terminated before, and then the
// pod later gets updated for other reasons with the termination record still in place.
if age.Microseconds() > 5000 {
return false
}
prevTime, ok := cachedTime.(metav1.Time)
if !ok {
// If this happened we have bad data in the cache and we are best off to
// not do anything anymore
return false
}
return !seen || finishedAt.After(prevTime.Time)
}
func (app application) handleEventAdd(obj interface{}) {
evt, ok := obj.(*v1.Event)
if !ok {
sentry.CaptureMessage("Unexpected event type")
return
}
if skipEvent(evt) {
return
}
sentryEvent := sentry.NewEvent()
if app.defaultEnvironment != "" {
sentryEvent.Environment = app.defaultEnvironment
} else {
sentryEvent.Environment = evt.InvolvedObject.Namespace
}
sentryEvent.Logger = "kubernetes"
sentryEvent.Message = fmt.Sprintf("%s/%s: %s", evt.InvolvedObject.Kind, evt.InvolvedObject.Name, evt.Message)
sentryEvent.Level = getSentryLevel(evt)
sentryEvent.Timestamp = evt.ObjectMeta.CreationTimestamp.Time
sentryEvent.Fingerprint = []string{
evt.Source.Component,
evt.Type,
evt.Reason,
evt.Message,
}
sentryEvent.Tags["namespace"] = evt.InvolvedObject.Namespace
sentryEvent.Tags["component"] = evt.Source.Component
if evt.ClusterName != "" {
sentryEvent.Tags["cluster"] = evt.ClusterName
}
sentryEvent.Tags["reason"] = evt.Reason
sentryEvent.Tags["kind"] = evt.InvolvedObject.Kind
sentryEvent.Tags["type"] = evt.Type
if evt.Action != "" {
sentryEvent.Extra["action"] = evt.Action
}
sentryEvent.Extra["count"] = evt.Count
handler := NewEventHandler(&app, evt)
sentryEvent.Fingerprint = append(sentryEvent.Fingerprint, handler.Fingerprint()...)
for k, v := range handler.Tags() {
sentryEvent.Tags[k] = v
}
log.Printf("%s %s\n", evt.Type, sentryEvent.Message)
sentry.CaptureEvent(sentryEvent)
}
func skipEvent(evt *v1.Event) bool {
return evt.Type == v1.EventTypeNormal
}
func getSentryLevel(evt *v1.Event) sentry.Level {
switch evt.Type {
case v1.EventTypeWarning:
return sentry.LevelWarning
case "Error":
return sentry.LevelError
default:
fmt.Printf("Unexpected event type: %v\n", evt.Type)
return sentry.LevelInfo
}
}
func inCluster() bool {
return os.Getenv("KUBERNETES_SERVICE_HOST") != "" && os.Getenv("KUBERNETES_SERVICE_PORT") != ""
}