@@ -17,13 +17,12 @@ limitations under the License.
17
17
package log
18
18
19
19
import (
20
+ "context"
20
21
"sync"
21
-
22
- "github.com/go-logr/logr"
23
22
)
24
23
25
24
// KubeAPIWarningLoggerOptions controls the behavior
26
- // of a rest.WarningHandler constructed using NewKubeAPIWarningLogger().
25
+ // of a rest.WarningHandlerWithContext constructed using NewKubeAPIWarningLogger().
27
26
type KubeAPIWarningLoggerOptions struct {
28
27
// Deduplicate indicates a given warning message should only be written once.
29
28
// Setting this to true in a long-running process handling many warnings can
@@ -33,10 +32,8 @@ type KubeAPIWarningLoggerOptions struct {
33
32
34
33
// KubeAPIWarningLogger is a wrapper around
35
34
// a provided logr.Logger that implements the
36
- // rest.WarningHandler interface.
35
+ // rest.WarningHandlerWithContext interface.
37
36
type KubeAPIWarningLogger struct {
38
- // logger is used to log responses with the warning header
39
- logger logr.Logger
40
37
// opts contain options controlling warning output
41
38
opts KubeAPIWarningLoggerOptions
42
39
// writtenLock gurads written
@@ -46,9 +43,11 @@ type KubeAPIWarningLogger struct {
46
43
written map [string ]struct {}
47
44
}
48
45
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
+
52
51
if code != 299 || len (message ) == 0 {
53
52
return
54
53
}
@@ -62,13 +61,13 @@ func (l *KubeAPIWarningLogger) HandleWarningHeader(code int, agent string, messa
62
61
}
63
62
l .written [message ] = struct {}{}
64
63
}
65
- l . logger .Info (message )
64
+ log .Info (message )
66
65
}
67
66
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 }
72
71
if opts .Deduplicate {
73
72
h .written = map [string ]struct {}{}
74
73
}
0 commit comments