From 991d848e344250e055f5066bcd1b24894908db4f Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Tue, 5 Apr 2022 20:33:07 +0200 Subject: [PATCH] logcheck: enable helper check This is in preparation of converting code to contextual logging. --- hack/logcheck.conf | 11 +++++++++++ .../k8s.io/component-base/logs/example/cmd/logger.go | 2 +- .../src/k8s.io/component-base/logs/json/json_test.go | 1 + .../src/k8s.io/component-base/logs/options_test.go | 2 ++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/hack/logcheck.conf b/hack/logcheck.conf index ced35224675..3ec1cf4f9a5 100644 --- a/hack/logcheck.conf +++ b/hack/logcheck.conf @@ -20,3 +20,14 @@ structured k8s.io/kubernetes/cmd/kubelet/.* structured k8s.io/kubernetes/pkg/kubelet/.* structured k8s.io/kubernetes/pkg/proxy/.* structured k8s.io/kubernetes/pkg/scheduler/.* + +# The following packages have been migrated to contextual logging. +# Packages matched here do not have to be listed above because +# "contextual" implies "structured". +# TODO next: contextual k8s.io/kubernetes/pkg/scheduler/.* + +# As long as contextual logging is alpha or beta, all WithName, WithValues, +# NewContext calls have to go through klog. Once it is GA, we can lift +# this restriction. Whether we then do a global search/replace remains +# to be decided. +with-helpers .* diff --git a/staging/src/k8s.io/component-base/logs/example/cmd/logger.go b/staging/src/k8s.io/component-base/logs/example/cmd/logger.go index 020ad071cc0..4bf5639ac05 100644 --- a/staging/src/k8s.io/component-base/logs/example/cmd/logger.go +++ b/staging/src/k8s.io/component-base/logs/example/cmd/logger.go @@ -58,7 +58,7 @@ func NewLoggerCommand() *cobra.Command { } // Initialize contextual logging. - logger := klog.Background().WithName("example").WithValues("foo", "bar") + logger := klog.LoggerWithValues(klog.LoggerWithName(klog.Background(), "example"), "foo", "bar") ctx := klog.NewContext(context.Background(), logger) runLogger(ctx) diff --git a/staging/src/k8s.io/component-base/logs/json/json_test.go b/staging/src/k8s.io/component-base/logs/json/json_test.go index e634c9bfbe9..aaffbe68094 100644 --- a/staging/src/k8s.io/component-base/logs/json/json_test.go +++ b/staging/src/k8s.io/component-base/logs/json/json_test.go @@ -78,6 +78,7 @@ func TestZapLoggerInfo(t *testing.T) { writer := zapcore.AddSync(&buffer) sampleInfoLogger, _ := NewJSONLogger(0, writer, nil, nil) for _, name := range data.names { + // nolint:logcheck // This intentionally ignore the feature gate and always tests with a name. sampleInfoLogger = sampleInfoLogger.WithName(name) } // nolint:logcheck // The linter cannot and doesn't need to check the key/value pairs. diff --git a/staging/src/k8s.io/component-base/logs/options_test.go b/staging/src/k8s.io/component-base/logs/options_test.go index 0d6ed1c314c..1671bb87a00 100644 --- a/staging/src/k8s.io/component-base/logs/options_test.go +++ b/staging/src/k8s.io/component-base/logs/options_test.go @@ -128,7 +128,9 @@ func testContextualLogging(t *testing.T, enabled bool) { defer klog.EnableContextualLogging(true) ctx := context.Background() + // nolint:logcheck // This intentionally adds a name independently of the feature gate. logger := klog.NewKlogr().WithName("contextual") + // nolint:logcheck // This intentionally creates a new context independently of the feature gate. ctx = logr.NewContext(ctx, logger) if enabled { assert.Equal(t, logger, klog.FromContext(ctx), "FromContext")