Skip to content

Commit 934ef05

Browse files
committed
chore: replace zap.New with bootstrap.New
Signed-off-by: STRRL <[email protected]>
1 parent a8805b2 commit 934ef05

File tree

14 files changed

+128
-40
lines changed

14 files changed

+128
-40
lines changed

examples/builtins/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/controller"
2727
"sigs.k8s.io/controller-runtime/pkg/handler"
2828
"sigs.k8s.io/controller-runtime/pkg/log"
29-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
29+
"sigs.k8s.io/controller-runtime/pkg/log/bootstrap"
3030
"sigs.k8s.io/controller-runtime/pkg/manager"
3131
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
3232
"sigs.k8s.io/controller-runtime/pkg/source"
3333
"sigs.k8s.io/controller-runtime/pkg/webhook"
3434
)
3535

3636
func init() {
37-
log.SetLogger(zap.New())
37+
log.SetLogger(bootstrap.New())
3838
}
3939

4040
func main() {

examples/configfile/builtin/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/client/config"
2929
cfg "sigs.k8s.io/controller-runtime/pkg/config"
3030
"sigs.k8s.io/controller-runtime/pkg/log"
31-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
31+
"sigs.k8s.io/controller-runtime/pkg/log/bootstrap"
3232
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
3333
)
3434

3535
var scheme = runtime.NewScheme()
3636

3737
func init() {
38-
log.SetLogger(zap.New())
38+
log.SetLogger(bootstrap.New())
3939
clientgoscheme.AddToScheme(scheme)
4040
}
4141

examples/configfile/custom/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ import (
2929
"sigs.k8s.io/controller-runtime/pkg/client/config"
3030
cfg "sigs.k8s.io/controller-runtime/pkg/config"
3131
"sigs.k8s.io/controller-runtime/pkg/log"
32-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
32+
"sigs.k8s.io/controller-runtime/pkg/log/bootstrap"
3333
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
3434
)
3535

3636
var scheme = runtime.NewScheme()
3737

3838
func init() {
39-
log.SetLogger(zap.New())
39+
log.SetLogger(bootstrap.New())
4040
clientgoscheme.AddToScheme(scheme)
4141
v1alpha1.AddToScheme(scheme)
4242
}

examples/crd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
3131
"sigs.k8s.io/controller-runtime/pkg/client"
3232
"sigs.k8s.io/controller-runtime/pkg/log"
33-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
33+
"sigs.k8s.io/controller-runtime/pkg/log/bootstrap"
3434
)
3535

3636
var (
@@ -102,7 +102,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
102102
}
103103

104104
func main() {
105-
ctrl.SetLogger(zap.New())
105+
ctrl.SetLogger(bootstrap.New())
106106

107107
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
108108
if err != nil {

examples/scratch-env/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
ctrl "sigs.k8s.io/controller-runtime"
2727
"sigs.k8s.io/controller-runtime/pkg/envtest"
28+
"sigs.k8s.io/controller-runtime/pkg/log/bootstrap"
2829
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2930
)
3031

@@ -45,7 +46,7 @@ func runMain() int {
4546
flag.CommandLine.AddGoFlagSet(&goFlagSet)
4647
}
4748
flag.Parse()
48-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(loggerOpts)))
49+
ctrl.SetLogger(bootstrap.New(zap.UseFlagOptions(loggerOpts)))
4950

5051
log := ctrl.Log.WithName("main")
5152

examples/tokenreview/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import (
2222
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2323
"sigs.k8s.io/controller-runtime/pkg/client/config"
2424
"sigs.k8s.io/controller-runtime/pkg/log"
25-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
25+
"sigs.k8s.io/controller-runtime/pkg/log/bootstrap"
2626
"sigs.k8s.io/controller-runtime/pkg/manager"
2727
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
2828
"sigs.k8s.io/controller-runtime/pkg/webhook/authentication"
2929
)
3030

3131
func init() {
32-
log.SetLogger(zap.New())
32+
log.SetLogger(bootstrap.New())
3333
}
3434

