-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
event_recorder_manager.go
48 lines (41 loc) · 1.49 KB
/
event_recorder_manager.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
package events
import (
"sync"
log "github.com/sirupsen/logrus"
apiv1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
)
// by default, allow a source to send 10000 events about an object
const defaultSpamBurst = 10000
type EventRecorderManager interface {
Get(namespace string) record.EventRecorder
}
type eventRecorderManager struct {
kubernetes kubernetes.Interface
lock sync.Mutex
eventRecorders map[string]record.EventRecorder
}
func (m *eventRecorderManager) Get(namespace string) record.EventRecorder {
m.lock.Lock()
defer m.lock.Unlock()
eventRecorder, ok := m.eventRecorders[namespace]
if ok {
return eventRecorder
}
eventCorrelationOption := record.CorrelatorOptions{BurstSize: defaultSpamBurst}
eventBroadcaster := record.NewBroadcasterWithCorrelatorOptions(eventCorrelationOption)
eventBroadcaster.StartLogging(log.Debugf)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: m.kubernetes.CoreV1().Events(namespace)})
m.eventRecorders[namespace] = eventBroadcaster.NewRecorder(scheme.Scheme, apiv1.EventSource{Component: "workflow-controller"})
return m.eventRecorders[namespace]
}
func NewEventRecorderManager(kubernetes kubernetes.Interface) EventRecorderManager {
return &eventRecorderManager{
kubernetes: kubernetes,
lock: sync.Mutex{},
eventRecorders: make(map[string]record.EventRecorder),
}
}