@@ -17,13 +17,12 @@ limitations under the License.
1717package log
1818
1919import (
20+ "context"
2021 "sync"
21-
22- "github.com/go-logr/logr"
2322)
2423
2524// KubeAPIWarningLoggerOptions controls the behavior
26- // of a rest.WarningHandler constructed using NewKubeAPIWarningLogger().
25+ // of a rest.WarningHandlerWithContext constructed using NewKubeAPIWarningLogger().
2726type KubeAPIWarningLoggerOptions struct {
2827 // Deduplicate indicates a given warning message should only be written once.
2928 // Setting this to true in a long-running process handling many warnings can
@@ -33,10 +32,8 @@ type KubeAPIWarningLoggerOptions struct {
3332
3433// KubeAPIWarningLogger is a wrapper around
3534// a provided logr.Logger that implements the
36- // rest.WarningHandler interface.
35+ // rest.WarningHandlerWithContext interface.
3736type KubeAPIWarningLogger struct {
38- // logger is used to log responses with the warning header
39- logger logr.Logger
4037 // opts contain options controlling warning output
4138 opts KubeAPIWarningLoggerOptions
4239 // writtenLock gurads written
@@ -46,9 +43,11 @@ type KubeAPIWarningLogger struct {
4643 written map [string ]struct {}
4744}
4845
49- // HandleWarningHeader handles logging for responses from API server that are
50- // warnings with code being 299 and uses a logr.Logger for its logging purposes.
51- func (l * KubeAPIWarningLogger ) HandleWarningHeader (code int , agent string , message string ) {
46+ // HandleWarningHeaderWithContext handles logging for responses from API server that are
47+ // warnings with code being 299 and uses a logr.Logger from context for its logging purposes.
48+ func (l * KubeAPIWarningLogger ) HandleWarningHeaderWithContext (ctx context.Context , code int , _ string , message string ) {
49+ log := FromContext (ctx )
50+
5251 if code != 299 || len (message ) == 0 {
5352 return
5453 }
@@ -62,13 +61,13 @@ func (l *KubeAPIWarningLogger) HandleWarningHeader(code int, agent string, messa
6261 }
6362 l .written [message ] = struct {}{}
6463 }
65- l . logger .Info (message )
64+ log .Info (message )
6665}
6766
68- // NewKubeAPIWarningLogger returns an implementation of rest.WarningHandler that logs warnings
69- // with code = 299 to the provided logr.Logger .
70- func NewKubeAPIWarningLogger (l logr. Logger , opts KubeAPIWarningLoggerOptions ) * KubeAPIWarningLogger {
71- h := & KubeAPIWarningLogger {logger : l , opts : opts }
67+ // NewKubeAPIWarningLogger returns an implementation of rest.WarningHandlerWithContext that logs warnings
68+ // with code = 299 to the logger passed into HandleWarningHeaderWithContext via the context .
69+ func NewKubeAPIWarningLogger (opts KubeAPIWarningLoggerOptions ) * KubeAPIWarningLogger {
70+ h := & KubeAPIWarningLogger {opts : opts }
7271 if opts .Deduplicate {
7372 h .written = map [string ]struct {}{}
7473 }
0 commit comments