Skip to content

Commit 4f211d5

Browse files
committed
client-go: finish conversion to contextual logging
This changes the log output of kubectl, which affects some test cases which check for GET results in that log output.
1 parent 8b9900b commit 4f211d5

File tree

11 files changed

+38
-32
lines changed

11 files changed

+38
-32
lines changed

hack/golangci-hints.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ linters-settings: # please keep this alphabetized
148148
# "contextual" implies "structured".
149149
contextual k8s.io/api/.*
150150
contextual k8s.io/apimachinery/.*
151-
contextual k8s.io/client-go/metadata/.*
152-
contextual k8s.io/client-go/rest/.*
153-
contextual k8s.io/client-go/tools/cache/.*
154-
contextual k8s.io/client-go/tools/events/.*
155-
contextual k8s.io/client-go/tools/record/.*
151+
contextual k8s.io/client-go/.*
156152
contextual k8s.io/component-helpers/.*
157153
contextual k8s.io/cri-api/.*
158154
contextual k8s.io/cri-client/.*

hack/golangci.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,7 @@ linters-settings: # please keep this alphabetized
214214
# "contextual" implies "structured".
215215
contextual k8s.io/api/.*
216216
contextual k8s.io/apimachinery/.*
217-
contextual k8s.io/client-go/metadata/.*
218-
contextual k8s.io/client-go/rest/.*
219-
contextual k8s.io/client-go/tools/cache/.*
220-
contextual k8s.io/client-go/tools/events/.*
221-
contextual k8s.io/client-go/tools/record/.*
217+
contextual k8s.io/client-go/.*
222218
contextual k8s.io/component-helpers/.*
223219
contextual k8s.io/cri-api/.*
224220
contextual k8s.io/cri-client/.*

hack/logcheck.conf

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ structured k8s.io/apiserver/pkg/server/options/encryptionconfig/.*
2626
# "contextual" implies "structured".
2727
contextual k8s.io/api/.*
2828
contextual k8s.io/apimachinery/.*
29-
contextual k8s.io/client-go/metadata/.*
30-
contextual k8s.io/client-go/rest/.*
31-
contextual k8s.io/client-go/tools/cache/.*
32-
contextual k8s.io/client-go/tools/events/.*
33-
contextual k8s.io/client-go/tools/record/.*
29+
contextual k8s.io/client-go/.*
3430
contextual k8s.io/component-helpers/.*
3531
contextual k8s.io/cri-api/.*
3632
contextual k8s.io/cri-client/.*

