Skip to content

Commit 498973d

Browse files
committed
✨ Add util/record package from CAPA
Signed-off-by: Vince Prignano <[email protected]>
1 parent e111513 commit 498973d

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

util/record/recorder.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package record
18+
19+
import (
20+
"strings"
21+
"sync"
22+
23+
corev1 "k8s.io/api/core/v1"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
"k8s.io/client-go/tools/record"
26+
)
27+
28+
var (
29+
initOnce sync.Once
30+
defaultRecorder record.EventRecorder
31+
)
32+
33+
func init() {
34+
defaultRecorder = new(record.FakeRecorder)
35+
}
36+
37+
// InitFromRecorder initializes the global default recorder. It can only be called once.
38+
// Subsequent calls are considered noops.
39+
func InitFromRecorder(recorder record.EventRecorder) {
40+
initOnce.Do(func() {
41+
defaultRecorder = recorder
42+
})
43+
}
44+
45+
// Event constructs an event from the given information and puts it in the queue for sending.
46+
func Event(object runtime.Object, reason, message string) {
47+
defaultRecorder.Event(object, corev1.EventTypeNormal, strings.Title(reason), message)
48+
}
49+
50+
// Eventf is just like Event, but with Sprintf for the message field.
51+
func Eventf(object runtime.Object, reason, message string, args ...interface{}) {
52+
defaultRecorder.Eventf(object, corev1.EventTypeNormal, strings.Title(reason), message, args...)
53+
}
54+
55+
// Event constructs a warning event from the given information and puts it in the queue for sending.
56+
func Warn(object runtime.Object, reason, message string) {
57+
defaultRecorder.Event(object, corev1.EventTypeWarning, strings.Title(reason), message)
58+
}
59+
60+
// Eventf is just like Event, but with Sprintf for the message field.
61+
func Warnf(object runtime.Object, reason, message string, args ...interface{}) {
62+
defaultRecorder.Eventf(object, corev1.EventTypeWarning, strings.Title(reason), message, args...)
63+
}

0 commit comments

Comments
 (0)