Skip to content

Commit

Permalink
Merge pull request kubernetes#105603 from shawnhanx/eventstructuredlo…
Browse files Browse the repository at this point in the history
…gging

Support SetStructuredLogging with the new events library
  • Loading branch information
k8s-ci-robot authored Oct 11, 2021
2 parents 1ae4af4 + 230cb00 commit 668c666
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions staging/src/k8s.io/client-go/tools/events/event_broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ func getKey(event *eventsv1.Event) eventKey {
return key
}

// StartStructuredLogging starts sending events received from this EventBroadcaster to the structured logging function.
// The return value can be ignored or used to stop recording, if desired.
func (e *eventBroadcasterImpl) StartStructuredLogging(verbosity klog.Level) func() {
return e.StartEventWatcher(
func(obj runtime.Object) {
event, ok := obj.(*eventsv1.Event)
if !ok {
klog.Errorf("unexpected type, expected eventsv1.Event")
return
}
klog.V(verbosity).InfoS("Event occurred", "object", klog.KRef(event.Regarding.Namespace, event.Regarding.Name), "kind", event.Regarding.Kind, "apiVersion", event.Regarding.APIVersion, "type", event.Type, "reason", event.Reason, "action", event.Action, "note", event.Note)
})
}

// StartEventWatcher starts sending events received from this EventBroadcaster to the given event handler function.
// The return value is used to stop recording
func (e *eventBroadcasterImpl) StartEventWatcher(eventHandler func(event runtime.Object)) func() {
Expand Down
5 changes: 5 additions & 0 deletions staging/src/k8s.io/client-go/tools/events/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
eventsv1 "k8s.io/api/events/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
)

// EventRecorder knows how to record events on behalf of an EventSource.
Expand Down Expand Up @@ -56,6 +57,10 @@ type EventBroadcaster interface {
// TODO: figure out if this can be removed.
StartEventWatcher(eventHandler func(event runtime.Object)) func()

// StartStructuredLogging starts sending events received from this EventBroadcaster to the structured
// logging function. The return value can be ignored or used to stop recording, if desired.
StartStructuredLogging(verbosity klog.Level) func()

// Shutdown shuts down the broadcaster
Shutdown()
}
Expand Down

0 comments on commit 668c666

Please sign in to comment.