Skip to content

Commit 17e3c55

Browse files
committed
hack: integrate logcheck into golangci-lint
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
1 parent edffc70 commit 17e3c55

8 files changed

+45
-118
lines changed

.golangci.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ linters:
2121
# - structcheck
2222
# - varcheck
2323
- ineffassign
24+
- logcheck
2425
- staticcheck
2526
- unused
2627

2728
linters-settings: # please keep this alphabetized
29+
custom:
30+
logcheck:
31+
# Installed there by hack/verify-golangci-lint.sh.
32+
path: _output/local/bin/logcheck.so
33+
description: structured logging checker
34+
original-url: k8s.io/klog/hack/tools
2835
staticcheck:
2936
go: "1.17"
3037
checks: [

hack/.structured_logging

-15
This file was deleted.

hack/logcheck.conf

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file contains regular expressions that are matched against <pkg>/<file>,
2+
# for example k8s.io/cmd/kube-scheduler/app/config/config.go.
3+
#
4+
# By default, structured logging call parameters are checked, but usage of
5+
# those calls is not required. That is changed on a per-file basis.
6+
#
7+
# Remember to clean the golangci-lint cache when changing the configuration and
8+
# running the verify-golangci-lint.sh script multiple times, otherwise
9+
# golangci-lint will report stale results:
10+
# _output/local/bin/golangci-lint cache clean
11+
12+
# At this point we don't enforce the usage structured logging calls except in
13+
# those packages that were migrated. This disables the check for other files.
14+
-structured .*
15+
16+
# Now enable it again for migrated packages.
17+
structured k8s.io/kubernetes/cmd/kube-proxy/.*
18+
structured k8s.io/kubernetes/cmd/kube-scheduler/.*
19+
structured k8s.io/kubernetes/cmd/kubelet/.*
20+
structured k8s.io/kubernetes/pkg/kubelet/.*
21+
structured k8s.io/kubernetes/pkg/proxy/.*
22+
structured k8s.io/kubernetes/pkg/scheduler/.*

hack/make-rules/verify.sh

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ EXCLUDED_PATTERNS=(
3535
"verify-linkcheck.sh" # runs in separate Jenkins job once per day due to high network usage
3636
"verify-*-dockerized.sh" # Don't run any scripts that intended to be run dockerized
3737
"verify-govet-levee.sh" # Do not run levee analysis by default while KEP-1933 implementation is in alpha.
38-
"verify-structured-logging.sh" # TODO(dims) Need to get this running with golang 1.18
3938
)
4039

4140
# Exclude generated-files-remake in certain cases, if they're running in a separate job.

hack/tools/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ require (
1111
github.com/google/go-flow-levee v0.1.5
1212
gotest.tools/gotestsum v1.6.4
1313
honnef.co/go/tools v0.2.2
14-
k8s.io/klog/hack/tools v0.0.0-20210917071902-331d2323a192
14+
k8s.io/klog/hack/tools v0.0.0-20220321210246-c697110cd8ac
1515
sigs.k8s.io/zeitgeist v0.2.0
1616
)

hack/tools/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1521,8 +1521,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
15211521
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
15221522
honnef.co/go/tools v0.2.2 h1:MNh1AVMyVX23VUHE2O27jm6lNj3vjO5DexS4A1xvnzk=
15231523
honnef.co/go/tools v0.2.2/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY=
1524-
k8s.io/klog/hack/tools v0.0.0-20210917071902-331d2323a192 h1:u27Xm1of9MTDM1CZW3hg0Vv04ohywEG152G8mpr9n8Y=
1525-
k8s.io/klog/hack/tools v0.0.0-20210917071902-331d2323a192/go.mod h1:DXW3Mv8xqJvjXWiBSBHrK2O4mq5LMD0clqkv3b1g9HA=
1524+
k8s.io/klog/hack/tools v0.0.0-20220321210246-c697110cd8ac h1:c2NQfDLtcwrPdD9pnUcRPlMJ719zgRzdlufHULIW7mU=
1525+
k8s.io/klog/hack/tools v0.0.0-20220321210246-c697110cd8ac/go.mod h1:DXW3Mv8xqJvjXWiBSBHrK2O4mq5LMD0clqkv3b1g9HA=
15261526
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
15271527
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
15281528
mvdan.cc/gofumpt v0.3.0 h1:kTojdZo9AcEYbQYhGuLf/zszYthRdhDNDUi2JKTxas4=

hack/verify-golangci-lint.sh

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,25 @@ PATH="${GOBIN}:${PATH}"
3636
export GO111MODULE=on
3737

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

4445
cd "${KUBE_ROOT}"
4546

46-
# The config is in ${KUBE_ROOT}/.golangci.yaml
47+
# The config is in ${KUBE_ROOT}/.golangci.yaml where it will be found
48+
# even when golangci-lint is invoked in a sub-directory.
49+
#
50+
# The logcheck plugin currently has to be configured via env variables
51+
# (https://github.com/golangci/golangci-lint/issues/1512).
52+
#
53+
# Remember to clean the golangci-lint cache when changing
54+
# the configuration and running this script multiple times,
55+
# otherwise golangci-lint will report stale results:
56+
# _output/local/bin/golangci-lint cache clean
57+
export LOGCHECK_CONFIG="${KUBE_ROOT}/hack/logcheck.conf"
4758
echo 'running golangci-lint ' >&2
4859
res=0
4960
if [[ "$#" -gt 0 ]]; then

hack/verify-structured-logging.sh

-97
This file was deleted.

0 commit comments

Comments
 (0)