...

Source file src/k8s.io/client-go/tools/events/event_recorder.go

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

     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 events
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	eventsv1 "k8s.io/api/events/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    28  	"k8s.io/apimachinery/pkg/watch"
    29  	"k8s.io/client-go/tools/record/util"
    30  	"k8s.io/client-go/tools/reference"
    31  	"k8s.io/klog/v2"
    32  	"k8s.io/utils/clock"
    33  )
    34  
    35  type recorderImpl struct {
    36  	scheme              *runtime.Scheme
    37  	reportingController string
    38  	reportingInstance   string
    39  	*watch.Broadcaster
    40  	clock clock.Clock
    41  }
    42  
    43  var _ EventRecorder = &recorderImpl{}
    44  
    45  func (recorder *recorderImpl) Eventf(regarding runtime.Object, related runtime.Object, eventtype, reason, action, note string, args ...interface{}) {
    46  	recorder.eventf(klog.Background(), regarding, related, eventtype, reason, action, note, args...)
    47  }
    48  
    49  type recorderImplLogger struct {
    50  	*recorderImpl
    51  	logger klog.Logger
    52  }
    53  
    54  var _ EventRecorderLogger = &recorderImplLogger{}
    55  
    56  func (recorder *recorderImplLogger) Eventf(regarding runtime.Object, related runtime.Object, eventtype, reason, action, note string, args ...interface{}) {
    57  	recorder.eventf(recorder.logger, regarding, related, eventtype, reason, action, note, args...)
    58  }
    59  
    60  func (recorder *recorderImplLogger) WithLogger(logger klog.Logger) EventRecorderLogger {
    61  	return &recorderImplLogger{recorderImpl: recorder.recorderImpl, logger: logger}
    62  }
    63  
    64  func (recorder *recorderImpl) eventf(logger klog.Logger, regarding runtime.Object, related runtime.Object, eventtype, reason, action, note string, args ...interface{}) {
    65  	timestamp := metav1.MicroTime{Time: time.Now()}
    66  	message := fmt.Sprintf(note, args...)
    67  	refRegarding, err := reference.GetReference(recorder.scheme, regarding)
    68  	if err != nil {
    69  		logger.Error(err, "Could not construct reference, will not report event", "object", regarding, "eventType", eventtype, "reason", reason, "message", message)
    70  		return
    71  	}
    72  
    73  	var refRelated *v1.ObjectReference
    74  	if related != nil {
    75  		refRelated, err = reference.GetReference(recorder.scheme, related)
    76  		if err != nil {
    77  			logger.V(9).Info("Could not construct reference", "object", related, "err", err)
    78  		}
    79  	}
    80  	if !util.ValidateEventType(eventtype) {
    81  		logger.Error(nil, "Unsupported event type", "eventType", eventtype)
    82  		return
    83  	}
    84  	event := recorder.makeEvent(refRegarding, refRelated, timestamp, eventtype, reason, message, recorder.reportingController, recorder.reportingInstance, action)
    85  	go func() {
    86  		defer utilruntime.HandleCrash()
    87  		recorder.Action(watch.Added, event)
    88  	}()
    89  }
    90  
    91  func (recorder *recorderImpl) makeEvent(refRegarding *v1.ObjectReference, refRelated *v1.ObjectReference, timestamp metav1.MicroTime, eventtype, reason, message string, reportingController string, reportingInstance string, action string) *eventsv1.Event {
    92  	t := metav1.Time{Time: recorder.clock.Now()}
    93  	namespace := refRegarding.Namespace
    94  	if namespace == "" {
    95  		namespace = metav1.NamespaceDefault
    96  	}
    97  	return &eventsv1.Event{
    98  		ObjectMeta: metav1.ObjectMeta{
    99  			Name:      fmt.Sprintf("%v.%x", refRegarding.Name, t.UnixNano()),
   100  			Namespace: namespace,
   101  		},
   102  		EventTime:           timestamp,
   103  		Series:              nil,
   104  		ReportingController: reportingController,
   105  		ReportingInstance:   reportingInstance,
   106  		Action:              action,
   107  		Reason:              reason,
   108  		Regarding:           *refRegarding,
   109  		Related:             refRelated,
   110  		Note:                message,
   111  		Type:                eventtype,
   112  	}
   113  }
   114  

View as plain text