-
Notifications
You must be signed in to change notification settings - Fork 183
/
events.go
214 lines (184 loc) · 6.38 KB
/
events.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
package events
import (
"strings"
"time"
"github.com/signalfx/golib/event"
"github.com/signalfx/signalfx-agent/internal/core/common/kubernetes"
"github.com/signalfx/signalfx-agent/internal/core/config"
"github.com/signalfx/signalfx-agent/internal/monitors"
"github.com/signalfx/signalfx-agent/internal/monitors/kubernetes/leadership"
"github.com/signalfx/signalfx-agent/internal/monitors/types"
"github.com/signalfx/signalfx-agent/internal/utils"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
const monitorType = "kubernetes-events"
// MONITOR(kubernetes-events): This monitor sends Kubernetes events as SignalFx
// events. Upon startup, it will send all of the events that K8s has that are
// still persisted and then send any new events that come in. The various
// agents perform leader election amongs themselves to decide which instance
// will send events, unless the `alwaysClusterReporter` config option is set to
// true.
//
// You can see the types of events happening in your cluster with `kubectl get
// events -o yaml --all-namespaces`.
var logger = log.WithFields(log.Fields{"monitorType": monitorType})
func init() {
monitors.Register(monitorType, func() interface{} { return &Monitor{} }, &Config{})
}
// EventInclusionSpec specifies a type of event to send
type EventInclusionSpec struct {
Reason string `yaml:"reason"`
InvolvedObjectKind string `yaml:"involvedObjectKind"`
}
// Config for the K8s event monitor
type Config struct {
config.MonitorConfig
// Configuration of the Kubernetes API client
KubernetesAPI *kubernetes.APIConfig `yaml:"kubernetesAPI" default:"{}"`
// A list of event types to send events for. Only events matching these
// items will be sent.
WhitelistedEvents []EventInclusionSpec `yaml:"whitelistedEvents"`
// If true, all events from Kubernetes will be sent. Please don't use this
// option unless you really want to act on all possible K8s events.
SendAllEvents bool `yaml:"sendAllEvents"`
// Whether to always send events from this agent instance or to do leader
// election to only send from one agent instance.
AlwaysClusterReporter bool `yaml:"alwaysClusterReporter"`
}
// Monitor for K8s Cluster Metrics. Also handles syncing certain properties
// about pods.
type Monitor struct {
Output types.Output
stopper chan struct{}
sendAllEvents bool
whitelistSet map[EventInclusionSpec]bool
}
// Configure the monitor and kick off event syncing
func (m *Monitor) Configure(conf *Config) error {
k8sClient, err := kubernetes.MakeClient(conf.KubernetesAPI)
if err != nil {
return err
}
m.sendAllEvents = conf.SendAllEvents
m.whitelistSet = make(map[EventInclusionSpec]bool, len(conf.WhitelistedEvents))
for i := range conf.WhitelistedEvents {
spec := conf.WhitelistedEvents[i]
spec.InvolvedObjectKind = strings.ToLower(spec.InvolvedObjectKind)
spec.Reason = strings.ToLower(spec.Reason)
m.whitelistSet[spec] = true
}
m.stopper = make(chan struct{})
return m.start(k8sClient, conf.AlwaysClusterReporter)
}
func (m *Monitor) start(k8sClient *k8s.Clientset, alwaysReport bool) error {
var syncStopper chan struct{}
runSync := func() {
syncStopper = make(chan struct{})
syncEvents(k8sClient, cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
ev := obj.(*v1.Event)
m.handleNewEvent(ev)
},
}, syncStopper)
}
var leaderCh <-chan bool
var unregister func()
if alwaysReport {
logger.Info("This instance will send K8s events")
runSync()
} else {
var err error
leaderCh, unregister, err = leadership.RequestLeaderNotification(k8sClient.CoreV1())
if err != nil {
return err
}
}
go func() {
for {
select {
case isLeader := <-leaderCh:
if isLeader {
logger.Info("This instance is now the leader and will send events")
runSync()
} else {
logger.Info("No longer leader")
close(syncStopper)
syncStopper = nil
}
case <-m.stopper:
logger.Info("Stopping k8s event syncing")
if unregister != nil {
unregister()
}
if syncStopper != nil {
close(syncStopper)
}
return
}
}
}()
return nil
}
func (m *Monitor) shouldSendEvent(ev *v1.Event) bool {
// Always ignore any events older than 1 minute so we don't cause an event
// flood upon agent restarts. This doesn't eliminate the possibility of
// duplicated events but should limit them to a fairly narrow time window.
if ev.LastTimestamp.Time.Before(time.Now().Add(-1 * time.Minute)) {
return false
}
if m.sendAllEvents {
return true
}
return m.whitelistSet[EventInclusionSpec{
Reason: strings.ToLower(ev.Reason),
InvolvedObjectKind: strings.ToLower(ev.InvolvedObject.Kind),
}]
}
func (m *Monitor) handleNewEvent(ev *v1.Event) {
if m.shouldSendEvent(ev) {
sfxEvent := k8sEventToSignalFxEvent(ev)
m.Output.SendEvent(sfxEvent)
}
}
func k8sEventToSignalFxEvent(ev *v1.Event) *event.Event {
dims := map[string]string{
"kubernetes_kind": ev.InvolvedObject.Kind,
"kubernetes_namespace": ev.InvolvedObject.Namespace,
"obj_field_path": ev.InvolvedObject.FieldPath,
}
// Reuse the existing pod dimensions that we send for metrics
if ev.InvolvedObject.Kind == "Pod" {
dims["kubernetes_pod_name"] = ev.InvolvedObject.Name
dims["kubernetes_pod_uid"] = string(ev.InvolvedObject.UID)
} else {
dims["kubernetes_name"] = ev.InvolvedObject.Name
dims["kubernetes_uid"] = string(ev.InvolvedObject.UID)
}
properties := utils.RemoveEmptyMapValues(map[string]string{
"message": ev.Message,
"source_component": ev.Source.Component,
"source_host": ev.Source.Host,
"kubernetes_event_type": ev.Type,
"kubernetes_resource_version": ev.InvolvedObject.ResourceVersion,
})
return event.NewWithProperties(
ev.Reason,
event.AGENT,
utils.RemoveEmptyMapValues(dims),
utils.StringMapToInterfaceMap(properties),
ev.LastTimestamp.Time)
}
func syncEvents(clientset *k8s.Clientset, handlers cache.ResourceEventHandlerFuncs, stopper chan struct{}) {
client := clientset.CoreV1().RESTClient()
watchList := cache.NewListWatchFromClient(client, "events", v1.NamespaceAll, fields.Everything())
_, controller := cache.NewInformer(watchList, &v1.Event{}, 0, handlers)
go controller.Run(stopper)
}
// Shutdown the monitor and stop any syncing
func (m *Monitor) Shutdown() {
close(m.stopper)
}