3535
func main() {

pkg/log/bootstrap/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package bootstrap is used to bootstrap the logger with both customized zap and logr.
18+
package bootstrap

pkg/log/bootstrap/log.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package bootstrap
18+
19+
import (
20+
gologr "github.com/go-logr/logr"
21+
gozapr "github.com/go-logr/zapr"
22+
"sigs.k8s.io/controller-runtime/pkg/log/logr"
23+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
24+
)
25+
26+
// New returns a brand new Logger configured with Opts. It
27+
// uses logr.NewKubeAwareLogSink which adds Type information and
28+
// Namespace/Name to the log.
29+
func New(opts ...zap.Opts) gologr.Logger {
30+
zapLogger := gozapr.NewLogger(zap.NewRaw(opts...))
31+
32+
o := &zap.Options{}
33+
for _, opt := range opts {
34+
opt(o)
35+
}
36+
return logr.NewKubeAwareLogger(zapLogger, o.Development)
37+
}

pkg/log/logr/kube_aware_logger_sink.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,41 @@ import (
2323
"k8s.io/apimachinery/pkg/types"
2424
)
2525

26+
var _ logr.LogSink = (*KubeAwareLogSink)(nil)
27+
28+
// KubeAwareLogSink is a Kubernetes-aware logr.LogSink.
29+
// zapcore.ObjectMarshaler would be bypassed when using zapr and WithValues.
30+
// It would use a wrapper implements logr.Marshaler instead of using origin Kubernetes objects.
2631
type KubeAwareLogSink struct {
2732
sink logr.LogSink
2833
kubeAwareEnabled *atomic.Bool
2934
}
3035

36+
// NewKubeAwareLogger return the wrapper with existed logr.Logger.
37+
// logger is the backend logger.
38+
// kubeAwareEnabled is the flag to enable kube aware logging.
3139
func NewKubeAwareLogger(logger logr.Logger, kubeAwareEnabled bool) logr.Logger {
3240
return logr.New(NewKubeAwareLogSink(logger.GetSink(), kubeAwareEnabled))
3341
}
3442

43+
// NewKubeAwareLogSink return the wrapper with existed logr.LogSink.
44+
// sink is the backend logr.LogSink.
45+
// kubeAwareEnabled is the flag to enable kube aware logging.
3546
func NewKubeAwareLogSink(logSink logr.LogSink, kubeAwareEnabled bool) *KubeAwareLogSink {
3647
return &KubeAwareLogSink{sink: logSink, kubeAwareEnabled: atomic.NewBool(kubeAwareEnabled)}
3748
}
3849

50+
// Init implements logr.LogSink.
3951
func (k *KubeAwareLogSink) Init(info logr.RuntimeInfo) {
4052
k.sink.Init(info)
4153
}
4254

55+
// Enabled implements logr.LogSink.
4356
func (k *KubeAwareLogSink) Enabled(level int) bool {
4457
return k.sink.Enabled(level)
4558
}
4659

60+
// Info implements logr.LogSink.
4761
func (k *KubeAwareLogSink) Info(level int, msg string, keysAndValues ...interface{}) {
4862
if !k.KubeAwareEnabled() {
4963
k.sink.Info(level, msg, keysAndValues...)
@@ -53,6 +67,7 @@ func (k *KubeAwareLogSink) Info(level int, msg string, keysAndValues ...interfac
5367
k.sink.Info(level, msg, k.wrapKeyAndValues(keysAndValues)...)
5468
}
5569

70+
// Error implements logr.LogSink.
5671
func (k *KubeAwareLogSink) Error(err error, msg string, keysAndValues ...interface{}) {
5772
if !k.KubeAwareEnabled() {
5873
k.sink.Error(err, msg, keysAndValues...)
@@ -61,45 +76,50 @@ func (k *KubeAwareLogSink) Error(err error, msg string, keysAndValues ...interfa
6176
k.sink.Error(err, msg, k.wrapKeyAndValues(keysAndValues)...)
6277
}
6378

64-
func (k *KubeAwareLogSink) wrapKeyAndValues(keysAndValues []interface{}) []interface{} {
65-
result := make([]interface{}, len(keysAndValues))
66-
for i, item := range keysAndValues {
67-
if i%2 == 0 {
68-
// item is key, no need to resolve
69-
result[i] = item
70-
continue
71-
}
72-
73-
switch val := item.(type) {
74-
case runtime.Object:
75-
result[i] = &kubeObjectWrapper{obj: val}
76-
case types.NamespacedName:
77-
result[i] = &namespacedNameWrapper{NamespacedName: val}
78-
default:
79-
result[i] = item
80-
}
81-
}
82-
return result
83-
}
84-
79+
// WithValues implements logr.LogSink.
8580
func (k *KubeAwareLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink {
8681
return &KubeAwareLogSink{
8782
kubeAwareEnabled: k.kubeAwareEnabled,
8883
sink: k.sink.WithValues(k.wrapKeyAndValues(keysAndValues)...),
8984
}
9085
}
9186

87+
// WithName implements logr.LogSink.
9288
func (k *KubeAwareLogSink) WithName(name string) logr.LogSink {
9389
return &KubeAwareLogSink{
9490
kubeAwareEnabled: k.kubeAwareEnabled,
9591
sink: k.sink.WithName(name),
9692
}
9793
}
9894

95+
// KubeAwareEnabled return kube aware logging is enabled or not.
9996
func (k *KubeAwareLogSink) KubeAwareEnabled() bool {
10097
return k.kubeAwareEnabled.Load()
10198
}
10299

100+
// SetKubeAwareEnabled could update the kube aware logging flag.
103101
func (k *KubeAwareLogSink) SetKubeAwareEnabled(enabled bool) {
104102
k.kubeAwareEnabled.Store(enabled)
105103
}
104+
105+
// wrapKeyAndValues would replace the kubernetes objects with wrappers.
106+
func (k *KubeAwareLogSink) wrapKeyAndValues(keysAndValues []interface{}) []interface{} {
107+
result := make([]interface{}, len(keysAndValues))
108+
for i, item := range keysAndValues {
109+
if i%2 == 0 {
110+
// item is key, no need to resolve
111+
result[i] = item
112+
continue
113+
}
114+
115+
switch val := item.(type) {
116+
case runtime.Object:
117+
result[i] = &kubeObjectWrapper{obj: val}
118+
case types.NamespacedName:
119+
result[i] = &namespacedNameWrapper{NamespacedName: val}
120+
default:
121+
result[i] = item
122+
}
123+
}
124+
return result
125+
}

pkg/log/logr/kube_object_wrapper.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ limitations under the License.
1717
package logr
1818

1919
import (
20+
"reflect"
21+
22+
"github.com/go-logr/logr"
2023
"k8s.io/apimachinery/pkg/api/meta"
2124
"k8s.io/apimachinery/pkg/runtime"
22-
"reflect"
2325
)
2426

27+
var _ logr.Marshaler = (*kubeObjectWrapper)(nil)
28+
2529
type kubeObjectWrapper struct {
2630
obj runtime.Object
2731
}

pkg/log/logr/logr_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package logr
1818

1919
import (
2020
"bytes"
21+
2122
"encoding/json"
23+
2224
"github.com/go-logr/logr"
2325
. "github.com/onsi/ginkgo"
2426
. "github.com/onsi/gomega"
@@ -79,7 +81,7 @@ var _ = Describe("Logr logSink setup", func() {
7981

8082
It("should log a standard non-namespaced Kubernetes object name", func() {
8183
node := &corev1.Node{}
82-
node.Name = "some-node"
84+
node.Name = "some-node-1"
8385
logger.Info("here's a kubernetes object", "thing", node)
8486

8587
outRaw := logOut.Bytes()
@@ -93,7 +95,7 @@ var _ = Describe("Logr logSink setup", func() {
9395

9496
It("should log a standard Kubernetes object's kind, if set", func() {
9597
node := &corev1.Node{}
96-
node.Name = "some-node"
98+
node.Name = "some-node-2"
9799
node.APIVersion = "v1"
98100
node.Kind = "Node"
99101
logger.Info("here's a kubernetes object", "thing", node)
@@ -110,7 +112,7 @@ var _ = Describe("Logr logSink setup", func() {
110112
})
111113

112114
It("should log a standard non-namespaced NamespacedName name", func() {
113-
name := types.NamespacedName{Name: "some-node"}
115+
name := types.NamespacedName{Name: "some-node-3"}
114116
logger.Info("here's a kubernetes object", "thing", name)
115117

116118
outRaw := logOut.Bytes()

pkg/log/logr/namespaced_name_wrapper.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ limitations under the License.
1616

1717
package logr
1818

19-
import "k8s.io/apimachinery/pkg/types"
19+
import (
20+
"github.com/go-logr/logr"
21+
"k8s.io/apimachinery/pkg/types"
22+
)
23+
24+
var _ logr.Marshaler = (*namespacedNameWrapper)(nil)
2025

2126
type namespacedNameWrapper struct {
2227
types.NamespacedName

pkg/log/zap/zap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type NewEncoderFunc func(...EncoderConfigOption) zapcore.Encoder
3939
// New returns a brand new Logger configured with Opts. It
4040
// uses KubeAwareEncoder which adds Type information and
4141
// Namespace/Name to the log.
42+
// Deprecated: use bootstrap.New as instead.
4243
func New(opts ...Opts) logr.Logger {
4344
return zapr.NewLogger(NewRaw(opts...))
4445
}

pkg/log/zap/zap_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ var _ = Describe("Zap logger setup", func() {
178178

179179
It("should log a standard non-namespaced Kubernetes object name", func() {
180180
node := &corev1.Node{}
181-
node.Name = "some-node"
181+
node.Name = "some-node-1"
182182
logger.Info("here's a kubernetes object", "thing", node)
183183

184184
outRaw := logOut.Bytes()
@@ -192,7 +192,7 @@ var _ = Describe("Zap logger setup", func() {
192192

193193
It("should log a standard Kubernetes object's kind, if set", func() {
194194
node := &corev1.Node{}
195-
node.Name = "some-node"
195+
node.Name = "some-node-2"
196196
node.APIVersion = "v1"
197197
node.Kind = "Node"
198198
logger.Info("here's a kubernetes object", "thing", node)
@@ -209,7 +209,7 @@ var _ = Describe("Zap logger setup", func() {
209209
})
210210

211211
It("should log a standard non-namespaced NamespacedName name", func() {
212-
name := types.NamespacedName{Name: "some-node"}
212+
name := types.NamespacedName{Name: "some-node-3"}
213213
logger.Info("here's a kubernetes object", "thing", name)
214214

215215
outRaw := logOut.Bytes()

0 commit comments

Comments
 (0)