...
1
16
17 package record
18
19 import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/runtime"
23 "k8s.io/klog/v2"
24 )
25
26
27
28
29 type FakeRecorder struct {
30 Events chan string
31
32 IncludeObject bool
33 }
34
35 var _ EventRecorderLogger = &FakeRecorder{}
36
37 func objectString(object runtime.Object, includeObject bool) string {
38 if !includeObject {
39 return ""
40 }
41 return fmt.Sprintf(" involvedObject{kind=%s,apiVersion=%s}",
42 object.GetObjectKind().GroupVersionKind().Kind,
43 object.GetObjectKind().GroupVersionKind().GroupVersion(),
44 )
45 }
46
47 func annotationsString(annotations map[string]string) string {
48 if len(annotations) == 0 {
49 return ""
50 } else {
51 return " " + fmt.Sprint(annotations)
52 }
53 }
54
55 func (f *FakeRecorder) writeEvent(object runtime.Object, annotations map[string]string, eventtype, reason, messageFmt string, args ...interface{}) {
56 if f.Events != nil {
57 f.Events <- fmt.Sprintf(eventtype+" "+reason+" "+messageFmt, args...) +
58 objectString(object, f.IncludeObject) + annotationsString(annotations)
59 }
60 }
61
62 func (f *FakeRecorder) Event(object runtime.Object, eventtype, reason, message string) {
63 f.writeEvent(object, nil, eventtype, reason, "%s", message)
64 }
65
66 func (f *FakeRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) {
67 f.writeEvent(object, nil, eventtype, reason, messageFmt, args...)
68 }
69
70 func (f *FakeRecorder) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventtype, reason, messageFmt string, args ...interface{}) {
71 f.writeEvent(object, annotations, eventtype, reason, messageFmt, args...)
72 }
73
74 func (f *FakeRecorder) WithLogger(logger klog.Logger) EventRecorderLogger {
75 return f
76 }
77
78
79
80 func NewFakeRecorder(bufferSize int) *FakeRecorder {
81 return &FakeRecorder{
82 Events: make(chan string, bufferSize),
83 }
84 }
85
View as plain text