...

Source file src/k8s.io/client-go/tools/record/fake.go

Documentation: k8s.io/client-go/tools/record

     1  /*
     2  Copyright 2015 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  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/klog/v2"
    24  )
    25  
    26  // FakeRecorder is used as a fake during tests. It is thread safe. It is usable
    27  // when created manually and not by NewFakeRecorder, however all events may be
    28  // thrown away in this case.
    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  // NewFakeRecorder creates new fake event recorder with event channel with
    79  // buffer of given size.
    80  func NewFakeRecorder(bufferSize int) *FakeRecorder {
    81  	return &FakeRecorder{
    82  		Events: make(chan string, bufferSize),
    83  	}
    84  }
    85  

View as plain text