From b450224c12947f1eef12d1777acd9f8d61a7534f Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Tue, 6 Jun 2023 11:26:57 +0200 Subject: [PATCH] golangci-lint: inline logcheck configuration This has the advantage that the golangci-lint cache gets invalidated automatically each time the logcheck config changes. --- hack/golangci-hints.yaml | 44 ++++++++++++++++++++++++++++++++++++ hack/golangci-strict.yaml | 44 ++++++++++++++++++++++++++++++++++++ hack/golangci.yaml | 44 ++++++++++++++++++++++++++++++++++++ hack/golangci.yaml.in | 3 +++ hack/logcheck.conf | 2 +- hack/verify-golangci-lint.sh | 10 +------- 6 files changed, 137 insertions(+), 10 deletions(-) diff --git a/hack/golangci-hints.yaml b/hack/golangci-hints.yaml index 82c976d4a94..8ffe7fbf249 100644 --- a/hack/golangci-hints.yaml +++ b/hack/golangci-hints.yaml @@ -98,6 +98,50 @@ linters-settings: # please keep this alphabetized path: ../_output/local/bin/logcheck.so description: structured logging checker original-url: k8s.io/logtools/logcheck + settings: + config: | + # hack/logcheck.conf contains regular expressions that are matched against /, + # for example k8s.io/cmd/kube-scheduler/app/config/config.go. + # + # By default, structured logging call parameters are checked, but usage of + # those calls is not required. That is changed on a per-file basis. + # + # Remember to clean the golangci-lint cache when changing the configuration and + # running the verify-golangci-lint.sh script multiple times, otherwise + # golangci-lint will report stale results: + # _output/local/bin/golangci-lint cache clean + + # At this point we don't enforce the usage structured logging calls except in + # those packages that were migrated. This disables the check for other files. + -structured .* + + # Now enable it again for migrated packages. + structured k8s.io/kubernetes/cmd/kube-proxy/.* + structured k8s.io/kubernetes/cmd/kubelet/.* + structured k8s.io/kubernetes/pkg/kubelet/.* + structured k8s.io/kubernetes/pkg/proxy/.* + structured k8s.io/kubernetes/pkg/scheduler/.* + structured k8s.io/kms/.* + structured k8s.io/apiserver/pkg/storage/value/.* + structured k8s.io/apiserver/pkg/server/options/encryptionconfig/.* + + # The following packages have been migrated to contextual logging. + # Packages matched here do not have to be listed above because + # "contextual" implies "structured". + contextual k8s.io/client-go/metadata/.* + contextual k8s.io/client-go/tools/events/.* + contextual k8s.io/client-go/tools/record/.* + contextual k8s.io/dynamic-resource-allocation/.* + contextual k8s.io/kubernetes/cmd/kube-scheduler/.* + contextual k8s.io/kubernetes/pkg/controller/.* + contextual k8s.io/kubernetes/pkg/scheduler/.* + contextual k8s.io/kubernetes/test/e2e/dra/.* + + # 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 .* forbidigo: analyze-types: true forbid: diff --git a/hack/golangci-strict.yaml b/hack/golangci-strict.yaml index 29014b3b474..9535b9c48c2 100644 --- a/hack/golangci-strict.yaml +++ b/hack/golangci-strict.yaml @@ -145,6 +145,50 @@ linters-settings: # please keep this alphabetized path: ../_output/local/bin/logcheck.so description: structured logging checker original-url: k8s.io/logtools/logcheck + settings: + config: | + # hack/logcheck.conf contains regular expressions that are matched against /, + # for example k8s.io/cmd/kube-scheduler/app/config/config.go. + # + # By default, structured logging call parameters are checked, but usage of + # those calls is not required. That is changed on a per-file basis. + # + # Remember to clean the golangci-lint cache when changing the configuration and + # running the verify-golangci-lint.sh script multiple times, otherwise + # golangci-lint will report stale results: + # _output/local/bin/golangci-lint cache clean + + # At this point we don't enforce the usage structured logging calls except in + # those packages that were migrated. This disables the check for other files. + -structured .* + + # Now enable it again for migrated packages. + structured k8s.io/kubernetes/cmd/kube-proxy/.* + structured k8s.io/kubernetes/cmd/kubelet/.* + structured k8s.io/kubernetes/pkg/kubelet/.* + structured k8s.io/kubernetes/pkg/proxy/.* + structured k8s.io/kubernetes/pkg/scheduler/.* + structured k8s.io/kms/.* + structured k8s.io/apiserver/pkg/storage/value/.* + structured k8s.io/apiserver/pkg/server/options/encryptionconfig/.* + + # The following packages have been migrated to contextual logging. + # Packages matched here do not have to be listed above because + # "contextual" implies "structured". + contextual k8s.io/client-go/metadata/.* + contextual k8s.io/client-go/tools/events/.* + contextual k8s.io/client-go/tools/record/.* + contextual k8s.io/dynamic-resource-allocation/.* + contextual k8s.io/kubernetes/cmd/kube-scheduler/.* + contextual k8s.io/kubernetes/pkg/controller/.* + contextual k8s.io/kubernetes/pkg/scheduler/.* + contextual k8s.io/kubernetes/test/e2e/dra/.* + + # 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 .* forbidigo: analyze-types: true forbid: diff --git a/hack/golangci.yaml b/hack/golangci.yaml index 4c511c912d2..c58da467ad4 100644 --- a/hack/golangci.yaml +++ b/hack/golangci.yaml @@ -148,6 +148,50 @@ linters-settings: # please keep this alphabetized path: ../_output/local/bin/logcheck.so description: structured logging checker original-url: k8s.io/logtools/logcheck + settings: + config: | + # hack/logcheck.conf contains regular expressions that are matched against /, + # for example k8s.io/cmd/kube-scheduler/app/config/config.go. + # + # By default, structured logging call parameters are checked, but usage of + # those calls is not required. That is changed on a per-file basis. + # + # Remember to clean the golangci-lint cache when changing the configuration and + # running the verify-golangci-lint.sh script multiple times, otherwise + # golangci-lint will report stale results: + # _output/local/bin/golangci-lint cache clean + + # At this point we don't enforce the usage structured logging calls except in + # those packages that were migrated. This disables the check for other files. + -structured .* + + # Now enable it again for migrated packages. + structured k8s.io/kubernetes/cmd/kube-proxy/.* + structured k8s.io/kubernetes/cmd/kubelet/.* + structured k8s.io/kubernetes/pkg/kubelet/.* + structured k8s.io/kubernetes/pkg/proxy/.* + structured k8s.io/kubernetes/pkg/scheduler/.* + structured k8s.io/kms/.* + structured k8s.io/apiserver/pkg/storage/value/.* + structured k8s.io/apiserver/pkg/server/options/encryptionconfig/.* + + # The following packages have been migrated to contextual logging. + # Packages matched here do not have to be listed above because + # "contextual" implies "structured". + contextual k8s.io/client-go/metadata/.* + contextual k8s.io/client-go/tools/events/.* + contextual k8s.io/client-go/tools/record/.* + contextual k8s.io/dynamic-resource-allocation/.* + contextual k8s.io/kubernetes/cmd/kube-scheduler/.* + contextual k8s.io/kubernetes/pkg/controller/.* + contextual k8s.io/kubernetes/pkg/scheduler/.* + contextual k8s.io/kubernetes/test/e2e/dra/.* + + # 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 .* forbidigo: analyze-types: true forbid: diff --git a/hack/golangci.yaml.in b/hack/golangci.yaml.in index 319bd621f7b..c2284551c75 100644 --- a/hack/golangci.yaml.in +++ b/hack/golangci.yaml.in @@ -159,6 +159,9 @@ linters-settings: # please keep this alphabetized path: ../_output/local/bin/logcheck.so description: structured logging checker original-url: k8s.io/logtools/logcheck + settings: + config: | + {{include "hack/logcheck.conf" | indent 10 | trim}} forbidigo: analyze-types: true forbid: diff --git a/hack/logcheck.conf b/hack/logcheck.conf index 874a47e1bb6..2cc516d8fba 100644 --- a/hack/logcheck.conf +++ b/hack/logcheck.conf @@ -1,4 +1,4 @@ -# This file contains regular expressions that are matched against /, +# hack/logcheck.conf contains regular expressions that are matched against /, # for example k8s.io/cmd/kube-scheduler/app/config/config.go. # # By default, structured logging call parameters are checked, but usage of diff --git a/hack/verify-golangci-lint.sh b/hack/verify-golangci-lint.sh index e0a286aa7fb..96a40915461 100755 --- a/hack/verify-golangci-lint.sh +++ b/hack/verify-golangci-lint.sh @@ -49,15 +49,7 @@ export GOBIN="${KUBE_OUTPUT_BINPATH}" PATH="${GOBIN}:${PATH}" invocation=(./hack/verify-golangci-lint.sh "$@") - -# The logcheck plugin currently has to be configured via env variables -# (https://github.com/golangci/golangci-lint/issues/1512). -# -# Remember to clean the golangci-lint cache when changing -# the configuration and running this script multiple times, -# otherwise golangci-lint will report stale results: -# _output/local/bin/golangci-lint cache clean -golangci=(env LOGCHECK_CONFIG="${KUBE_ROOT}/hack/logcheck.conf" "${GOBIN}/golangci-lint" run) +golangci=("${GOBIN}/golangci-lint" run) golangci_config="${KUBE_ROOT}/hack/golangci.yaml" base= strict=