Skip to content

Commit

Permalink
✨ Surface API Server warnings in client (#1468)
Browse files Browse the repository at this point in the history
* Surface API Server warnings in client

- Add a wrapper around `logr.Logger` that implements WarnignHandler
- Add options in client.go for settiing up behaviour of warning handler

Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>

* rename logger type

Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
  • Loading branch information
MadhavJivrajani committed Apr 13, 2021
1 parent 2d6828d commit 7ef2da0
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,34 @@ import (
"k8s.io/client-go/rest"

"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// WarningHandlerOptions are options for configuring a
// warning handler for the client which is responsible
// for surfacing API Server warnings.
type WarningHandlerOptions struct {
// SuppressWarnings decides if the warnings from the
// API server are suppressed or surfaced in the client.
SuppressWarnings bool
// AllowDuplicateLogs does not deduplicate the to-be
// logged surfaced warnings messages. See
// log.WarningHandlerOptions for considerations
// regarding deuplication
AllowDuplicateLogs bool
}

// Options are creation options for a Client
type Options struct {
// Scheme, if provided, will be used to map go structs to GroupVersionKinds
Scheme *runtime.Scheme

// Mapper, if provided, will be used to map GroupVersionKinds to Resources
Mapper meta.RESTMapper

// Opts is used to configure the warning handler responsible for
// surfacing and handling warnings messages sent by the API server.
Opts WarningHandlerOptions
}

// New returns a new Client using the provided config and Options.
Expand All @@ -61,6 +80,23 @@ func newClient(config *rest.Config, options Options) (*client, error) {
return nil, fmt.Errorf("must provide non-nil rest.Config to client.New")
}

if !options.Opts.SuppressWarnings {
// surface warnings
logger := log.Log.WithName("KubeAPIWarningLogger")
// Set a WarningHandler, the default WarningHandler
// is log.KubeAPIWarningLogger with deduplication enabled.
// See log.KubeAPIWarningLoggerOptions for considerations
// regarding deduplication.
rest.SetDefaultWarningHandler(
log.NewKubeAPIWarningLogger(
logger,
log.KubeAPIWarningLoggerOptions{
Deduplicate: !options.Opts.AllowDuplicateLogs,
},
),
)
}

// Init a scheme if none provided
if options.Scheme == nil {
options.Scheme = scheme.Scheme
Expand Down
76 changes: 76 additions & 0 deletions pkg/log/warning_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package log

import (
"sync"

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

// KubeAPIWarningLoggerOptions controls the behavior
// of a rest.WarningHandler 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
// result in increased memory use.
Deduplicate bool
}

// KubeAPIWarningLogger is a wrapper around
// a provided logr.Logger that implements the
// rest.WarningHandler 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
writtenLock sync.Mutex
// used to keep track of already logged messages
// and help in de-duplication.
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 it's logging purposes.
func (l *KubeAPIWarningLogger) HandleWarningHeader(code int, agent string, message string) {
if code != 299 || len(message) == 0 {
return
}

if l.opts.Deduplicate {
l.writtenLock.Lock()
defer l.writtenLock.Unlock()

if _, alreadyLogged := l.written[message]; alreadyLogged {
return
}
l.written[message] = struct{}{}
}
l.logger.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}
if opts.Deduplicate {
h.written = map[string]struct{}{}
}
return h
}

0 comments on commit 7ef2da0

Please sign in to comment.