/
recorder_in_memory.go
86 lines (70 loc) · 2.21 KB
/
recorder_in_memory.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
package events
import (
"context"
"fmt"
"sync"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)
type inMemoryEventRecorder struct {
events []*corev1.Event
source string
ctx context.Context
sync.Mutex
}
// inMemoryDummyObjectReference is used for fake events.
var inMemoryDummyObjectReference = corev1.ObjectReference{
Kind: "Pod",
Namespace: "dummy",
Name: "dummy",
APIVersion: "v1",
}
type InMemoryRecorder interface {
Events() []*corev1.Event
Recorder
}
// NewInMemoryRecorder provides event recorder that stores all events recorded in memory and allow to replay them using the Events() method.
// This recorder should be only used in unit tests.
func NewInMemoryRecorder(sourceComponent string) InMemoryRecorder {
return &inMemoryEventRecorder{events: []*corev1.Event{}, source: sourceComponent}
}
func (r *inMemoryEventRecorder) ComponentName() string {
return r.source
}
func (r *inMemoryEventRecorder) Shutdown() {}
func (r *inMemoryEventRecorder) ForComponent(component string) Recorder {
r.Lock()
defer r.Unlock()
r.source = component
return r
}
func (r *inMemoryEventRecorder) WithContext(ctx context.Context) Recorder {
r.ctx = ctx
return r
}
func (r *inMemoryEventRecorder) WithComponentSuffix(suffix string) Recorder {
return r.ForComponent(fmt.Sprintf("%s-%s", r.ComponentName(), suffix))
}
// Events returns list of recorded events
func (r *inMemoryEventRecorder) Events() []*corev1.Event {
return r.events
}
func (r *inMemoryEventRecorder) Event(reason, message string) {
r.Lock()
defer r.Unlock()
event := makeEvent(&inMemoryDummyObjectReference, r.source, corev1.EventTypeNormal, reason, message)
r.events = append(r.events, event)
}
func (r *inMemoryEventRecorder) Eventf(reason, messageFmt string, args ...interface{}) {
r.Event(reason, fmt.Sprintf(messageFmt, args...))
}
func (r *inMemoryEventRecorder) Warning(reason, message string) {
r.Lock()
defer r.Unlock()
event := makeEvent(&inMemoryDummyObjectReference, r.source, corev1.EventTypeWarning, reason, message)
klog.Info(event.String())
r.events = append(r.events, event)
}
func (r *inMemoryEventRecorder) Warningf(reason, messageFmt string, args ...interface{}) {
r.Warning(reason, fmt.Sprintf(messageFmt, args...))
}