Skip to content

🌱 Adopt WarningHandlerWithContext #3176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type NewClientFunc func(config *rest.Config, options Options) (Client, error)
// New returns a new Client using the provided config and Options.
//
// By default, the client surfaces warnings returned by the server. To
// suppress warnings, set config.WarningHandler = rest.NoWarnings{}. To
// define custom behavior, implement the rest.WarningHandler interface.
// suppress warnings, set config.WarningHandlerWithContext = rest.NoWarnings{}. To
// define custom behavior, implement the rest.WarningHandlerWithContext interface.
// See [sigs.k8s.io/controller-runtime/pkg/log.KubeAPIWarningLogger] for
// an example.
//
Expand Down Expand Up @@ -112,10 +112,9 @@ func newClient(config *rest.Config, options Options) (*client, error) {
config.UserAgent = rest.DefaultKubernetesUserAgent()
}

if config.WarningHandler == nil {
if config.WarningHandler == nil && config.WarningHandlerWithContext == nil {
// By default, we surface warnings.
config.WarningHandler = log.NewKubeAPIWarningLogger(
log.Log.WithName("KubeAPIWarningLogger"),
config.WarningHandlerWithContext = log.NewKubeAPIWarningLogger(
log.KubeAPIWarningLoggerOptions{
Deduplicate: false,
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func ExampleNew() {

func ExampleNew_suppress_warnings() {
cfg := config.GetConfigOrDie()
// Use a rest.WarningHandler that discards warning messages.
cfg.WarningHandler = rest.NoWarnings{}
// Use a rest.WarningHandlerWithContext that discards warning messages.
cfg.WarningHandlerWithContext = rest.NoWarnings{}

cl, err := client.New(cfg, client.Options{})
if err != nil {
Expand Down
27 changes: 13 additions & 14 deletions pkg/log/warning_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ limitations under the License.
package log

import (
"context"
"sync"

"github.com/go-logr/logr"
)

// KubeAPIWarningLoggerOptions controls the behavior
// of a rest.WarningHandler constructed using NewKubeAPIWarningLogger().
// of a rest.WarningHandlerWithContext constructed using NewKubeAPIWarningLogger().
type KubeAPIWarningLoggerOptions struct {
// Deduplicate indicates a given warning message should only be written once.
// Setting this to true in a long-running process handling many warnings can
Expand All @@ -33,10 +32,8 @@ type KubeAPIWarningLoggerOptions struct {

// KubeAPIWarningLogger is a wrapper around
// a provided logr.Logger that implements the
// rest.WarningHandler interface.
// rest.WarningHandlerWithContext interface.
type KubeAPIWarningLogger struct {
// logger is used to log responses with the warning header
logger logr.Logger
// opts contain options controlling warning output
opts KubeAPIWarningLoggerOptions
// writtenLock gurads written
Expand All @@ -46,9 +43,11 @@ type KubeAPIWarningLogger struct {
written map[string]struct{}
}

// HandleWarningHeader handles logging for responses from API server that are
// warnings with code being 299 and uses a logr.Logger for its logging purposes.
func (l *KubeAPIWarningLogger) HandleWarningHeader(code int, agent string, message string) {
// HandleWarningHeaderWithContext handles logging for responses from API server that are
// warnings with code being 299 and uses a logr.Logger from context for its logging purposes.
func (l *KubeAPIWarningLogger) HandleWarningHeaderWithContext(ctx context.Context, code int, _ string, message string) {
log := FromContext(ctx)

if code != 299 || len(message) == 0 {
return
}
Expand All @@ -62,13 +61,13 @@ func (l *KubeAPIWarningLogger) HandleWarningHeader(code int, agent string, messa
}
l.written[message] = struct{}{}
}
l.logger.Info(message)
log.Info(message)
}

// NewKubeAPIWarningLogger returns an implementation of rest.WarningHandler that logs warnings
// with code = 299 to the provided logr.Logger.
func NewKubeAPIWarningLogger(l logr.Logger, opts KubeAPIWarningLoggerOptions) *KubeAPIWarningLogger {
h := &KubeAPIWarningLogger{logger: l, opts: opts}
// NewKubeAPIWarningLogger returns an implementation of rest.WarningHandlerWithContext that logs warnings
// with code = 299 to the logger passed into HandleWarningHeaderWithContext via the context.
func NewKubeAPIWarningLogger(opts KubeAPIWarningLoggerOptions) *KubeAPIWarningLogger {
h := &KubeAPIWarningLogger{opts: opts}
if opts.Deduplicate {
h.written = map[string]struct{}{}
}
Expand Down
Loading