Skip to content

Commit

Permalink
Merge pull request #1329 from vincepri/backport-071
Browse files Browse the repository at this point in the history
🌱 Backport bug fixes for v0.7.1 release
  • Loading branch information
k8s-ci-robot authored Jan 11, 2021
2 parents 096b2e0 + 7684ee5 commit 1d5261b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
36 changes: 34 additions & 2 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,50 @@ package log

import (
"context"
"sync"
"time"

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

// SetLogger sets a concrete logging implementation for all deferred Loggers.
func SetLogger(l logr.Logger) {
loggerWasSetLock.Lock()
defer loggerWasSetLock.Unlock()

loggerWasSet = true
Log.Fulfill(l)
}

// It is safe to assume that if this wasn't set within the first 30 seconds of a binaries
// lifetime, it will never get set. The DelegatingLogger causes a high number of memory
// allocations when not given an actual Logger, so we set a NullLogger to avoid that.
//
// We need to keep the DelegatingLogger because we have various inits() that get a logger from
// here. They will always get executed before any code that imports controller-runtime
// has a chance to run and hence to set an actual logger.
func init() {
// Init is blocking, so start a new goroutine
go func() {
time.Sleep(30 * time.Second)
loggerWasSetLock.Lock()
defer loggerWasSetLock.Unlock()
if !loggerWasSet {
Log.Fulfill(NullLogger{})
}
}()
}

var (
loggerWasSetLock sync.Mutex
loggerWasSet bool
)

// Log is the base logger used by kubebuilder. It delegates
// to another logr.Logger. You *must* call SetLogger to
// get any actual logging.
// to another logr.Logger. You *must* call SetLogger to
// get any actual logging. If SetLogger is not called within
// the first 30 seconds of a binaries lifetime, it will get
// set to a NullLogger.
var Log = NewDelegatingLogger(NullLogger{})

// FromContext returns a logger with predefined values from a context.Context.
Expand Down
10 changes: 8 additions & 2 deletions pkg/webhook/admission/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ func PatchResponseFromRaw(original, current []byte) Response {
return Response{
Patches: patches,
AdmissionResponse: admissionv1.AdmissionResponse{
Allowed: true,
PatchType: func() *admissionv1.PatchType { pt := admissionv1.PatchTypeJSONPatch; return &pt }(),
Allowed: true,
PatchType: func() *admissionv1.PatchType {
if len(patches) == 0 {
return nil
}
pt := admissionv1.PatchTypeJSONPatch
return &pt
}(),
},
}
}
Expand Down

0 comments on commit 1d5261b

Please sign in to comment.