sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/record/recorder.go (about) 1 /* 2 Copyright 2018 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 "sync" 21 22 "golang.org/x/text/cases" 23 "golang.org/x/text/language" 24 corev1 "k8s.io/api/core/v1" 25 runtime "k8s.io/apimachinery/pkg/runtime" 26 cgrecord "k8s.io/client-go/tools/record" 27 ) 28 29 var ( 30 initOnce sync.Once 31 defaultRecorder cgrecord.EventRecorder 32 ) 33 34 func init() { 35 defaultRecorder = new(cgrecord.FakeRecorder) 36 } 37 38 // InitFromRecorder initializes the global default recorder. It can only be called once. 39 // Subsequent calls are considered noops. 40 func InitFromRecorder(recorder cgrecord.EventRecorder) { 41 initOnce.Do(func() { 42 defaultRecorder = recorder 43 }) 44 } 45 46 // Event constructs an event from the given information and puts it in the queue for sending. 47 func Event(object runtime.Object, reason, message string) { 48 defaultRecorder.Event(object, corev1.EventTypeNormal, title(reason), message) 49 } 50 51 // Eventf is just like Event, but with Sprintf for the message field. 52 func Eventf(object runtime.Object, reason, message string, args ...interface{}) { 53 defaultRecorder.Eventf(object, corev1.EventTypeNormal, title(reason), message, args...) 54 } 55 56 // Warn constructs a warning event from the given information and puts it in the queue for sending. 57 func Warn(object runtime.Object, reason, message string) { 58 defaultRecorder.Event(object, corev1.EventTypeWarning, title(reason), message) 59 } 60 61 // Warnf is just like Event, but with Sprintf for the message field. 62 func Warnf(object runtime.Object, reason, message string, args ...interface{}) { 63 defaultRecorder.Eventf(object, corev1.EventTypeWarning, title(reason), message, args...) 64 } 65 66 // title returns a copy of the string s with all Unicode letters that begin words 67 // mapped to their Unicode title case. 68 func title(source string) string { 69 return cases.Title(language.Und, cases.NoLower).String(source) 70 }