generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'pohly/logcheck' into main
The branch was created by checking out https://github.com/kubernetes/klog/tree/ea358027b5d8b2123cb77792ae4d4f03a5556866 and rewriting its history with: git-filter-repo --path hack/tools --prune-degenerate=always --no-ff --path-rename hack/tools/go.mod:go.mod --path-rename hack/tools/go.sum:go.sum --path-rename hack/tools/logcheck:logcheck --replace-message logcheck-replacement.txt --replace-text logcheck-replacement.txt where logcheck-replacement.txt had: logcheck:logcheck --replace-message logcheck-replacement.txt --replace-text logcheck-replacement.txt regex:Merge pull request #([0-9]+)==>Merge pull request https://github.com/kubernetes/klog/pull/\1 k8s.io/klog/hack/tools==>sigs.k8s.io/logtools hack/tools: ==>
- Loading branch information
Showing
25 changed files
with
1,941 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module sigs.k8s.io/logtools | ||
|
||
go 1.15 | ||
|
||
require ( | ||
golang.org/x/exp v0.0.0-20210220032938-85be41e4509f | ||
golang.org/x/tools v0.1.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= | ||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= | ||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= | ||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= | ||
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= | ||
golang.org/x/exp v0.0.0-20210220032938-85be41e4509f h1:GrkO5AtFUU9U/1f5ctbIBXtBGeSJbWwIYfIsTcFMaX4= | ||
golang.org/x/exp v0.0.0-20210220032938-85be41e4509f/go.mod h1:I6l2HNBLBZEcrOoCpyKLdY2lHoRZ8lI4x60KMCQDft4= | ||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= | ||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= | ||
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= | ||
golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4= | ||
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= | ||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= | ||
golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449 h1:xUIPaMhvROX9dhPvRCenIJtU78+lbEenGbgqB5hfHCQ= | ||
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k= | ||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= | ||
golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= | ||
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY= | ||
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
This directory contains a linter for checking log calls. It was originally | ||
created to detect when unstructured logging calls like `klog.Infof` get added | ||
to files that should only use structured logging calls like `klog.InfoS` | ||
and now also supports other checks. | ||
|
||
# Installation | ||
|
||
`go install sigs.k8s.io/logtools/logcheck` | ||
|
||
# Usage | ||
|
||
`$logcheck.go <package_name>` | ||
`e.g $logcheck ./pkg/kubelet/lifecycle/` | ||
|
||
# Configuration | ||
|
||
Checks can be enabled or disabled globally via command line flags and env | ||
variables. In addition, the global setting for a check can be modified per file | ||
via a configuration file. That file contains lines in this format: | ||
|
||
``` | ||
<checks> <regular expression> | ||
``` | ||
|
||
`<checks>` is a comma-separated list of the names of checks that get enabled or | ||
disabled when a file name matches the regular expression. A check gets disabled | ||
when its name has `-` as prefix and enabled when there is no prefix or `+` as | ||
prefix. Only checks that are mentioned explicitly are modified. All regular | ||
expressions are checked in order, so later lines can override the previous | ||
ones. | ||
|
||
In this example, checking for klog calls is enabled for all files under | ||
`pkg/scheduler` in the Kubernetes repo except for `scheduler.go` | ||
itself. Parameter checking is disabled everywhere. | ||
|
||
``` | ||
klog,-parameters k8s.io/kubernetes/pkg/scheduler/.* | ||
-klog k8s.io/kubernetes/pkg/scheduler/scheduler.go | ||
``` | ||
|
||
The names of all supported checks are the ones used as sub-section titles in | ||
the next section. | ||
|
||
# Checks | ||
|
||
## structured (enabled by default) | ||
|
||
Unstructured klog logging calls are flagged as error. | ||
|
||
## klog (disabled by default) | ||
|
||
None of the klog logging methods may be used. This is even stricter than | ||
`unstructured`. Instead, code should retrieve a logr.Logger from klog and log | ||
through that. | ||
|
||
## parameters (enabled by default) | ||
|
||
This ensures that if certain logging functions are allowed and are used, those | ||
functions are passed correct parameters. | ||
|
||
### all calls | ||
|
||
Format strings are not allowed where plain strings are expected. | ||
|
||
### structured logging calls | ||
|
||
Key/value parameters for logging calls are checked: | ||
- For each key there must be a value. | ||
- Keys must be constant strings. | ||
|
||
This also warns about code that is valid, for example code that collects | ||
key/value pairs in an `[]interface` variable before passing that on to a log | ||
call. Such valid code can use `nolint:logcheck` to disable the warning (when | ||
invoking logcheck through golangci-lint) or the `parameters` check can be | ||
disabled for the file. | ||
|
||
## with-helpers (disabled by default) | ||
|
||
`logr.Logger.WithName`, `logr.Logger.WithValues` and `logr.NewContext` must not | ||
be used. The corresponding helper calls from `k8s.io/klogr` should be used | ||
instead. This is relevant when support contextual logging is disabled at | ||
runtime in klog. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright 2021 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 main | ||
|
||
import ( | ||
"golang.org/x/tools/go/analysis/singlechecker" | ||
|
||
"sigs.k8s.io/logtools/logcheck/pkg" | ||
) | ||
|
||
func main() { | ||
singlechecker.Main(pkg.Analyser()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Copyright 2021 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 main | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
|
||
"sigs.k8s.io/logtools/logcheck/pkg" | ||
) | ||
|
||
func TestAnalyzer(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
enabled map[string]string | ||
override string | ||
testPackage string | ||
}{ | ||
{ | ||
name: "Allow unstructured logs", | ||
enabled: map[string]string{ | ||
"structured": "false", | ||
}, | ||
testPackage: "allowUnstructuredLogs", | ||
}, | ||
{ | ||
name: "Do not allow unstructured logs", | ||
testPackage: "doNotAllowUnstructuredLogs", | ||
}, | ||
{ | ||
name: "Per-file config", | ||
enabled: map[string]string{ | ||
"structured": "false", | ||
}, | ||
override: "testdata/src/mixed/structured_logging", | ||
testPackage: "mixed", | ||
}, | ||
{ | ||
name: "Function call parameters", | ||
enabled: map[string]string{ | ||
"structured": "false", | ||
}, | ||
testPackage: "parameters", | ||
}, | ||
{ | ||
name: "Only allow contextual calls", | ||
enabled: map[string]string{ | ||
"structured": "true", | ||
"contextual": "true", | ||
}, | ||
testPackage: "onlyAllowContextual", | ||
}, | ||
{ | ||
name: "Only allow contextual calls through config", | ||
enabled: map[string]string{ | ||
"structured": "false", | ||
"contextual": "false", | ||
}, | ||
override: "testdata/src/onlyAllowContextual/klog_logging", | ||
testPackage: "onlyAllowContextual", | ||
}, | ||
{ | ||
name: "importrename", | ||
testPackage: "importrename", | ||
}, | ||
{ | ||
name: "verbose", | ||
testPackage: "verbose", | ||
}, | ||
{ | ||
name: "gologr", | ||
enabled: map[string]string{ | ||
"contextual": "true", | ||
}, | ||
testPackage: "gologr", | ||
}, | ||
{ | ||
name: "contextual", | ||
enabled: map[string]string{ | ||
"contextual": "true", | ||
}, | ||
testPackage: "contextual", | ||
}, | ||
{ | ||
name: "helpers", | ||
enabled: map[string]string{ | ||
"with-helpers": "true", | ||
}, | ||
testPackage: "helpers", | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
analyzer := pkg.Analyser() | ||
set := func(flag, value string) { | ||
if value != "" { | ||
if err := analyzer.Flags.Set(flag, value); err != nil { | ||
t.Fatalf("unexpected error for %s: %v", flag, err) | ||
} | ||
} | ||
} | ||
for key, value := range tc.enabled { | ||
set("check-"+key, value) | ||
} | ||
if tc.override != "" { | ||
set("config", tc.override) | ||
} | ||
analysistest.Run(t, analysistest.TestData(), analyzer, tc.testPackage) | ||
}) | ||
} | ||
} |
Oops, something went wrong.