diff --git a/examples/builtins/main.go b/examples/builtins/main.go index 44ef212575..ff1f0dfa3b 100644 --- a/examples/builtins/main.go +++ b/examples/builtins/main.go @@ -26,7 +26,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/bootstrap" + "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/manager/signals" "sigs.k8s.io/controller-runtime/pkg/source" @@ -34,7 +34,7 @@ import ( ) func init() { - log.SetLogger(bootstrap.New()) + log.SetLogger(zap.New()) } func main() { diff --git a/examples/configfile/builtin/main.go b/examples/configfile/builtin/main.go index 793755a1f2..abd6180d19 100644 --- a/examples/configfile/builtin/main.go +++ b/examples/configfile/builtin/main.go @@ -28,14 +28,14 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/config" cfg "sigs.k8s.io/controller-runtime/pkg/config" "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/bootstrap" + "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager/signals" ) var scheme = runtime.NewScheme() func init() { - log.SetLogger(bootstrap.New()) + log.SetLogger(zap.New()) clientgoscheme.AddToScheme(scheme) } diff --git a/examples/configfile/custom/main.go b/examples/configfile/custom/main.go index a4a011a14c..e0fc95e337 100644 --- a/examples/configfile/custom/main.go +++ b/examples/configfile/custom/main.go @@ -29,14 +29,14 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/config" cfg "sigs.k8s.io/controller-runtime/pkg/config" "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/bootstrap" + "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager/signals" ) var scheme = runtime.NewScheme() func init() { - log.SetLogger(bootstrap.New()) + log.SetLogger(zap.New()) clientgoscheme.AddToScheme(scheme) v1alpha1.AddToScheme(scheme) } diff --git a/examples/crd/main.go b/examples/crd/main.go index bdef1ff287..1f6cd5fac2 100644 --- a/examples/crd/main.go +++ b/examples/crd/main.go @@ -30,7 +30,7 @@ import ( api "sigs.k8s.io/controller-runtime/examples/crd/pkg" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/bootstrap" + "sigs.k8s.io/controller-runtime/pkg/log/zap" ) var ( @@ -102,7 +102,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } func main() { - ctrl.SetLogger(bootstrap.New()) + ctrl.SetLogger(zap.New()) mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{}) if err != nil { diff --git a/examples/scratch-env/main.go b/examples/scratch-env/main.go index 6c73d5f000..6be4d127c2 100644 --- a/examples/scratch-env/main.go +++ b/examples/scratch-env/main.go @@ -25,7 +25,6 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/envtest" - "sigs.k8s.io/controller-runtime/pkg/log/bootstrap" "sigs.k8s.io/controller-runtime/pkg/log/zap" ) @@ -46,7 +45,7 @@ func runMain() int { flag.CommandLine.AddGoFlagSet(&goFlagSet) } flag.Parse() - ctrl.SetLogger(bootstrap.New(zap.UseFlagOptions(loggerOpts))) + ctrl.SetLogger(zap.New(zap.UseFlagOptions(loggerOpts))) log := ctrl.Log.WithName("main") diff --git a/examples/tokenreview/main.go b/examples/tokenreview/main.go index bb1cb5a982..d018956f96 100644 --- a/examples/tokenreview/main.go +++ b/examples/tokenreview/main.go @@ -22,14 +22,14 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" "sigs.k8s.io/controller-runtime/pkg/client/config" "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/bootstrap" + "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/manager/signals" "sigs.k8s.io/controller-runtime/pkg/webhook/authentication" ) func init() { - log.SetLogger(bootstrap.New()) + log.SetLogger(zap.New()) } func main() { diff --git a/go.mod b/go.mod index 0775b9365d..c454e4a5f2 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/onsi/gomega v1.18.1 github.com/prometheus/client_golang v1.12.1 github.com/prometheus/client_model v0.2.0 + go.uber.org/atomic v1.7.0 go.uber.org/goleak v1.1.12 go.uber.org/zap v1.19.1 golang.org/x/sys v0.0.0-20220209214540-3681064d5158 @@ -57,7 +58,6 @@ require ( github.com/prometheus/common v0.32.1 // indirect github.com/prometheus/procfs v0.7.3 // indirect github.com/spf13/pflag v1.0.5 // indirect - go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect diff --git a/pkg/log/bootstrap/doc.go b/pkg/log/bootstrap/doc.go deleted file mode 100644 index 2fafec144b..0000000000 --- a/pkg/log/bootstrap/doc.go +++ /dev/null @@ -1,18 +0,0 @@ -/* -Copyright 2022 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 bootstrap is used to bootstrap the logger with both customized zap and logr. -package bootstrap diff --git a/pkg/log/bootstrap/log.go b/pkg/log/bootstrap/log.go deleted file mode 100644 index 3a362a277d..0000000000 --- a/pkg/log/bootstrap/log.go +++ /dev/null @@ -1,37 +0,0 @@ -/* -Copyright 2022 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 bootstrap - -import ( - gologr "github.com/go-logr/logr" - gozapr "github.com/go-logr/zapr" - "sigs.k8s.io/controller-runtime/pkg/log/logr" - "sigs.k8s.io/controller-runtime/pkg/log/zap" -) - -// New returns a brand new Logger configured with Opts. It -// uses logr.NewKubeAwareLogSink which adds Type information and -// Namespace/Name to the log. -func New(opts ...zap.Opts) gologr.Logger { - zapLogger := gozapr.NewLogger(zap.NewRaw(opts...)) - - o := &zap.Options{} - for _, opt := range opts { - opt(o) - } - return logr.NewKubeAwareLogger(zapLogger, o.Development) -} diff --git a/pkg/log/logr/doc.go b/pkg/log/logr/doc.go deleted file mode 100644 index 6f955300bb..0000000000 --- a/pkg/log/logr/doc.go +++ /dev/null @@ -1,19 +0,0 @@ -/* -Copyright 2022 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 logr introduces an implementation of logr.LogSink, which could -// encode Kubernetes Objects into Type information and Namespace/Name. -package logr diff --git a/pkg/log/logr/logr_suite_test.go b/pkg/log/logr/logr_suite_test.go deleted file mode 100644 index 64bf783069..0000000000 --- a/pkg/log/logr/logr_suite_test.go +++ /dev/null @@ -1,31 +0,0 @@ -/* -Copyright 2022 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 logr - -import ( - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "sigs.k8s.io/controller-runtime/pkg/envtest/printer" -) - -func TestSource(t *testing.T) { - RegisterFailHandler(Fail) - suiteName := "Logr Log Suite" - RunSpecsWithDefaultAndCustomReporters(t, suiteName, []Reporter{printer.NewlineReporter{}, printer.NewProwReporter(suiteName)}) -} diff --git a/pkg/log/logr/logr_test.go b/pkg/log/logr/logr_test.go deleted file mode 100644 index 66a4d07267..0000000000 --- a/pkg/log/logr/logr_test.go +++ /dev/null @@ -1,226 +0,0 @@ -/* -Copyright 2022 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 logr - -import ( - "bytes" - - "encoding/json" - - "github.com/go-logr/logr" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/log/zap" -) - -// testStringer is a fmt.Stringer. -type testStringer struct{} - -func (testStringer) String() string { - return "value" -} - -var _ = Describe("Logr logSink setup", func() { - - Context("when logging kubernetes objects", func() { - var logOut *bytes.Buffer - var zaplogger logr.Logger - var logger logr.Logger - - Context("with logger created using zap.New", func() { - BeforeEach(func() { - logOut = new(bytes.Buffer) - By("setting up the logger") - // use production settings (false) to get just json output - zaplogger = zap.New(zap.WriteTo(logOut), zap.UseDevMode(false)) - logger = NewKubeAwareLogger(zaplogger, true) - - }) - - It("should log a standard namespaced Kubernetes object name and namespace", func() { - pod := &corev1.Pod{} - pod.Name = "some-pod" - pod.Namespace = "some-ns" - logger.Info("here's a kubernetes object", "thing", pod) - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": pod.Name, - "namespace": pod.Namespace, - })) - }) - - It("should work fine with normal stringers", func() { - logger.Info("here's a non-kubernetes stringer", "thing", testStringer{}) - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", "value")) - }) - - It("should log a standard non-namespaced Kubernetes object name", func() { - node := &corev1.Node{} - node.Name = "some-node-1" - logger.Info("here's a kubernetes object", "thing", node) - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": node.Name, - })) - }) - - It("should log a standard Kubernetes object's kind, if set", func() { - node := &corev1.Node{} - node.Name = "some-node-2" - node.APIVersion = "v1" - node.Kind = "Node" - logger.Info("here's a kubernetes object", "thing", node) - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": node.Name, - "apiVersion": "v1", - "kind": "Node", - })) - }) - - It("should log a standard non-namespaced NamespacedName name", func() { - name := types.NamespacedName{Name: "some-node-3"} - logger.Info("here's a kubernetes object", "thing", name) - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": name.Name, - })) - }) - - It("should log an unstructured Kubernetes object", func() { - pod := &unstructured.Unstructured{ - Object: map[string]interface{}{ - "metadata": map[string]interface{}{ - "name": "some-pod", - "namespace": "some-ns", - }, - }, - } - logger.Info("here's a kubernetes object", "thing", pod) - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": "some-pod", - "namespace": "some-ns", - })) - }) - - It("should log a standard namespaced NamespacedName name and namespace", func() { - name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"} - logger.Info("here's a kubernetes object", "thing", name) - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": name.Name, - "namespace": name.Namespace, - })) - }) - - It("should not panic with nil obj", func() { - var pod *corev1.Pod - logger.Info("here's a kubernetes object", "thing", pod) - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - Expect(res).To(HaveKey("thing")) - Expect(res["things"]).To(BeNil()) - }) - - It("should log a standard namespaced when using logrLogger.WithValues", func() { - name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"} - logger.WithValues("thing", name).Info("here's a kubernetes object") - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": name.Name, - "namespace": name.Namespace, - })) - }) - - It("should log a standard Kubernetes objects when using logrLogger.WithValues", func() { - node := &corev1.Node{} - node.Name = "some-node" - node.APIVersion = "v1" - node.Kind = "Node" - logger.WithValues("thing", node).Info("here's a kubernetes object") - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": node.Name, - "apiVersion": "v1", - "kind": "Node", - })) - }) - - It("should log a standard unstructured Kubernetes object when using logrLogger.WithValues", func() { - pod := &unstructured.Unstructured{ - Object: map[string]interface{}{ - "metadata": map[string]interface{}{ - "name": "some-pod", - "namespace": "some-ns", - }, - }, - } - logger.WithValues("thing", pod).Info("here's a kubernetes object") - - outRaw := logOut.Bytes() - res := map[string]interface{}{} - Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - - Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ - "name": "some-pod", - "namespace": "some-ns", - })) - }) - }) - }) -}) diff --git a/pkg/log/logr/kube_aware_logger_sink.go b/pkg/log/zap/kube_aware_logger_sink.go similarity index 99% rename from pkg/log/logr/kube_aware_logger_sink.go rename to pkg/log/zap/kube_aware_logger_sink.go index b88df38d26..e4bdb14f79 100644 --- a/pkg/log/logr/kube_aware_logger_sink.go +++ b/pkg/log/zap/kube_aware_logger_sink.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package logr +package zap import ( "github.com/go-logr/logr" diff --git a/pkg/log/logr/kube_object_wrapper.go b/pkg/log/zap/kube_object_wrapper.go similarity index 87% rename from pkg/log/logr/kube_object_wrapper.go rename to pkg/log/zap/kube_object_wrapper.go index 589078589c..f6f317719e 100644 --- a/pkg/log/logr/kube_object_wrapper.go +++ b/pkg/log/zap/kube_object_wrapper.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package logr +package zap import ( "reflect" @@ -24,13 +24,13 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) -var _ logr.Marshaler = (*kubeObjectWrapper)(nil) +var _ logr.Marshaler = (*logrLoggerKubeObjectWrapper)(nil) -type kubeObjectWrapper struct { +type logrLoggerKubeObjectWrapper struct { obj runtime.Object } -func (w *kubeObjectWrapper) MarshalLog() interface{} { +func (w *logrLoggerKubeObjectWrapper) MarshalLog() interface{} { result := make(map[string]string) if reflect.ValueOf(w.obj).IsNil() { diff --git a/pkg/log/logr/namespaced_name_wrapper.go b/pkg/log/zap/namespaced_name_wrapper.go similarity index 81% rename from pkg/log/logr/namespaced_name_wrapper.go rename to pkg/log/zap/namespaced_name_wrapper.go index 171413fd4d..3742bf9e2b 100644 --- a/pkg/log/logr/namespaced_name_wrapper.go +++ b/pkg/log/zap/namespaced_name_wrapper.go @@ -14,20 +14,20 @@ See the License for the specific language governing permissions and limitations under the License. */ -package logr +package zap import ( "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/types" ) -var _ logr.Marshaler = (*namespacedNameWrapper)(nil) +var _ logr.Marshaler = (*logrLoggerNamespacedNameWrapper)(nil) -type namespacedNameWrapper struct { +type logrLoggerNamespacedNameWrapper struct { types.NamespacedName } -func (w *namespacedNameWrapper) MarshalLog() interface{} { +func (w *logrLoggerNamespacedNameWrapper) MarshalLog() interface{} { result := make(map[string]string) if w.Namespace != "" { result["namespace"] = w.Namespace diff --git a/pkg/log/zap/zap.go b/pkg/log/zap/zap.go index 093e1c1127..db46ab7b1b 100644 --- a/pkg/log/zap/zap.go +++ b/pkg/log/zap/zap.go @@ -37,11 +37,11 @@ type EncoderConfigOption func(*zapcore.EncoderConfig) type NewEncoderFunc func(...EncoderConfigOption) zapcore.Encoder // New returns a brand new Logger configured with Opts. It -// uses KubeAwareEncoder which adds Type information and -// Namespace/Name to the log. -// Deprecated: use bootstrap.New as instead. +// uses KubeAwareLogger/KubeAwareEncoder which adds Type +// information and Namespace/Name to the log. func New(opts ...Opts) logr.Logger { - return zapr.NewLogger(NewRaw(opts...)) + zaprLogger := zapr.NewLogger(NewRaw(opts...)) + return NewKubeAwareLogger(zaprLogger, true) } // Opts allows to manipulate Options. @@ -171,11 +171,6 @@ type Options struct { // TimeEncoder specifies the encoder for the timestamps in log messages. // Defaults to EpochTimeEncoder as this is the default in Zap currently. TimeEncoder zapcore.TimeEncoder - // UseKubeAwareEncoder configures the logger to use the KubeAwareEncoder for encoding kubernetes - // object as Type information and Namespace/Name. - // Note: KubeAwareEncoder could NOT resolve the object passed by logger.WithValues(), see - // https://github.com/kubernetes-sigs/controller-runtime/issues/1290 - UseKubeAwareEncoder bool } // addDefaults adds defaults to the Options. @@ -251,11 +246,7 @@ func NewRaw(opts ...Opts) *zap.Logger { sink := zapcore.AddSync(o.DestWriter) o.ZapOpts = append(o.ZapOpts, zap.AddCallerSkip(1), zap.ErrorOutput(sink)) - encoder := o.Encoder - if o.UseKubeAwareEncoder { - encoder = &KubeAwareEncoder{Encoder: o.Encoder, Verbose: o.Development} - } - log := zap.New(zapcore.NewCore(encoder, sink, o.Level)) + log := zap.New(zapcore.NewCore(&KubeAwareEncoder{Encoder: o.Encoder, Verbose: o.Development}, sink, o.Level)) log = log.WithOptions(o.ZapOpts...) return log } diff --git a/pkg/log/zap/zap_test.go b/pkg/log/zap/zap_test.go index 819236adbd..aa67de8b20 100644 --- a/pkg/log/zap/zap_test.go +++ b/pkg/log/zap/zap_test.go @@ -145,6 +145,8 @@ var _ = Describe("Zap options setup", func() { }) }) +const kindNode = "Node" + var _ = Describe("Zap logger setup", func() { Context("when logging kubernetes objects", func() { var logOut *bytes.Buffer @@ -194,7 +196,7 @@ var _ = Describe("Zap logger setup", func() { node := &corev1.Node{} node.Name = "some-node-2" node.APIVersion = "v1" - node.Kind = "Node" + node.Kind = kindNode logger.Info("here's a kubernetes object", "thing", node) outRaw := logOut.Bytes() @@ -204,7 +206,7 @@ var _ = Describe("Zap logger setup", func() { Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ "name": node.Name, "apiVersion": "v1", - "kind": "Node", + "kind": kindNode, })) }) @@ -263,9 +265,7 @@ var _ = Describe("Zap logger setup", func() { outRaw := logOut.Bytes() Expect(string(outRaw)).Should(ContainSubstring("got nil for runtime.Object")) }) - - // see https://github.com/kubernetes-sigs/controller-runtime/issues/1290 - It("should log NamespacedName as origin stringer when using logrLogger.WithValues", func() { + It("should log a standard namespaced when using logrLogger.WithValues", func() { name := types.NamespacedName{Name: "some-pod", Namespace: "some-ns"} logger.WithValues("thing", name).Info("here's a kubernetes object") @@ -273,35 +273,31 @@ var _ = Describe("Zap logger setup", func() { res := map[string]interface{}{} Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - Expect(res).NotTo(HaveKeyWithValue("thing", map[string]interface{}{ + Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ "name": name.Name, "namespace": name.Namespace, })) - Expect(res).To(HaveKeyWithValue("thing", "some-ns/some-pod")) }) - // see https://github.com/kubernetes-sigs/controller-runtime/issues/1290 - It("should log Kubernetes objects as origin stringer when using logrLogger.WithValues", func() { + It("should log a standard Kubernetes objects when using logrLogger.WithValues", func() { node := &corev1.Node{} node.Name = "some-node" node.APIVersion = "v1" - node.Kind = "Node" + node.Kind = kindNode logger.WithValues("thing", node).Info("here's a kubernetes object") outRaw := logOut.Bytes() res := map[string]interface{}{} Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - Expect(res).NotTo(HaveKeyWithValue("thing", map[string]interface{}{ + Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ "name": node.Name, "apiVersion": "v1", - "kind": "Node", + "kind": kindNode, })) - Expect(res).To(HaveKeyWithValue("thing", "&Node{ObjectMeta:{some-node 0 0001-01-01 00:00:00 +0000 UTC map[] map[] [] [] []},Spec:NodeSpec{PodCIDR:,DoNotUseExternalID:,ProviderID:,Unschedulable:false,Taints:[]Taint{},ConfigSource:nil,PodCIDRs:[],},Status:NodeStatus{Capacity:ResourceList{},Allocatable:ResourceList{},Phase:,Conditions:[]NodeCondition{},Addresses:[]NodeAddress{},DaemonEndpoints:NodeDaemonEndpoints{KubeletEndpoint:DaemonEndpoint{Port:0,},},NodeInfo:NodeSystemInfo{MachineID:,SystemUUID:,BootID:,KernelVersion:,OSImage:,ContainerRuntimeVersion:,KubeletVersion:,KubeProxyVersion:,OperatingSystem:,Architecture:,},Images:[]ContainerImage{},VolumesInUse:[],VolumesAttached:[]AttachedVolume{},Config:nil,},}")) }) - // see https://github.com/kubernetes-sigs/controller-runtime/issues/1290 - It("should log an unstructured Kubernetes object as origin stringer when using logrLogger.WithValues", func() { + It("should log a standard unstructured Kubernetes object when using logrLogger.WithValues", func() { pod := &unstructured.Unstructured{ Object: map[string]interface{}{ "metadata": map[string]interface{}{ @@ -316,11 +312,10 @@ var _ = Describe("Zap logger setup", func() { res := map[string]interface{}{} Expect(json.Unmarshal(outRaw, &res)).To(Succeed()) - Expect(res).NotTo(HaveKeyWithValue("thing", map[string]interface{}{ + Expect(res).To(HaveKeyWithValue("thing", map[string]interface{}{ "name": "some-pod", "namespace": "some-ns", })) - Expect(res).To(HaveKeyWithValue("thing", pod.Object)) }) }