Skip to content

Commit b713937

Browse files
authored
Merge pull request #3176 from sbueringer/pr-warning-handler-ctx
🌱 Adopt WarningHandlerWithContext
2 parents 53f325f + cf1740f commit b713937

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

pkg/client/client.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ type NewClientFunc func(config *rest.Config, options Options) (Client, error)
7474
// New returns a new Client using the provided config and Options.
7575
//
7676
// By default, the client surfaces warnings returned by the server. To
77-
// suppress warnings, set config.WarningHandler = rest.NoWarnings{}. To
78-
// define custom behavior, implement the rest.WarningHandler interface.
77+
// suppress warnings, set config.WarningHandlerWithContext = rest.NoWarnings{}. To
78+
// define custom behavior, implement the rest.WarningHandlerWithContext interface.
7979
// See [sigs.k8s.io/controller-runtime/pkg/log.KubeAPIWarningLogger] for
8080
// an example.
8181
//
@@ -112,10 +112,9 @@ func newClient(config *rest.Config, options Options) (*client, error) {
112112
config.UserAgent = rest.DefaultKubernetesUserAgent()
113113
}
114114

115-
if config.WarningHandler == nil {
115+
if config.WarningHandler == nil && config.WarningHandlerWithContext == nil {
116116
// By default, we surface warnings.
117-
config.WarningHandler = log.NewKubeAPIWarningLogger(
118-
log.Log.WithName("KubeAPIWarningLogger"),
117+
config.WarningHandlerWithContext = log.NewKubeAPIWarningLogger(
119118
log.KubeAPIWarningLoggerOptions{
120119
Deduplicate: false,
121120
},

pkg/client/example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func ExampleNew() {
5959

6060
func ExampleNew_suppress_warnings() {
6161
cfg := config.GetConfigOrDie()
62-
// Use a rest.WarningHandler that discards warning messages.
63-
cfg.WarningHandler = rest.NoWarnings{}
62+
// Use a rest.WarningHandlerWithContext that discards warning messages.
63+
cfg.WarningHandlerWithContext = rest.NoWarnings{}
6464

6565
cl, err := client.New(cfg, client.Options{})
6666
if err != nil {

pkg/log/warning_handler.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ limitations under the License.
1717
package log
1818

1919
import (
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().
2726
type 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.
3736
type 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

Comments
 (0)