-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
event_recorder_manager.go
82 lines (73 loc) · 2.67 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
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
package events
import (
"sort"
"strings"
"sync"
"github.com/argoproj/argo-workflows/v3/util/env"
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
}
// customEventAggregatorFuncWithAnnotations enhances the default `EventAggregatorByReasonFunc` by
// including annotation values as part of the event aggregation key.
func customEventAggregatorFuncWithAnnotations(event *apiv1.Event) (string, string) {
var joinedAnnotationsStr string
includeAnnotations := env.LookupEnvStringOr("EVENT_AGGREGATION_WITH_ANNOTATIONS", "false")
if annotations := event.GetAnnotations(); includeAnnotations == "true" && annotations != nil {
annotationVals := make([]string, 0, len(annotations))
for _, v := range annotations {
annotationVals = append(annotationVals, v)
}
sort.Strings(annotationVals)
joinedAnnotationsStr = strings.Join(annotationVals, "")
}
return strings.Join([]string{
event.Source.Component,
event.Source.Host,
event.InvolvedObject.Kind,
event.InvolvedObject.Namespace,
event.InvolvedObject.Name,
string(event.InvolvedObject.UID),
event.InvolvedObject.APIVersion,
event.Type,
event.Reason,
event.ReportingController,
event.ReportingInstance,
joinedAnnotationsStr,
},
""), event.Message
}
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, KeyFunc: customEventAggregatorFuncWithAnnotations}
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),
}
}