staging/src/k8s.io/client-go/dynamic/simple.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ func (c *dynamicResourceClient) Get(ctx context.Context, name string, opts metav
252252

253253
func (c *dynamicResourceClient) List(ctx context.Context, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) {
254254
if watchListOptions, hasWatchListOptionsPrepared, watchListOptionsErr := watchlist.PrepareWatchListOptionsFromListOptions(opts); watchListOptionsErr != nil {
255-
klog.Warningf("Failed preparing watchlist options for %v, falling back to the standard LIST semantics, err = %v", c.resource, watchListOptionsErr)
255+
klog.FromContext(ctx).Info("Warning: failed preparing watchlist options, falling back to the standard LIST semantics", "resource", c.resource, "err", watchListOptionsErr)
256256
} else if hasWatchListOptionsPrepared {
257257
result, err := c.watchList(ctx, watchListOptions)
258258
if err == nil {
259259
consistencydetector.CheckWatchListFromCacheDataConsistencyIfRequested(ctx, fmt.Sprintf("watchlist request for %v", c.resource), c.list, opts, result)
260260
return result, nil
261261
}
262-
klog.Warningf("The watchlist request for %v ended with an error, falling back to the standard LIST semantics, err = %v", c.resource, err)
262+
klog.FromContext(ctx).Info("Warning: the watchlist request ended with an error, falling back to the standard LIST semantics", "resource", c.resource, "err", err)
263263
}
264264
result, err := c.list(ctx, opts)
265265
if err == nil {

staging/src/k8s.io/client-go/gentype/type.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ func (c *Client[T]) Get(ctx context.Context, name string, options metav1.GetOpti
173173
// List takes label and field selectors, and returns the list of resources that match those selectors.
174174
func (l *alsoLister[T, L]) List(ctx context.Context, opts metav1.ListOptions) (L, error) {
175175
if watchListOptions, hasWatchListOptionsPrepared, watchListOptionsErr := watchlist.PrepareWatchListOptionsFromListOptions(opts); watchListOptionsErr != nil {
176-
klog.Warningf("Failed preparing watchlist options for $.type|resource$, falling back to the standard LIST semantics, err = %v", watchListOptionsErr)
176+
klog.FromContext(ctx).Info("Warning: failed preparing watchlist options, falling back to the standard LIST semantics", "resource", l.client.resource, "err", watchListOptionsErr)
177177
} else if hasWatchListOptionsPrepared {
178178
result, err := l.watchList(ctx, watchListOptions)
179179
if err == nil {
180180
consistencydetector.CheckWatchListFromCacheDataConsistencyIfRequested(ctx, "watchlist request for "+l.client.resource, l.list, opts, result)
181181
return result, nil
182182
}
183-
klog.Warningf("The watchlist request for %s ended with an error, falling back to the standard LIST semantics, err = %v", l.client.resource, err)
183+
klog.FromContext(ctx).Info("Warning: the watchlist request ended with an error, falling back to the standard LIST semantics", "resource", l.client.resource, "err", err)
184184
}
185185
result, err := l.list(ctx, opts)
186186
if err == nil {

staging/src/k8s.io/client-go/tools/cache/reflector_data_consistency_detector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import (
3333
//
3434
// Note that this function will panic when data inconsistency is detected.
3535
// This is intentional because we want to catch it in the CI.
36+
//
37+
// The identity string can be left empty if the logger in the context already
38+
// identifies what is being watched.
3639
func checkWatchListDataConsistencyIfRequested[T runtime.Object, U any](ctx context.Context, identity string, lastSyncedResourceVersion string, listFn consistencydetector.ListFunc[T], retrieveItemsFn consistencydetector.RetrieveItemsFunc[U]) {
3740
if !consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
3841
return

staging/src/k8s.io/client-go/tools/events/event_broadcaster.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func (e *eventBroadcasterImpl) StartStructuredLogging(verbosity klog.Level) func
328328
// To adjust verbosity, use the logger's V method (i.e. pass `logger.V(3)` instead of `logger`).
329329
// The returned function can be ignored or used to stop recording, if desired.
330330
func (e *eventBroadcasterImpl) StartLogging(logger klog.Logger) (func(), error) {
331-
return e.StartEventWatcher(
331+
return e.startEventWatcher(logger,
332332
func(obj runtime.Object) {
333333
event, ok := obj.(*eventsv1.Event)
334334
if !ok {
@@ -342,12 +342,16 @@ func (e *eventBroadcasterImpl) StartLogging(logger klog.Logger) (func(), error)
342342
// StartEventWatcher starts sending events received from this EventBroadcaster to the given event handler function.
343343
// The return value is used to stop recording
344344
func (e *eventBroadcasterImpl) StartEventWatcher(eventHandler func(event runtime.Object)) (func(), error) {
345+
return e.startEventWatcher(klog.Background(), eventHandler)
346+
}
347+
348+
func (e *eventBroadcasterImpl) startEventWatcher(logger klog.Logger, eventHandler func(event runtime.Object)) (func(), error) {
345349
watcher, err := e.Watch()
346350
if err != nil {
347351
return nil, err
348352
}
349353
go func() {
350-
defer utilruntime.HandleCrash()
354+
defer utilruntime.HandleCrashWithLogger(logger)
351355
for {
352356
watchEvent, ok := <-watcher.ResultChan()
353357
if !ok {

staging/src/k8s.io/client-go/tools/events/event_recorder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (recorder *recorderImpl) eventf(logger klog.Logger, regarding runtime.Objec
8383
}
8484
event := recorder.makeEvent(refRegarding, refRelated, timestamp, eventtype, reason, message, recorder.reportingController, recorder.reportingInstance, action)
8585
go func() {
86-
defer utilruntime.HandleCrash()
86+
defer utilruntime.HandleCrashWithLogger(logger)
8787
recorder.Action(watch.Added, event)
8888
}()
8989
}

staging/src/k8s.io/client-go/tools/pager/pager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (p *ListPager) eachListChunkBuffered(ctx context.Context, options metav1.Li
219219
chunkC := make(chan runtime.Object, p.PageBufferSize)
220220
bgResultC := make(chan error, 1)
221221
go func() {
222-
defer utilruntime.HandleCrash()
222+
defer utilruntime.HandleCrashWithContext(ctx)
223223

224224
var err error
225225
defer func() {

staging/src/k8s.io/client-go/tools/record/event.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (e *eventBroadcasterImpl) recordToSink(sink EventSink, event *v1.Event, eve
292292
event = &eventCopy
293293
result, err := eventCorrelator.EventCorrelate(event)
294294
if err != nil {
295-
utilruntime.HandleError(err)
295+
utilruntime.HandleErrorWithContext(e.cancelationCtx, err, "Event correlation failed")
296296
}
297297
if result.Skip {
298298
return
@@ -402,7 +402,7 @@ func (e *eventBroadcasterImpl) StartEventWatcher(eventHandler func(*v1.Event)) w
402402
return watch.NewEmptyWatch()
403403
}
404404
go func() {
405-
defer utilruntime.HandleCrash()
405+
defer utilruntime.HandleCrashWithContext(e.cancelationCtx)
406406
for {
407407
select {
408408
case <-e.cancelationCtx.Done():

0 commit comments

Comments
 (0)