sigs.k8s.io/cluster-api@v1.7.1/util/record/recorder.go (about)

     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 implements recording functionality.
    18  package record
    19  
    20  import (
    21  	"sync"
    22  
    23  	"golang.org/x/text/cases"
    24  	"golang.org/x/text/language"
    25  	corev1 "k8s.io/api/core/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/client-go/tools/record"
    28  )
    29  
    30  var (
    31  	initOnce        sync.Once
    32  	defaultRecorder record.EventRecorder
    33  )
    34  
    35  func init() {
    36  	defaultRecorder = new(record.FakeRecorder)
    37  }
    38  
    39  // InitFromRecorder initializes the global default recorder. It can only be called once.
    40  // Subsequent calls are considered noops.
    41  func InitFromRecorder(recorder record.EventRecorder) {
    42  	initOnce.Do(func() {
    43  		defaultRecorder = recorder
    44  	})
    45  }
    46  
    47  // Event constructs an event from the given information and puts it in the queue for sending.
    48  func Event(object runtime.Object, reason, message string) {
    49  	defaultRecorder.Event(object, corev1.EventTypeNormal, cases.Title(language.Und, cases.NoLower).String(reason), message)
    50  }
    51  
    52  // Eventf is just like Event, but with Sprintf for the message field.
    53  func Eventf(object runtime.Object, reason, message string, args ...interface{}) {
    54  	defaultRecorder.Eventf(object, corev1.EventTypeNormal, cases.Title(language.Und, cases.NoLower).String(reason), message, args...)
    55  }
    56  
    57  // Warn constructs a warning event from the given information and puts it in the queue for sending.
    58  func Warn(object runtime.Object, reason, message string) {
    59  	defaultRecorder.Event(object, corev1.EventTypeWarning, cases.Title(language.Und, cases.NoLower).String(reason), message)
    60  }
    61  
    62  // Warnf is just like Warn, but with Sprintf for the message field.
    63  func Warnf(object runtime.Object, reason, message string, args ...interface{}) {
    64  	defaultRecorder.Eventf(object, corev1.EventTypeWarning, cases.Title(language.Und, cases.NoLower).String(reason), message, args...)
    65  }