Skip to content

Commit

Permalink
hack: integrate logcheck into golangci-lint
Browse files Browse the repository at this point in the history
Running logcheck as part of golangci-lint has several advantages:
- faster checking because finding files and parsing is shared
  with other linters
- gets rid of the complex and buggy
  hack/verify-structured-logging.sh (kubernetes#106746)
- support for // nolint:logcheck
- works with Go 1.18

Replay of 17e3c55
  • Loading branch information
pohly authored and liggitt committed Dec 20, 2022
1 parent 9441767 commit c95a8a1
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 117 deletions.
7 changes: 7 additions & 0 deletions .golangci.yaml
Expand Up @@ -21,10 +21,17 @@ linters:
# - structcheck
# - varcheck
- ineffassign
- logcheck
- staticcheck
- unused

linters-settings: # please keep this alphabetized
custom:
logcheck:
# Installed there by hack/verify-golangci-lint.sh.
path: _output/local/bin/logcheck.so
description: structured logging checker
original-url: k8s.io/klog/hack/tools
staticcheck:
go: "1.17"
checks: [
Expand Down
15 changes: 0 additions & 15 deletions hack/.structured_logging

This file was deleted.

22 changes: 22 additions & 0 deletions hack/logcheck.conf
@@ -0,0 +1,22 @@
# This file contains regular expressions that are matched against <pkg>/<file>,
# 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/kube-scheduler/.*
structured k8s.io/kubernetes/cmd/kubelet/.*
structured k8s.io/kubernetes/pkg/kubelet/.*
structured k8s.io/kubernetes/pkg/proxy/.*
structured k8s.io/kubernetes/pkg/scheduler/.*
2 changes: 1 addition & 1 deletion hack/tools/go.mod
Expand Up @@ -11,6 +11,6 @@ require (
github.com/google/go-flow-levee v0.1.5
gotest.tools/gotestsum v1.6.4
honnef.co/go/tools v0.2.2
k8s.io/klog/hack/tools v0.0.0-20210917071902-331d2323a192
k8s.io/klog/hack/tools v0.0.0-20220321081409-5b4c326b9f57
sigs.k8s.io/zeitgeist v0.2.0
)
68 changes: 66 additions & 2 deletions hack/tools/go.sum

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions hack/verify-golangci-lint.sh
Expand Up @@ -36,14 +36,25 @@ PATH="${GOBIN}:${PATH}"
export GO111MODULE=on

# Install golangci-lint
echo 'installing golangci-lint '
echo "installing golangci-lint and logcheck plugin from hack/tools into ${GOBIN}"
pushd "${KUBE_ROOT}/hack/tools" >/dev/null
go install github.com/golangci/golangci-lint/cmd/golangci-lint
go build -o "${GOBIN}/logcheck.so" -buildmode=plugin k8s.io/klog/hack/tools/logcheck/plugin
popd >/dev/null

cd "${KUBE_ROOT}"

# The config is in ${KUBE_ROOT}/.golangci.yaml
# The config is in ${KUBE_ROOT}/.golangci.yaml where it will be found
# even when golangci-lint is invoked in a sub-directory.
#
# 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
export LOGCHECK_CONFIG="${KUBE_ROOT}/hack/logcheck.conf"
echo 'running golangci-lint ' >&2
res=0
if [[ "$#" -gt 0 ]]; then
Expand Down
97 changes: 0 additions & 97 deletions hack/verify-structured-logging.sh

This file was deleted.

0 comments on commit c95a8a1

Please sign in to comment.