k8s.io/client-go@v0.22.2/tools/events/event_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 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  	"k8s.io/apimachinery/pkg/util/clock"
    28  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    29  	"k8s.io/apimachinery/pkg/watch"
    30  	"k8s.io/client-go/tools/record/util"
    31  	"k8s.io/client-go/tools/reference"
    32  	"k8s.io/klog/v2"
    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  func (recorder *recorderImpl) Eventf(regarding runtime.Object, related runtime.Object, eventtype, reason, action, note string, args ...interface{}) {
    44  	timestamp := metav1.MicroTime{time.Now()}
    45  	message := fmt.Sprintf(note, args...)
    46  	refRegarding, err := reference.GetReference(recorder.scheme, regarding)
    47  	if err != nil {
    48  		klog.Errorf("Could not construct reference to: '%#v' due to: '%v'. Will not report event: '%v' '%v' '%v'", regarding, err, eventtype, reason, message)
    49  		return
    50  	}
    51  	refRelated, err := reference.GetReference(recorder.scheme, related)
    52  	if err != nil {
    53  		klog.V(9).Infof("Could not construct reference to: '%#v' due to: '%v'.", related, err)
    54  	}
    55  	if !util.ValidateEventType(eventtype) {
    56  		klog.Errorf("Unsupported event type: '%v'", eventtype)
    57  		return
    58  	}
    59  	event := recorder.makeEvent(refRegarding, refRelated, timestamp, eventtype, reason, message, recorder.reportingController, recorder.reportingInstance, action)
    60  	go func() {
    61  		defer utilruntime.HandleCrash()
    62  		recorder.Action(watch.Added, event)
    63  	}()
    64  }
    65  
    66  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 {
    67  	t := metav1.Time{Time: recorder.clock.Now()}
    68  	namespace := refRegarding.Namespace
    69  	if namespace == "" {
    70  		namespace = metav1.NamespaceDefault
    71  	}
    72  	return &eventsv1.Event{
    73  		ObjectMeta: metav1.ObjectMeta{
    74  			Name:      fmt.Sprintf("%v.%x", refRegarding.Name, t.UnixNano()),
    75  			Namespace: namespace,
    76  		},
    77  		EventTime:           timestamp,
    78  		Series:              nil,
    79  		ReportingController: reportingController,
    80  		ReportingInstance:   reportingInstance,
    81  		Action:              action,
    82  		Reason:              reason,
    83  		Regarding:           *refRegarding,
    84  		Related:             refRelated,
    85  		Note:                message,
    86  		Type:                eventtype,
    87  	}
    88  }